appointment.js 11 KB

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