appointment.js 12 KB

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