appointment.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 > 4 ? 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. that.setData({
  156. choseDate: _year + '-' + _month + '-' + _day
  157. })
  158. }
  159. item.dateStr = _year + '-' + _month + '-' + _day
  160. } else {
  161. item.disabled = true
  162. item.dateStr = baseDate.getFullYear() + '-' + _month + '-' + _day
  163. if (index == that.data.currentWeek) {
  164. item.date = '约满'
  165. } else {
  166. item.date = baseDate.getDate()
  167. }
  168. }
  169. })
  170. return _mapWeekList
  171. },
  172. createCalendarGrild (count) {
  173. return new Promise((resolve, reject) => {
  174. var _list = []
  175. for (let index = 0; index < count; index++) {
  176. _list.push({
  177. date: '-',
  178. dateStr: '-',
  179. disabled: false,
  180. active: false,
  181. status: '',
  182. busy: false
  183. })
  184. }
  185. this.setData({
  186. weekList: _list
  187. })
  188. resolve()
  189. })
  190. },
  191. // 选择日期
  192. handleSelectDate (e) {
  193. const disabled = e.currentTarget.dataset.disabled
  194. if (!disabled) {
  195. const cDate = e.currentTarget.dataset.date
  196. const index = e.currentTarget.dataset.index
  197. console.log(cDate, 'cDate', new Date(cDate).getDay(), index);
  198. const _year = cDate.split('-')[0]
  199. const _month = cDate.split('-')[1]
  200. const _weeklist = this.data.weekList
  201. //_weeklist[index].active = true
  202. _weeklist.map((item, i) => {
  203. if (index == i ) {
  204. item.active = true
  205. if (new Date(cDate).getDay() == 6 || new Date(cDate).getDay() == 0) {
  206. item.busy = true
  207. } else {
  208. item.busy = false
  209. }
  210. } else {
  211. item.active = false
  212. item.busy = false
  213. }
  214. })
  215. this.setData({
  216. curYear: _year,
  217. curMonth: _month,
  218. weekList: _weeklist,
  219. choseDate: cDate
  220. })
  221. }
  222. },
  223. /**
  224. * 选择地址
  225. */
  226. handleChoseAddress() {
  227. wx.navigateTo({
  228. url: '/pages/address/address?form=appointment&back=1',
  229. })
  230. },
  231. handleChangeAddress () {
  232. if (!this.data.appointmentSuccess) {
  233. wx.navigateTo({
  234. url: '/pages/address/address?form=appointment&back=1',
  235. })
  236. }
  237. },
  238. handleTime (e) {
  239. console.log(e);
  240. var timeActiveIndex = e.currentTarget.dataset.time
  241. this.setData({
  242. timeActive: timeActiveIndex
  243. })
  244. },
  245. // 去充值
  246. handleCharge () {},
  247. // 确定 先查询 区域的剩余预约次数
  248. handleConfirm () {
  249. if (this.data.accountNumber == 0) {
  250. wx.showModal({
  251. content: '您当前的检测次数为0,先充值次数',
  252. cancelColor: '#666',
  253. cancelText: '卡激活',
  254. confirmText: '去充值',
  255. confirmColor: '#333',
  256. success (res) {
  257. if (res.confirm) {
  258. wx.navigateTo({
  259. url: '/pages/buy/buy?from=appointment',
  260. })
  261. } else if (res.cancel) {
  262. wx.navigateTo({
  263. url: '/pages/exchange/exchange',
  264. })
  265. }
  266. }
  267. })
  268. } else if (!this.data.hasAddress) {
  269. wx.showToast({
  270. title: '请选择地址',
  271. icon: 'error',
  272. mask: true
  273. })
  274. } else {
  275. var itt = this
  276. wx.showLoading({
  277. title: '预约中...'
  278. })
  279. var data = {
  280. province: this.data.province,
  281. city: this.data.city,
  282. county: this.data.county,
  283. appointmentDate: this.data.choseDate
  284. }
  285. getResetNumber(data).then(res => {
  286. wx.hideLoading()
  287. itt.submitAppointmentFn()
  288. }).catch(e => {
  289. wx.hideLoading()
  290. wx.showModal({
  291. content: e,
  292. confirmColor: '#333',
  293. showCancel: false
  294. })
  295. })
  296. }
  297. },
  298. // 提交预约信息
  299. submitAppointmentFn () {
  300. var data = {
  301. appointmentTime: this.data.choseDate,
  302. duration: this.data.timeActive,
  303. addressId: this.data.addressId
  304. }
  305. var timer = null
  306. submitAppointment(data).then(res => {
  307. wx.hideLoading()
  308. wx.showToast({
  309. title: '预约成功',
  310. icon: 'success'
  311. })
  312. timer = setTimeout(() => {
  313. wx.navigateTo({
  314. url: '/pages/myAppointment/myAppointment?from=appointment',
  315. })
  316. clearTimeout(timer)
  317. }, 1000);
  318. /* this.setData({
  319. appointmentSuccess: true
  320. }) */
  321. }).catch(e => {
  322. wx.hideLoading()
  323. wx.showModal({
  324. content: e,
  325. confirmColor: '#333',
  326. showCancel: false
  327. })
  328. })
  329. },
  330. resetData () {
  331. app.globalData.navigateBackParams.address = ''
  332. app.globalData.navigateBackParams.contactName = ''
  333. app.globalData.navigateBackParams.contactPhone = ''
  334. app.globalData.navigateBackParams.addressId = ''
  335. app.globalData.navigateBackParams.province = ''
  336. app.globalData.navigateBackParams.city = ''
  337. app.globalData.navigateBackParams.county = ''
  338. this.setData({
  339. hasAddress: true,
  340. appointmentSuccess: false,
  341. timeActive: '1',
  342. addressId: '',
  343. address: '',
  344. province: '',
  345. city: '',
  346. county: '',
  347. })
  348. },
  349. handleCancel () {
  350. this.resetData()
  351. wx.navigateBack()
  352. }
  353. })