// 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: '' }, isExpanded: false, paymentType: null as any, // 支付类型 paymentChannel: null as any, showService: false, showArea: false, areaList: [] as any, currentValues: [] as any, addressShow: false, addressAfterLeave: false, // 添加地址表单信息 id: "", name: '', phoneNumber: '', detailAddress: '', cityCode: 0, cityName: "", provinceCode: 0, provinceName: "", regionCode: '', regionName: "", }, /** * 生命周期函数--监听页面加载 */ onLoad(options: any) { this.queryPayType() 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 // console.log(infos, 'infos') 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 beneficiary: any = result.beneficiary const tempSchoolAddress = [beneficiary?.provinceName, beneficiary?.cityName || '', beneficiary?.regionName || '', beneficiary?.schoolAreaName, GRADE_ENUM[beneficiary?.currentGradeNum], beneficiary?.currentClass + '班'] this.setData({ receiveAddress: addresses?.id, receiveAddressInfo: { addressDetail: addresses?.detailAddress, name: addresses?.name, phoneNumber: addresses?.phoneNumber }, userBeneficiaryId: beneficiary.schoolAreaId, // 添加购买人信息 userBeneficiaryInfo: { name: beneficiary.name, phoneNumber: beneficiary.phone, schoolInfo: tempSchoolAddress.join('') }, 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() { 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 { this.onShowAddress() } }, 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 }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ 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 || [] }, () => { if (this.data.addressList.length <= 0) { this.getAreas() } }) } catch { // } }, /** 获取省市区 */ async getAreas() { try { const { data } = await api_sysAreaQueryAllProvince({}) const areaList: any = this.formateArea(data.data) const currentValues = [] if (areaList?.province_list) { // 获取第一个键值对 const firstKey = Object.keys(areaList?.province_list)[0]; // 通过键获取值 const firstValue = areaList?.province_list[firstKey]; currentValues.push({ code: firstKey, name: firstValue }) } if (areaList?.city_list) { // 获取第一个键值对 const firstKey = Object.keys(areaList?.city_list)[0]; // 通过键获取值 const firstValue = areaList?.city_list[firstKey]; currentValues.push({ code: firstKey, name: firstValue }) } if (areaList?.county_list) { // 获取第一个键值对 const firstKey = Object.keys(areaList?.county_list)[0]; // 通过键获取值 const firstValue = areaList?.county_list[firstKey]; currentValues.push({ code: firstKey, name: firstValue }) } this.setData({ areaList, currentValues }) } catch { // } }, formateArea(area: any[]) { const province_list: { [_: string]: string } = {}; const city_list: { [_: string]: string } = {}; const county_list: { [_: string]: string } = {}; area.forEach((item: any) => { province_list[item.code] = item.name; }); area.forEach((item: any) => { item.areas && item.areas.forEach((city: any) => { city_list[city.code] = city.name; }); }); area.forEach((item: any) => { item.areas && item.areas.forEach((city: any) => { city.areas && city.areas.forEach((county: any) => { county_list[county.code] = county.name; }); }); }); return { province_list, city_list, county_list }; }, /** 显示选择地区 */ async onShowAreaList() { this.setData({ showArea: true }) }, /** 关闭选择地区 */ onCloseAreaList() { this.setData({ showArea: false }) }, /** 确定选择地区 */ submitArea(e: any) { const selectedOptions: any = e.detail.values this.setData({ provinceCode: selectedOptions[0].code, cityCode: selectedOptions[1].code, regionCode: selectedOptions[2].code, provinceName: selectedOptions[0].name, cityName: selectedOptions[1].name, regionName: selectedOptions[2].name, showArea: false, }) }, onShowAddress() { this.setData({ addressAfterLeave: false, addressShow: true }) }, onCloseAddress() { this.setData({ addressShow: false }) }, onAddressAfterLeave() { this.setData({ addressAfterLeave: true, name: '', phoneNumber: '', detailAddress: '', cityCode: 0, cityName: "", provinceCode: 0, provinceName: "", regionCode: '', regionName: "", }) }, /** 创建/修改收货地址 */ async onOperationAddress() { const addressForm = this.data try { if (!addressForm.name) { wx.showToast({ title: '请输入收货人姓名', icon: "none" }) return } if (!addressForm.phoneNumber || !/^1[3456789]\d{9}$/.test(addressForm.phoneNumber)) { wx.showToast({ title: '请输入正确的手机号码', icon: "none" }) return } if (!addressForm.provinceCode || !addressForm.cityCode || !addressForm.regionCode) { wx.showToast({ title: '请选择地区', icon: "none" }) return } if (!addressForm.detailAddress) { wx.showToast({ title: '请输入详细地址', icon: "none" }) return } const params = { name: addressForm.name, phoneNumber: addressForm.phoneNumber, province: addressForm.provinceCode, city: addressForm.cityCode, region: addressForm.regionCode, detailAddress: addressForm.detailAddress } const { data } = await api_userReceiveAddressSave({ ...params }) wx.showToast({ title: '添加成功', icon: 'none' }) this.setData({ receiveAddress: data.data, // 选择的地址信息 receiveAddressInfo: { addressDetail: addressForm.provinceName + addressForm.cityName + addressForm.regionName + addressForm.detailAddress, name: addressForm.name, phoneNumber: addressForm.phoneNumber } }) this.onCloseAddress() } catch (e) { // console.log(e, '1212') } }, onExpanded() { this.setData({ isExpanded: !this.data.isExpanded }) }, 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/1733312164991.png' } } })