appointment.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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.date = '约满'
  89. item.disabled = true
  90. that.setData({
  91. curYear: _year,
  92. curMonth: _month,
  93. })
  94. } else {
  95. item.date = baseDate.getDate()
  96. }
  97. if (index == that.data.currentWeek + 2) {
  98. item.active = true
  99. that.setData({
  100. choseDate: _year + '-' + _month + '-' + _day
  101. })
  102. }
  103. item.dateStr = _year + '-' + _month + '-' + _day
  104. } else {
  105. item.disabled = true
  106. item.dateStr = baseDate.getFullYear() + '-' + _month + '-' + _day
  107. if (index == that.data.currentWeek) {
  108. item.date = '约满'
  109. } else {
  110. item.date = baseDate.getDate()
  111. }
  112. }
  113. })
  114. return _mapWeekList
  115. },
  116. createCalendarGrild (count) {
  117. return new Promise((resolve, reject) => {
  118. var _list = []
  119. for (let index = 0; index < count; index++) {
  120. _list.push({
  121. date: '-',
  122. dateStr: '-',
  123. disabled: false,
  124. active: false,
  125. status: '',
  126. })
  127. }
  128. this.setData({
  129. weekList: _list
  130. })
  131. resolve()
  132. })
  133. },
  134. // 选择日期
  135. handleSelectDate (e) {
  136. const disabled = e.currentTarget.dataset.disabled
  137. if (!disabled) {
  138. const cDate = e.currentTarget.dataset.date
  139. const index = e.currentTarget.dataset.index
  140. console.log(cDate, 'cDate');
  141. const _year = cDate.split('-')[0]
  142. const _month = cDate.split('-')[1]
  143. const _weeklist = this.data.weekList
  144. _weeklist[index].active = true
  145. _weeklist.map((item, i) => {
  146. if (index == i ) {
  147. item.active = true
  148. } else {
  149. item.active = false
  150. }
  151. })
  152. this.setData({
  153. curYear: _year,
  154. curMonth: _month,
  155. weekList: _weeklist,
  156. choseDate: cDate
  157. })
  158. }
  159. },
  160. /**
  161. * 生命周期函数--监听页面显示
  162. */
  163. onShow() {
  164. if (app.globalData.navigateBackParams.address) {
  165. this.setData({
  166. hasAddress: true,
  167. address: app.globalData.navigateBackParams.address,
  168. contactName: app.globalData.navigateBackParams.contactName,
  169. contactPhone: app.globalData.navigateBackParams.contactPhone,
  170. addressId: app.globalData.navigateBackParams.addressId,
  171. province: app.globalData.navigateBackParams.province,
  172. city: app.globalData.navigateBackParams.city,
  173. county: app.globalData.navigateBackParams.county
  174. })
  175. }
  176. },
  177. /**
  178. * 选择地址
  179. */
  180. handleChoseAddress() {
  181. wx.navigateTo({
  182. url: '/pages/address/address?form=appointment&back=1',
  183. })
  184. },
  185. handleChangeAddress () {
  186. if (!this.data.appointmentSuccess) {
  187. wx.navigateTo({
  188. url: '/pages/address/address?form=appointment&back=1',
  189. })
  190. }
  191. },
  192. handleTime (e) {
  193. console.log(e);
  194. var timeActiveIndex = e.currentTarget.dataset.time
  195. this.setData({
  196. timeActive: timeActiveIndex
  197. })
  198. },
  199. // 激活卡片
  200. handleActiveCard () {
  201. wx.navigateTo({
  202. url: '/pages/scan/scan?form=appointment',
  203. })
  204. },
  205. // 去充值
  206. handleCharge () {},
  207. // 确定 先查询 区域的剩余预约次数
  208. handleConfirm () {
  209. if (!this.data.hasAddress) {
  210. wx.showToast({
  211. title: '请选择地址',
  212. icon: 'error',
  213. mask: true
  214. })
  215. } else {
  216. var itt = this
  217. wx.showLoading({
  218. title: '预约中...'
  219. })
  220. var data = {
  221. province: this.data.province,
  222. city: this.data.city,
  223. county: this.data.county,
  224. appointmentDate: this.data.choseDate
  225. }
  226. getResetNumber(data).then(res => {
  227. wx.hideLoading()
  228. itt.submitAppointmentFn()
  229. }).catch(e => {
  230. wx.hideLoading()
  231. wx.showModal({
  232. content: e,
  233. confirmColor: '#333',
  234. showCancel: false
  235. })
  236. })
  237. }
  238. },
  239. // 提交预约信息
  240. submitAppointmentFn () {
  241. var data = {
  242. appointmentTime: this.data.choseDate,
  243. duration: this.data.timeActive,
  244. addressId: this.data.addressId
  245. }
  246. submitAppointment(data).then(res => {
  247. wx.hideLoading()
  248. wx.showToast({
  249. title: '预约成功',
  250. icon: 'success'
  251. })
  252. this.setData({
  253. appointmentSuccess: true
  254. })
  255. }).catch(e => {
  256. wx.hideLoading()
  257. wx.showModal({
  258. content: e,
  259. confirmColor: '#333',
  260. showCancel: false
  261. })
  262. })
  263. },
  264. resetData () {
  265. app.globalData.navigateBackParams.address = ''
  266. app.globalData.navigateBackParams.contactName = ''
  267. app.globalData.navigateBackParams.contactPhone = ''
  268. app.globalData.navigateBackParams.addressId = ''
  269. app.globalData.navigateBackParams.province = ''
  270. app.globalData.navigateBackParams.city = ''
  271. app.globalData.navigateBackParams.county = ''
  272. this.setData({
  273. hasAddress: true,
  274. appointmentSuccess: false,
  275. timeActive: '1',
  276. addressId: '',
  277. address: '',
  278. province: '',
  279. city: '',
  280. county: '',
  281. })
  282. },
  283. handleCancel () {
  284. this.resetData()
  285. wx.navigateBack()
  286. }
  287. })