// pages/buy/buy.js import { goodsList } from '../../api/index' import { submitOrder } from '../../api/pay' import { getAccountNmber, appointmentList } from '../../api/appointment' import { documentList } from "../../api/document" const app = getApp() Page({ /** * 页面的初始数据 */ data: { count: 0, activeIndex: -1, goodsList: [], goodsId: '', orderAmount: '', num: '', appusername: '', appuserheadimg: '', from: '' // 从哪个页面进入的页面 }, /** * 生命周期函数--监听页面显示 */ onLoad(options) { var headImg = wx.getStorageSync('headImg') var userName = wx.getStorageSync('userName') this.setData({ from: options.from, appusername: userName.length > 8 ? userName.substring(0,4) : userName, appuserheadimg: headImg }) this.initPage() }, initPage () { this.getGoodsList() this.getAccountNmberFn() }, // 获取套餐次数 getAccountNmberFn () { var that = this getAccountNmber({}).then(res => { that.setData({ count: res.data }) app.globalData.useNumber = 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 }) 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 this.setData({ activeIndex: this.data.goodsList.length == 1 ? 0 : 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() that.paySuccess() }, fail (res) { wx.showToast({ title: '支付失败', icon: "error" }) } }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, // 支付成功 paySuccess () { var that = this wx.showModal({ title: '支付成功,立即去预约', confirmText: '去预约', confirmColor: '#333', cancelColor: '#666', success (modalRes) { if (modalRes.confirm) { if (that.data.from == 'appointment') { wx.navigateBack() } else { that.hasAppointmentFn() } } } }) }, // 获取用户预约信息 hasAppointmentFn (flag = true) { var that = this wx.showNavigationBarLoading() appointmentList({ status: 1, currentPage: 1 }).then(res => { wx.hideNavigationBarLoading() if (res.data.vos.length !== 0) { app.globalData.hasAppointment = true that.navigateAppointment() } else { app.globalData.hasAppointment = false that.getDocumentList() } }).catch(e => { wx.hideNavigationBarLoading() }) }, navigateAppointment () { wx.showModal({ content: '您预约的健康筛查还未体验,请先体验', cancelColor: '#666', cancelText: '取消', confirmText: '我的预约', confirmColor: '#333', success (res) { if (res.confirm) { wx.navigateTo({ url: '/pages/myAppointment/myAppointment' }) } } }) }, // 获取用户档案信息 getDocumentList () { wx.showLoading({ title: '加载中...', mask: true }) documentList({ currentPage: 1 }).then(res => { wx.hideLoading() const response = res.data.vos || [] if (response.length == 0) { wx.showModal({ content: '预约用户前需要先添加检测人员信息', cancelColor: '#666', confirmColor: '#333', success (res) { if (res.confirm) { wx.navigateTo({ url: '/pages/createFile/createFile?from=buy' }) } } }) } else { wx.navigateTo({ url: '/pages/appointment/appointment?from=buy' }) } }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, })