appointment.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. const app = getApp();
  2. import productApi from '../../api/product'
  3. import storeApi from '../../api/store'
  4. import createTimeApi from '../../utils/date'
  5. import orderApi from '../../api/order'
  6. import until from '../../utils/util'
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. peopleNum: 1,
  13. activeTab: 0,
  14. shopInfo: {},
  15. shopId: 0,
  16. storeName: '',
  17. productId: 0,
  18. productImg: '',
  19. productName: '',
  20. price: 0,
  21. tabs: [],
  22. orderTime: "",
  23. orderTimeHeight: 0,
  24. days: [],
  25. hasMobile: false,
  26. mobile: '',
  27. address:'',
  28. shopName:'',
  29. userName:''
  30. },
  31. /**
  32. * 生命周期函数--监听页面加载
  33. */
  34. onLoad: async function (options) {
  35. console.log(options)
  36. let shopId = options.shopId;
  37. let productId = options.productId
  38. const isAuth = await app.isAuth()
  39. if (!isAuth) {
  40. wx.redirectTo({
  41. url: '/pages/prompt/prompt?page=' + this.route+`&shopId=${shopId}&productId=${productId}`,
  42. })
  43. return
  44. }
  45. let storeInfo = await storeApi.getStoreById(shopId);
  46. let address = storeInfo.info.address;
  47. let shopName = storeInfo.info.shopName;
  48. let product = await productApi.getProductById(productId);
  49. let productImg = product.info.productImg;
  50. let productName = product.info.productName;
  51. let price = product.info.currentPrice;
  52. this.setData({
  53. shopId,
  54. productId,
  55. productImg,
  56. productName,
  57. price,
  58. address,
  59. shopName
  60. })
  61. let userInfo = await app.getWxUserInfo();
  62. console.log(userInfo.name)
  63. if (userInfo) {
  64. this.setData({
  65. userInfo: userInfo,
  66. hasUserInfo: true,
  67. hasMobile: userInfo.mobile.length > 1 ? true : false,
  68. mobile: userInfo.mobile,
  69. userName:userInfo.name
  70. })
  71. }
  72. await this.createTimeList();
  73. },
  74. checkAuth: async function () {
  75. const self = this
  76. const isAuth = await app.isAuth()
  77. if (!isAuth) {
  78. wx.redirectTo({
  79. url: '/pages/appointment/appointment?page=' + this.route,
  80. })
  81. return
  82. }
  83. if (!self.data.orderTime){
  84. wx.showModal({
  85. title: '提示',
  86. content: '请选择预约时间',
  87. showCancel:false,
  88. })
  89. return
  90. }
  91. if (!self.data.mobile){
  92. wx.showModal({
  93. title: '提示',
  94. content: '请输入手机号',
  95. showCancel:false,
  96. })
  97. return
  98. }
  99. let data = {
  100. productId: self.data.productId,
  101. storeId: self.data.shopId,
  102. appointmentTime: self.data.orderTime,
  103. num: self.data.peopleNum,
  104. mobile: self.data.mobile
  105. }
  106. const rs = await orderApi.createOrder(data)
  107. let orderId = rs.orderId;
  108. wx.requestPayment({
  109. timeStamp: rs.timeStamp,
  110. nonceStr: rs.nonceStr,
  111. package: rs.package,
  112. signType: rs.signType,
  113. paySign: rs.paySign,
  114. success (res) {
  115. setTimeout(()=>{
  116. wx.navigateTo({
  117. url: '../order/order?id=' + orderId,
  118. })
  119. },2000)
  120. },
  121. fail (res) {
  122. wx.navigateTo({
  123. url: '../order/order?id=' + orderId,
  124. })
  125. }
  126. })
  127. },
  128. getMobile: async function (params) {
  129. console.log(params);
  130. let _self = this
  131. const data = {
  132. encryptedData:params.detail.encryptedData,
  133. iv: params.detail.iv
  134. }
  135. const userInfo = await app.getPhoneNumber(data)
  136. console.log(userInfo.mobile)
  137. let hasMobile = false
  138. if (userInfo.mobile > 1) {
  139. hasMobile = true
  140. }
  141. _self.setData({
  142. hasMobile: hasMobile,
  143. mobile: userInfo.mobile,
  144. })
  145. },
  146. onSlideChangeEnd: function (e) {
  147. var that = this;
  148. that.setData({
  149. activeTab: e.detail.index
  150. })
  151. },
  152. //生成当前可预约的时间
  153. createTimeList: async function () {
  154. var storeId = this.data.shopId;
  155. var price = this.data.price;
  156. var shopInfo = await storeApi.getStoreById(storeId);
  157. var openTime = shopInfo.info.openTime;
  158. var closeTime = shopInfo.info.closeTime;
  159. var date = await storeApi.getStoreAppointTime(storeId);
  160. var days = date.date;
  161. console.log(days)
  162. console.log(price)
  163. const tabs = createTimeApi.createTimeList(openTime, closeTime, days, price);
  164. console.log(tabs)
  165. this.setData({
  166. storeName: shopInfo.info.storeName,
  167. days,
  168. tabs
  169. })
  170. },
  171. getOrderTime: function (e) {
  172. //选中日期的下标
  173. var i = this.data.activeTab;
  174. //选中时间的下标
  175. var index = e.currentTarget.dataset.i;
  176. //获取选中的日期
  177. let date = this.data.days[i];
  178. let time = e.currentTarget.dataset.time;
  179. let year = (new Date()).getFullYear()
  180. let oTime = year + '-' + date + ' ' + time;
  181. //设置选中的样式
  182. var tabs = this.data.tabs;
  183. var list = tabs[i].list;
  184. //先清空其他选中的样式
  185. list.forEach(function (item, i, array) {
  186. item.checked = false;
  187. })
  188. //设置在开通时间内选中的样式
  189. if (list[index].inTime) {
  190. list[index].checked = true;
  191. this.data.tabs[i].list = list;
  192. this.setData({
  193. tabs,
  194. orderTime: oTime,
  195. })
  196. }
  197. },
  198. addPeopleNum: function (e) {
  199. var num = e.currentTarget.dataset.num;
  200. var maxNum = 9;
  201. if (num < maxNum) {
  202. num++;
  203. }
  204. this.setData({
  205. peopleNum: num
  206. })
  207. },
  208. reducePeopleNum: function (e) {
  209. var num = e.currentTarget.dataset.num;
  210. if (num > 1) {
  211. num--;
  212. }
  213. this.setData({
  214. peopleNum: num
  215. })
  216. },
  217. /**
  218. * 生命周期函数--监听页面初次渲染完成
  219. */
  220. onReady: function () {
  221. },
  222. /**
  223. * 生命周期函数--监听页面显示
  224. */
  225. onShow: async function () {
  226. },
  227. /**
  228. * 生命周期函数--监听页面隐藏
  229. */
  230. onHide: function () {
  231. },
  232. /**
  233. * 生命周期函数--监听页面卸载
  234. */
  235. onUnload: function () {
  236. },
  237. /**
  238. * 页面相关事件处理函数--监听用户下拉动作
  239. */
  240. onPullDownRefresh: function () {
  241. },
  242. /**
  243. * 页面上拉触底事件的处理函数
  244. */
  245. onReachBottom: function () {
  246. },
  247. /**
  248. * 用户点击右上角分享
  249. */
  250. onShareAppMessage: function () {
  251. },
  252. })