扫二维码码支付

This commit is contained in:
weiwei committed 2025-03-21 11:42:16 +08:00
1 parent 8dc692796c
commit 8bff006f5a
5 files changed
+154 -63

No files matched your search

+27 -6
View File
@@ -7,7 +7,9 @@
<span class="text-2xl text-[#333] mr-1 line-height-normal">¥</span>
<span class="text-3xl font-medium text-[#333]">{{ displayAmount }}</span>
</tm-sheet>
<view>{{ scanData }}</view>
<view>扫码信息:{{ JSON.stringify(scanData) }}</view>
<tm-button class="mt-40" :block="true" @click="disabled ? '' : handlePay()" :disabled="disabled">支付</tm-button>
<view class="bg-[#f5f5f5] px-1 py-2 rounded-4 absolute bottom-0 w-full">
@@ -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()
}
}
</script>
+42 -4
View File
@@ -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
+62 -51
View File
@@ -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,
},
})
},
}
+2 -2
View File
@@ -1,9 +1,9 @@
import { defineStore } from "pinia"
export const usePayStore = defineStore("pay", {
state: () => {
state: (): { payData: { id?: string; name?: string } } => {
return {
payData: "",
payData: {},
}
},
actions: {
+21
View File
@@ -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)
}