appointment.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. timeActive: ''
  303. })
  304. this.getResetNumberFn(cDate)
  305. }
  306. }
  307. },
  308. /**
  309. * 选择地址
  310. */
  311. handleChoseAddress() {
  312. wx.navigateTo({
  313. url: '/pages/address/address?form=appointment&back=1',
  314. })
  315. },
  316. handleChangeAddress () {
  317. if (!this.data.appointmentSuccess) {
  318. wx.navigateTo({
  319. url: '/pages/address/address?form=appointment&back=1',
  320. })
  321. }
  322. },
  323. handleTime (e) {
  324. if (!this.data.hasAddress) {
  325. wx.showToast({
  326. title: '请选择地址',
  327. icon: 'error',
  328. mask: true
  329. })
  330. return
  331. }
  332. if (this.data.choseDate == '') {
  333. wx.showToast({
  334. title: '请选择日期',
  335. icon: 'error',
  336. mask: true
  337. })
  338. return
  339. }
  340. var timeActiveIndex = e.currentTarget.dataset.time
  341. var disabled = e.currentTarget.dataset.dis
  342. if (!disabled) {
  343. this.setData({
  344. timeActive: timeActiveIndex
  345. })
  346. }
  347. },
  348. // 去充值
  349. handleCharge () {},
  350. // 确定 先查询 区域的剩余预约次数
  351. handleConfirm () {
  352. if (this.data.accountNumber == 0) {
  353. wx.showModal({
  354. content: '您当前的检测次数为0,先充值次数',
  355. cancelColor: '#666',
  356. cancelText: '卡激活',
  357. confirmText: '去充值',
  358. confirmColor: '#333',
  359. success (res) {
  360. if (res.confirm) {
  361. wx.navigateTo({
  362. url: '/pages/buy/buy?from=appointment',
  363. })
  364. } else if (res.cancel) {
  365. wx.navigateTo({
  366. url: '/pages/exchange/exchange',
  367. })
  368. }
  369. }
  370. })
  371. } else if (!this.data.hasAddress) {
  372. wx.showToast({
  373. title: '请选择地址',
  374. icon: 'error',
  375. mask: true
  376. })
  377. } else {
  378. if (this.data.choseDate == '') {
  379. wx.showToast({
  380. title: '请选择日期',
  381. icon: 'error',
  382. mask: true
  383. })
  384. } else {
  385. if (this.data.timeActive == '') {
  386. wx.showToast({
  387. title: '请选择时间段',
  388. icon: 'error',
  389. mask: true
  390. })
  391. } else {
  392. this.submitAppointmentFn()
  393. }
  394. }
  395. }
  396. },
  397. // 提交预约信息
  398. submitAppointmentFn () {
  399. var data = {
  400. appointmentTime: this.data.choseDate,
  401. duration: this.data.timeActive,
  402. addressId: this.data.addressId
  403. }
  404. wx.showLoading({
  405. title: '预约中...'
  406. })
  407. var timer = null
  408. submitAppointment(data).then(res => {
  409. wx.hideLoading()
  410. wx.showToast({
  411. title: '预约成功',
  412. icon: 'success'
  413. })
  414. timer = setTimeout(() => {
  415. wx.navigateTo({
  416. url: '/pages/myAppointment/myAppointment?from=appointment',
  417. })
  418. clearTimeout(timer)
  419. }, 1000);
  420. /* this.setData({
  421. appointmentSuccess: true
  422. }) */
  423. }).catch(e => {
  424. wx.hideLoading()
  425. wx.showModal({
  426. content: e,
  427. confirmColor: '#333',
  428. showCancel: false
  429. })
  430. })
  431. },
  432. resetData () {
  433. app.globalData.navigateBackParams.address = ''
  434. app.globalData.navigateBackParams.contactName = ''
  435. app.globalData.navigateBackParams.contactPhone = ''
  436. app.globalData.navigateBackParams.addressId = ''
  437. app.globalData.navigateBackParams.province = ''
  438. app.globalData.navigateBackParams.city = ''
  439. app.globalData.navigateBackParams.county = ''
  440. this.setData({
  441. hasAddress: true,
  442. appointmentSuccess: false,
  443. timeActive: '',
  444. addressId: '',
  445. address: '',
  446. province: '',
  447. city: '',
  448. county: '',
  449. })
  450. },
  451. handleCancel () {
  452. this.resetData()
  453. wx.navigateBack()
  454. }
  455. })