101 lines
3.2 KiB
TypeScript
101 lines
3.2 KiB
TypeScript
import { router } from ".."
|
|
import { getUrlParamsObj, isDevEnv } from "../utils/app"
|
|
|
|
const normalizeScanResult = (scanResult: string) => scanResult.replace("https://stgl.pj.gov.cn", "https://www.pjzhhq.cn")
|
|
|
|
export function useScanCode(actModalShow) {
|
|
async function scanCodeAction(): Promise<string | undefined> {
|
|
if (isDevEnv()) {
|
|
const testQrCode = "https://stgl.pj.gov.cn/h5/#/pages/pay/scanPay?id=1931916121241079810"
|
|
uni.showModal({ content: "开发环境不是浙政钉和诗画环境,扫码仅供测试" })
|
|
return normalizeScanResult(testQrCode)
|
|
}
|
|
const scanResult = await new Promise<string>((resolve, reject) => {
|
|
uni.scanCode({
|
|
success: (res) => {
|
|
console.log("uniapp扫码成功:", JSON.stringify(res))
|
|
resolve(res.result || "")
|
|
},
|
|
fail: (err) => {
|
|
console.error("uniapp扫码失败", JSON.stringify(err))
|
|
resolve("")
|
|
},
|
|
})
|
|
})
|
|
return normalizeScanResult(scanResult || "")
|
|
}
|
|
|
|
// 处理url扫码结果
|
|
function processUrlScanResult(url: string) {
|
|
if (!url) return
|
|
const searchParams = getUrlParamsObj(url)
|
|
const { hardDoorId, cabinetNo, action, id, name } = searchParams
|
|
// 智能柜扫码
|
|
if (hardDoorId && cabinetNo) {
|
|
router.jump("/pages/cabinet/buy", { hardDoorId, cabinetNo })
|
|
return
|
|
}
|
|
// 扫收款机上生成的码支付
|
|
if (action === "harQrCodeScanPay") {
|
|
const { mchId, orderId } = searchParams
|
|
router.jump("/pages/pay/scanPay", { action, mchId, orderId })
|
|
return
|
|
}
|
|
// 扫商户url收款码
|
|
if (id) {
|
|
router.jump("/pages/pay/scanPay", { id, name })
|
|
return
|
|
}
|
|
uni.showToast({ title: "无效的二维码", icon: "none" })
|
|
}
|
|
|
|
// 处理base64扫码结果
|
|
function processBase64ScanResult(scanResult: string) {
|
|
const decodedString = decodeBase64(scanResult)
|
|
if (!decodedString) return
|
|
try {
|
|
const decodedJson = JSON.parse(decodedString)
|
|
// 商户活动优惠券扫码核销
|
|
if (decodedJson.cancelId) {
|
|
actModalShow({ cancelId: decodedJson.cancelId })
|
|
return
|
|
}
|
|
// 商户扫码支付
|
|
if (decodedJson.id) {
|
|
router.jump("/pages/pay/scanPay", { id: decodedJson.id })
|
|
return
|
|
}
|
|
// 智能柜扫码开门
|
|
if (decodedJson.hardDoorId && decodedJson.cabinetNo) {
|
|
router.jump("/pages/cabinet/buy", { hardDoorId: decodedJson.hardDoorId, cabinetNo: decodedJson.cabinetNo })
|
|
return
|
|
}
|
|
uni.showToast({ title: "无效的二维码", icon: "none" })
|
|
} catch (error) {
|
|
uni.showToast({ title: "无效的二维码内容", icon: "none" })
|
|
}
|
|
}
|
|
|
|
// 解码base64
|
|
function decodeBase64(base64String: string): string | undefined {
|
|
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" })
|
|
}
|
|
}
|
|
|
|
return { scanCodeAction, processUrlScanResult, processBase64ScanResult }
|
|
}
|