// pages/orders/order-detail.ts import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderUnpaid, api_getUserReceiveAddressPage, api_userPaymentOrderDetail } from "../../api/login"; const GRADE_ENUM = { '1': '一年级', '2': '二年级', '3': '三年级', '4': '四年级', '5': '五年级', '6': '六年级', '7': '七年级', '8': '八年级', '9': '九年级' } as any; // 获取应用实例 const app = getApp() Page({ /** * 页面的初始数据 */ data: { popupShow: false, isAddressInfoTip: false, addressInfo: {} as any, serviceShow: true, status: '', statusList: { WAIT_PAY: { logo: './images/ing.png', title: '等待支付', content: '请尽快完成支付,以确保订单顺利进行' }, PAID: { logo: './images/success.png', title: '交易完成', content: '您的订单已完成,感谢您的信任与支持' }, CLOSED: { logo: './images/error.png', title: '交易取消', content: '您的订单已取消,如需购买请重新下单' }, WAIT_USE: { logo: './images/wait.png', title: '待使用', content: '为了顺利使用,请尽快扫描下方二维码进行激活' }, REFUNDING: { logo: './images/refounding.png', title: '退款中', content: '您的订单正在退款中,预计7个工作日内审核完' }, REFUNDED: { logo: './images/refounded.png', title: '已退款', content: '您的订单已成功退款,感谢您的耐心等待' } }, orderNo: "", goodsInfo: {} as any, instrumentsInfo: {} as any, memberInfo: {} as any, orderInfo: {} as any, //订单信息 paymentType: null as any, // 支付类型 paymentChannel: null as any, }, onTimeing() { if (!app.globalData.isLogin) { setTimeout(() => { this.onTimeing() }, 500); } else { this.getDetail() } }, /** * 生命周期函数--监听页面加载 */ onLoad(options: any) { this.queryPayType() // if (options.orderInfo) { // const goods = JSON.parse(decodeURIComponent(options.orderInfo)); // const instrumentsInfo = JSON.parse(decodeURIComponent(options.instrumentsInfo)); // const memberInfo = JSON.parse(decodeURIComponent(options.memberInfo)); // console.log(goods, 'goods', instrumentsInfo, memberInfo) // this.setData({ // goodsInfo: goods, // instrumentsInfo, // memberInfo, // status: goods.status // }); // } if (options.orderNo) { this.setData({ orderNo: options.orderNo }, () => { this.onTimeing() }) } }, async getDetail(callback?: any) { try { const { data } = await api_userPaymentOrderDetail(this.data.orderNo); if (data.code == 200) { const { goodsInfos, addresses, beneficiary, wechatStatus, createTime } = data.data const goodsInfo = {} if (goodsInfos[0]) { goodsInfo.pic = goodsInfos[0].goodsUrl goodsInfo.name = goodsInfos[0].goodsName goodsInfo.originalPrice = goodsInfos[0].originalPrice goodsInfo.salePrice = goodsInfos[0].paymentCashAmount goodsInfo.typeName = this.formatPeriod(goodsInfos[0].activationCodeInfo.times, goodsInfos[0].activationCodeInfo.type) goodsInfo.orderNo = this.data.orderNo } const instrumentsInfo = {} if (goodsInfos[1]) { instrumentsInfo.pic = goodsInfos[1].goodsUrl instrumentsInfo.name = goodsInfos[1].goodsName instrumentsInfo.originalPrice = goodsInfos[1].originalPrice instrumentsInfo.salePrice = goodsInfos[1].paymentCashAmount instrumentsInfo.id = goodsInfos[1].id } const addressInfo = {} if (addresses && instrumentsInfo.id) { addressInfo.id = addresses.id addressInfo.name = addresses.name addressInfo.phoneNumber = addresses.phoneNumber addressInfo.addressDes = addresses.detailAddress } const memberInfo = {} if (beneficiary) { memberInfo.name = beneficiary.name memberInfo.phone = beneficiary.phone memberInfo.schoolInfo = beneficiary.provinceName + beneficiary.cityName + beneficiary.regionName + beneficiary.schoolAreaName + GRADE_ENUM[beneficiary.currentGradeNum] + beneficiary.currentClass + "班" } const orderInfo = { createTime, orderNo: this.data.orderNo } this.setData({ goodsInfo, instrumentsInfo, addressInfo, memberInfo, status: wechatStatus, orderInfo }) } } catch (error) { console.log(error, "error"); } }, // 格式化类型 formatPeriod(num: number, type: string) { if (!num || !type) { return '' } const template: any = { DAY: "天", MONTH: "月", YEAR: "年" } if (type === "YEAR" && num >= 99) { return '时长: 永久' } return "时长: " + num + template[type] }, onCopy(e: { currentTarget: any }) { wx.setClipboardData({ data: e.currentTarget.dataset.orderno, success: () => { wx.showToast({ title: '复制成功', icon: 'none' }) }, fail: () => { wx.showToast({ title: '复制失败,请稍后再试', icon: 'none' }) } }) }, // 获取后台配置的支付方式 async queryPayType() { try { // wxlite_payment_service_provider const { data } = await api_queryByParamName({ paramName: app.globalData.appId }); if (data.code == 200) { const paramValue = data.data.paramValue ? JSON.parse(data.data.paramValue) : {} this.setData({ paymentType: paramValue.vendor, paymentChannel: paramValue.channel }); } } catch (error) { console.log(error, "error"); } }, onPayError(message?: string) { wx.hideLoading() wx.showToast({ title: message || '支付取消', icon: 'none' }) }, // 购买 async onSubmit() { // 有乐器必须填收货信息 // if (this.data.instrumentsInfo.id && !this.data.addressInfo.id) { // wx.showToast({ // title: "请填写收货信息", // icon: 'none' // }) // this.setData({ // isAddressInfoTip: true // }) // return // } console.log(this.data.addressInfo) wx.showLoading({ mask: true, title: "订单提交中...", }); try { const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo if (orderNo) { const { data } = await api_userPaymentOrderUnpaid({ orderNo: orderNo, paymentType: 'WECHAT_MINI' }) if (data.code === 200) { const { paymentConfig, paymentType, orderNo } = data.data.paymentConfig this.onExecutePay(paymentConfig, paymentType, orderNo) } else { this.onPayError() } } else { const goodsInfos = [{ "goodsId": id, "goodsNum": 1, "goodsType": "ACTIVATION_CODE", "paymentCashAmount": salePrice, "paymentCouponAmount": 0 }] // 乐器 if (this.data.instrumentsInfo.id) { goodsInfos.push({ "goodsId": this.data.instrumentsInfo.id, "goodsNum": 1, "goodsType": "INSTRUMENTS", "paymentCashAmount": this.data.instrumentsInfo.salePrice, "paymentCouponAmount": 0 }) } const { data } = await api_executeOrder({ "orderType": "WECHAT_MINI", "paymentType": this.data.paymentType, "paymentCashAmount": salePrice + (this.data.instrumentsInfo.salePrice || 0), "paymentCouponAmount": 0, "shopId": shopId, "openId": app.globalData.userInfo?.liteOpenid, goodsInfos, "orderName": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""), "orderDesc": name + (this.data.instrumentsInfo.name ? `+${this.data.instrumentsInfo.name}` : ""), "receiveAddress": this.data.addressInfo.id || "", "userBeneficiaryId": this.data.memberInfo.id }) if (data.code === 200) { const { paymentConfig, paymentType, orderNo } = data.data this.onExecutePay(paymentConfig, paymentType, orderNo) } else if (data.code === 5200) { wx.hideLoading() wx.showToast({ title: data.message, icon: 'none' }) } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(data.code)) { wx.hideLoading() wx.showToast({ title: data.message, icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1000) } else { this.onPayError(data.message) } } } catch { wx.hideLoading() } }, async onExecutePay(paymentConfig: any, paymentType: string, orderNo: string) { wx.login({ success: async (wxres: any) => { const res = await api_executePayment({ merOrderNo: paymentConfig.merOrderNo, paymentChannel: this.data.paymentChannel || 'wx_lite', // 'wx_pub', // paymentType, userId: app.globalData.userInfo?.id, code: wxres.code, wxMiniAppId: app.globalData.appId // code: '011yjYkl289aye4q2zml24UEWT3yjYkn', // wxPubAppId: 'wxbde13f59d40cb4f2' }) wx.hideLoading() if (res.data.code === 200) { this.onPay(paymentType, res.data.data.reqParams, orderNo) } else if ([5435, 5436, 5437, 5439, 5442, 5443, 5408, 5427, 5432].includes(res.data.code)) { wx.hideLoading() wx.showToast({ title: res.data.message, icon: 'none' }) setTimeout(() => { wx.navigateBack() }, 1000) } else { this.onPayError(res.data.message) } }, fail: () => { this.onPayError() } }) }, onPay(paymentType: string, paymentConfig: any, orderNo: string) { const isYeePay = paymentType.indexOf('yeepay') !== -1 const prePayInfo = isYeePay ? JSON.parse(paymentConfig.prePayTn) : paymentConfig?.expend ? JSON.parse(paymentConfig?.expend?.pay_info) : paymentConfig const that = this wx.requestPayment({ timeStamp: prePayInfo.timeStamp, nonceStr: prePayInfo.nonceStr, package: prePayInfo.package ? prePayInfo.package : prePayInfo.packageValue, paySign: prePayInfo.paySign, signType: prePayInfo.signType ? prePayInfo.signType : 'MD5', success() { wx.showToast({ title: '支付成功', icon: 'success' }); setTimeout(() => { that.getDetail() }, 1500) }, fail(ressonInfo) { console.log('支付失败', ressonInfo) that.onPayError() const goodsInfo = that.data.goodsInfo goodsInfo.orderNo = orderNo that.setData({ goodsInfo }) } }) }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: ' 器乐数字ai助手', path: '/pages/index/index', imageUrl: 'https://oss.dayaedu.com/ktyq/1739929620914.png' } } })