appointment.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. // pages/appointment/appointment.js
  2. const app = getApp()
  3. import { submitAppointment, getResetNumber, getAccountNmber } from '../../api/appointment'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. isBusyDay: true,
  10. hasAddress: false,
  11. accountNumber: 0,
  12. addressId: '',
  13. address: '',
  14. province: '',
  15. city: '',
  16. county: '',
  17. timeActive: '',
  18. appointmentSuccess: false,
  19. curYear: '-',
  20. curMonth: '-',
  21. choseDate: '',
  22. weekList: [],
  23. backupWeekList: [],
  24. currentTime: '',
  25. currentWeek: '',
  26. currentYear: '',
  27. appusername: '',
  28. appuserheadimg: '',
  29. durationOne: false,
  30. durationOneBusy: false,
  31. durationTwo: false,
  32. durationTwoBusy: false,
  33. durationThree: false,
  34. durationThreeBusy: false,
  35. allDayBusy: false
  36. },
  37. /**
  38. * 生命周期函数--监听页面加载
  39. */
  40. onLoad(options) {
  41. this.initCalendar()
  42. },
  43. /**
  44. * 生命周期函数--监听页面显示
  45. */
  46. onShow() {
  47. var headImg = wx.getStorageSync('headImg')
  48. var userName = wx.getStorageSync('userName')
  49. this.getAccountNmberFn()
  50. this.setData({
  51. appusername: userName.length > 8 ? userName.substring(0,4) : userName,
  52. appuserheadimg: headImg
  53. })
  54. if (app.globalData.navigateBackParams.address) {
  55. this.setData({
  56. hasAddress: true,
  57. address: app.globalData.navigateBackParams.address,
  58. contactName: app.globalData.navigateBackParams.contactName,
  59. contactPhone: app.globalData.navigateBackParams.contactPhone,
  60. addressId: app.globalData.navigateBackParams.addressId,
  61. province: app.globalData.navigateBackParams.province,
  62. city: app.globalData.navigateBackParams.city,
  63. county: app.globalData.navigateBackParams.county
  64. })
  65. }
  66. },
  67. // 获取当天可预约的次数
  68. getResetNumberFn (cDate, index) {
  69. var that = this
  70. var data = {
  71. province: this.data.province,
  72. city: this.data.city,
  73. county: this.data.county,
  74. appointmentDate: cDate
  75. }
  76. wx.showLoading({
  77. title: '加载中...',
  78. mask: true
  79. })
  80. getResetNumber(data).then(res => {
  81. wx.hideLoading()
  82. const response = res.data.carDurationConfigs
  83. const allBusy = response.find(item => {
  84. return item.isBusy == false
  85. })
  86. if (allBusy == undefined) {
  87. that.handleAllDayBusy(cDate)
  88. } else {
  89. that.handleAllDayAppointment(cDate)
  90. }
  91. response.forEach(item => {
  92. if (item.duration == 1) {
  93. that.setData({
  94. durationOne: item.isBusy,
  95. durationOneBusy: item.rest == 0 ? true : false
  96. })
  97. }
  98. if (item.duration == 2) {
  99. that.setData({
  100. durationTwo: item.isBusy,
  101. durationTwoBusy: item.rest == 0 ? true : false
  102. })
  103. }
  104. if (item.duration == 3) {
  105. that.setData({
  106. durationThree: item.isBusy,
  107. durationThreeBusy: item.rest == 0 ? true : false
  108. })
  109. }
  110. })
  111. }).catch(e => {
  112. wx.hideLoading()
  113. wx.showModal({
  114. content: e,
  115. confirmColor: '#333',
  116. showCancel: false
  117. })
  118. })
  119. },
  120. handleAllDayBusy (cDate) {
  121. const _weekList = this.data.weekList
  122. _weekList.forEach(item => {
  123. if (item.dateStr == cDate) {
  124. item.date = '约满'
  125. }
  126. })
  127. this.setData({
  128. weekList: _weekList
  129. })
  130. },
  131. handleAllDayAppointment (cDate) {
  132. console.log(cDate, '可预约')
  133. const _weekList = this.data.weekList
  134. _weekList.forEach(item => {
  135. if (item.dateStr == cDate && item.date == '约满') {
  136. console.log(item.dateStr);
  137. item.date = Number(item.dateStr.split('-')[2])
  138. }
  139. })
  140. this.setData({
  141. weekList: _weekList
  142. })
  143. },
  144. onPullDownRefresh () {
  145. wx.vibrateShort({
  146. type: 'medium'
  147. })
  148. this.getAccountNmberFn(true)
  149. },
  150. // 查询账户剩余可使用的次数
  151. getAccountNmberFn (pullDownRefresh = false) {
  152. var that = this
  153. wx.showLoading({
  154. title: '加载中...',
  155. mask: true
  156. })
  157. getAccountNmber({}).then(res => {
  158. wx.hideLoading()
  159. if (pullDownRefresh) {
  160. wx.stopPullDownRefresh()
  161. }
  162. that.setData({
  163. accountNumber: res.data
  164. })
  165. }).catch(e => {
  166. wx.hideLoading()
  167. wx.showModal({
  168. content: e,
  169. confirmColor: '#333',
  170. showCancel: false
  171. })
  172. })
  173. },
  174. // 初始化日历
  175. async initCalendar () {
  176. var that = this
  177. var currentDate = new Date()
  178. this.setData({
  179. currentTime: currentDate.getTime(),
  180. currentWeek: currentDate.getDay(),
  181. currentYear: currentDate.getFullYear(),
  182. currentDay: currentDate.getDate()
  183. })
  184. await that.createCalendarGrild(33)
  185. var _weekList = that.data.weekList
  186. _weekList.map((item, index) => {
  187. if (index == that.data.currentWeek) {
  188. item.date = that.data.currentDay
  189. }
  190. })
  191. that.setData({
  192. weekList: _weekList
  193. })
  194. // 周一开始
  195. if (that.data.currentWeek >= 1) {
  196. that.setData({
  197. weekList: that.setCalendarItem()
  198. })
  199. }
  200. // 周日
  201. if (that.data.currentWeek < 1) {
  202. let _bankupWeekList = []
  203. for (let index = 0; index < 7; index++) {
  204. const bDate = new Date(that.data.currentTime - (index + 1) * 86400000)
  205. const _month = Number(bDate.getMonth() + 1) < 10 ? '0' + Number(bDate.getMonth() + 1) : bDate.getMonth() + 1
  206. const _day = Number(bDate.getDate()) < 10 ? '0' + bDate.getDate() : bDate.getDate()
  207. _bankupWeekList.unshift({
  208. date: bDate.getDate(),
  209. dateStr: bDate.getFullYear() + '-' + _month + '-' + _day,
  210. disabled: true,
  211. status: '',
  212. busy: false
  213. })
  214. }
  215. that.setData({
  216. backupWeekList: _bankupWeekList,
  217. weekList: that.setCalendarItem()
  218. })
  219. }
  220. },
  221. setCalendarItem () {
  222. var that = this
  223. var _mapWeekList = that.data.weekList
  224. _mapWeekList.map((item, index) => {
  225. const baseDate = new Date((index - that.data.currentWeek) * 86400000 + that.data.currentTime)
  226. const _month = Number(baseDate.getMonth() + 1) < 10 ? '0' + Number(baseDate.getMonth() + 1) : baseDate.getMonth() + 1
  227. const _year = baseDate.getFullYear()
  228. const _day = Number(baseDate.getDate()) < 10 ? '0' + baseDate.getDate() : baseDate.getDate()
  229. const _week = baseDate.getDay()
  230. item.week = _week
  231. if (_week == 0) {
  232. item.weekStr = '周日'
  233. item.busy = true
  234. }
  235. if (_week == 1) {
  236. item.weekStr = '周一'
  237. }
  238. if (_week == 2) {
  239. item.weekStr = '周二'
  240. }
  241. if (_week == 3) {
  242. item.weekStr = '周三'
  243. }
  244. if (_week == 4) {
  245. item.weekStr = '周四'
  246. }
  247. if (_week == 5) {
  248. item.weekStr = '周五'
  249. }
  250. if (_week == 6) {
  251. item.weekStr = '周六'
  252. item.busy = true
  253. }
  254. if (index > that.data.currentWeek) {
  255. if (index == that.data.currentWeek + 1) {
  256. item.date = '约满'
  257. item.busy = false
  258. item.disabled = true
  259. that.setData({
  260. curYear: _year,
  261. curMonth: _month,
  262. })
  263. } else {
  264. item.date = baseDate.getDate()
  265. }
  266. if (index == that.data.currentWeek + 2) {
  267. // item.active = true
  268. /* that.setData({
  269. choseDate: _year + '-' + _month + '-' + _day
  270. }) */
  271. }
  272. item.dateStr = _year + '-' + _month + '-' + _day
  273. } else {
  274. item.disabled = true
  275. item.busy = false
  276. item.dateStr = baseDate.getFullYear() + '-' + _month + '-' + _day
  277. if (index == that.data.currentWeek) {
  278. item.date = '约满'
  279. } else {
  280. item.date = baseDate.getDate()
  281. }
  282. }
  283. })
  284. return _mapWeekList
  285. },
  286. createCalendarGrild (count) {
  287. return new Promise((resolve, reject) => {
  288. var _list = []
  289. for (let index = 0; index < count; index++) {
  290. _list.push({
  291. date: '-',
  292. dateStr: '-',
  293. disabled: false,
  294. active: false,
  295. status: '',
  296. busy: false
  297. })
  298. }
  299. this.setData({
  300. weekList: _list
  301. })
  302. resolve()
  303. })
  304. },
  305. // 选择日期
  306. handleSelectDate (e) {
  307. if (!this.data.hasAddress) {
  308. wx.showToast({
  309. title: '请选择地址',
  310. icon: 'error',
  311. mask: true
  312. })
  313. } else {
  314. const disabled = e.currentTarget.dataset.disabled
  315. if (!disabled) {
  316. const cDate = e.currentTarget.dataset.date
  317. const index = e.currentTarget.dataset.index
  318. const _year = cDate.split('-')[0]
  319. const _month = cDate.split('-')[1]
  320. const _weeklist = this.data.weekList
  321. _weeklist.map((item, i) => {
  322. if (index == i ) {
  323. item.active = true
  324. } else {
  325. item.active = false
  326. }
  327. })
  328. this.setData({
  329. curYear: _year,
  330. curMonth: _month,
  331. weekList: _weeklist,
  332. choseDate: cDate,
  333. timeActive: ''
  334. })
  335. this.getResetNumberFn(cDate,index)
  336. }
  337. }
  338. },
  339. /**
  340. * 选择地址
  341. */
  342. handleChoseAddress() {
  343. wx.navigateTo({
  344. url: '/pages/address/address?form=appointment&back=1',
  345. })
  346. },
  347. handleChangeAddress () {
  348. if (!this.data.appointmentSuccess) {
  349. wx.navigateTo({
  350. url: '/pages/address/address?form=appointment&back=1',
  351. })
  352. }
  353. },
  354. handleTime (e) {
  355. if (!this.data.hasAddress) {
  356. wx.showToast({
  357. title: '请选择地址',
  358. icon: 'error',
  359. mask: true
  360. })
  361. return
  362. }
  363. if (this.data.choseDate == '') {
  364. wx.showToast({
  365. title: '请选择日期',
  366. icon: 'error',
  367. mask: true
  368. })
  369. return
  370. }
  371. var timeActiveIndex = e.currentTarget.dataset.time
  372. var disabled = e.currentTarget.dataset.dis
  373. if (!disabled) {
  374. this.setData({
  375. timeActive: timeActiveIndex
  376. })
  377. }
  378. },
  379. // 去充值
  380. handleCharge () {},
  381. // 确定 先查询 区域的剩余预约次数
  382. handleConfirm () {
  383. if (this.data.accountNumber == 0) {
  384. wx.showModal({
  385. content: '您当前的检测次数为0,先充值次数',
  386. cancelColor: '#666',
  387. cancelText: '卡激活',
  388. confirmText: '去充值',
  389. confirmColor: '#333',
  390. success (res) {
  391. if (res.confirm) {
  392. wx.navigateTo({
  393. url: '/pages/buy/buy?from=appointment',
  394. })
  395. } else if (res.cancel) {
  396. wx.navigateTo({
  397. url: '/pages/exchange/exchange',
  398. })
  399. }
  400. }
  401. })
  402. } else if (!this.data.hasAddress) {
  403. wx.showToast({
  404. title: '请选择地址',
  405. icon: 'error',
  406. mask: true
  407. })
  408. } else {
  409. if (this.data.choseDate == '') {
  410. wx.showToast({
  411. title: '请选择日期',
  412. icon: 'error',
  413. mask: true
  414. })
  415. } else {
  416. if (this.data.timeActive == '') {
  417. wx.showToast({
  418. title: '请选择时间段',
  419. icon: 'error',
  420. mask: true
  421. })
  422. } else {
  423. this.submitAppointmentFn()
  424. }
  425. }
  426. }
  427. },
  428. // 提交预约信息
  429. submitAppointmentFn () {
  430. var data = {
  431. appointmentTime: this.data.choseDate,
  432. duration: this.data.timeActive,
  433. addressId: this.data.addressId
  434. }
  435. wx.showLoading({
  436. title: '预约中...'
  437. })
  438. var timer = null
  439. submitAppointment(data).then(res => {
  440. wx.hideLoading()
  441. wx.showToast({
  442. title: '预约成功',
  443. icon: 'success'
  444. })
  445. timer = setTimeout(() => {
  446. wx.navigateTo({
  447. url: '/pages/myAppointment/myAppointment?from=appointment',
  448. })
  449. clearTimeout(timer)
  450. }, 1000);
  451. /* this.setData({
  452. appointmentSuccess: true
  453. }) */
  454. }).catch(e => {
  455. wx.hideLoading()
  456. wx.showModal({
  457. content: e,
  458. confirmColor: '#333',
  459. showCancel: false
  460. })
  461. })
  462. },
  463. resetData () {
  464. app.globalData.navigateBackParams.address = ''
  465. app.globalData.navigateBackParams.contactName = ''
  466. app.globalData.navigateBackParams.contactPhone = ''
  467. app.globalData.navigateBackParams.addressId = ''
  468. app.globalData.navigateBackParams.province = ''
  469. app.globalData.navigateBackParams.city = ''
  470. app.globalData.navigateBackParams.county = ''
  471. this.setData({
  472. hasAddress: true,
  473. appointmentSuccess: false,
  474. timeActive: '',
  475. addressId: '',
  476. address: '',
  477. province: '',
  478. city: '',
  479. county: '',
  480. })
  481. },
  482. handleCancel () {
  483. this.resetData()
  484. wx.navigateBack()
  485. }
  486. })