reserveProject.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. projectTime: [],
  53. projectData: {},
  54. deviceList: [],
  55. currentChangeTime: null,
  56. currentChangeDevice: null,
  57. source: 'project', // 来源
  58. inFirstIn: false,
  59. inSecIn: false
  60. },
  61. knowSubmit: function () {
  62. this.setData({
  63. inFirstIn: false,
  64. inSecIn: false
  65. })
  66. let version3 = wx.getStorageSync('version3')
  67. if (!version3 || version3 != 1) {
  68. this.setData({
  69. inSecIn : true
  70. })
  71. wx.setStorageSync('version3', 1)
  72. }
  73. },
  74. noneEnoughPeople() {
  75. },
  76. /**
  77. * 生命周期函数--监听页面加载
  78. */
  79. onLoad: function (options) {
  80. let date = new Date()
  81. let currentTimestamp = date.getTime() / 1000 + 60 * 2
  82. let _time = timestampToDate(currentTimestamp, 'H:i')
  83. let source = options.source || 'project'
  84. if (options.projectData) {
  85. let projectData = decodeURIComponent(options.projectData)
  86. projectData = JSON.parse(projectData)
  87. projectData.storeData = app.globalData.storeData
  88. this.setData({
  89. projectData: projectData,
  90. source: source,
  91. currentTimestamp: currentTimestamp,
  92. time: _time
  93. })
  94. // 项目预约 - 获取可选时间
  95. this.getTime()
  96. }
  97. },
  98. /**
  99. * 生命周期函数--监听页面初次渲染完成
  100. */
  101. onReady: function () {},
  102. /**
  103. * 生命周期函数--监听页面显示
  104. */
  105. onShow: function () {
  106. let version2 = wx.getStorageSync('version2')
  107. if (!version2 || version2 != 1) {
  108. this.setData({
  109. inFirstIn : true
  110. })
  111. wx.setStorageSync('version2', 1)
  112. } else {
  113. let version3 = wx.getStorageSync('version3')
  114. if (!version3 || version3 != 1) {
  115. this.setData({
  116. inSecIn : true
  117. })
  118. wx.setStorageSync('version3', 1)
  119. }
  120. }
  121. },
  122. /**
  123. * 生命周期函数--监听页面隐藏
  124. */
  125. onHide: function () {},
  126. /**
  127. * 生命周期函数--监听页面卸载
  128. */
  129. onUnload: function () {},
  130. /**
  131. * 页面相关事件处理函数--监听用户下拉动作
  132. */
  133. onPullDownRefresh: function () {},
  134. /**
  135. * 页面上拉触底事件的处理函数
  136. */
  137. onReachBottom: function () {},
  138. /**
  139. * 用户点击右上角分享
  140. */
  141. onShareAppMessage: function () {},
  142. /**
  143. * 日期选择
  144. */
  145. onChangeDate(e) {
  146. let {
  147. currentDateIndex
  148. } = this.data
  149. if (currentDateIndex != e.currentTarget.dataset.index) {
  150. this.setData({
  151. currentDateIndex: e.currentTarget.dataset.index,
  152. currentChangeDevice: null,
  153. currentChangeTime: null,
  154. currentTimeIndex: -2,
  155. currentSwiperIndex: 0,
  156. currentDeviceIndex: -1,
  157. deviceList: []
  158. },
  159. () => {
  160. this.getTime()
  161. }
  162. )
  163. }
  164. },
  165. /**
  166. * 时间段切换
  167. */
  168. onChangeSwiper(e) {
  169. this.setData({
  170. currentSwiperIndex: e.currentTarget.dataset.index
  171. })
  172. },
  173. /**
  174. * 时间段切换
  175. */
  176. changeSwiper(e) {
  177. this.setData({
  178. currentSwiperIndex: e.detail.current
  179. })
  180. },
  181. /**
  182. * 时间选择
  183. */
  184. onChangeTime(e) {
  185. let {
  186. projectTime,
  187. currentSwiperIndex,
  188. currentChangeTime,
  189. currentTimeIndex
  190. } = this.data
  191. projectTime[currentSwiperIndex].list.forEach((item, index) => {
  192. if (item.index == e.currentTarget.dataset.index) {
  193. currentChangeTime = item
  194. }
  195. })
  196. if (currentTimeIndex != e.currentTarget.dataset.index) {
  197. this.setData({
  198. currentTimeIndex: e.currentTarget.dataset.index,
  199. currentChangeTime,
  200. currentChangeDevice: null,
  201. currentDeviceIndex: -1
  202. })
  203. this.getProjectDevice()
  204. }
  205. },
  206. /**
  207. * 点击自定义时间
  208. */
  209. bindTimeChange(e) {
  210. let {
  211. currentDateIndex,
  212. currentChangeTime
  213. } = this.data
  214. currentChangeTime = {
  215. order_day: times[currentDateIndex].date,
  216. time_id: e.detail.value
  217. }
  218. this.setData({
  219. time: e.detail.value,
  220. currentTimeIndex: -1,
  221. currentChangeTime,
  222. currentChangeDevice: null,
  223. currentDeviceIndex: -1
  224. })
  225. this.getProjectDevice()
  226. },
  227. /**
  228. * 选择设备
  229. */
  230. onChangeDevice(e) {
  231. let {
  232. deviceList
  233. } = this.data
  234. this.setData({
  235. currentDeviceIndex: e.currentTarget.dataset.index,
  236. currentChangeDevice: deviceList[e.currentTarget.dataset.index]
  237. })
  238. },
  239. /**
  240. * 获取可预约时间
  241. */
  242. getTime() {
  243. let {
  244. projectData,
  245. currentDateIndex,
  246. projectTime,
  247. currentSwiperIndex,
  248. time
  249. } = this.data
  250. get(
  251. 'api/project/time', {
  252. store_id: projectData.store_id,
  253. project_id: projectData.id,
  254. order_day: times[currentDateIndex].date,
  255. use_time: projectData.use_time || 40
  256. },
  257. (res) => {
  258. projectTime[0] = res.data.day
  259. projectTime[1] = res.data.night
  260. let sum = 0
  261. let defaultTimeIndex = -1
  262. projectTime.forEach((item, index) => {
  263. if (item.active == 1) {
  264. currentSwiperIndex = index
  265. }
  266. item.list.forEach((item, index) => {
  267. item.index = sum
  268. sum++
  269. // 默认选中第一个可预约时间(用于后面的默认绑定设备)
  270. if (item.status == 1 && defaultTimeIndex == -1) {
  271. defaultTimeIndex = item.index
  272. }
  273. })
  274. })
  275. this.setData({
  276. projectTime,
  277. currentSwiperIndex
  278. }, () => {
  279. // 默认选中第一个可预约时间
  280. // TODO 产品调整需求默认选中自定义时间
  281. // if (defaultTimeIndex != -1) {
  282. // this.onChangeTime({
  283. // currentTarget: { dataset: { index: defaultTimeIndex } }
  284. // })
  285. // }
  286. this.bindTimeChange({
  287. detail: {
  288. value: time
  289. }
  290. })
  291. })
  292. }
  293. )
  294. },
  295. /**
  296. * 获取项目可预约的设备
  297. * /api/project/device
  298. */
  299. getProjectDevice() {
  300. let {
  301. currentTimeIndex,
  302. time,
  303. projectTime,
  304. currentSwiperIndex,
  305. projectData,
  306. currentDateIndex
  307. } = this.data
  308. let order_time_id
  309. if (currentTimeIndex != -1) {
  310. projectTime[currentSwiperIndex].list.forEach((item, index) => {
  311. if (item.index == currentTimeIndex) {
  312. order_time_id = item.time_id
  313. }
  314. })
  315. } else {
  316. order_time_id = time
  317. }
  318. console.log(projectData)
  319. console.log(order_time_id)
  320. return new Promise((resolve, reject) => {
  321. get(
  322. 'api/project/device', {
  323. store_id: projectData.store_id,
  324. project_id: projectData.id,
  325. order_day: times[currentDateIndex].date,
  326. order_time_id: order_time_id,
  327. use_time: projectData.use_time
  328. },
  329. (res) => {
  330. this.setData({
  331. deviceList: res.data
  332. },
  333. () => {
  334. this.onChangeDevice({
  335. currentTarget: {
  336. dataset: {
  337. index: 0
  338. }
  339. }
  340. })
  341. }
  342. )
  343. resolve()
  344. console.log(res)
  345. },
  346. (rej) => {
  347. this.setData({
  348. currentTimeIndex: -2,
  349. currentChangeTime: null
  350. })
  351. console.log(rej)
  352. }
  353. )
  354. })
  355. },
  356. /**
  357. * 提交订单
  358. */
  359. addOrder() {
  360. let {
  361. projectData,
  362. currentTimeIndex,
  363. time,
  364. currentDateIndex,
  365. currentChangeTime,
  366. currentChangeDevice,
  367. deviceList
  368. } = this.data
  369. if (currentTimeIndex == -1) {
  370. currentChangeTime = {
  371. order_day: times[currentDateIndex].date,
  372. time_id: time,
  373. order_time: time
  374. }
  375. }
  376. console.log(currentChangeTime)
  377. if (currentChangeTime == null) {
  378. wx.showToast({
  379. title: '请选择预约时间',
  380. icon: 'none'
  381. })
  382. } else if (
  383. new Date(
  384. `${currentChangeTime.order_day} ${currentChangeTime.order_time}`
  385. ) <= new Date()
  386. ) {
  387. wx.showToast({
  388. title: '所选时间已超时,请重新选择',
  389. icon: 'none'
  390. })
  391. } else if (currentChangeDevice == null) {
  392. let _title = '请选择设备'
  393. if (deviceList.length == 0) {
  394. _title = '当前所选时间无可用设备'
  395. }
  396. wx.showToast({
  397. title: _title,
  398. icon: 'none'
  399. })
  400. } else {
  401. wx.navigateTo({
  402. url: `/pages/orderConfirm/orderConfirm?source=${
  403. this.data.source
  404. }&data=${JSON.stringify(projectData)}&deviceData=${JSON.stringify(
  405. currentChangeDevice
  406. )}&timeData=${JSON.stringify(currentChangeTime)}`
  407. })
  408. }
  409. }
  410. })