appointment.js 7.5 KB

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