appointment.js 6.7 KB

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