// 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_userReceiveAddressRemove, api_userReceiveAddressSave, api_userReceiveAddressUpdate } from "../../api/new"; import { formatPrice, GRADE_ENUM } from "../../utils/util"; // 获取应用实例 const app = getApp() Page({ /** * 页面的初始数据 */ data: { backParams: null, serviceShow: true, status: '', statusList: { ing: { logo: './images/ing.png', title: '确认订单', content: '请尽快完成支付,以便我们为您处理订单' }, }, 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, areaList: [] as any, currentValues: [] as any, showDialog: false, // 删除弹窗 selectAddressId: '', // 选中的编号 showArea: false, // 地区 addressShow: false, // 添加/修改收货地址 addressListShow: false, // 收货地址列表 addressList: [] as any, // 收货地址列表 // 添加地址表单信息 id: "", name: '', phoneNumber: '', detailAddress: '', cityCode: 0, cityName: "", provinceCode: 0, provinceName: "", regionCode: '', regionName: "", }, /** * 生命周期函数--监听页面加载 */ onLoad(options: any) { this.queryPayType() this.getAddresss() this.getAreas() if (options.orderInfo) { const goods = JSON.parse(decodeURIComponent(options.orderInfo)); // console.log(goods, 'goods') // console.log(options, 'options') const infos = { allSalePrice: 0, allOriginPrice: 0, allDiscountPrice: '', integerPart: '', decimalPart: '', name: '', shopId: '', orderNo: options.orderNo || '', goodsList: [] as any } // 是否有乐器 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) infos.goodsList.push({ ...item, ...afterPrice }) infos.allSalePrice += Number(item.salePrice) infos.allOriginPrice += Number(item.originalPrice) } const allAfterPrice: any = formatPrice(infos.allSalePrice) // console.log(infos.allOriginPrice, infos.allSalePrice) infos.allDiscountPrice = formatPrice(infos.allOriginPrice - infos.allSalePrice, 'ALL') as string infos.integerPart = allAfterPrice.integerPart infos.decimalPart = allAfterPrice.decimalPart // console.log(infos, 'infos') this.setData({ goodsInfo: infos, status: options.status || '', hasInstrument }); } this.getOrderDetail() }, onShow() { if (this.data.backParams) { // console.log(this.data.backParams, 'backParams'); // { key: 'value' } const backParams: any = this.data.backParams || {}; this.setData({ userBeneficiaryId: backParams.userBeneficiaryId, userBeneficiaryInfo: { name: backParams.name, phoneNumber: backParams.phone, schoolInfo: backParams.schoolInfo }, backParams: null // 清空参数 }) } this.setData({ serviceShow: true }) }, onHide() { this.setData({ serviceShow: false }) }, /** 获取订单详情 */ async getOrderDetail() { try { if (!this.data.goodsInfo.orderNo) return const { data } = await api_userPaymentOrderDetail(this.data.goodsInfo.orderNo, { version: 'V2' }); // console.log(data, 'data') 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('') }, }) } catch { // } }, /** 地址列表 */ async getAddresss() { try { const { data } = await api_userReceiveAddressPage({ page: 1, rows: -1 }) this.setData({ addressList: data.data.rows || [] }) } 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 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.receiveAddress && this.data.hasInstrument) { wx.showToast({ title: '请选择收货地址', icon: 'none' }) return } if (!this.data.userBeneficiaryId) { wx.showToast({ title: '请添加购买人', icon: 'none' }) return } wx.showLoading({ mask: true, title: "订单提交中...", }); try { // const { salePrice, shopId, name, id, orderNo } = this.data.goodsInfo 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.navigateBack() } 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.onPay(paymentType, res.data.data.reqParams, orderNo) } 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() const goodsInfo = that.data.goodsInfo goodsInfo.orderNo = orderNo that.setData({ goodsInfo }) } }) }, /** 客服 */ onService() { // console.log("showService") this.setData({ showService: true }) }, changePop(event: { detail: any }) { this.setData({ showService: event.detail }) }, /** 选择收货地址 */ onTagAddress() { if(this.data.goodsInfo.orderNo) return if (this.data.addressList.length > 0) { this.onShowAddressList() } else { this.onShowAddress() } }, /** 显示添加/修改地址 */ onShowAddress() { this.setData({ addressShow: true }) }, /** 关闭添加/修改地址 */ onCloseAddress() { this.setData({ addressShow: false, id: "", name: '', phoneNumber: '', detailAddress: '', cityCode: 0, cityName: "", provinceCode: 0, provinceName: "", regionCode: null, regionName: "", }) }, /** 显示收货地址 */ onShowAddressList() { this.setData({ addressListShow: true }) }, /** 关闭收货地址 */ onCloseAddressList() { this.setData({ addressListShow: false }) }, /** 添加购买人 */ onAddBuyer() { wx.navigateTo({ url: "../buyerInformation/index?userBeneficiaryId=" + this.data.userBeneficiaryId, }); }, /** 显示选择地区 */ onShowAreaList() { this.setData({ showArea: true }) }, /** 关闭选择地区 */ onCloseAreaList() { this.setData({ showArea: false }) }, /** 确定选择地区 */ submitArea() { const selectedOptions = this.data.currentValues 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 }) }, // cancelArea() { // this.setData({ showArea: false }) // }, // confirmArea(event: any) { // console.log(event) // // const selectedOptions = event.detail.values // const selectedOptions = this.data.currentValues // 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, // areaComponent: null as any // }) // // forms.provinceCode = selectedOptions[0].value; // // forms.cityCode = selectedOptions[1].value; // // forms.regionCode = selectedOptions[2].value; // // data.cityName = selectedOptions // // .map((item: any) => item.text) // // .join('-'); // // data.showArea = false; // // }} // }, changeArea(e: any) { console.log(e.detail.values, 'e.detail.values') this.setData({ currentValues: e.detail.values }) }, /** 创建/修改收货地址 */ 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 } if (addressForm.id) { await api_userReceiveAddressUpdate({ id: addressForm.id, name: addressForm.name, phoneNumber: addressForm.phoneNumber, province: addressForm.provinceCode, city: addressForm.cityCode, region: addressForm.regionCode, detailAddress: addressForm.detailAddress }) this.getAddresss() this.onCloseAddress() } else { const { data } = await api_userReceiveAddressSave({ name: addressForm.name, phoneNumber: addressForm.phoneNumber, province: addressForm.provinceCode, city: addressForm.cityCode, region: addressForm.regionCode, detailAddress: addressForm.detailAddress }) this.setData({ receiveAddress: this.data.receiveAddress || data.data, 'receiveAddressInfo.addressDetail': addressForm.provinceName + addressForm.cityName + addressForm.regionName + addressForm.detailAddress, 'receiveAddressInfo.name': addressForm.name, 'receiveAddressInfo.phoneNumber': addressForm.phoneNumber }) this.getAddresss() this.onCloseAddress() } } catch { // } }, /** 选择地址 */ onSelectAddress(e: any) { const id = e.currentTarget.dataset.id const addressInfo = this.data.addressList.find((item: any) => item.id === id) this.setData({ receiveAddress: addressInfo.id, 'receiveAddressInfo.addressDetail': addressInfo.provinceName + addressInfo.cityName + addressInfo.regionName + addressInfo.detailAddress, 'receiveAddressInfo.name': addressInfo.name, 'receiveAddressInfo.phoneNumber': addressInfo.phoneNumber, addressListShow: false }) }, /** Dialog 确定 */ async onDialogConfirm() { try { await api_userReceiveAddressRemove({ id: this.data.selectAddressId }) this.getAddresss() // 如果删除的是已经选中的地址,则需要重置数据 if (this.data.selectAddressId === this.data.receiveAddress) { this.setData({ selectAddressId: '', receiveAddress: '', 'receiveAddressInfo.name': '', 'receiveAddressInfo.phoneNumber': '', 'receiveAddressInfo.addressDetail': '' }) } this.onDialogClose() } catch { } }, /** Dialog 隐藏 */ onDialogClose() { this.setData({ showDialog: false }) }, /** 删除地址 */ onRemoveAddress(e: any) { this.setData({ showDialog: true, selectAddressId: e.target.dataset.id }) }, /** 修改地址 */ onUpdateAddress(e: any) { const id = e.target.dataset.id const addressInfo = this.data.addressList.find((item: any) => item.id === id) this.setData({ addressShow: true, id: addressInfo.id, name: addressInfo.name, phoneNumber: addressInfo.phoneNumber, detailAddress: addressInfo.detailAddress, cityCode: addressInfo.city, cityName: addressInfo.cityName, provinceCode: addressInfo.province, provinceName: addressInfo.provinceName, regionCode: addressInfo.region, regionName: addressInfo.regionName, }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, /** * 用户点击右上角分享 */ onShareAppMessage() { return { title: '音乐数字AI', path: '/pages/index/index', imageUrl: 'https://oss.dayaedu.com/ktyq/1733309357691.png' } } })