checkout.js 39 KB

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