appointment.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // pages/appointment/appointment.js
  2. const app = getApp()
  3. import { submitAppointment } from '../../api/appointment'
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. hasAddress: false,
  10. address: '',
  11. timeActive: '0',
  12. appointmentSuccess: false,
  13. curYear: '-',
  14. curMonth: '-',
  15. weekList: [],
  16. backupWeekList: [],
  17. currentTime: '',
  18. currentWeek: '',
  19. currentYear: ''
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad(options) {
  25. this.initCalendar()
  26. },
  27. // 初始化日历
  28. async initCalendar () {
  29. var that = this
  30. var currentDate = new Date()
  31. this.setData({
  32. currentTime: currentDate.getTime(),
  33. currentWeek: currentDate.getDay(),
  34. currentYear: currentDate.getFullYear(),
  35. currentDay: currentDate.getDate()
  36. })
  37. await that.createCalendarGrild(33)
  38. var _weekList = that.data.weekList
  39. _weekList.map((item, index) => {
  40. if (index == that.data.currentWeek) {
  41. item.date = that.data.currentDay
  42. }
  43. })
  44. that.setData({
  45. weekList: _weekList
  46. })
  47. // 周一开始
  48. if (that.data.currentWeek >= 1) {
  49. that.setData({
  50. weekList: that.setCalendarItem()
  51. })
  52. }
  53. // 周日
  54. if (that.data.currentWeek < 1) {
  55. let _bankupWeekList = []
  56. for (let index = 0; index < 7; index++) {
  57. const bDate = new Date(that.data.currentTime - (index + 1) * 86400000).getDate()
  58. const _month = baseDate.getMonth() + 1
  59. _bankupWeekList.unshift({
  60. date: index == 0 ? '约满' : bDate.getDate(),
  61. dateStr: bDate.getFullYear() + '-' + _month,
  62. disabled: true,
  63. status: ''
  64. })
  65. }
  66. that.setData({
  67. backupWeekList: _bankupWeekList,
  68. weekList: that.setCalendarItem()
  69. })
  70. }
  71. },
  72. setCalendarItem () {
  73. var that = this
  74. var _mapWeekList = that.data.weekList
  75. _mapWeekList.map((item, index) => {
  76. const baseDate = new Date((index - that.data.currentWeek) * 86400000 + that.data.currentTime)
  77. const _month = baseDate.getMonth() + 1
  78. const _year = baseDate.getFullYear()
  79. if (index > that.data.currentWeek) {
  80. if (index == that.data.currentWeek + 1) {
  81. item.active = true
  82. that.setData({
  83. curYear: _year,
  84. curMonth: _month
  85. })
  86. }
  87. item.date = baseDate.getDate()
  88. item.dateStr = baseDate.getFullYear() + '-' + _month
  89. } else {
  90. item.disabled = true
  91. item.dateStr = baseDate.getFullYear() + '-' + _month
  92. if (index == that.data.currentWeek || index == that.data.currentWeek - 1) {
  93. item.date = '约满'
  94. } else {
  95. item.date = baseDate.getDate()
  96. }
  97. }
  98. })
  99. return _mapWeekList
  100. },
  101. createCalendarGrild (count) {
  102. return new Promise((resolve, reject) => {
  103. var _list = []
  104. for (let index = 0; index < count; index++) {
  105. _list.push({
  106. date: '-',
  107. dateStr: '-',
  108. disabled: false,
  109. active: false,
  110. status: '',
  111. })
  112. }
  113. this.setData({
  114. weekList: _list
  115. })
  116. resolve()
  117. })
  118. },
  119. // 选择日期
  120. handleSelectDate (e) {
  121. const disabled = e.currentTarget.dataset.disabled
  122. if (!disabled) {
  123. const cDate = e.currentTarget.dataset.date
  124. const index = e.currentTarget.dataset.index
  125. console.log(index, 'cDate');
  126. const _year = cDate.split('-')[0]
  127. const _month = cDate.split('-')[1]
  128. const _weeklist = this.data.weekList
  129. _weeklist[index].active = true
  130. _weeklist.map((item, i) => {
  131. if (index == i ) {
  132. item.active = true
  133. } else {
  134. item.active = false
  135. }
  136. })
  137. this.setData({
  138. curYear: _year,
  139. curMonth: _month,
  140. weekList: _weeklist
  141. })
  142. }
  143. },
  144. /**
  145. * 生命周期函数--监听页面显示
  146. */
  147. onShow() {
  148. if (app.globalData.navigateBackParams.address ) {
  149. this.setData({
  150. hasAddress: true,
  151. address: app.globalData.navigateBackParams.address
  152. })
  153. }
  154. },
  155. formatterFn (day) {
  156. console.log(day);
  157. },
  158. /**
  159. * 选择地址
  160. */
  161. handleChoseAddress() {
  162. wx.navigateTo({
  163. url: '/pages/address/address?form=appointment&back=1',
  164. })
  165. },
  166. handleChangeAddress () {
  167. if (!this.data.appointmentSuccess) {
  168. wx.navigateTo({
  169. url: '/pages/address/address?form=appointment&back=1',
  170. })
  171. }
  172. },
  173. handleTime (e) {
  174. console.log(e);
  175. var timeActiveIndex = e.currentTarget.dataset.time
  176. this.setData({
  177. timeActive: timeActiveIndex
  178. })
  179. },
  180. // 激活卡片
  181. handleActiveCard () {
  182. wx.navigateTo({
  183. url: '/pages/scan/scan?form=appointment',
  184. })
  185. },
  186. // 去充值
  187. handleCharge () {},
  188. // 确定
  189. handleConfirm () {
  190. if (!this.data.hasAddress) {
  191. wx.showToast({
  192. title: '请选择地址',
  193. icon: 'error',
  194. mask: true
  195. })
  196. } else {
  197. var itt = this
  198. wx.showLoading({
  199. title: '预约中...'
  200. })
  201. setTimeout(() => {
  202. wx.hideLoading({
  203. success: (res) => {
  204. wx.showToast({
  205. title: '预约成功',
  206. })
  207. itt.setData({
  208. appointmentSuccess: true
  209. })
  210. }
  211. })
  212. }, 1200);
  213. }
  214. }
  215. })