reserveCar.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. // pages/reserveProject/reserveProject.js
  2. let date = new Date()
  3. let times = []
  4. var weekday = new Array(7)
  5. weekday[0] = '周日'
  6. weekday[1] = '周一'
  7. weekday[2] = '周二'
  8. weekday[3] = '周三'
  9. weekday[4] = '周四'
  10. weekday[5] = '周五'
  11. weekday[6] = '周六'
  12. for (let i = 0; i < 15; i++) {
  13. let stamp = date.setDate(date.getDate() + (i == 0 ? 0 : 1))
  14. let year = new Date(stamp).getFullYear()
  15. let month =
  16. new Date(stamp).getMonth() + 1 < 10 ?
  17. '0' + (new Date(stamp).getMonth() + 1) :
  18. new Date(stamp).getMonth() + 1
  19. let day =
  20. new Date(stamp).getDate() < 10 ?
  21. '0' + new Date(stamp).getDate() :
  22. new Date(stamp).getDate()
  23. let week = weekday[new Date(stamp).getDay()]
  24. times.push({
  25. date: `${year + '-' + month + '-' + day}`,
  26. day: `${month + '-' + day}`,
  27. week: week
  28. })
  29. }
  30. // let _hour = ( 10 > date.getHours() ) ? '0' + date.getHours() : date.getHours();
  31. // let _minute = ( 10 > date.getMinutes() ) ? '0' + date.getMinutes() : date.getMinutes();
  32. const app = getApp()
  33. import {
  34. get,
  35. post
  36. } from '../../utils/http'
  37. import {
  38. timestampToDate
  39. } from '../../utils/time'
  40. Page({
  41. /**
  42. * 页面的初始数据
  43. */
  44. data: {
  45. times,
  46. time: '', // 选中的时间(小时:分钟)
  47. currentTimestamp: 0, // 选中的时间戳
  48. currentDateIndex: 0,
  49. currentSwiperIndex: 0,
  50. currentTimeIndex: -2,
  51. currentDeviceIndex: -1,
  52. money: 0,
  53. projectTime: [],
  54. projectData: {},
  55. projectList: [],
  56. deviceList: [],
  57. currentChangeTime: null,
  58. currentChangeDevice: null,
  59. source: 'project', // 来源
  60. inFirstIn: false,
  61. inSecIn: false,
  62. ids: '',
  63. needTime: 0,
  64. showStore: false, //选择店铺开关
  65. storeList: [], //店铺列表
  66. },
  67. // 预约数据包,json字符,其中id为项目ID,product_id为耗材ID,如:[{id:10,product_id:11},{id:12}]
  68. // v2/api/order/add
  69. toBuyNow: function () {
  70. // 判断是否是预约的益田店
  71. // if(this.data.projectData.store_id==17){
  72. // this.setData({
  73. // showStore:true
  74. // })
  75. // wx.showToast({
  76. // title: '请您切换店铺',
  77. // icon:'none'
  78. // })
  79. // return
  80. // }
  81. let {
  82. projectData,
  83. currentDateIndex,
  84. projectList,
  85. time
  86. } = this.data
  87. let dataIds = []
  88. if (this.data.onType == 'one') {
  89. dataIds = [{
  90. id: this.data.keyData.id
  91. }]
  92. } else {
  93. for (let i = 0; i < projectList.length; i++) {
  94. let ins = {}
  95. ins.id = projectList[i].id
  96. if (projectList[i].selectMaterial) {
  97. ins.product_id = projectList[i].selectMaterial.id
  98. }
  99. dataIds.push(ins)
  100. }
  101. }
  102. console.log(dataIds)
  103. // 预约数据包,json字符,其中id为项目ID(一站式项目不需要),product_id为耗材ID或一站式ID,如:[{id:10,product_id:11},{id:12}]
  104. wx.showLoading({
  105. title: '预约中',
  106. mask: true
  107. });
  108. post(
  109. 'v2/api/order/add', {
  110. order_data: JSON.stringify(dataIds),
  111. store_id: projectData.store_id,
  112. order_day: times[currentDateIndex].date,
  113. order_time: time,
  114. type: this.data.onType,
  115. share_user_id: app.globalData.shareUserId || '',
  116. },
  117. (res) => {
  118. console.log('xxxxxx')
  119. console.log(res.data)
  120. let currentPage = getCurrentPages(); // 获取当前页的数据,
  121. let previousPage = currentPage[currentPage.length - 2]; // 上一页的数据,
  122. previousPage.setData({ //对上一个页面data中的参数进行赋值(上一个页面的数据是在当前页面进行赋值的),就变相实现了向上一个页面传值
  123. selectProductCar: []
  124. })
  125. // previousPage.showCarMoney()
  126. wx.hideLoading();
  127. // 获取下发权限
  128. wx.requestSubscribeMessage({
  129. tmplIds: ['RNife3ZhTYYpUuT26ylGQj8W0v07IPo2TGe941iR3y8'],
  130. success(res) {
  131. console.log(res);
  132. let status = ''
  133. if (res['RNife3ZhTYYpUuT26ylGQj8W0v07IPo2TGe941iR3y8'] == 'reject') {
  134. status = 2
  135. } else {
  136. status = 1
  137. }
  138. get('/v2/api/activity/push', {
  139. type: 2,
  140. status,
  141. }, res => {})
  142. },
  143. })
  144. // 推荐方案 测肤 埋点
  145. if (app.globalData.isSkinPlan || app.globalData.isTestSkin) {
  146. let url = app.globalData.isTestSkin ? '/v2/api/order/skin' : 'v2/api/order/recommend'
  147. get(url, {
  148. type: 2
  149. }, (res) => {
  150. if (res.code == 200) {
  151. this.globalData.isSkinPlan = false
  152. this.globalData.isTestSkin = false
  153. }
  154. })
  155. }
  156. wx.redirectTo({
  157. url: `/pages/orderBySuccess/orderBySuccess?&store_id=${this.data.projectData.store_id}&&orderdata=` + JSON.stringify(res.data),
  158. })
  159. })
  160. },
  161. knowSubmit: function () {
  162. this.setData({
  163. inFirstIn: false,
  164. inSecIn: false
  165. })
  166. let version3 = wx.getStorageSync('version3')
  167. if (!version3 || version3 != 1) {
  168. this.setData({
  169. inSecIn: true
  170. })
  171. wx.setStorageSync('version3', 1)
  172. }
  173. },
  174. noneEnoughPeople() {
  175. },
  176. /**
  177. * 生命周期函数--监听页面加载
  178. */
  179. onLoad: function (options) {
  180. let date = new Date()
  181. let currentTimestamp = date.getTime() / 1000 + 60 * 2
  182. let _time = timestampToDate(currentTimestamp, 'H:i')
  183. let projectList = {}
  184. if (options.type == 'one') {
  185. projectList = JSON.parse(options.data).items
  186. this.setData({
  187. keyData: JSON.parse(options.data)
  188. })
  189. } else {
  190. projectList = JSON.parse(options.data)
  191. }
  192. let ids = ''
  193. let needTime = 0
  194. for (let i = 0; i < projectList.length; i++) {
  195. ids = ids + ',' + projectList[i].id
  196. needTime = needTime + projectList[i].use_time
  197. }
  198. // let source = options.source || 'project'
  199. // console.log(JSON.parse(options.data))
  200. let projectData = {}
  201. // if (options.projectData) {
  202. // let projectData = decodeURIComponent(options.projectData)
  203. // projectData = JSON.parse(projectData)
  204. console.log('app.globalData.storeData')
  205. console.log(app.globalData.storeData)
  206. console.log('app.globalData.storeData')
  207. projectData.store_id = app.globalData.storeData.id
  208. projectData.storeData = app.globalData.storeData
  209. this.setData({
  210. onType: options.type,
  211. money: options.money,
  212. ids: ids.substring(1, ids.length),
  213. projectList: projectList,
  214. projectData: projectData,
  215. source: 'project',
  216. currentTimestamp: currentTimestamp,
  217. time: _time,
  218. needTime: needTime,
  219. })
  220. this.getHourDate()
  221. //获取店铺列表
  222. if (projectData.store_id == 17) {
  223. this.getStoreList()
  224. }
  225. // 项目预约 - 获取可选时间
  226. this.getTime()
  227. let lat1 = app.address.latitude
  228. let lng1 = app.address.longitude
  229. this.setData({
  230. lat1: lat1,
  231. lng1: lng1,
  232. })
  233. },
  234. getHourDate() {
  235. let dataTime = this.data.time.split(':')
  236. let needTime = this.data.needTime
  237. let minHour = parseInt((dataTime[1] * 1 + needTime * 1) / 60)
  238. let dataHour = Number(minHour) + Number(dataTime[0])
  239. let min = null
  240. if ((Number(dataTime[1]) + needTime) > 60) {
  241. min = (Number(dataTime[1]) + needTime) % 60
  242. } else if ((Number(dataTime[1]) + needTime) === 60) {
  243. min = 0
  244. } else {
  245. min = (Number(dataTime[1]) + needTime)
  246. }
  247. let dataHourShow = dataHour > 23 ? '(明天)' + (dataHour * 1 - 24) : dataHour
  248. this.setData({
  249. showTimes: dataHourShow + ":" + (min < 10 ? `0${min}` : min)
  250. })
  251. },
  252. /**
  253. * 生命周期函数--监听页面初次渲染完成
  254. */
  255. onReady: function () {},
  256. /**
  257. * 生命周期函数--监听页面显示
  258. */
  259. onShow: function () {
  260. let version2 = wx.getStorageSync('version2')
  261. if (!version2 || version2 != 1) {
  262. this.setData({
  263. inFirstIn: true
  264. })
  265. wx.setStorageSync('version2', 1)
  266. } else {
  267. let version3 = wx.getStorageSync('version3')
  268. if (!version3 || version3 != 1) {
  269. this.setData({
  270. inSecIn: true
  271. })
  272. wx.setStorageSync('version3', 1)
  273. }
  274. }
  275. },
  276. /**
  277. * 生命周期函数--监听页面隐藏
  278. */
  279. onHide: function () {},
  280. /**
  281. * 生命周期函数--监听页面卸载
  282. */
  283. onUnload: function () {},
  284. /**
  285. * 页面相关事件处理函数--监听用户下拉动作
  286. */
  287. onPullDownRefresh: function () {},
  288. /**
  289. * 页面上拉触底事件的处理函数
  290. */
  291. onReachBottom: function () {},
  292. /**
  293. * 用户点击右上角分享
  294. */
  295. onShareAppMessage: function () {},
  296. /**
  297. * 日期选择
  298. */
  299. onChangeDate(e) {
  300. let {
  301. currentDateIndex
  302. } = this.data
  303. if (currentDateIndex != e.currentTarget.dataset.index) {
  304. this.setData({
  305. currentDateIndex: e.currentTarget.dataset.index,
  306. currentChangeDevice: null,
  307. currentChangeTime: null,
  308. currentTimeIndex: -2,
  309. currentSwiperIndex: 0,
  310. currentDeviceIndex: -1,
  311. deviceList: []
  312. },
  313. () => {
  314. this.getTime()
  315. }
  316. )
  317. }
  318. },
  319. /**
  320. * 时间段切换
  321. */
  322. onChangeSwiper(e) {
  323. this.setData({
  324. currentSwiperIndex: e.currentTarget.dataset.index
  325. })
  326. },
  327. /**
  328. * 时间段切换
  329. */
  330. changeSwiper(e) {
  331. this.setData({
  332. currentSwiperIndex: e.detail.current
  333. })
  334. },
  335. /**
  336. * 时间选择
  337. */
  338. onChangeTime(e) {
  339. console.log('eeeee')
  340. let {
  341. projectTime,
  342. currentSwiperIndex,
  343. currentChangeTime,
  344. currentTimeIndex
  345. } = this.data
  346. projectTime[currentSwiperIndex].list.forEach((item, index) => {
  347. if (item.index == e.currentTarget.dataset.index) {
  348. currentChangeTime = item
  349. }
  350. })
  351. if (currentTimeIndex != e.currentTarget.dataset.index) {
  352. console.log('asdasdasdasd')
  353. console.log(currentChangeTime)
  354. this.setData({
  355. currentTimeIndex: e.currentTarget.dataset.index,
  356. currentChangeTime,
  357. time: currentChangeTime.order_time,
  358. currentChangeDevice: null,
  359. currentDeviceIndex: -1
  360. })
  361. // this.bindTimeChange({
  362. // detail: {
  363. // value: currentChangeTime.order_time
  364. // }
  365. // })
  366. this.getHourDate()
  367. // this.getProjectDevice()
  368. }
  369. },
  370. /**
  371. * 点击自定义时间
  372. * v2/api/product/check_time
  373. */
  374. bindTimeChange(e) {
  375. let that = this
  376. let {
  377. projectData,
  378. currentDateIndex,
  379. projectTime,
  380. currentSwiperIndex,
  381. time
  382. } = this.data
  383. get(
  384. 'v2/api/product/check_time', {
  385. ids: this.data.ids,
  386. store_id: projectData.store_id,
  387. order_day: times[currentDateIndex].date,
  388. order_time: e.detail.value
  389. },
  390. (res) => {
  391. console.log(res)
  392. if (res.data.status == 1) {
  393. that.setData({
  394. time: e.detail.value,
  395. currentTimeIndex: -1
  396. })
  397. that.getHourDate()
  398. } else {
  399. }
  400. }
  401. )
  402. // let {
  403. // currentDateIndex,
  404. // currentChangeTime
  405. // } = this.data
  406. // currentChangeTime = {
  407. // order_day: times[currentDateIndex].date,
  408. // time_id: e.detail.value
  409. // }
  410. // this.setData({
  411. // time: e.detail.value,
  412. // currentTimeIndex: -1,
  413. // currentChangeTime,
  414. // currentChangeDevice: null,
  415. // currentDeviceIndex: -1
  416. // })
  417. // this.getProjectDevice()
  418. },
  419. // /**
  420. // * 点击自定义时间
  421. // */
  422. // bindTimeChange(e) {
  423. // let {
  424. // currentDateIndex,
  425. // currentChangeTime
  426. // } = this.data
  427. // currentChangeTime = {
  428. // order_day: times[currentDateIndex].date,
  429. // time_id: e.detail.value
  430. // }
  431. // this.setData({
  432. // time: e.detail.value,
  433. // currentTimeIndex: -1,
  434. // currentChangeTime,
  435. // currentChangeDevice: null,
  436. // currentDeviceIndex: -1
  437. // })
  438. // this.getProjectDevice()
  439. // },
  440. /**
  441. * 选择设备
  442. */
  443. onChangeDevice(e) {
  444. let {
  445. deviceList
  446. } = this.data
  447. this.setData({
  448. currentDeviceIndex: e.currentTarget.dataset.index,
  449. currentChangeDevice: deviceList[e.currentTarget.dataset.index]
  450. })
  451. },
  452. /**
  453. * 获取可预约时间
  454. */
  455. getTime() {
  456. let {
  457. projectData,
  458. currentDateIndex,
  459. projectTime,
  460. currentSwiperIndex,
  461. time
  462. } = this.data
  463. get(
  464. 'v2/api/product/order_time', {
  465. ids: this.data.ids,
  466. store_id: projectData.store_id,
  467. order_day: times[currentDateIndex].date,
  468. },
  469. (res) => {
  470. projectTime[0] = res.data.day
  471. projectTime[1] = res.data.night
  472. let sum = 0
  473. let defaultTimeIndex = -1
  474. projectTime.forEach((item, index) => {
  475. if (item.active == 1) {
  476. currentSwiperIndex = index
  477. }
  478. item.list.forEach((item, index) => {
  479. item.index = sum
  480. sum++
  481. // 默认选中第一个可预约时间(用于后面的默认绑定设备)
  482. if (item.status == 1 && defaultTimeIndex == -1) {
  483. defaultTimeIndex = item.index
  484. }
  485. })
  486. })
  487. this.setData({
  488. projectTime,
  489. currentSwiperIndex
  490. }, () => {
  491. // 默认选中第一个可预约时间
  492. // TODO 产品调整需求默认选中自定义时间
  493. // if (defaultTimeIndex != -1) {
  494. // this.onChangeTime({
  495. // currentTarget: { dataset: { index: defaultTimeIndex } }
  496. // })
  497. // }
  498. this.bindTimeChange({
  499. detail: {
  500. value: time
  501. }
  502. })
  503. })
  504. }
  505. )
  506. },
  507. /**
  508. * 获取项目可预约的设备
  509. * /api/project/device
  510. */
  511. getProjectDevice() {
  512. let {
  513. currentTimeIndex,
  514. time,
  515. projectTime,
  516. currentSwiperIndex,
  517. projectData,
  518. currentDateIndex
  519. } = this.data
  520. let order_time_id
  521. if (currentTimeIndex != -1) {
  522. projectTime[currentSwiperIndex].list.forEach((item, index) => {
  523. if (item.index == currentTimeIndex) {
  524. order_time_id = item.time_id
  525. }
  526. })
  527. } else {
  528. order_time_id = time
  529. }
  530. console.log(projectData)
  531. console.log(order_time_id)
  532. return new Promise((resolve, reject) => {
  533. get(
  534. 'api/project/device', {
  535. store_id: projectData.store_id,
  536. project_id: projectData.id,
  537. order_day: times[currentDateIndex].date,
  538. order_time_id: order_time_id,
  539. use_time: projectData.use_time
  540. },
  541. (res) => {
  542. this.setData({
  543. deviceList: res.data
  544. },
  545. () => {
  546. this.onChangeDevice({
  547. currentTarget: {
  548. dataset: {
  549. index: 0
  550. }
  551. }
  552. })
  553. }
  554. )
  555. resolve()
  556. console.log(res)
  557. },
  558. (rej) => {
  559. this.setData({
  560. currentTimeIndex: -2,
  561. currentChangeTime: null
  562. })
  563. console.log(rej)
  564. }
  565. )
  566. })
  567. },
  568. /**
  569. * 提交订单
  570. */
  571. addOrder() {
  572. let {
  573. projectData,
  574. currentTimeIndex,
  575. time,
  576. currentDateIndex,
  577. currentChangeTime,
  578. currentChangeDevice,
  579. deviceList
  580. } = this.data
  581. if (currentTimeIndex == -1) {
  582. currentChangeTime = {
  583. order_day: times[currentDateIndex].date,
  584. time_id: time,
  585. order_time: time
  586. }
  587. }
  588. console.log(currentChangeTime)
  589. if (currentChangeTime == null) {
  590. wx.showToast({
  591. title: '请选择预约时间',
  592. icon: 'none'
  593. })
  594. } else if (
  595. new Date(
  596. `${currentChangeTime.order_day} ${currentChangeTime.order_time}`
  597. ) <= new Date()
  598. ) {
  599. wx.showToast({
  600. title: '所选时间已超时,请重新选择',
  601. icon: 'none'
  602. })
  603. } else if (currentChangeDevice == null) {
  604. let _title = '请选择设备'
  605. if (deviceList.length == 0) {
  606. _title = '当前所选时间无可用设备'
  607. }
  608. wx.showToast({
  609. title: _title,
  610. icon: 'none'
  611. })
  612. } else {
  613. wx.navigateTo({
  614. url: `/pages/orderConfirm/orderConfirm?source=${
  615. this.data.source
  616. }&data=${JSON.stringify(projectData)}&deviceData=${JSON.stringify(
  617. currentChangeDevice
  618. )}&timeData=${JSON.stringify(currentChangeTime)}`
  619. })
  620. }
  621. },
  622. // 切换店铺
  623. changeStore() {
  624. console.log(this.data.projectData.storeData);
  625. wx.navigateTo({
  626. url: `/pages/store/store?isSelectProduct=true&&project_id=${JSON.stringify(this.data.ids)}&&defaultStore=${JSON.stringify(this.data.projectData.storeData)}`,
  627. })
  628. },
  629. /**
  630. * 店铺信息
  631. * /api/store/info
  632. */
  633. getStoreInfo(store_id) {
  634. get(
  635. 'api/store/info', {
  636. store_id
  637. },
  638. (res) => {
  639. let projectData = this.data.projectData
  640. projectData.store_id = res.data.id
  641. projectData.storeData = res.data
  642. this.setData({
  643. projectData,
  644. // showStore:false
  645. })
  646. this.getTime()
  647. //获取店铺列表
  648. if (res.data.id == 17) {
  649. this.getStoreList()
  650. }
  651. }
  652. )
  653. },
  654. // 获取店铺列表
  655. getStoreList() {
  656. get(
  657. 'api/store', {
  658. page: 1,
  659. limit: 10,
  660. },
  661. (res) => {
  662. // res.data.list.forEach((item, index) => {
  663. // if (item.id == 17) {
  664. // this.setData({
  665. // showStore: true
  666. // })
  667. // }
  668. // if (item.id == 17 || item.id == 1) {
  669. // res.data.list.splice(index, 1)
  670. // }
  671. // })
  672. this.setData({
  673. storeList: res.data.list
  674. })
  675. }
  676. )
  677. },
  678. /**
  679. * 选中店铺
  680. */
  681. onChangeStore(e) {
  682. let storeId = e.currentTarget.dataset.item.id
  683. this.getStoreInfo(storeId)
  684. },
  685. // 关闭选择店铺弹窗
  686. // onCloseLandscape() {
  687. // this.setData({
  688. // showStore: false
  689. // })
  690. // }
  691. })