appointment.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. hasAddress: false,
  10. accountNumber: 0,
  11. addressId: '',
  12. address: '',
  13. province: '',
  14. city: '',
  15. county: '',
  16. timeActive: '1',
  17. appointmentSuccess: false,
  18. curYear: '-',
  19. curMonth: '-',
  20. choseDate: '',
  21. weekList: [],
  22. backupWeekList: [],
  23. currentTime: '',
  24. currentWeek: '',
  25. currentYear: ''
  26. },
  27. /**
  28. * 生命周期函数--监听页面加载
  29. */
  30. onLoad(options) {
  31. this.initCalendar()
  32. },
  33. /**
  34. * 生命周期函数--监听页面显示
  35. */
  36. onShow() {
  37. this.getAccountNmberFn()
  38. if (app.globalData.navigateBackParams.address) {
  39. this.setData({
  40. hasAddress: true,
  41. address: app.globalData.navigateBackParams.address,
  42. contactName: app.globalData.navigateBackParams.contactName,
  43. contactPhone: app.globalData.navigateBackParams.contactPhone,
  44. addressId: app.globalData.navigateBackParams.addressId,
  45. province: app.globalData.navigateBackParams.province,
  46. city: app.globalData.navigateBackParams.city,
  47. county: app.globalData.navigateBackParams.county
  48. })
  49. }
  50. },
  51. onPullDownRefresh () {
  52. wx.vibrateShort({
  53. type: 'medium'
  54. })
  55. this.getAccountNmberFn(true)
  56. },
  57. // 查询账户剩余可使用的次数
  58. getAccountNmberFn (pullDownRefresh = false) {
  59. var that = this
  60. wx.showLoading({
  61. title: '加载中...',
  62. mask: true
  63. })
  64. getAccountNmber({}).then(res => {
  65. wx.hideLoading()
  66. if (pullDownRefresh) {
  67. wx.stopPullDownRefresh()
  68. }
  69. that.setData({
  70. accountNumber: res.data
  71. })
  72. }).catch(e => {
  73. wx.hideLoading()
  74. wx.showModal({
  75. content: e,
  76. confirmColor: '#333',
  77. showCancel: false
  78. })
  79. })
  80. },
  81. // 初始化日历
  82. async initCalendar () {
  83. var that = this
  84. var currentDate = new Date()
  85. this.setData({
  86. currentTime: currentDate.getTime(),
  87. currentWeek: currentDate.getDay(),
  88. currentYear: currentDate.getFullYear(),
  89. currentDay: currentDate.getDate()
  90. })
  91. await that.createCalendarGrild(33)
  92. var _weekList = that.data.weekList
  93. _weekList.map((item, index) => {
  94. if (index == that.data.currentWeek) {
  95. item.date = that.data.currentDay
  96. }
  97. })
  98. that.setData({
  99. weekList: _weekList
  100. })
  101. // 周一开始
  102. if (that.data.currentWeek >= 1) {
  103. that.setData({
  104. weekList: that.setCalendarItem()
  105. })
  106. }
  107. // 周日
  108. if (that.data.currentWeek < 1) {
  109. let _bankupWeekList = []
  110. for (let index = 0; index < 7; index++) {
  111. const bDate = new Date(that.data.currentTime - (index + 1) * 86400000).getDate()
  112. const _month = Number(baseDate.getMonth() + 1) < 10 ? '0' + Number(baseDate.getMonth() + 1) : baseDate.getMonth() + 1
  113. const _day = Number(baseDate.getDate()) < 10 ? '0' + baseDate.getDate() : baseDate.getDate()
  114. _bankupWeekList.unshift({
  115. date: index == 0 ? '约满' : bDate.getDate(),
  116. dateStr: bDate.getFullYear() + '-' + _month + '-' + _day,
  117. disabled: true,
  118. status: ''
  119. })
  120. }
  121. that.setData({
  122. backupWeekList: _bankupWeekList,
  123. weekList: that.setCalendarItem()
  124. })
  125. }
  126. },
  127. setCalendarItem () {
  128. var that = this
  129. var _mapWeekList = that.data.weekList
  130. _mapWeekList.map((item, index) => {
  131. const baseDate = new Date((index - that.data.currentWeek) * 86400000 + that.data.currentTime)
  132. const _month = Number(baseDate.getMonth() + 1) < 10 ? '0' + Number(baseDate.getMonth() + 1) : baseDate.getMonth() + 1
  133. const _year = baseDate.getFullYear()
  134. const _day = Number(baseDate.getDate()) < 10 ? '0' + baseDate.getDate() : baseDate.getDate()
  135. if (index > that.data.currentWeek) {
  136. if (index == that.data.currentWeek + 1) {
  137. item.date = '约满'
  138. item.disabled = true
  139. that.setData({
  140. curYear: _year,
  141. curMonth: _month,
  142. })
  143. } else {
  144. item.date = baseDate.getDate()
  145. }
  146. if (index == that.data.currentWeek + 2) {
  147. item.active = true
  148. that.setData({
  149. choseDate: _year + '-' + _month + '-' + _day
  150. })
  151. }
  152. item.dateStr = _year + '-' + _month + '-' + _day
  153. } else {
  154. item.disabled = true
  155. item.dateStr = baseDate.getFullYear() + '-' + _month + '-' + _day
  156. if (index == that.data.currentWeek) {
  157. item.date = '约满'
  158. } else {
  159. item.date = baseDate.getDate()
  160. }
  161. }
  162. })
  163. return _mapWeekList
  164. },
  165. createCalendarGrild (count) {
  166. return new Promise((resolve, reject) => {
  167. var _list = []
  168. for (let index = 0; index < count; index++) {
  169. _list.push({
  170. date: '-',
  171. dateStr: '-',
  172. disabled: false,
  173. active: false,
  174. status: '',
  175. })
  176. }
  177. this.setData({
  178. weekList: _list
  179. })
  180. resolve()
  181. })
  182. },
  183. // 选择日期
  184. handleSelectDate (e) {
  185. const disabled = e.currentTarget.dataset.disabled
  186. if (!disabled) {
  187. const cDate = e.currentTarget.dataset.date
  188. const index = e.currentTarget.dataset.index
  189. console.log(cDate, 'cDate');
  190. const _year = cDate.split('-')[0]
  191. const _month = cDate.split('-')[1]
  192. const _weeklist = this.data.weekList
  193. _weeklist[index].active = true
  194. _weeklist.map((item, i) => {
  195. if (index == i ) {
  196. item.active = true
  197. } else {
  198. item.active = false
  199. }
  200. })
  201. this.setData({
  202. curYear: _year,
  203. curMonth: _month,
  204. weekList: _weeklist,
  205. choseDate: cDate
  206. })
  207. }
  208. },
  209. /**
  210. * 选择地址
  211. */
  212. handleChoseAddress() {
  213. wx.navigateTo({
  214. url: '/pages/address/address?form=appointment&back=1',
  215. })
  216. },
  217. handleChangeAddress () {
  218. if (!this.data.appointmentSuccess) {
  219. wx.navigateTo({
  220. url: '/pages/address/address?form=appointment&back=1',
  221. })
  222. }
  223. },
  224. handleTime (e) {
  225. console.log(e);
  226. var timeActiveIndex = e.currentTarget.dataset.time
  227. this.setData({
  228. timeActive: timeActiveIndex
  229. })
  230. },
  231. // 激活卡片
  232. handleActiveCard () {
  233. wx.navigateTo({
  234. url: '/pages/scan/scan?form=appointment',
  235. })
  236. },
  237. // 去充值
  238. handleCharge () {},
  239. // 确定 先查询 区域的剩余预约次数
  240. handleConfirm () {
  241. if (this.data.accountNumber == 0) {
  242. wx.showModal({
  243. content: '您当前的检测次数为0,先充值次数',
  244. cancelColor: '#666',
  245. cancelText: '卡激活',
  246. confirmText: '去充值',
  247. confirmColor: '#333',
  248. success (res) {
  249. if (res.confirm) {
  250. wx.navigateTo({
  251. url: '/pages/buy/buy?form=appointment',
  252. })
  253. } else if (res.cancel) {
  254. wx.navigateTo({
  255. url: '/pages/scan/scan?form=appointment',
  256. })
  257. }
  258. }
  259. })
  260. } else if (!this.data.hasAddress) {
  261. wx.showToast({
  262. title: '请选择地址',
  263. icon: 'error',
  264. mask: true
  265. })
  266. } else {
  267. var itt = this
  268. wx.showLoading({
  269. title: '预约中...'
  270. })
  271. var data = {
  272. province: this.data.province,
  273. city: this.data.city,
  274. county: this.data.county,
  275. appointmentDate: this.data.choseDate
  276. }
  277. getResetNumber(data).then(res => {
  278. wx.hideLoading()
  279. itt.submitAppointmentFn()
  280. }).catch(e => {
  281. wx.hideLoading()
  282. wx.showModal({
  283. content: e,
  284. confirmColor: '#333',
  285. showCancel: false
  286. })
  287. })
  288. }
  289. },
  290. // 提交预约信息
  291. submitAppointmentFn () {
  292. var data = {
  293. appointmentTime: this.data.choseDate,
  294. duration: this.data.timeActive,
  295. addressId: this.data.addressId
  296. }
  297. submitAppointment(data).then(res => {
  298. wx.hideLoading()
  299. wx.showToast({
  300. title: '预约成功',
  301. icon: 'success'
  302. })
  303. this.setData({
  304. appointmentSuccess: true
  305. })
  306. }).catch(e => {
  307. wx.hideLoading()
  308. wx.showModal({
  309. content: e,
  310. confirmColor: '#333',
  311. showCancel: false
  312. })
  313. })
  314. },
  315. resetData () {
  316. app.globalData.navigateBackParams.address = ''
  317. app.globalData.navigateBackParams.contactName = ''
  318. app.globalData.navigateBackParams.contactPhone = ''
  319. app.globalData.navigateBackParams.addressId = ''
  320. app.globalData.navigateBackParams.province = ''
  321. app.globalData.navigateBackParams.city = ''
  322. app.globalData.navigateBackParams.county = ''
  323. this.setData({
  324. hasAddress: true,
  325. appointmentSuccess: false,
  326. timeActive: '1',
  327. addressId: '',
  328. address: '',
  329. province: '',
  330. city: '',
  331. county: '',
  332. })
  333. },
  334. handleCancel () {
  335. this.resetData()
  336. wx.navigateBack()
  337. }
  338. })