From 15a8c695e0be080b5bbc0b1a39955885811b11cf Mon Sep 17 00:00:00 2001 From: weiwei Date: Tue, 2 Sep 2025 20:05:27 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E6=89=AB=E6=94=B6=E6=AC=BE=E6=9C=BA?= =?UTF-8?q?=E5=99=A8=E4=BA=8C=E7=BB=B4=E7=A0=81=E6=94=AF=E4=BB=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/pages/pay/payComplete.vue | 10 +-- src/pages/pay/scanPay.vue | 156 ++++++++++++++++++++-------------- src/sheep/stores/pay.ts | 21 ++++- 3 files changed, 113 insertions(+), 74 deletions(-) diff --git a/src/pages/pay/payComplete.vue b/src/pages/pay/payComplete.vue index e961b9e..da5b076 100644 --- a/src/pages/pay/payComplete.vue +++ b/src/pages/pay/payComplete.vue @@ -24,13 +24,13 @@ - ¥{{ payData.customPrice }} + ¥{{ scanPayData.payPrice }} 商家 - {{ payData.name }} + {{ scanPayData.mchName }}
@@ -50,11 +50,11 @@ import { ref } from "vue" import { onShow } from "@dcloudio/uni-app" import { usePayStore } from "@/sheep/stores/pay" -const payData = ref<{ id?: string; name?: string; customPrice?: number }>({}) +const scanPayData = ref({}) onShow(() => { - if (!usePayStore().payData.id) return uni.showToast({ title: "支付信息不存在", icon: "none" }) - payData.value = usePayStore().payData + if (!usePayStore().scanPayData.mchId) return uni.showToast({ title: "支付信息不存在", icon: "none" }) + scanPayData.value = usePayStore().scanPayData }) const goBack = () => { diff --git a/src/pages/pay/scanPay.vue b/src/pages/pay/scanPay.vue index 781a58b..58091c0 100644 --- a/src/pages/pay/scanPay.vue +++ b/src/pages/pay/scanPay.vue @@ -1,7 +1,7 @@ @@ -39,79 +39,95 @@ import { ref, computed, reactive } from "vue" import { onLoad, onShow } from "@dcloudio/uni-app" import payApi from "@@/api/pay" -import { merchantApi, walletApi } from "@/sheep/api" +import { merchantApi, orderApi, walletApi } from "@/sheep/api" import { hooks } from "@@/index" import { router } from "@/sheep" import { filePath } from "@/sheep/utils/file" -import { storePrice } from "@/sheep/utils/formatMoney" +import { displayPrice, storePrice } from "@/sheep/utils/formatMoney" import { usePayStore } from "@/sheep/stores/pay" import { isEmpty } from "radash" -import numberKeyboard from "@/sheep/components/numberKeyboard.vue" +import NumberKeyboard from "@/sheep/components/numberKeyboard.vue" -const scanCodeData = reactive({ - id: "", - name: "", +const loading = ref(false) +const scanPayState = reactive({ + mchId: "", // 商户id + mchName: "", // 商户名 + orderId: "", // 订单id + action: "", }) -const walletList = ref([]) -const curWalletId = ref("") -const disabled = ref(false) -const amount = ref("") +const walletList = ref([]) // 钱包列表 +const curWalletId = ref("") // 当前选中的钱包id +const orderInfo = ref({}) // 订单信息 +const amount = ref("") // 键盘输入的金额 -onLoad((options: any) => { - if (!isEmpty(options.id)) { - scanCodeData.id = options.id - } else { - uni.showToast({ title: "参数不存在", icon: "none" }) +onLoad(async (options: any) => { + uni.showLoading({ title: "加载中..." }) + const { mchId, orderId, action } = options + if (action === "harQrCodeScanPay") { + if (!options.mchId || !options.orderId) return uni.showToast({ title: "扫码参数错误", icon: "none" }) + scanPayState.mchId = mchId + scanPayState.orderId = orderId + scanPayState.action = action + await getMchDetail(mchId) + await getOrderInfo() + amount.value = displayPrice(orderInfo.value.totalPrice || 0 + orderInfo.value.extraPrice || 0, "") + return } + + const { id } = options + if (!isEmpty(id)) { + scanPayState.mchId = id + await getMchDetail(id) + return + } + + return uni.showToast({ title: "扫码参数错误", icon: "none" }) }) onShow(async () => { - uni.showLoading({ title: "加载中...", mask: true }) - if (scanCodeData.id) { - const { data } = await merchantApi.detail({ id: scanCodeData.id }) - if (data) { - scanCodeData.name = data.name - disabled.value = false - } else { - disabled.value = true - return uni.showToast({ title: "商户id错误", icon: "none" }) - } - } else { - uni.showToast({ title: "参数不存在", icon: "none" }) - disabled.value = true + try { + await getWalletList() + } finally { + uni.hideLoading() } - - const res = await walletApi.all() - if (res.code == 0) { - walletList.value = res.data.records - if (walletList.value.length > 0) { - curWalletId.value = walletList.value.find((v: any) => v.walletTypeUniqueCode == "1").id - } - } - uni.hideLoading() }) +// 获取商户名 +const getMchDetail = async (mchId: string) => { + if (!mchId) return uni.showToast({ title: "商户id错误", icon: "none" }) + const { data } = await merchantApi.detail({ id: mchId }) + scanPayState.mchName = data.name +} + +// 获取钱包列表 +const getWalletList = async () => { + const { code, data } = await walletApi.all() + if (code !== 0) return uni.showToast({ title: "获取钱包列表失败", icon: "none" }) + walletList.value = data.records + if (walletList.value.length > 0) { + curWalletId.value = walletList.value.find((v: any) => v.walletTypeUniqueCode == "1").id + } +} + +// 获取订单信息 +const getOrderInfo = async () => { + const { code, data }: any = await orderApi.detail({ id: scanPayState.orderId }) + if (code !== 0) return uni.showToast({ title: "获取订单信息失败", icon: "none" }) + orderInfo.value = data +} + +// 格式化金额显示 const displayAmount = computed(() => { if (!amount.value) return "0" - return ( - amount.value - // .toString() - // .toFixed(2) - .replace(/\B(?=(\d{3})+(?!\d))/g, ",") - ) + return amount.value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") }) // 点击支付 const handlePay = () => { - if (!scanCodeData.id || !scanCodeData.name) return uni.showToast({ title: "扫码参数不存在", icon: "none" }) - if (!amount.value) { - uni.showToast({ title: "请输入金额", icon: "none" }) - return - } - if (Number(amount.value) === 0) { - uni.showToast({ title: "金额不能为0", icon: "none" }) - return - } + if (!scanPayState.mchId) return uni.showToast({ title: "扫码参数不存在", icon: "none" }) + if (!amount.value) return uni.showToast({ title: "请输入金额", icon: "none" }) + if (Number(amount.value) === 0) return uni.showToast({ title: "金额不能为0", icon: "none" }) + uni.showModal({ title: "确定支付吗?", success: async (res) => { @@ -126,18 +142,28 @@ const handlePay = () => { const pay = async () => { try { uni.showLoading({ title: "支付中...", mask: true }) - const res = await payApi.createQrCodeOrder({ - merchantId: scanCodeData.id, - customPrice: storePrice(Number(amount.value)), - walletId: curWalletId.value, - }) - if (res.code != 0) { - uni.showToast({ title: res.msg || "支付失败", icon: "error" }) + if (scanPayState.action === "harQrCodeScanPay") { + const { code, msg }: any = await orderApi.doneHardQrCodeScanOrder({ + orderId: scanPayState.orderId, + walletId: curWalletId.value, + }) + if (code != 0) return uni.showToast({ title: msg || "支付失败", icon: "error" }) } else { - uni.hideLoading() - usePayStore().setPayData({ id: scanCodeData.id, name: scanCodeData.name, customPrice: Number(amount.value).toFixed(2) }) - router.jump("/pages/pay/payComplete", {}, 2) + const { code, msg } = await payApi.createQrCodeOrder({ + merchantId: scanPayState.mchId, + customPrice: storePrice(Number(amount.value)), + walletId: curWalletId.value, + }) + if (code != 0) return uni.showToast({ title: msg || "支付失败", icon: "error" }) } + + uni.hideLoading() + usePayStore().setScanPayData({ + mchId: scanPayState.mchId, + mchName: scanPayState.mchName, + payPrice: Number(amount.value).toFixed(2), + }) + router.jump("/pages/pay/payComplete", {}, 2) } catch (error: any) { uni.showToast({ title: "请求错误", icon: "error" }) uni.hideLoading() diff --git a/src/sheep/stores/pay.ts b/src/sheep/stores/pay.ts index e82da98..317fc7d 100644 --- a/src/sheep/stores/pay.ts +++ b/src/sheep/stores/pay.ts @@ -1,16 +1,29 @@ import { defineStore } from "pinia" +type State = { + scanPayData: { + mchId: string + mchName: string + payPrice: number | string + } + sourceWalletData: any +} + export const usePayStore = defineStore("pay", { - state: (): { payData: { id?: string; name?: string; customPrice?: number }; sourceWalletData: any } => { + state: (): State => { return { - payData: {}, + scanPayData: { + mchId: "", + mchName: "", + payPrice: 0, + }, sourceWalletData: {}, } }, actions: { // 支付 - setPayData(data: any) { - this.payData = data + setScanPayData(data: any) { + this.scanPayData = data }, // 转账 setSourceWalletData(data: any) {