appointment.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. wx.navigateTo({
  116. url: '../order/order?id=' + orderId,
  117. })
  118. },
  119. fail (res) {
  120. wx.navigateTo({
  121. url: '../order/order?id=' + orderId,
  122. })
  123. }
  124. })
  125. },
  126. getMobile: async function (params) {
  127. console.log(params);
  128. let _self = this
  129. const data = {
  130. encryptedData:params.detail.encryptedData,
  131. iv: params.detail.iv
  132. }
  133. const userInfo = await app.getPhoneNumber(data)
  134. console.log(userInfo.mobile)
  135. let hasMobile = false
  136. if (userInfo.mobile > 1) {
  137. hasMobile = true
  138. }
  139. _self.setData({
  140. hasMobile: hasMobile,
  141. mobile: userInfo.mobile,
  142. })
  143. },
  144. onSlideChangeEnd: function (e) {
  145. var that = this;
  146. that.setData({
  147. activeTab: e.detail.index
  148. })
  149. },
  150. //生成当前可预约的时间
  151. createTimeList: async function () {
  152. var storeId = this.data.shopId;
  153. var price = this.data.price;
  154. var shopInfo = await storeApi.getStoreById(storeId);
  155. var openTime = shopInfo.info.openTime;
  156. var closeTime = shopInfo.info.closeTime;
  157. var date = await storeApi.getStoreAppointTime(storeId);
  158. var days = date.date;
  159. console.log(days)
  160. console.log(price)
  161. const tabs = createTimeApi.createTimeList(openTime, closeTime, days, price);
  162. console.log(tabs)
  163. this.setData({
  164. storeName: shopInfo.info.storeName,
  165. days,
  166. tabs
  167. })
  168. },
  169. getOrderTime: function (e) {
  170. //选中日期的下标
  171. var i = this.data.activeTab;
  172. //选中时间的下标
  173. var index = e.currentTarget.dataset.i;
  174. //获取选中的日期
  175. let date = this.data.days[i];
  176. let time = e.currentTarget.dataset.time;
  177. let year = (new Date()).getFullYear()
  178. let oTime = year + '-' + date + ' ' + time;
  179. //设置选中的样式
  180. var tabs = this.data.tabs;
  181. var list = tabs[i].list;
  182. //先清空其他选中的样式
  183. list.forEach(function (item, i, array) {
  184. item.checked = false;
  185. })
  186. //设置在开通时间内选中的样式
  187. if (list[index].inTime) {
  188. list[index].checked = true;
  189. this.data.tabs[i].list = list;
  190. this.setData({
  191. tabs,
  192. orderTime: oTime,
  193. })
  194. }
  195. },
  196. addPeopleNum: function (e) {
  197. var num = e.currentTarget.dataset.num;
  198. var maxNum = 9;
  199. if (num < maxNum) {
  200. num++;
  201. }
  202. this.setData({
  203. peopleNum: num
  204. })
  205. },
  206. reducePeopleNum: function (e) {
  207. var num = e.currentTarget.dataset.num;
  208. if (num > 1) {
  209. num--;
  210. }
  211. this.setData({
  212. peopleNum: num
  213. })
  214. },
  215. /**
  216. * 生命周期函数--监听页面初次渲染完成
  217. */
  218. onReady: function () {
  219. },
  220. /**
  221. * 生命周期函数--监听页面显示
  222. */
  223. onShow: async function () {
  224. },
  225. /**
  226. * 生命周期函数--监听页面隐藏
  227. */
  228. onHide: function () {
  229. },
  230. /**
  231. * 生命周期函数--监听页面卸载
  232. */
  233. onUnload: function () {
  234. },
  235. /**
  236. * 页面相关事件处理函数--监听用户下拉动作
  237. */
  238. onPullDownRefresh: function () {
  239. },
  240. /**
  241. * 页面上拉触底事件的处理函数
  242. */
  243. onReachBottom: function () {
  244. },
  245. /**
  246. * 用户点击右上角分享
  247. */
  248. onShareAppMessage: function () {
  249. },
  250. })