appointment.js 12 KB

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