import { api_executePayment, api_queryByParamName, api_studentOrderPage, api_userPaymentCancelRefund, api_userPaymentOrderUnpaid } from "../../api/login"; import { formatPrice } from "../../utils/util"; // 获取应用实例 const app = getApp() Page({ /** * 页面的初始数据 */ data: { serviceShow: true, tabList: [ { id: 0, label: "全部", }, { id: 1, label: "待支付", }, // { // id: 2, // label: "待使用", // }, { id: 3, label: "已完成", }, { id: 4, label: "已取消", }, // { // id: 5, // 改编号会影响详情页的退款按钮 // label: "售后", // }, ], paymentType: null as any, // 支付类型 paymentChannel: null as any, tabIdx: 0, // 当前选中的tab索引 page: 1, rows: 10, recordList: [], maxPage: 1, // 总分页数 refoundStatus: false, cancelRefoundStatus: false, goodsInfo: {}, // 选中的数据 }, /** * 生命周期函数--监听页面加载 */ onLoad() { }, onShow() { this.setData({ serviceShow: true, page: 1, maxPage: 1, recordList: [], }, () => { this.getList(false) }) }, onHide() { this.setData({ serviceShow: false }) }, /** 切换分类 */ switchTab(e: { currentTarget: { dataset: { idx: any } } }) { const idx = e.currentTarget.dataset.idx; if (idx != this.data.tabIdx) { this.setData( { tabIdx: idx, page: 1, maxPage: 1, recordList: [], }, () => { this.getList(false); } ); } }, async getList(noLoading: boolean) { if (!noLoading) { wx.showLoading({ mask: true, title: "加载中...", }); } const currentPage = this.data.page, currentRow = this.data.rows, tabIdx = this.data.tabIdx; try { // @ApiModelProperty("订单状态 WAIT_PAY:待支付,WAIT_USE:待使用,SUCCESS:已完成,CLOSE:已取消") const { data } = await api_studentOrderPage({ openId: app.globalData.userInfo?.liteOpenid, page: currentPage, rows: this.data.rows, version: 'V2', wechatOrderStatus: tabIdx == 0 ? "" : tabIdx == 1 ? "WAIT_PAY" : tabIdx == 2 ? "WAIT_USE" : tabIdx == 3 ? "PAID" : tabIdx == 4 ? "CLOSED" : tabIdx == 5 ? 'SALE_AFTER' : "", }) if (data.code == 200) { const { rows, total } = data.data; rows.forEach((item: any) => { item.amount = formatPrice(item.paymentCashAmount, 'ALL') item.statusName = this.formatOrderStatus(item.wechatStatus) let originalPrice = 0 const studentPaymentOrderDetails = item.studentPaymentOrderDetails || []; studentPaymentOrderDetails.forEach((student: any) => { student.originalPrice = formatPrice(student.originalPrice || 0, 'ALL'); const prices: any = formatPrice(student.paymentCashAmount || 0); student.integerPart = prices.integerPart; student.decimalPart = prices.decimalPart; // student.typeName = this.formatPeriod(student.activationCodeInfo?.times || 1, student.activationCodeInfo?.type); // 格式化赠送内容 // student.giftLongTime = this.formatGiftPeriod(student.giftVipDay, student.giftPeriod); // 总的日常价 originalPrice += Number(student.originalPrice || 0) }) item.originalPrice = originalPrice item.discountPrice = formatPrice( originalPrice - item.paymentCashAmount, "ALL" ) // console.log(originalPrice, "originalPrice") item.studentPaymentOrderDetails = studentPaymentOrderDetails }); const newList = this.data.recordList.concat(rows); this.setData( { recordList: newList, maxPage: Math.ceil(total / currentRow), }, () => wx.hideLoading() ); } else { wx.hideLoading(); } } catch (e) { console.log(e, 'e') wx.hideLoading() } }, // 格式化中文 formatOrderStatus(status: string) { // 订单状态 WAIT_PAY:待支付, WAIT_USE:待使用, SUCCESS:已完成, CLOSE:已取消 const template: any = { WAIT_PAY: '待支付', WAIT_USE: '待使用', PAID: '已完成', CLOSED: '已取消', REFUNDING: '退款中', REFUNDED: '退款成功' } return template[status] }, // 格式化类型 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] }, formatGiftPeriod(num: number, type: string) { if (!num || !type) { return '' } const template: any = { DAY: "天", MONTH: "个月", YEAR: "年" } return num + template[type] }, /** 加载更多 */ loadMore() { const currentPage = this.data.page; if (this.data.page >= this.data.maxPage) { // wx.showToast({ // title: "没有更多数据了", // icon: "none", // duration: 1000, // }); } else { this.setData( { page: currentPage + 1, }, () => { this.getList(false); } ); } }, onPay(e: any) { const { dataset } = e.currentTarget const item: any = this.data.recordList.find((item: any) => item.id === dataset.id) if (item) { this.onSubmit({ orderNo: item.orderNo }) } }, onOne() { wx.redirectTo({ url: '../index/index', }) }, onDetail(e: any) { const { dataset } = e.currentTarget if (dataset.wechatstatus === "WAIT_PAY") { const orderDetail: any = this.data.recordList.find((item: any) => item.orderNo === dataset.orderno) if (!orderDetail) return const details = orderDetail.studentPaymentOrderDetails || [] const params = [] as any details.forEach((item: any) => { params.push({ pic: item.goodsUrl, name: item.goodsName, originalPrice: item.originalPrice, salePrice: item.paymentCashAmount, // shopId: item.shopId, // id: item.id, goodsType: item.goodsType, // INSTRUMENTS }) }) let info = JSON.stringify({ ...params }); info = encodeURIComponent(info); wx.navigateTo({ url: `../orders/order-detail?orderInfo=${info}&orderNo=${orderDetail.orderNo}&status=${orderDetail.wechatStatus}`, }); } else { wx.navigateTo({ url: `../orders/order-result?orderNo=${dataset.orderno}&tabIdx=${this.data.tabIdx}` }) } }, // 购买 async onSubmit(goodsInfo: any) { wx.showLoading({ mask: true, title: "订单提交中...", }); try { const { orderNo } = goodsInfo 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() } } 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', paymentType, userId: app.globalData.userInfo?.id, code: wxres.code, wxMiniAppId: app.globalData.appId }) wx.hideLoading() if (res.data.code === 200) { this.onPaying(paymentType, res.data.data.reqParams, orderNo) } else { this.onPayError(res.data.message) } }, fail: () => { this.onPayError() } }) }, onPaying(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' }); // that.onRefoundComfirm() }, fail(ressonInfo) { console.log('支付失败', ressonInfo) that.onPayError() } }) }, // 获取后台配置的支付方式 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 onRefounded(e: any) { const { dataset } = e.currentTarget const item: any = this.data.recordList.find((item: any) => item.id === dataset.id) console.log(dataset, item, 'item') if (!item) { return } if (item.wechatStatus === "REFUNDING") { this.setData({ cancelRefoundStatus: true }) try { const refundOrderId = item.refundOrderId const { data } = await api_userPaymentCancelRefund(refundOrderId) console.log(data, 'data') if (data.code == 200) { wx.showToast({ title: '取消退款成功', icon: 'none' }) this.onRefoundComfirm() } else { wx.showToast({ title: data.message, icon: 'none' }) this.setData({ cancelRefoundStatus: false }) } } catch { } } else { const { orderNo, studentPaymentOrderDetails } = item const goodsInfo: any = { orderNo, goods: [] } if (Array.isArray(studentPaymentOrderDetails)) { studentPaymentOrderDetails.forEach((item: any) => { goodsInfo.goods.push({ ...item, id: item.userPaymentOrderDetailId, currentPrice: item.paymentCashAmount }) }) } this.setData({ goodsInfo, cancelRefoundStatus: true, refoundStatus: true }) } }, changeRefoundStatus(e: { detail: any }) { this.setData({ cancelRefoundStatus: false, refoundStatus: e.detail }) }, onRefoundComfirm() { const that = this this.setData({ refoundStatus: false }) // setTimeout(() => { that.setData({ cancelRefoundStatus: false, page: 1, maxPage: 1, recordList: [], }, () => { this.getList(true) }) // }, 1500); }, onShareAppMessage() { return { title: '音乐数字AI', path: '/pages/index/index', imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png' } }, })