appointment.js 12 KB

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