checkout.js 39 KB

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