diff --git a/src/pages/pay/scanPay.vue b/src/pages/pay/scanPay.vue index ba4ea70..5ef0c9b 100644 --- a/src/pages/pay/scanPay.vue +++ b/src/pages/pay/scanPay.vue @@ -7,7 +7,9 @@ ¥ {{ displayAmount }} - {{ scanData }} + + 扫码信息:{{ JSON.stringify(scanData) }} + 支付 @@ -75,6 +77,9 @@ import { ref, computed } from "vue" import { onShow } from "@dcloudio/uni-app" import { usePayStore } from "@/sheep/stores/pay" +import payApi from "@@/api/pay" +import { storePrice } from "@/sheep/utils/formatMoney" +import { router } from "@/sheep" const amount = ref("") const keyboardLayout = ref([ @@ -82,12 +87,12 @@ const keyboardLayout = ref([ ["4", "5", "6"], ["7", "8", "9"], ]) -const scanData = ref("") +const scanData = ref<{ id?: string; name?: string }>({}) const disabled = ref(false) onShow(() => { scanData.value = usePayStore().payData - if (scanData.value) { + if (scanData.value.id && scanData.value.name) { disabled.value = false } else { uni.showToast({ title: "扫码信息不存在", icon: "none" }) @@ -124,7 +129,7 @@ const handleDelete = () => { } // 点击支付 -const handlePay = () => { +const handlePay = async () => { if (!amount.value) { uni.showToast({ title: "请输入金额", icon: "none" }) return @@ -133,7 +138,23 @@ const handlePay = () => { uni.showToast({ title: "金额不能为0", icon: "none" }) return } - uni.showToast({ title: "支付中。。。", icon: "none" }) - usePayStore().payData = "" + try { + uni.showLoading({ title: "支付中...", mask: true }) + const res = await payApi.createQrCodeOrder({ + id: scanData.value.id, + customPrice: storePrice(Number(amount.value)), + }) + if (res.code === 0) { + uni.showToast({ title: "支付成功", icon: "none" }) + usePayStore().payData = {} + router.back() + } else { + uni.showToast({ title: "支付失败", icon: "none" }) + } + } catch (error: any) { + uni.showToast({ title: "错误", icon: "none" }) + } finally { + uni.hideLoading() + } } diff --git a/src/pages/tabbar/my.vue b/src/pages/tabbar/my.vue index b84f684..300ddd1 100644 --- a/src/pages/tabbar/my.vue +++ b/src/pages/tabbar/my.vue @@ -177,11 +177,28 @@ const doLogout = async () => { // 扫一扫 const doScanCode = async () => { + usePayStore().setPayData({}) try { - const res: any = await (window as any).dd.scan({ type: "all", sourceType: ["camera"], scanType: ["cameraScan"] }) - console.log(res) - usePayStore().setPayData(res.text) - router.jump("/pages/pay/scanPay") + // const res: any = await (window as any).dd.scan({ type: "all", sourceType: ["camera"], scanType: ["cameraScan"] }) + const decodedString = decodeBase64("eyJpZCI6MTg4OTQ4MzkzODQ4NDUzMTIwMSwibmFtZSI6Iua1i+ivlei2heW4giJ9") + // const decodedString = decodeBase64(res.text) + if (decodedString) { + const decodedJson = JSON.parse(decodedString) + if (decodedJson?.id && decodedJson?.name) { + usePayStore().setPayData(decodedJson) + router.jump("/pages/pay/scanPay") + } else { + uni.showToast({ + title: "无效的二维码", + icon: "none", + }) + } + } else { + uni.showToast({ + title: "无效的二维码", + icon: "none", + }) + } } catch (error: any) { if (error) { uni.showToast({ @@ -207,6 +224,27 @@ const onTabMenu = async (v: MenuItem, i: number) => { } } +// 解码base64 +const decodeBase64 = (base64String: string) => { + try { + const decodedBinaryString = atob(base64String) + const uint8Array = new Uint8Array(decodedBinaryString.length) + for (let i = 0; i < decodedBinaryString.length; i++) { + uint8Array[i] = decodedBinaryString.charCodeAt(i) + } + const decoder = new TextDecoder("utf-8") + const decodedString = decoder.decode(uint8Array) + console.log("解码结果", decodedString) + return decodedString + } catch (error) { + console.error("Base64 解码失败:", error) + uni.showToast({ + title: "解码失败,无效的二维码", + icon: "none", + }) + } +} + const getStorageUserInfo = () => { const tokenKey = constant.storageConstant.TOKEN const userInfoKey = constant.storageConstant.USERINFO diff --git a/src/sheep/api/pay.ts b/src/sheep/api/pay.ts index 3174fa2..feb10bf 100644 --- a/src/sheep/api/pay.ts +++ b/src/sheep/api/pay.ts @@ -1,53 +1,64 @@ -import request from "@/sheep/utils/request"; +import request from "@/sheep/utils/request" export default { - // 获得支付订单 - getOrder: (params) => { - return request({ - url: "/pay/order/get", - method: "GET", - params, - }); - }, - // 提交支付订单 - submitOrder: (data) => { - return request({ - url: "/pay/order/submit", - method: "POST", - data, - }); - }, - // 创建硬件订单 - createHardwareOrder: (params) => { - return request({ - url: "/hard/hard-order/app/create-order", - method: "GET", - params, - custom: { - showLoading: false, - }, - }); - }, - // 结算 - submitHardwareOrder: (data) => { - return request({ - url: "/hardware/order/submit-order", - method: "POST", - data, - custom: { - showLoading: false, - }, - }); - }, - // 退款 - refundHardwareOrder: (params) => { - return request({ - url: "/hardware/order/refund", - method: "GET", - params, - custom: { - showLoading: false, - }, - }); - }, -}; + // 获得支付订单 + getOrder: (params) => { + return request({ + url: "/pay/order/get", + method: "GET", + params, + }) + }, + // 提交支付订单 + submitOrder: (data) => { + return request({ + url: "/pay/order/submit", + method: "POST", + data, + }) + }, + // 创建硬件订单 + createHardwareOrder: (params) => { + return request({ + url: "/hard/hard-order/app/create-order", + method: "GET", + params, + custom: { + showLoading: false, + }, + }) + }, + // 创建二维码订单 + createQrCodeOrder: (data): any => { + return request({ + url: "/hard/hard-order/app/create-order-scan-qrcode", + method: "POST", + data, + custom: { + showLoading: false, + }, + }) + }, + // 结算 + submitHardwareOrder: (data) => { + return request({ + url: "/hardware/order/submit-order", + method: "POST", + data, + custom: { + showLoading: false, + }, + }) + }, + // 退款 + refundHardwareOrder: (params) => { + return request({ + url: "/hardware/order/refund", + method: "GET", + params, + custom: { + showLoading: false, + }, + }) + }, +} diff --git a/src/sheep/stores/pay.ts b/src/sheep/stores/pay.ts index a646ecd..c644023 100644 --- a/src/sheep/stores/pay.ts +++ b/src/sheep/stores/pay.ts @@ -1,9 +1,9 @@ import { defineStore } from "pinia" export const usePayStore = defineStore("pay", { - state: () => { + state: (): { payData: { id?: string; name?: string } } => { return { - payData: "", + payData: {}, } }, actions: { diff --git a/src/sheep/utils/formatMoney.ts b/src/sheep/utils/formatMoney.ts new file mode 100644 index 0000000..4615c6d --- /dev/null +++ b/src/sheep/utils/formatMoney.ts @@ -0,0 +1,21 @@ +/** + * 显示价格把分转换到元 + * @param price + * @returns + */ +export function displayPrice(price: any, suffix: string = "元", fixed = 2) { + if (typeof price === "undefined") return "0.00" + suffix + const parsedNumber = typeof price === "string" ? parseFloat(price) : price + return (parsedNumber / 100.0).toFixed(fixed) + suffix +} + +/** + * 把显示价格改为分 + * @param price + * @returns + */ +export const storePrice = (num: number | string | undefined) => { + if (typeof num === "undefined") return 0 + const parsedNumber = typeof num === "string" ? parseFloat(num) : num + return Math.round(parsedNumber * 100) +}