reserve.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. const api = require('../../config/api.js');
  2. const util = require('../../utils/util.js');
  3. import play from '../../utils/pay.js'
  4. // 手机号校验
  5. const PHONE = {
  6. REG: /^((13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8})|(8){11}$/,
  7. tip: '手机号格式不正确!'
  8. }
  9. // pages/reserve/reserve.js
  10. Page({
  11. /**
  12. * 页面的初始数据
  13. */
  14. data: {
  15. date: '',
  16. people: 0,
  17. showDate: '',
  18. name: '',
  19. phone: '',
  20. code: '',
  21. sex:1,
  22. showShare: false,
  23. sendMis: 0
  24. },
  25. focusHandle() {
  26. console.log(this.data.name, this.data.name.length)
  27. this.setData({
  28. name: this.data.name.substr(0, 4)
  29. })
  30. },
  31. setSex1() {
  32. this.setData({sex: 1})
  33. },
  34. setSex0() {
  35. this.setData({sex: 0})
  36. },
  37. async sendCode() {
  38. if (this.data.sendMis !== 0) return;
  39. if (!PHONE.REG.test(this.data.phone)) {
  40. return wx.showToast({
  41. icon: 'none',
  42. title: '请输入正确的手机号码',
  43. })
  44. }
  45. this.data.sendMis = 60
  46. try {
  47. await util.request(api.OrderSendCode, {phoneNum: this.data.phone}, 'GET')
  48. this.setData({
  49. sendMis: 60
  50. })
  51. let interval = setInterval(() => {
  52. if (this.data.sendMis === 0) return clearInterval(interval)
  53. this.setData({ sendMis: this.data.sendMis - 1 })
  54. }, 1000)
  55. } catch (e) {
  56. this.data.sendMis = 0
  57. }
  58. },
  59. updateName(ev) {
  60. this.setData({name: ev.detail.value})
  61. },
  62. updatePhone(ev) {
  63. this.setData({phone: ev.detail.value})
  64. },
  65. updateCode(ev) {
  66. this.setData({code: ev.detail.value})
  67. },
  68. enter() {
  69. this.quit()
  70. },
  71. quit() {
  72. wx.navigateBack()
  73. },
  74. async getBrandDetail(id) {
  75. let res = await util.request(api.BrandDetail, { canteenId: id}, 'GET')
  76. this.setData({
  77. brand: res.data
  78. })
  79. wx.setNavigationBarTitle({
  80. title: res.data.canteenName//页面标题为路由参数
  81. })
  82. },
  83. /**
  84. * 用户点击右上角分享
  85. */
  86. onShareAppMessage: function (res) {
  87. let userInfo = wx.getStorageSync('userInfo')
  88. if (res.from === 'button') {
  89. setTimeout(() => {
  90. this.quit()
  91. }, 50)
  92. return {
  93. title: '【好友分享】特设薄宴,静待贵客',
  94. imageUrl: '/static/images/share.png',
  95. path: `pages/scene/scene?id=${this.data.brand.id}&scene=${this.data.brand.sceneNum}&shareUserId=${userInfo.id}`
  96. }
  97. } else {
  98. return {
  99. title: '【好友推荐】场景还原,实现线上选座',
  100. imageUrl: '/static/images/share.png',
  101. path: `pages/scene/scene?id=${this.data.brand.id}&scene=${this.data.brand.sceneNum}&shareUserId=${userInfo.id}`
  102. }
  103. }
  104. },
  105. async getDeskDetail(canteenId, qid) {
  106. let res = await util.request(api.deskList, {canteenId: canteenId, pageNum: 1, pageSize: 1000}, 'GET')
  107. this.setData({
  108. desk: res.data.list.find(item => item.tmDesk.id === qid).tmDesk
  109. })
  110. },
  111. /**
  112. * 生命周期函数--监听页面加载
  113. */
  114. onLoad: async function (options) {
  115. this.getBrandDetail(options.canteenId)
  116. this.getDeskDetail(options.canteenId, options.deskId)
  117. this.setData({ people: options.people, date: options.date, showDate: options.date.substring(5, 16) })
  118. },
  119. async save() {
  120. if (this.saveIng) return;
  121. this.saveIng = true
  122. if (!PHONE.REG.test(this.data.phone)) {
  123. return wx.showToast({
  124. icon: 'none',
  125. title: '请输入正确的手机号码',
  126. })
  127. } else if (!Number(this.data.code) || this.data.code.length !== 6) {
  128. return wx.showToast({
  129. icon: 'none',
  130. title: '验证码错误',
  131. })
  132. } else if (!this.data.name.trim().length) {
  133. return wx.showToast({
  134. icon: 'none',
  135. title: '请输入预订人',
  136. })
  137. }
  138. let res = await util.request(api.OrderSubmit, {
  139. canteenId: this.data.brand.id,
  140. deskId: this.data.desk.id,
  141. deskName: this.data.desk.deskName,
  142. deskType: this.data.desk.type,
  143. msgCode: this.data.code,
  144. orderPrice: 0.01,
  145. reserveDate: this.options.date.substr(0, 10),
  146. reserveUserGender: this.data.sex,
  147. reservePersonNum: this.options.people,
  148. reserveTime: this.options.date+":00",
  149. // reserveUserId 预订人的ID false
  150. reserveUserName: this.data.name,
  151. reserveUserPhone: this.data.phone
  152. }, 'POST', 'application/json')
  153. if (res.code !== 0) {
  154. return wx.showToast({
  155. icon: 'none',
  156. title: res.msg,
  157. })
  158. }
  159. try {
  160. await play(res.data.id)
  161. let res1 = await util.request(api.OrderQuery, { orderId: res.data.id})
  162. wx.showToast({
  163. icon: 'success',
  164. title: res1.errmsg
  165. })
  166. getApp().callOrderBuyCbs({deskId: this.data.desk.id})
  167. this.saveIng = false
  168. return this.setData({showShare: true})
  169. } catch (e) {
  170. wx.showToast({
  171. icon: 'none',
  172. title: '付款失败',
  173. })
  174. wx.redirectTo({
  175. url: '/pages/ucenter/order/order',
  176. })
  177. this.saveIng = false
  178. }
  179. },
  180. })