123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398 |
- // 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: ''
- },
- 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'
- }
- }
- })
|