// pages/orders/order-detail.ts import { api_executeOrder, api_executePayment, api_queryByParamName, api_userPaymentOrderDetail, api_userPaymentOrderUnpaid } from "../../api/login"; import { api_sysAreaQueryAllProvince, api_userReceiveAddressPage, api_userReceiveAddressSave } from "../../api/new"; import { formatPrice, GRADE_ENUM } from "../../utils/util"; // 获取应用实例 const app = getApp() Page({ /** * 页面的初始数据 */ data: { status: 'WAIT_PAY', statusList: { WAIT_PAY: { logo: './images/ing.png', title: '等待付款', content: '请尽快完成支付,以便我们为您处理订单' }, }, backParams: null, addressList: [] as any, goodsInfo: {} as any, hasInstrument: false, // 是否有乐器 receiveAddress: '', // 选择的地址信息 receiveAddressInfo: { addressDetail: '', name: '', phoneNumber: '' }, userBeneficiaryId: '', // 添加购买人信息 userBeneficiaryInfo: { name: '', phoneNumber: '', schoolInfo: '' }, paymentType: null as any, // 支付类型 paymentChannel: null as any, showService: false, showArea: false, areaList: [] as any, }, /** * 生命周期函数--监听页面加载 */ onLoad(options: any) { this.queryPayType() this.getAddress() if (options.orderInfo) { // console.log('goods', options) const goods = JSON.parse(decodeURIComponent(options.orderInfo)); // console.log(goods, 'goods', options) const infos = { allSalePrice: 0, allOriginPrice: 0, allDiscountPrice: '', discountIntegerPart: '', discountDecimalPart: '', integerPart: '', decimalPart: '', name: '', shopId: '', orderNo: options.orderNo || '', goodsList: [] as any, createTime: '', // 订单时间 } // 是否有乐器 let hasInstrument = false for (let i in goods) { const item = goods[i] if (item.goodsType === "INSTRUMENTS") { hasInstrument = true } infos.name = infos.name ? infos.name + '+' + item.name : item.name infos.shopId = item.shopId const afterPrice: any = formatPrice(item.salePrice) // console.log(infos.goodsList, 'infos.goodsList') infos.goodsList.push({ ...item, typePeriod: this.formatPeriod(item.num, item.period), ...afterPrice }) infos.allSalePrice += Number(item.salePrice) infos.allOriginPrice += Number(item.originalPrice) } const allAfterPrice: any = formatPrice(infos.allSalePrice) infos.allDiscountPrice = formatPrice(infos.allOriginPrice - infos.allSalePrice, 'ALL') as string const allDiscount: any = formatPrice(infos.allOriginPrice - infos.allSalePrice) infos.integerPart = allAfterPrice.integerPart infos.decimalPart = allAfterPrice.decimalPart infos.discountIntegerPart = allDiscount.integerPart infos.discountDecimalPart = allDiscount.decimalPart this.setData({ goodsInfo: infos, userBeneficiaryId: options.userBeneficiaryId, status: options.status || '', hasInstrument }, () => { this.getOrderDetail() }); } }, /** 获取订单详情 */ async getOrderDetail() { try { if (!this.data.goodsInfo.orderNo) return const { data } = await api_userPaymentOrderDetail(this.data.goodsInfo.orderNo, { version: 'V2' }); const result = data.data || {} const addresses: any = result.addresses const tempSchoolAddress = [result.beneficiary?.provinceName || '', result.beneficiary?.cityName || '', result.beneficiary?.regionName || '', result.beneficiary?.schoolAreaName, GRADE_ENUM[result.beneficiary?.currentGradeNum], result.beneficiary?.currentClass + '班'] const beneficiary = { name: result.beneficiary?.name, phoneNumber: result.beneficiary?.phone, schoolInfo: tempSchoolAddress.join('') } this.setData({ receiveAddress: addresses?.id, receiveAddressInfo: { addressDetail: addresses?.detailAddress, name: addresses?.name, phoneNumber: addresses?.phoneNumber }, userBeneficiaryId: result.beneficiary.schoolAreaId, // 添加购买人信息 userBeneficiaryInfo: beneficiary, status: result.wechatStatus, 'goodsInfo.createTime': result.createTime }, () => { console.log(this.data) }) } catch { // } }, // 格式化类型 formatPeriod(num: number, type: string) { if (!type) return '' const template: any = { DAY: "天卡", MONTH: "月卡", YEAR: "年卡" } if (type === "YEAR" && num >= 99) { return '永久卡' } return num + (template[type] || '') }, // 获取后台配置的支付方式 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"); } }, /** 添加收货地址 */ onSelectAddress() { if (this.data.goodsInfo?.orderNo) { return } console.log(this.data.addressList.length > 0, this.data.receiveAddress) if (this.data.addressList.length > 0 || this.data.receiveAddress) { wx.navigateTo({ url: `../address/index?receiveAddress=${this.data.receiveAddress}`, }) } else { wx.navigateTo({ url: `../address/address-detail`, }) } }, onPayError(message?: string) { wx.hideLoading() wx.showToast({ title: message || '支付取消', icon: 'none' }) }, // 购买 async onSubmit() { if (!this.data.receiveAddress && this.data.hasInstrument) { wx.showToast({ title: '请选择收货地址', icon: 'none' }) return } wx.showLoading({ mask: true, title: "订单提交中...", }); try { const { allSalePrice, shopId, name, orderNo, goodsList } = this.data.goodsInfo const goodsInfos: any = [] goodsList.forEach((item: any) => { goodsInfos.push({ "goodsId": item.id, "goodsNum": 1, "goodsType": item.goodsType, "paymentCashAmount": item.salePrice, "paymentCouponAmount": 0 }) }) 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 { data } = await api_executeOrder({ orderType: "WECHAT_MINI", paymentType: this.data.paymentType, paymentCashAmount: allSalePrice, paymentCouponAmount: 0, shopId: shopId, openId: app.globalData.userInfo?.liteOpenid, goodsInfos: goodsInfos, receiveAddress: this.data.receiveAddress, userBeneficiaryId: this.data.userBeneficiaryId, orderName: name, orderDesc: name }) 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() } } } 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' }); wx.redirectTo({ url: '/pages/orders/order-result?orderNo=' + orderNo }) }, fail(ressonInfo) { console.log('支付失败', ressonInfo) that.onPayError() that.setData({ 'goodsInfo.orderNo': orderNo }, () => { that.getOrderDetail() }) } }) }, /** 客服 */ onService() { // console.log("showService") this.setData({ showService: true }) }, changePop(event: { detail: any }) { this.setData({ showService: event.detail }) }, /** * 生命周期函数--监听页面显示 */ onShow() { if (this.data.backParams) { console.log(this.data.backParams, 'backParams'); // { key: 'value' } const backParams: any = this.data.backParams || {}; this.setData({ receiveAddress: backParams.receiveAddress, receiveAddressInfo: backParams.receiveAddressInfo, backParams: null // 清空参数 }) } this.getAddress() }, /** 地址列表 */ async getAddress() { try { const { data } = await api_userReceiveAddressPage({ page: 1, rows: -1 }) this.setData({ addressList: data.data.rows || [] }) } catch { // } }, onCopy(e: { currentTarget: any }) { wx.setClipboardData({ data: e.currentTarget.dataset.orderno, success: () => { wx.showToast({ title: '复制成功', icon: 'none' }) }, fail: () => { wx.showToast({ title: '复制失败,请稍后再试', icon: 'none' }) } }) }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: '翼时代器乐数字Ai', path: '/pages/index/index', imageUrl: 'https://oss.dayaedu.com/ktyq/1739865626350.png' } } })