appointment.js 6.7 KB

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