reserveCar.js 16 KB

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