123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602 |
- // 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<IAppOption>()
- 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('')
- },
- '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'
- }
- }
- })
|