reserveProduct.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. // pages/reserveProduct/reserveProduct.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 { get, post } from '../../utils/http'
  34. import { timestampToDate } from '../../utils/time'
  35. Page({
  36. /**
  37. * 页面的初始数据
  38. */
  39. data: {
  40. times,
  41. time: '',
  42. currentDateIndex: 0,
  43. currentSwiperIndex: 0,
  44. currentTimeIndex: -2,
  45. currentDeviceIndex: -1,
  46. projectTime: [],
  47. productData: {}, // 项目耗材内容
  48. projectData: {}, // 项目内容
  49. deviceList: [],
  50. currentChangeTime: null,
  51. currentChangeDevice: null,
  52. source: 'product', // 来源
  53. projectOrderList: [], // 项目耗材-可用订单列表
  54. productProjectList: [], // 项目耗材-项目列表
  55. currentProjectOrderIndex: -1, // 项目耗材-选中的订单下标
  56. currentProductProjectIndex: -1, // 项目耗材-选中的项目下标
  57. projectOrderData: {}, // 项目耗材-可用订单内容
  58. showDateControl: 0, // 展示预约日期控件
  59. showDeviceControl: 0, // 展示预约设备控件
  60. allowAddOrder: 0 // 是否允许下单
  61. },
  62. /**
  63. * 生命周期函数--监听页面加载
  64. */
  65. onLoad: function (options) {
  66. let date = new Date()
  67. let currentTimestamp = date.getTime() / 1000 + 60 * 2
  68. let _time = timestampToDate(currentTimestamp, 'H:i')
  69. let source = options.source || 'product'
  70. if (options.productData) {
  71. let productData = decodeURIComponent(options.productData)
  72. productData = JSON.parse(productData)
  73. productData.store_address = app.globalData.storeData.belong_region
  74. this.setData({
  75. productData: productData,
  76. time: _time,
  77. source: source
  78. })
  79. if (source == 'product') {
  80. // 项目耗材预约 - 获取可绑定订单
  81. this.getProjectOrder()
  82. }
  83. }
  84. },
  85. /**
  86. * 生命周期函数--监听页面初次渲染完成
  87. */
  88. onReady: function () {},
  89. /**
  90. * 生命周期函数--监听页面显示
  91. */
  92. onShow: function () {},
  93. /**
  94. * 生命周期函数--监听页面隐藏
  95. */
  96. onHide: function () {},
  97. /**
  98. * 生命周期函数--监听页面卸载
  99. */
  100. onUnload: function () {},
  101. /**
  102. * 页面相关事件处理函数--监听用户下拉动作
  103. */
  104. onPullDownRefresh: function () {},
  105. /**
  106. * 页面上拉触底事件的处理函数
  107. */
  108. onReachBottom: function () {},
  109. /**
  110. * 用户点击右上角分享
  111. */
  112. onShareAppMessage: function () {},
  113. /**
  114. * 项目耗材预约
  115. * 获取可以绑定的预约订单
  116. */
  117. getProjectOrder() {
  118. let { productData } = this.data
  119. get(
  120. 'api/product/get_project_order',
  121. {
  122. product_id: productData.id,
  123. store_id: productData.store_id
  124. },
  125. (res) => {
  126. // res.data.project_list.map((item) => {
  127. // // TODO 处理会员价和原价
  128. // })
  129. this.setData({
  130. projectOrderList: res.data.order_list,
  131. productProjectList: res.data.project_list
  132. })
  133. }
  134. )
  135. },
  136. /**
  137. * 获取可预约时间
  138. */
  139. getTime() {
  140. let {
  141. productData,
  142. currentDateIndex,
  143. projectTime,
  144. currentSwiperIndex,
  145. projectData,
  146. time
  147. } = this.data
  148. get(
  149. 'api/project/time',
  150. {
  151. store_id: productData.store_id,
  152. project_id: projectData.project_id,
  153. order_day: times[currentDateIndex].date,
  154. use_time: projectData.use_time
  155. },
  156. (res) => {
  157. projectTime[0] = res.data.day
  158. projectTime[1] = res.data.night
  159. let sum = 0
  160. let defaultTimeIndex = -1
  161. projectTime.forEach((item, index) => {
  162. if (item.active == 1) {
  163. currentSwiperIndex = index
  164. }
  165. item.list.forEach((item, index) => {
  166. item.index = sum
  167. sum++
  168. // 默认选中第一个可预约时间(用于后面的默认绑定设备)
  169. if (item.status == 1 && defaultTimeIndex == -1) {
  170. defaultTimeIndex = item.index
  171. }
  172. })
  173. })
  174. this.setData(
  175. { projectTime, currentSwiperIndex, showDateControl: 1 },
  176. () => {
  177. // 默认选中第一个可预约时间
  178. // if (defaultTimeIndex != -1) {
  179. // this.onChangeTime({
  180. // currentTarget: { dataset: { index: defaultTimeIndex } }
  181. // })
  182. // }
  183. this.bindTimeChange({
  184. detail: { value: time }
  185. })
  186. }
  187. )
  188. }
  189. )
  190. },
  191. /**
  192. * 获取项目可预约的设备
  193. * /api/project/device
  194. */
  195. getProjectDevice() {
  196. let {
  197. currentTimeIndex,
  198. time,
  199. projectTime,
  200. currentSwiperIndex,
  201. productData,
  202. projectData,
  203. currentDateIndex
  204. } = this.data
  205. let order_time_id
  206. if (currentTimeIndex != -1) {
  207. projectTime[currentSwiperIndex].list.forEach((item, index) => {
  208. if (item.index == currentTimeIndex) {
  209. order_time_id = item.time_id
  210. }
  211. })
  212. } else {
  213. order_time_id = time
  214. }
  215. console.log(productData)
  216. console.log(order_time_id)
  217. return new Promise((resolve, reject) => {
  218. get(
  219. 'api/project/device',
  220. {
  221. store_id: productData.store_id,
  222. project_id: projectData.project_id,
  223. order_day: times[currentDateIndex].date,
  224. order_time_id: order_time_id,
  225. use_time: projectData.use_time
  226. },
  227. (res) => {
  228. this.setData(
  229. {
  230. deviceList: res.data
  231. },
  232. () => {
  233. this.onChangeDevice({ currentTarget: { dataset: { index: 0 } } })
  234. }
  235. )
  236. resolve()
  237. console.log(res)
  238. },
  239. (rej) => {
  240. this.setData({
  241. currentTimeIndex: -2,
  242. currentChangeTime: null,
  243. currentDeviceIndex: -1,
  244. currentChangeDevice: null,
  245. allowAddOrder: 0
  246. })
  247. console.log(rej)
  248. }
  249. )
  250. })
  251. },
  252. /**
  253. * 日期选择
  254. */
  255. onChangeDate(e) {
  256. let { currentDateIndex } = this.data
  257. if (currentDateIndex != e.currentTarget.dataset.index) {
  258. this.setData(
  259. {
  260. currentDateIndex: e.currentTarget.dataset.index,
  261. currentChangeDevice: null,
  262. currentChangeTime: null,
  263. currentTimeIndex: -2,
  264. currentSwiperIndex: 0,
  265. currentDeviceIndex: -1,
  266. deviceList: []
  267. },
  268. () => {
  269. this.getTime()
  270. }
  271. )
  272. }
  273. },
  274. /**
  275. * 时间段切换
  276. */
  277. onChangeSwiper(e) {
  278. this.setData({
  279. currentSwiperIndex: e.currentTarget.dataset.index
  280. })
  281. },
  282. /**
  283. * 时间段切换
  284. */
  285. changeSwiper(e) {
  286. this.setData({
  287. currentSwiperIndex: e.detail.current
  288. })
  289. },
  290. /**
  291. * 时间选择
  292. */
  293. onChangeTime(e) {
  294. let {
  295. projectTime,
  296. currentSwiperIndex,
  297. currentChangeTime,
  298. currentTimeIndex
  299. } = this.data
  300. projectTime[currentSwiperIndex].list.forEach((item, index) => {
  301. if (item.index == e.currentTarget.dataset.index) {
  302. currentChangeTime = item
  303. }
  304. })
  305. if (currentTimeIndex != e.currentTarget.dataset.index) {
  306. this.setData({
  307. currentTimeIndex: e.currentTarget.dataset.index,
  308. currentChangeTime,
  309. currentChangeDevice: null,
  310. currentDeviceIndex: -1
  311. })
  312. this.getProjectDevice()
  313. }
  314. },
  315. /**
  316. * 点击自定义时间
  317. */
  318. bindTimeChange(e) {
  319. let { currentDateIndex, currentChangeTime } = this.data
  320. currentChangeTime = {
  321. order_day: times[currentDateIndex].date,
  322. time_id: e.detail.value
  323. }
  324. this.setData({
  325. time: e.detail.value,
  326. currentTimeIndex: -1,
  327. currentChangeTime,
  328. currentChangeDevice: null,
  329. currentDeviceIndex: -1
  330. })
  331. this.getProjectDevice()
  332. },
  333. /**
  334. * 选择设备
  335. */
  336. onChangeDevice(e) {
  337. let { deviceList } = this.data
  338. this.setData(
  339. {
  340. currentDeviceIndex: e.currentTarget.dataset.index,
  341. currentChangeDevice: deviceList[e.currentTarget.dataset.index]
  342. },
  343. () => {
  344. // 控制确定按钮可点击
  345. this.setData({ allowAddOrder: 1 })
  346. }
  347. )
  348. },
  349. /**
  350. * 项目耗材预约
  351. * 选择推荐去支付的项目
  352. */
  353. selectProductProject(e) {
  354. let { productData } = this.data
  355. let item = e.currentTarget.dataset.item
  356. let currentProductProjectIndex = e.currentTarget.dataset.index
  357. if (currentProductProjectIndex == this.data.currentProductProjectIndex) {
  358. currentProductProjectIndex = -1
  359. this.setData({
  360. currentProductProjectIndex,
  361. currentDateIndex: -1,
  362. currentTimeIndex: -2,
  363. currentDeviceIndex: -1,
  364. ['productData.project_id']: -1,
  365. showDateControl: 0,
  366. showDeviceControl: 0,
  367. allowAddOrder: 0
  368. })
  369. return
  370. }
  371. // 参数字段不一致,对齐其他场景所需参数
  372. item = Object.assign(
  373. {
  374. id: item.project_id,
  375. name: item.project_name,
  376. banner_urls: [item.cover_url],
  377. price: item.original_price,
  378. store_id: productData.store_id
  379. },
  380. item
  381. )
  382. // 有效选项
  383. this.setData(
  384. {
  385. currentProductProjectIndex,
  386. currentDateIndex: 0,
  387. ['productData.project_id']: item.project_id,
  388. projectData: item
  389. },
  390. () => {
  391. this.getTime()
  392. }
  393. )
  394. },
  395. /**
  396. * 项目耗材预约
  397. * 选择已支付的项目订单
  398. */
  399. selectProjectOrder(e) {
  400. let { productData } = this.data
  401. let item = e.currentTarget.dataset.item
  402. let currentProjectOrderIndex = e.currentTarget.dataset.index
  403. if (currentProjectOrderIndex == this.data.currentProjectOrderIndex) {
  404. currentProjectOrderIndex = -1
  405. this.setData({
  406. currentProjectOrderIndex,
  407. allowAddOrder: 0
  408. })
  409. return
  410. }
  411. // 有效选项
  412. this.setData({
  413. currentProjectOrderIndex,
  414. projectOrderData: item,
  415. allowAddOrder: 1
  416. })
  417. },
  418. /**
  419. * 提交订单
  420. */
  421. addOrder() {
  422. let {
  423. productData,
  424. projectData,
  425. projectOrderData,
  426. currentTimeIndex,
  427. time,
  428. currentDateIndex,
  429. currentChangeTime,
  430. currentChangeDevice,
  431. allowAddOrder,
  432. deviceList
  433. } = this.data
  434. if (currentTimeIndex == -1) {
  435. currentChangeTime = {
  436. order_day: times[currentDateIndex].date,
  437. time_id: time,
  438. order_time: time
  439. }
  440. }
  441. console.log(currentChangeTime)
  442. if (!allowAddOrder) {
  443. wx.showToast({
  444. title: '请先选择使用项目',
  445. icon: 'none'
  446. })
  447. return
  448. }
  449. // 选择将升级的产品绑定至项目订单
  450. if (projectOrderData.order_id) {
  451. wx.navigateTo({
  452. url: `/pages/orderConfirm/orderConfirm?source=product_with_project_order&data=${JSON.stringify(
  453. projectOrderData
  454. )}&deviceData={}&timeData={}&productData=${JSON.stringify(
  455. productData
  456. )}&projectOrderData=${JSON.stringify(projectOrderData)}`
  457. })
  458. return
  459. }
  460. if (currentChangeTime == null) {
  461. wx.showToast({
  462. title: '请选择预约时间',
  463. icon: 'none'
  464. })
  465. } else if (
  466. new Date(
  467. `${currentChangeTime.order_day} ${currentChangeTime.order_time}`
  468. ) <= new Date()
  469. ) {
  470. wx.showToast({
  471. title: '所选时间已超时,请重新选择',
  472. icon: 'none'
  473. })
  474. } else if (currentChangeDevice == null) {
  475. let _title = '请选择设备'
  476. if (deviceList.length == 0) {
  477. _title = '当前所选时间无可用设备'
  478. }
  479. wx.showToast({
  480. title: _title,
  481. icon: 'none'
  482. })
  483. } else {
  484. wx.navigateTo({
  485. url: `/pages/orderConfirm/orderConfirm?source=${
  486. this.data.source
  487. }&data=${JSON.stringify(projectData)}&deviceData=${JSON.stringify(
  488. currentChangeDevice
  489. )}&timeData=${JSON.stringify(
  490. currentChangeTime
  491. )}&productData=${JSON.stringify(productData)}`
  492. })
  493. }
  494. }
  495. })