checkout.js 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. var util = require('../../utils/util.js');
  2. var user = require('../../utils/user.js');
  3. var api = require('../../config/api.js');
  4. var check = require('../../utils/check.js');
  5. var app = getApp();
  6. Page({
  7. data: {
  8. isRecommend: false,
  9. isNoMatch: true,
  10. codeShow: false,
  11. recCode: '',
  12. recommendTips: '',
  13. memberDiscount: '',
  14. isMember: false,
  15. orderId: '',
  16. payFailShow: false,
  17. choosePopup: false,
  18. noSerTimes: '',
  19. popupType: 1, //1单次预约 2 选择服务频次 3选择首次预约时间
  20. // timeList: check.getTimeList(1),
  21. timeList: [],
  22. dateList: util.getDateList(new Date(new Date().getTime() + 60 * 60 * 1000 * 24 * 0), 1, 21),
  23. // firstAppointList:util.getDateList(new Date(new Date().getTime()+60*60*1000*24),7),
  24. firstAppointList: [],
  25. timeIndex: '', //选中的时间段
  26. dateIndex: 2, //单次预约选中的日期
  27. firstDateIndex: 0, //首次预约时间
  28. activeTab: 1, //频次
  29. weekIndex: 0, //选中的周几
  30. weekArrList: [{
  31. id: 1,
  32. week: '周一',
  33. num: 0
  34. },
  35. {
  36. id: 2,
  37. week: '周二',
  38. num: 0
  39. },
  40. {
  41. id: 3,
  42. week: '周三',
  43. num: 0
  44. },
  45. {
  46. id: 4,
  47. week: '周四',
  48. num: 0
  49. },
  50. {
  51. id: 5,
  52. week: '周五',
  53. num: 0
  54. },
  55. {
  56. id: 6,
  57. week: '周六',
  58. num: 0
  59. },
  60. {
  61. id: 0,
  62. week: '周日',
  63. num: 0
  64. },
  65. ],
  66. selectedWeekList: [],
  67. selectedDateList: [],
  68. selectTimeIndex: 'null', //选中的时间段
  69. oneTimeAppoint: '2021-04-22 周一 13:00-14:00',
  70. isNeedAppoint: false, //是否需要预约
  71. isFinishAppoint: false, //已选择预约
  72. isOneAppoint: true, //单次卡
  73. appointFailTip: false, //预约失败提示
  74. bookStartDate: '', //可预约时间段
  75. bookEndDate: '',
  76. multiIndex: [0, 0, 0, 0, 0, 0],
  77. multiArray: [],
  78. array: [],
  79. year: "",
  80. month: "",
  81. day: "",
  82. startHour: "",
  83. endHour: "",
  84. orderData: "", //此时间为预计上门时间
  85. checkedGoodsList: [],
  86. checkedAddress: {},
  87. availableCouponLength: 0, // 可用的优惠券数量
  88. goodsTotalPrice: 0.00, //商品总价
  89. freightPrice: 0.00, //快递费
  90. couponPrice: 0.00, //优惠券的价格
  91. grouponPrice: 0.00, //团购优惠价格
  92. orderTotalPrice: 0.00, //订单总价
  93. actualPrice: 0.00, //实际需要支付的总价
  94. cartId: 0,
  95. addressId: 0,
  96. couponId: 0,
  97. userCouponId: 0,
  98. message: '',
  99. grouponLinkId: 0, //参与的团购
  100. grouponRulesId: 0, //团购规则ID
  101. nurseId: 0, //安心包认证id
  102. petinfo: null, //宠物保险内容
  103. shareId: 0,
  104. userInfo: {},
  105. remarkDisable: false,
  106. fraId: '', //加盟商id
  107. rId: '', //阿姨推荐码
  108. channelId: 5, //渠道id
  109. goodsType: 1 //商品类型 如果是6 可以不填写预约时间
  110. },
  111. //获取checkou信息
  112. getCheckoutInfo: function (appType) {
  113. let that = this;
  114. util.request(api.CartCheckout, {
  115. cartId: that.data.cartId,
  116. addressId: that.data.addressId,
  117. couponId: that.data.couponId,
  118. userCouponId: that.data.userCouponId,
  119. grouponRulesId: that.data.grouponRulesId,
  120. shareId: that.data.shareId,
  121. }).then(function (res) {
  122. if (res.errno === 0) {
  123. that.setData({
  124. isRecommend: res.data.checkedGoodsList[0].goods.attribute != 1,
  125. isMember: res.data.isMember,
  126. memberDiscount: res.data.memberDiscount,
  127. checkedGoodsList: res.data.checkedGoodsList,
  128. checkedAddress: res.data.checkedAddress,
  129. availableCouponLength: res.data.availableCouponLength,
  130. actualPrice: res.data.actualPrice,
  131. couponPrice: res.data.couponPrice,
  132. grouponPrice: res.data.grouponPrice,
  133. freightPrice: res.data.freightPrice,
  134. goodsTotalPrice: res.data.goodsTotalPrice,
  135. orderTotalPrice: res.data.orderTotalPrice,
  136. addressId: res.data.addressId,
  137. couponId: res.data.couponId,
  138. userCouponId: res.data.userCouponId,
  139. grouponRulesId: res.data.grouponRulesId,
  140. isNeedAppoint: appType == "1" ? true : false, //0需要预约 服务类商品,1保险 2中介 3买约分离次卡res.data.checkedGoodsList[0].goods.type === 0 ||
  141. isOneAppoint: res.data.checkedGoodsList[0].goods.serviceTimes == 1 ? true : false, //1为单次,其余为多次
  142. popupType: res.data.checkedGoodsList[0].goods.serviceTimes == 1 ? 1 : 2,
  143. bookStartDate: res.data.bookStartDate,
  144. bookEndDate: res.data.bookEndDate,
  145. goodsType: res.data.goodsType
  146. });
  147. let serviceDuration = res.data.checkedGoodsList[0].goods.serviceDuration;
  148. if (that.data.popupType == 2) {
  149. let timeList = [];
  150. if (res.data.timeRange != null && res.data.timeRange.length > 0) {
  151. res.data.timeRange.forEach((time, index) => {
  152. timeList.push({
  153. id: index + 1,
  154. time: time
  155. })
  156. })
  157. } else
  158. timeList = check.getTimeList(serviceDuration);
  159. that.setData({
  160. timeList: timeList
  161. });
  162. }
  163. that.LimitCanAppointmentDate();
  164. }
  165. wx.hideLoading();
  166. });
  167. },
  168. LimitCanAppointmentDate() { //特殊商品 如果需要限制可预约的时间范围 在次方法中实现
  169. if (this.data.checkedGoodsList[0].goodsId == 1190332) {
  170. let dates = [];
  171. this.data.dateList.forEach(item => {
  172. if (item.date < '2022-04-01')
  173. dates.push(item);
  174. })
  175. this.setData({
  176. dateList: dates
  177. })
  178. }
  179. //控制可预约时间范围
  180. let that = this;
  181. if (that.data.bookStartDate) {
  182. let dates = [];
  183. let dateIndex = -1;
  184. that.data.dateList.forEach((item, index) => {
  185. if (item.date < that.data.bookStartDate || item.date > that.data.bookEndDate) {
  186. item.isBook = false;
  187. item.mark = "不可约";
  188. } else {
  189. //设置第一个可预约的时间index
  190. if (dateIndex < 0) dateIndex = index;
  191. }
  192. dates.push(item);
  193. })
  194. that.setData({
  195. dateList: dates,
  196. dateIndex: dateIndex
  197. })
  198. }
  199. console.info(that.data.dateList)
  200. console.info(that.data.dateIndex)
  201. //设置默认起始可预约 index:dateIndex 近7天如果有周六 周日 不允许预约
  202. //存在一种情况 如果今天是周四,如果起始index=2 则周日允许预约 虽然页面显示为”约满"
  203. // if(this.data.dateList[3].id==0) {
  204. // console.info(" dateIndex:4")
  205. // this.setData({
  206. // dateIndex:4
  207. // })
  208. // }else{
  209. // if(this.data.dateList[2].id==0) {
  210. // console.info(" dateIndex:3")
  211. // this.setData({
  212. // dateIndex:3
  213. // })
  214. // }
  215. // }
  216. },
  217. selectAddress() {
  218. wx.navigateTo({
  219. url: '/pages/ucenter/address/address',
  220. })
  221. },
  222. selectCoupon() {
  223. wx.navigateTo({
  224. url: '/pages/ucenter/couponSelect/couponSelect',
  225. })
  226. },
  227. bindMessageInput: function (e) {
  228. this.setData({
  229. message: e.detail.value
  230. });
  231. },
  232. bindCodeInput: function (e) {
  233. this.setData({
  234. recCode: e.detail.value
  235. });
  236. if (e.detail.value.length == 5) {
  237. // 校验服务者真实性
  238. this.recCodeCheck();
  239. // this.setData({
  240. // codeShow:true
  241. // })
  242. } else {
  243. this.setData({
  244. codeShow: false
  245. });
  246. }
  247. },
  248. recCodeCheck() {
  249. wx.showLoading({
  250. title: '加载中',
  251. });
  252. util.request(api.checkRecommend, {
  253. recCode: this.data.recCode
  254. }, "GET")
  255. .then(res => {
  256. wx.hideLoading();
  257. if (res.errno == 0) {
  258. if (res.data) {
  259. this.setData({
  260. codeShow: true,
  261. isNoMatch: false,
  262. recommendTips: '推荐人:' + res.data
  263. });
  264. } else {
  265. this.setData({
  266. codeShow: true,
  267. isNoMatch: true,
  268. recommendTips: '未匹配到推荐者,请您和推荐人核对'
  269. });
  270. }
  271. } else {
  272. this.setData({
  273. codeShow: false,
  274. });
  275. wx.showToast({
  276. title: res.errmsg,
  277. icon: 'none',
  278. duration: 2000
  279. });
  280. }
  281. }).catch(function (err) {
  282. wx.hideLoading();
  283. this.setData({
  284. codeShow: false,
  285. });
  286. wx.showToast({
  287. title: err.errMsg,
  288. icon: 'none'
  289. })
  290. });
  291. },
  292. onReady: function () {
  293. // 页面渲染完成
  294. },
  295. onShow: function () {
  296. // console.log(this.data.dateList);
  297. // 页面显示
  298. user.checkLogin().then(res => {
  299. app.globalData.hasLogin = true;
  300. console.info("session 未过期")
  301. }).catch(() => {
  302. console.info("session 过期")
  303. app.globalData.hasLogin = false;
  304. wx.navigateTo({
  305. url: '/pages/auth/login/login',
  306. })
  307. });
  308. wx.showLoading({
  309. title: '加载中...',
  310. });
  311. try {
  312. var cartId = wx.getStorageSync('cartId');
  313. if (cartId === "") {
  314. cartId = 0;
  315. }
  316. var addressId = wx.getStorageSync('addressId');
  317. if (addressId === "") {
  318. addressId = 0;
  319. }
  320. var couponId = wx.getStorageSync('couponId');
  321. if (couponId === "") {
  322. couponId = 0;
  323. }
  324. var userCouponId = wx.getStorageSync('userCouponId');
  325. if (userCouponId === "") {
  326. userCouponId = 0;
  327. }
  328. var grouponRulesId = wx.getStorageSync('grouponRulesId');
  329. if (grouponRulesId === "") {
  330. grouponRulesId = 0;
  331. }
  332. var grouponLinkId = wx.getStorageSync('grouponLinkId');
  333. if (grouponLinkId === "") {
  334. grouponLinkId = 0;
  335. }
  336. //客户点选的预约动作
  337. var appType = wx.getStorageSync('appType');
  338. this.setData({
  339. cartId: cartId,
  340. addressId: addressId,
  341. couponId: couponId,
  342. userCouponId: userCouponId,
  343. grouponRulesId: grouponRulesId,
  344. grouponLinkId: grouponLinkId
  345. });
  346. this.getCheckoutInfo(appType);
  347. } catch (e) {
  348. // Do something when catch error
  349. console.log(e);
  350. }
  351. },
  352. onHide: function () {
  353. // 页面隐藏
  354. },
  355. onUnload: function () {
  356. // 页面关闭
  357. //清空宠物保险的缓存数据
  358. console.info('checkout onload')
  359. //wx.setStorageSync('petinfo'+this.data.cartId, null);
  360. },
  361. submitOrder: function () {
  362. let that = this;
  363. if (that.data.addressId <= 0) {
  364. util.showErrorToast('请选择收货地址');
  365. return;
  366. }
  367. // 检查是否已经预约
  368. if (that.data.goodsType != 6&&that.data.goodsType != 4 && that.data.isNeedAppoint && !that.data.isFinishAppoint) {
  369. // util.showErrorToast('请先预约服务时间');
  370. wx.showToast({
  371. title: '请先预约服务时间',
  372. icon: 'none',
  373. duration: 2000
  374. });
  375. return;
  376. }
  377. wx.showLoading({
  378. title: '检测中',
  379. });
  380. that.submitInfo();
  381. },
  382. submitInfo() {
  383. // 如果是多次卡先预估,单次卡不用预估
  384. if (!this.data.isOneAppoint) {
  385. let weeks = [];
  386. this.data.selectedDateList.forEach(item => {
  387. let obj = {
  388. dayOfWeek: this.data.weekArrList[item.week].id,
  389. startTime: item.time.split('-')[0],
  390. endTime: item.time.split('-')[1]
  391. };
  392. weeks.push(obj);
  393. });
  394. let tempStartDate = null;
  395. if (this.data.firstAppointList.length != 0 && this.data.firstAppointList.length >= this.data.firstDateIndex) {
  396. tempStartDate = this.data.firstAppointList[this.data.firstDateIndex].date;
  397. }
  398. let params = {
  399. weeks: weeks,
  400. type: this.data.activeTab,
  401. startDate: tempStartDate, //this.data.firstAppointList[this.data.firstDateIndex].date,
  402. lng: this.data.checkedAddress.lng,
  403. lat: this.data.checkedAddress.lat,
  404. shopId: this.data.checkedAddress.shopId,
  405. goodId: this.data.checkedGoodsList[0].goodsId
  406. };
  407. console.log(params);
  408. this.orderPay(params);
  409. } else {
  410. let weeks = [];
  411. if (this.data.isNeedAppoint && this.data.goodsType != 6 && this.data.goodsType != 4) {
  412. weeks = [{
  413. dayOfWeek: (this.data.dateList[this.data.dateIndex]).id,
  414. startTime: (this.data.timeList[this.data.timeIndex].time.split('-'))[0],
  415. endTime: (this.data.timeList[this.data.timeIndex].time.split('-'))[1]
  416. }];
  417. }
  418. let params = {
  419. weeks: weeks,
  420. type: 0,
  421. startDate: this.data.dateList[this.data.dateIndex].date,
  422. lng: this.data.checkedAddress.lng,
  423. lat: this.data.checkedAddress.lat,
  424. shopId: this.data.checkedAddress.shopId,
  425. goodId: this.data.checkedGoodsList[0].goodsId
  426. };
  427. console.log(params);
  428. this.orderPay(params);
  429. }
  430. },
  431. orderPay(orderData) {
  432. var orderInfo = {
  433. cartId: this.data.cartId,
  434. addressId: this.data.addressId,
  435. couponId: this.data.couponId,
  436. userCouponId: this.data.userCouponId,
  437. message: this.data.message,
  438. orderDate: this.data.orderData,
  439. grouponRulesId: this.data.grouponRulesId,
  440. grouponLinkId: this.data.grouponLinkId,
  441. nurseId: this.data.nurseId,
  442. petinfo: this.data.petinfo,
  443. shareId: this.data.shareId,
  444. recCode: this.data.recCode ? this.data.recCode : this.data.rId, //服务者推荐id(如果填写推荐码 以填写为准)
  445. fraId: this.data.fraId, //加盟商id
  446. channelId: this.data.channelId, //渠道码
  447. //预约参数信息
  448. orderDate: orderData
  449. };
  450. if (this.data.codeShow && !this.data.isNoMatch) {
  451. orderInfo.recCode = this.data.recCode;
  452. }
  453. console.log(orderInfo);
  454. util.request(api.OrderSubmit, orderInfo, 'POST').then(res => {
  455. if (res.errno === 0) {
  456. console.log(res.data.isNeedPay);
  457. if (res.data.isNeedPay == false) {
  458. console.log("跳过支付");
  459. util.redirect('/pages/ucenter/order/order');
  460. return;
  461. }
  462. // 下单成功,重置couponId
  463. wx.setStorageSync('couponId', 0);
  464. const orderId = res.data.orderId;
  465. this.setData({
  466. orderId: orderId
  467. });
  468. const grouponLinkId = res.data.grouponLinkId;
  469. util.request(api.OrderPrepay, {
  470. orderId: orderId
  471. }, 'POST').then(res => {
  472. if (res.errno === 0) {
  473. const payParam = res.data;
  474. console.log("支付过程开始");
  475. wx.requestPayment({
  476. 'timeStamp': payParam.timeStamp,
  477. 'nonceStr': payParam.nonceStr,
  478. 'package': payParam.packageValue,
  479. 'signType': payParam.signType,
  480. 'paySign': payParam.paySign,
  481. 'success': res => {
  482. console.log("支付过程成功");
  483. if (grouponLinkId) {
  484. setTimeout(() => {
  485. wx.redirectTo({
  486. url: '/pages/groupon/grouponDetail/grouponDetail?id=' + grouponLinkId
  487. })
  488. }, 1000);
  489. } else {
  490. wx.redirectTo({
  491. url: '/pages/checkout/payResult/payResult?status=1&orderId=' + orderId + '&actualPrice=' + this.data.actualPrice
  492. });
  493. }
  494. },
  495. 'fail': res => {
  496. console.log("支付过程失败");
  497. // wx.redirectTo({
  498. // url: '/pages/checkout/payResult/payResult?status=0&orderId=' + orderId
  499. // });
  500. // 弹出支付失败
  501. this.setData({
  502. payFailShow: true,
  503. remarkDisable: true
  504. });
  505. },
  506. 'complete': function (res) {
  507. console.log("支付过程结束")
  508. }
  509. });
  510. } else {
  511. // 弹出支付失败
  512. this.setData({
  513. payFailShow: true,
  514. remarkDisable: true
  515. });
  516. // wx.redirectTo({
  517. // url: '/pages/checkout/payResult/payResult?status=0&orderId=' + orderId
  518. // });
  519. }
  520. });
  521. } else {
  522. util.showErrorToast(res.errmsg);
  523. }
  524. });
  525. },
  526. surplusMonth: function (year) {
  527. var date = new Date();
  528. var year2 = date.getFullYear()
  529. var month = date.getMonth() + 1
  530. var day = date.getDate()
  531. var hour = date.getHours()
  532. var minute = date.getMinutes()
  533. var second = date.getSeconds()
  534. var monthDatas = [];
  535. if (year == year2) {
  536. var surplusMonth = 12 - month;
  537. monthDatas.push(month + "月")
  538. for (var i = month; i < 12; i++) {
  539. monthDatas.push(i + 1 + "月")
  540. }
  541. } else {
  542. for (var i = 0; i < 12; i++) {
  543. monthDatas.push(i + 1 + "月")
  544. }
  545. }
  546. return monthDatas;
  547. },
  548. //天数计算
  549. surplusDay: function (year, month, day) {
  550. var days = 31;
  551. var dayDatas = [];
  552. var date = new Date();
  553. var year2 = date.getFullYear()
  554. var month2 = date.getMonth() + 1
  555. switch (parseInt(month)) {
  556. case 1:
  557. case 3:
  558. case 5:
  559. case 7:
  560. case 8:
  561. case 10:
  562. case 12:
  563. days = 31;
  564. break;
  565. //对于2月份需要判断是否为闰年
  566. case 2:
  567. if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
  568. days = 29;
  569. break;
  570. } else {
  571. days = 28;
  572. break;
  573. }
  574. case 4:
  575. case 6:
  576. case 9:
  577. case 11:
  578. days = 30;
  579. break;
  580. }
  581. if (year == year2 && month == month2) {
  582. // dayDatas.push(day + "日")
  583. for (var i = day; i < days; i++) {
  584. dayDatas.push(i + 1 + "日")
  585. }
  586. } else {
  587. console.log(month + "月" + days + "天")
  588. for (var i = 0; i < days; i++) {
  589. dayDatas.push(i + 1 + "日")
  590. }
  591. }
  592. return dayDatas;
  593. },
  594. //时间计算
  595. surplusHour: function (year, month, day, hour) {
  596. var date = new Date();
  597. var year2 = date.getFullYear()
  598. var month2 = date.getMonth() + 1
  599. var day2 = date.getDate();
  600. var hourEnd = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21];
  601. var hours = [
  602. ['09时', '10时', '11时', '12时', '13时', '14时', '15时', '16时', '17时', '18时', '19时', '20时'],
  603. ['10时', '11时', '12时', '13时', '14时', '15时', '16时', '17时', '18时', '19时', '20时', '21时']
  604. ];
  605. return hours;
  606. },
  607. /**
  608. * 生命周期函数--监听页面加载
  609. */
  610. onLoad: function (options) {
  611. this.setData({
  612. userInfo: app.globalData.userInfo
  613. });
  614. let that = this;
  615. if (options.nurseId) {
  616. this.setData({
  617. nurseId: options.nurseId
  618. })
  619. }
  620. var rId = wx.getStorageSync('rId');
  621. if (rId) {
  622. this.setData({
  623. rId: rId,
  624. recCode: rId
  625. })
  626. }
  627. var fraId = wx.getStorageSync('fraId');
  628. if (fraId) {
  629. this.setData({
  630. fraId: fraId
  631. })
  632. }
  633. var channelId = wx.getStorageSync('channelId');
  634. if (channelId) {
  635. this.setData({
  636. channelId: parseInt(channelId)
  637. })
  638. }
  639. if (typeof options.shareId != 'undefined' && options.shareId > 0) {
  640. that.setData({
  641. shareId: options.shareId
  642. })
  643. }
  644. var date = new Date();
  645. var year = date.getFullYear()
  646. var month = date.getMonth() + 1
  647. var day = date.getDate()
  648. var hour = date.getHours()
  649. var surplusMonth = that.surplusMonth(year);
  650. console.log(surplusMonth)
  651. var surplusDay = this.surplusDay(year, month, day);
  652. console.log(surplusDay)
  653. var surplusHour = this.surplusHour(year, month, day, hour)
  654. console.log(surplusHour)
  655. //初始化picker
  656. that.setData({
  657. multiArray: [
  658. [year + '年', (year + 1) + '年', (year + 2) + '年'],
  659. surplusMonth,
  660. surplusDay,
  661. surplusHour[0],
  662. ['~'],
  663. surplusHour[1]
  664. ],
  665. year: year,
  666. month: month,
  667. day: day,
  668. startHour: surplusHour[0][0],
  669. endHour: surplusHour[1][0],
  670. })
  671. },
  672. varietiesChange: function (e) {
  673. var Varieties = this.data.array[parseInt(e.detail.value)]
  674. console.log(Varieties)
  675. this.setData({
  676. Varieties: Varieties
  677. })
  678. },
  679. warehouseChange: function (e) {
  680. var Warehouse = this.data.array[parseInt(e.detail.value)]
  681. console.log(Warehouse)
  682. this.setData({
  683. Warehouse: Warehouse
  684. })
  685. },
  686. //某一列的值改变时触发
  687. bindMultiPickerColumnChange: function (e) {
  688. var date = new Date();
  689. var year1 = date.getFullYear()
  690. var month1 = date.getMonth() + 1
  691. var day1 = date.getDate()
  692. var hour1 = date.getHours()
  693. console.log("当前月份" + this.data.month + '修改的列为', e.detail.column, ',值为', e.detail.value);
  694. var data = {
  695. multiArray: this.data.multiArray,
  696. multiIndex: this.data.multiIndex,
  697. year: this.data.year,
  698. month: this.data.month,
  699. day: this.data.day,
  700. startHour: this.data.startHour,
  701. endHour: this.data.startHour,
  702. };
  703. data.multiIndex[e.detail.column] = e.detail.value;
  704. switch (e.detail.column) {
  705. case 0:
  706. var yearStr = data.multiArray[e.detail.column][e.detail.value];
  707. var year = yearStr.substring(0, yearStr.length - 1)
  708. data.year = parseInt(year);
  709. var surplusMonth = this.surplusMonth(year);
  710. data.multiArray[1] = surplusMonth;
  711. if (data.year == year1) {
  712. data.month = month1;
  713. } else {
  714. data.month = 1;
  715. }
  716. if (data.year == year1 && month1 == data.month) {
  717. data.day = day1;
  718. } else {
  719. data.day = 1;
  720. }
  721. var surplusDay = this.surplusDay(data.year, data.month, data.day);
  722. data.multiArray[2] = surplusDay;
  723. var surplusHour;
  724. if (data.year == year1 && month1 == data.month && data.day == day1) {
  725. surplusHour = this.surplusHour(data.year, data.month, data.day, hour1)
  726. } else {
  727. surplusHour = this.surplusHour(data.year, data.month, data.day, 1)
  728. }
  729. console.log(surplusHour)
  730. data.multiArray[3] = surplusHour[0];
  731. data.multiArray[5] = surplusHour[1];
  732. data.startHour = surplusHour[0];
  733. data.endHour = surplusHour[1];
  734. data.multiIndex[1] = 0;
  735. data.multiIndex[2] = 0;
  736. data.multiIndex[3] = 0;
  737. data.multiIndex[5] = 0;
  738. break;
  739. case 1:
  740. console.log('选择月份' + data.multiArray[e.detail.column][e.detail.value]);
  741. var monthStr = data.multiArray[e.detail.column][e.detail.value];
  742. var month = monthStr.substring(0, monthStr.length - 1);
  743. data.month = month;
  744. data.day = 1;
  745. if (data.year == year1 && month1 == data.month) {
  746. data.day = day1;
  747. } else {
  748. data.day = 1;
  749. }
  750. var surplusDay = this.surplusDay(data.year, data.month, data.day);
  751. data.multiArray[2] = surplusDay;
  752. var surplusHour;
  753. if (data.year == year1 && month1 == data.month && data.day == day1) {
  754. surplusHour = this.surplusHour(data.year, data.month, data.day, hour1)
  755. } else {
  756. surplusHour = this.surplusHour(data.year, data.month, data.day, 1)
  757. }
  758. data.multiArray[3] = surplusHour[0];
  759. data.multiArray[5] = surplusHour[1];
  760. data.startHour = surplusHour[0];
  761. data.endHour = surplusHour[1];
  762. data.multiIndex[2] = 0;
  763. data.multiIndex[3] = 0;
  764. data.multiIndex[5] = 0;
  765. break;
  766. case 2:
  767. console.log('选择日' + data.multiArray[e.detail.column][e.detail.value]);
  768. var dayStr = data.multiArray[e.detail.column][e.detail.value];
  769. var day = dayStr.substring(0, dayStr.length - 1);
  770. data.day = day;
  771. var surplusHour;
  772. if (data.year == year1 && month1 == data.month && data.day == day1) {
  773. surplusHour = this.surplusHour(data.year, data.month, data.day, hour1)
  774. } else {
  775. surplusHour = this.surplusHour(data.year, data.month, data.day, 1)
  776. }
  777. data.multiArray[3] = surplusHour[0];
  778. data.multiArray[5] = surplusHour[1];
  779. data.startHour = surplusHour[0];
  780. data.endHour = surplusHour[1];
  781. data.multiIndex[3] = 0;
  782. data.multiIndex[5] = 0;
  783. break;
  784. case 3:
  785. console.log('起始时间' + data.multiArray[e.detail.column][e.detail.value]);
  786. var hourStr = data.multiArray[e.detail.column][e.detail.value];
  787. var hour = hourStr.substring(0, hourStr.length - 1);
  788. data.startHour = hour;
  789. console.log('起始时间' + hour + " day1:" + day1);
  790. /*var endhours2 = [];
  791. var end = ['09时', '11时', '12时', '13时', '14时', '15时', '16时', '17时', '18时', '19时', '20时', '20时'];
  792. for (var i = e.detail.value; i < end.length; i++) {
  793. endhours2.push(end[i]);
  794. }
  795. data.multiArray[5] = endhours2; */
  796. if (e.detail.value > data.multiIndex[5])
  797. data.multiIndex[5] = e.detail.value;
  798. break;
  799. case 5:
  800. var hourStr = data.multiArray[e.detail.column][e.detail.value];
  801. var hour = hourStr.substring(0, hourStr.length - 1);
  802. data.endHour = hour;
  803. if (e.detail.value < data.multiIndex[3])
  804. data.multiIndex[3] = e.detail.value;
  805. //console.log('index:'+data.multiIndex[3]+'value:'+e.detail.value+'结束时间' + data.multiArray[e.detail.column][e.detail.value]);
  806. break;
  807. }
  808. this.setData(data)
  809. },
  810. //value 改变时触发 change 事件
  811. bindMultiPickerChange: function (e) {
  812. console.info(e);
  813. console.info(this.data.multiArray);
  814. console.info(this.data);
  815. console.info(this.data.multiIndex);
  816. var dateStr =
  817. this.data.multiArray[0][this.data.multiIndex[0]] +
  818. this.data.multiArray[1][this.data.multiIndex[1]] +
  819. this.data.multiArray[2][this.data.multiIndex[2]] +
  820. this.data.multiArray[3][this.data.multiIndex[3]] +
  821. this.data.multiArray[4][this.data.multiIndex[4]] +
  822. this.data.multiArray[5][this.data.multiIndex[5]];
  823. console.log('picker发送选择改变,携带值为', dateStr)
  824. this.setData({
  825. orderData: dateStr
  826. })
  827. },
  828. onClose() {
  829. this.setData({
  830. choosePopup: false,
  831. remarkDisable: false,
  832. });
  833. },
  834. dateCheck(e) {
  835. let index = e.currentTarget.dataset.index;
  836. let date = e.currentTarget.dataset.date;
  837. let week = e.currentTarget.dataset.week;
  838. let isbook = e.currentTarget.dataset.isbook;
  839. console.info("dateCheck:" + index)
  840. if (this.data.checkedGoodsList[0].goodsId == 1190332) {
  841. this.setData({
  842. dateIndex: index
  843. });
  844. } else if (isbook) { //前三天不可预约
  845. this.setData({
  846. dateIndex: index
  847. });
  848. this.getDayStock();
  849. }
  850. },
  851. timeCheck(e) {
  852. let index = e.currentTarget.dataset.index;
  853. // let stock = e.currentTarget.dataset.stock;
  854. // if (stock > 0) {
  855. this.setData({
  856. timeIndex: index
  857. });
  858. // }
  859. },
  860. weekCheck(e) {
  861. let index = e.currentTarget.dataset.index;
  862. this.setData({
  863. weekIndex: index,
  864. // selectTimeIndex:'null'
  865. });
  866. },
  867. timeSelect(e) {
  868. let index = e.currentTarget.dataset.index;
  869. let time = e.currentTarget.dataset.time;
  870. if (this.data.activeTab != 2) {
  871. let weekList = this.data.weekArrList;
  872. weekList.forEach(item => {
  873. item.num = 0;
  874. });
  875. this.setData({
  876. weekArrList: weekList,
  877. });
  878. let itemData = "weekArrList[" + this.data.weekIndex + "].num";
  879. let selectedDateList = [];
  880. let dateItem = {
  881. week: this.data.weekIndex,
  882. time: time,
  883. weekStr: this.data.weekArrList[this.data.weekIndex].week
  884. };
  885. selectedDateList.push(dateItem);
  886. console.log(this.data);
  887. this.setData({
  888. selectTimeIndex: index,
  889. [itemData]: 1,
  890. selectedWeekList: [this.data.weekIndex],
  891. selectedDateList: selectedDateList
  892. });
  893. console.log(this.data);
  894. } else {
  895. // 如果选中的时间段小于2个
  896. if (this.data.selectedDateList.length < 2) {
  897. let itemData = "weekArrList[" + this.data.weekIndex + "].num";
  898. let num = this.data.weekArrList[this.data.weekIndex].num;
  899. let selectedDateList = this.data.selectedDateList;
  900. selectedDateList.push({
  901. week: this.data.weekIndex,
  902. time: time,
  903. weekStr: this.data.weekArrList[this.data.weekIndex].week
  904. });
  905. let selectedWeekList = this.data.selectedWeekList;
  906. if (selectedWeekList.indexOf(this.data.weekIndex) == -1) {
  907. selectedWeekList.push(this.data.weekIndex);
  908. }
  909. this.setData({
  910. [itemData]: num + 1,
  911. selectTimeIndex: index,
  912. // selectedWeekList:selectedWeekList,
  913. selectedWeekList: [this.data.weekIndex],
  914. selectedDateList: selectedDateList
  915. });
  916. } else {
  917. // 删除第一个
  918. let firstTimeSelected = this.data.selectedDateList[0];
  919. let firstWeekSelectedIndex = firstTimeSelected.week;
  920. // let firstWeekSelected=this.data.weekArrList[firstWeekSelectedIndex];
  921. let itemDataDel = "weekArrList[" + firstWeekSelectedIndex + "].num";
  922. let numDel = this.data.weekArrList[firstWeekSelectedIndex].num;
  923. let selectedDateListDel = this.data.selectedDateList;
  924. selectedDateListDel.shift();
  925. this.setData({
  926. [itemDataDel]: numDel - 1,
  927. // selectTimeIndex:index,
  928. selectedDateList: selectedDateListDel
  929. });
  930. // 加入新选中的
  931. let itemData = "weekArrList[" + this.data.weekIndex + "].num";
  932. let num = this.data.weekArrList[this.data.weekIndex].num;
  933. let selectedDateList = this.data.selectedDateList;
  934. selectedDateList.push({
  935. week: this.data.weekIndex,
  936. time: time,
  937. weekStr: this.data.weekArrList[this.data.weekIndex].week
  938. });
  939. this.setData({
  940. [itemData]: num + 1,
  941. // selectTimeIndex:index,
  942. selectedDateList: selectedDateList
  943. });
  944. let weekListNew = [];
  945. this.data.selectedDateList.forEach(item => {
  946. if (weekListNew.indexOf(item.week) == -1) {
  947. weekListNew.push(item.week);
  948. }
  949. });
  950. this.setData({
  951. selectedWeekList: weekListNew,
  952. });
  953. }
  954. console.log(this.data);
  955. }
  956. },
  957. firstDateCheck(e) {
  958. let index = e.currentTarget.dataset.index;
  959. this.setData({
  960. firstDateIndex: index
  961. });
  962. },
  963. onTabChange(e) {
  964. let weekList = this.data.weekArrList;
  965. weekList.forEach(item => {
  966. item.num = 0;
  967. });
  968. this.setData({
  969. weekArrList: weekList,
  970. activeTab: e.detail.name,
  971. selectTimeIndex: 'null',
  972. selectedWeekList: [],
  973. selectedDateList: []
  974. });
  975. },
  976. showAppoint() {
  977. if (this.data.addressId == 0) {
  978. util.showErrorToast("请选择服务地址");
  979. return;
  980. }
  981. this.setData({
  982. choosePopup: true,
  983. remarkDisable: true,
  984. popupType: (this.data.popupType == 3 ? 2 : this.data.popupType),
  985. isFinishAppoint: false
  986. });
  987. if (this.data.popupType == '1') {
  988. // let index = 2;
  989. // let length = this.data.dateList.length;
  990. // let list = this.data.dateList;
  991. // for (let i = 0; i < length; i++) {
  992. // if (i < 7 && i >= 2 && list[i].week != '周六' && list[i].week != '周日') {
  993. // index = i;
  994. // break;
  995. // }
  996. // }
  997. // console.log(index);
  998. // this.setData({
  999. // dateIndex: index
  1000. // })
  1001. // 获取时间段
  1002. this.getDayStock();
  1003. }
  1004. },
  1005. confirmTime() {
  1006. console.log(this.data.activeTab);
  1007. console.log(this.data);
  1008. if (this.data.popupType == 1) {
  1009. if (this.data.timeIndex === '') {
  1010. wx.showToast({
  1011. title: '请选择服务时间',
  1012. icon: 'none',
  1013. duration: 2000
  1014. });
  1015. return false;
  1016. }
  1017. let date = this.data.dateList[this.data.dateIndex].date;
  1018. let week = this.data.dateList[this.data.dateIndex].week;
  1019. let time = this.data.timeList[this.data.timeIndex].time;
  1020. console.log(week, time);
  1021. this.setData({
  1022. oneTimeAppoint: date + ' ' + week + ' ' + time,
  1023. choosePopup: false,
  1024. remarkDisable: false,
  1025. isFinishAppoint: true
  1026. });
  1027. } else if (this.data.popupType == 2) {
  1028. if (this.data.activeTab == 2) {
  1029. if (this.data.selectedDateList.length < 1) {
  1030. wx.showToast({
  1031. title: '请选择服务时间',
  1032. icon: 'none',
  1033. duration: 2000
  1034. });
  1035. } else if (this.data.selectedDateList.length == 1) {
  1036. wx.showToast({
  1037. title: '请选择第二次服务时间',
  1038. icon: 'none',
  1039. duration: 2000
  1040. });
  1041. } else {
  1042. // 选择频次
  1043. let futureDateList = util.getDateList(new Date(new Date().getTime() + 60 * 60 * 1000 * 24), 1);
  1044. let minIndex = Math.min.apply(null, this.data.selectedWeekList);
  1045. console.log(minIndex);
  1046. console.log((this.data.weekArrList[minIndex]).id);
  1047. let beginDate = futureDateList.filter(item => {
  1048. return (new Date(item.date)).getDay() == (this.data.weekArrList[minIndex]).id
  1049. });
  1050. console.log(beginDate);
  1051. let inter = 7;
  1052. let firstAppointList = util.getDateList(new Date(new Date(beginDate[0].date).getTime()), inter);
  1053. this.setData({
  1054. popupType: 3,
  1055. firstAppointList: firstAppointList
  1056. })
  1057. }
  1058. } else {
  1059. if (this.data.selectedDateList.length == 0) {
  1060. wx.showToast({
  1061. title: '请选择服务时间',
  1062. icon: 'none',
  1063. duration: 2000
  1064. });
  1065. } else {
  1066. // 选择频次
  1067. let futureDateList = util.getDateList(new Date(new Date().getTime() + 60 * 60 * 1000 * 24), 1);
  1068. let minIndex = Math.min.apply(null, this.data.selectedWeekList);
  1069. console.log(minIndex);
  1070. let beginDate = futureDateList.filter(item => {
  1071. return (new Date(item.date)).getDay() == (this.data.weekArrList[minIndex]).id
  1072. });
  1073. console.log(beginDate);
  1074. let inter = (this.data.activeTab == 3 ? 14 : 7);
  1075. let firstAppointList = util.getDateList(new Date(new Date(beginDate[0].date).getTime()), inter);
  1076. this.setData({
  1077. popupType: 3,
  1078. firstAppointList: firstAppointList
  1079. })
  1080. }
  1081. }
  1082. } else if (this.data.popupType == 3) {
  1083. let firstTime = this.data.firstAppointList[this.data.firstDateIndex].date;
  1084. console.log(firstTime);
  1085. console.log(this.data.selectedDateList);
  1086. // this.orderAppoint();
  1087. this.setData({
  1088. choosePopup: false,
  1089. remarkDisable: false,
  1090. isFinishAppoint: true,
  1091. popupType: 2,
  1092. // noSerTimes: res.data,
  1093. // appointFailTip: (res.data >= 1 ? true : false)
  1094. });
  1095. // this.setData({
  1096. // choosePopup:false,
  1097. // isFinishAppoint:true
  1098. // });
  1099. }
  1100. },
  1101. orderAppoint() {
  1102. // 如果是多次卡先预估,单次卡不用预估
  1103. let weeks = [];
  1104. this.data.selectedDateList.forEach(item => {
  1105. let obj = {
  1106. dayOfWeek: this.data.weekArrList[item.week].id,
  1107. startTime: item.time.split('-')[0],
  1108. endTime: item.time.split('-')[1]
  1109. };
  1110. weeks.push(obj);
  1111. });
  1112. let params = {
  1113. weeks: weeks,
  1114. type: this.data.activeTab,
  1115. startDate: this.data.firstAppointList[this.data.firstDateIndex].date,
  1116. lng: this.data.checkedAddress.lng,
  1117. lat: this.data.checkedAddress.lat,
  1118. shopId: this.data.checkedAddress.shopId,
  1119. goodId: this.data.checkedGoodsList[0].goodsId
  1120. };
  1121. wx.showLoading({
  1122. title: '加载中',
  1123. });
  1124. util.request(api.OrderAppoint, params, "POST")
  1125. .then(res => {
  1126. wx.hideLoading();
  1127. if (res.errno == 0) {
  1128. // this.orderPay(params);
  1129. this.setData({
  1130. choosePopup: false,
  1131. remarkDisable: false,
  1132. isFinishAppoint: true,
  1133. popupType: 2,
  1134. // noSerTimes: res.data,
  1135. // appointFailTip: (res.data >= 1 ? true : false)
  1136. });
  1137. } else {
  1138. // util.showErrorToast(res.errmsg);
  1139. wx.showToast({
  1140. title: res.errmsg,
  1141. icon: 'none',
  1142. duration: 2000
  1143. });
  1144. }
  1145. }).catch(function (err) {
  1146. console.log(err);
  1147. wx.hideLoading();
  1148. wx.showToast({
  1149. title: err.errMsg,
  1150. icon: 'none'
  1151. })
  1152. });
  1153. },
  1154. cancelPay() {
  1155. this.setData({
  1156. payFailShow: false,
  1157. remarkDisable: false
  1158. });
  1159. wx.navigateBack({
  1160. delta: 1,
  1161. })
  1162. },
  1163. rePay() {
  1164. wx.showLoading({
  1165. title: '支付中',
  1166. })
  1167. this.setData({
  1168. payFailShow: false,
  1169. remarkDisable: false
  1170. });
  1171. util.request(api.OrderPrepay, {
  1172. orderId: this.data.orderId
  1173. }, 'POST').then(res => {
  1174. if (res.errno === 0) {
  1175. const payParam = res.data;
  1176. console.log("支付过程开始")
  1177. wx.requestPayment({
  1178. 'timeStamp': payParam.timeStamp,
  1179. 'nonceStr': payParam.nonceStr,
  1180. 'package': payParam.packageValue,
  1181. 'signType': payParam.signType,
  1182. 'paySign': payParam.paySign,
  1183. 'success': res => {
  1184. console.log("支付过程成功");
  1185. wx.redirectTo({
  1186. url: '/pages/checkout/payResult/payResult?status=1&orderId=' + this.data.orderId + '&actualPrice=' + this.data.actualPrice
  1187. });
  1188. },
  1189. 'fail': res => {
  1190. console.log("支付过程失败")
  1191. util.showErrorToast('支付失败');
  1192. },
  1193. 'complete': res => {
  1194. wx.hideLoading();
  1195. console.log("支付过程结束");
  1196. }
  1197. })
  1198. } else {
  1199. wx.hideLoading();
  1200. };
  1201. }).catch(res => {
  1202. wx.hideLoading();
  1203. });
  1204. },
  1205. goBuyMember() {
  1206. wx.navigateTo({
  1207. url: "/pages/member/buyMember/buyMember"
  1208. });
  1209. },
  1210. getDayStock() {
  1211. if (this.data.dateIndex < 0) {
  1212. return false;
  1213. }
  1214. let params = {
  1215. day: this.data.dateList[this.data.dateIndex].date,
  1216. goodsId: this.data.checkedGoodsList[0].goodsId,
  1217. lng: this.data.checkedAddress.lng,
  1218. lat: this.data.checkedAddress.lat
  1219. }
  1220. wx.showLoading({
  1221. title: '加载中',
  1222. });
  1223. util.request(api.EveryDayStock, params, "GET")
  1224. .then(res => {
  1225. wx.hideLoading();
  1226. if (res.errno == 0) {
  1227. this.setData({
  1228. timeList: res.data,
  1229. timeIndex: 0
  1230. });
  1231. // let validList = this.data.timeList.filter(item => {
  1232. // return item.stock > 0
  1233. // });
  1234. // if (validList.length > 0) {
  1235. // this.setData({
  1236. // timeIndex: validList[0].id - 1
  1237. // });
  1238. // } else {
  1239. // this.setData({
  1240. // timeIndex: ''
  1241. // });
  1242. // }
  1243. } else {
  1244. wx.showToast({
  1245. title: res.errmsg,
  1246. icon: 'none',
  1247. duration: 2000
  1248. });
  1249. this.setData({
  1250. timeList: [],
  1251. timeIndex: ''
  1252. });
  1253. }
  1254. }).catch(function (err) {
  1255. console.log(err);
  1256. wx.hideLoading();
  1257. wx.showToast({
  1258. title: err.errMsg,
  1259. icon: 'none'
  1260. });
  1261. this.setData({
  1262. timeList: [],
  1263. timeIndex: ''
  1264. });
  1265. });
  1266. },
  1267. });