appointment.js 12 KB

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