// pages/buy/buy.js import { goodsList } from '../../api/index' import { submitOrder } from '../../api/pay' import { getAccountNmber } from '../../api/appointment' const app = getApp() Page({ /** * 页面的初始数据 */ data: { count: 0, activeIndex: -1, goodsList: [], goodsId: '', orderAmount: '', num: '', appusername: '', appuserheadimg: '', from: '' // 从哪个页面进入的页面 }, /** * 生命周期函数--监听页面显示 */ onLoad(options) { console.log(options, 'options'); this.getGoodsList() this.getAccountNmberFn() this.setData({ from: options.from, appusername: app.globalData.userInfo.userName.length > 8 ? app.globalData.userInfo.userName.substring(0,4) : app.globalData.userInfo.userName, appuserheadimg: app.globalData.userInfo.headImg }) }, // 获取套餐次数 getAccountNmberFn () { var that = this getAccountNmber({}).then(res => { that.setData({ count: res.data }) }) }, //获取套餐列表 getGoodsList () { wx.showLoading({ title: '加载中...', mask: true }) goodsList({}).then(res => { wx.hideLoading() var response = res.data var recommendIndx = 0 var recommendItem = response.find((item, index) => { recommendIndx = index return item.isRecommend == true }) console.log(recommendItem,recommendIndx,'recommendItem'); if (recommendItem) { this.setData({ activeIndex: recommendIndx, goodsId: recommendItem.goodsId, orderAmount: recommendItem.salePrice, num: recommendItem.donateNumber + recommendItem.useNumber }) } else { var choseItem = response[0] this.setData({ activeIndex: 0, goodsId: choseItem.goodsId, orderAmount: choseItem.salePrice, num: choseItem.donateNumber + choseItem.useNumber }) } this.setData({ goodsList: response }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, handlePayType (e) { var index = Number(e.currentTarget.dataset.index) var goodsInfo = e.currentTarget.dataset.goodsinfo console.log(goodsInfo, 'goodsInfo'); this.setData({ activeIndex: index, goodsId: goodsInfo.goodsId, orderAmount: goodsInfo.salePrice, num: goodsInfo.donateNumber + goodsInfo.useNumber }) }, // 调用微信支付 handleWXPay () { var that = this var data = { goodsId: this.data.goodsId, orderAmount: this.data.orderAmount, num: 1 } wx.showLoading({ title: '加载中...', mask: true }) submitOrder(data).then(res => { wx.hideLoading() // 拉起微信支付 wx.requestPayment({ timeStamp: res.data.timeStamp, nonceStr: res.data.nonceStr, package: res.data.packageStr, signType: res.data.signType, paySign: res.data.paySign, success (res) { that.getAccountNmberFn() wx.showModal({ title: '支付成功,立即去预约', confirmText: '去预约', confirmColor: '#333', cancelColor: '#666', success (modalRes) { if (modalRes.confirm) { if (that.data.from == 'appointment') { wx.navigateBack() } else { wx.navigateTo({ url: '/pages/appointment/appointment?from=buy' }) } } } }) }, fail (res) { wx.showToast({ title: '支付失败', icon: "error" }) } }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) } })