appointment.js 9.6 KB

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