106 lines
3.9 KiB
TypeScript
106 lines
3.9 KiB
TypeScript
import { router } from ".."
|
|
import { APPENV } from "../constant"
|
|
import { getAppInfo, isDevEnv } from "../utils/app"
|
|
|
|
export function useScanCode() {
|
|
async function scanCodeAction(): Promise<string | undefined> {
|
|
const appInfo = await getAppInfo()
|
|
|
|
if (isDevEnv()) {
|
|
// 开发环境模拟扫码
|
|
// const testQrCode = "eyJpZCI6IjE5MDU4ODg4NjQ0NTQ1MzMxMjEiLCJuYW1lIjoi6ICB5Y6/5bqc6aOf5aCCIn0=" // 测试线上-老县府食堂-收款二维码
|
|
// const testQrCode = "eyJoYXJkRG9vcklkIjoiMTkxNDIxNTYwODQzNzQxMTg0MSIsImNhYmluZXRObyI6IjEifQ==" // 测试线上-智能柜-收款二维码
|
|
// const testQrCode = "eyJpZCI6IjE5MDU4MTY3NTEwNDM0NTI5MzAiLCJuYW1lIjoi5paw5Y6/5bqc6LaF5biCIn0=" // 邓本地-测试环境-新县府超市收款二维码
|
|
// const testQrCode = "eyJoYXJkRG9vcklkIjoiMTkxMzE1NzA4MDQxODI5NTgwOSIsImNhYmluZXRObyI6IjEifQ==" // 邓本地-测试单门智能柜柜门二维码
|
|
const testQrCode = "http://192.168.101.45:8889/h5/#/pages/pay/scanPay?id=1905816751043452930&name=测试新县府超市" // 邓本地-测试环境-新县府超市收款二维码
|
|
// const testQrCode = "http://192.168.101.45:8889/h5/#/pages/pay/scanPay?hardDoorId=1913157080418295809&cabinetNo=1" // 测试智能柜柜门二维码
|
|
uni.showModal({ content: "开发环境不是浙政钉和诗画环境,扫码仅供测试" })
|
|
return Promise.resolve(testQrCode)
|
|
}
|
|
|
|
if (appInfo == APPENV.ZZD) {
|
|
// 浙政钉扫码
|
|
const res: any = await (window as any).dd.scan({ type: "all" })
|
|
return res.text || ""
|
|
}
|
|
|
|
if (appInfo == APPENV.SH) {
|
|
// 诗画扫码
|
|
const res = await (window as any).CP2.showScanQr({ needResult: 1 })
|
|
if (res.code == 1) return res.data.qrCode || ""
|
|
return ""
|
|
}
|
|
uni.showToast({ title: "当前环境未知", icon: "none" })
|
|
}
|
|
|
|
// 处理url扫码结果
|
|
function processUrlScanResult(url: string) {
|
|
if (!url) return
|
|
const searchParams = new URL(url).searchParams
|
|
const paramsObj: Record<string, string> = {}
|
|
searchParams.forEach((value, key) => (paramsObj[key] = value))
|
|
const { hardDoorId, cabinetNo, action, id, name } = paramsObj
|
|
|
|
// 智能柜扫码
|
|
if (hardDoorId && cabinetNo) {
|
|
router.jump("/pages/cabinet/buy", { hardDoorId, cabinetNo })
|
|
return
|
|
}
|
|
// 扫收款机上生成的码支付
|
|
if (action === "harQrCodeScanPay") {
|
|
const { mchId, orderId } = paramsObj
|
|
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.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 }
|
|
}
|