appointment.js 9.3 KB

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