fix: 修复抖音支付双端UTS编译兼容

This commit is contained in:
Codex
2026-09-10 10:57:53 +08:00
parent 3e2e1829a8
commit e692f3d9b1
6 changed files with 180 additions and 9 deletions
+20 -7
View File
@@ -1,4 +1,4 @@
import { PayOptions, PayCallback, PayResult } from './interface.uts'
import { PayInfo, PayOptions, PayCallback, PayResult } from './interface.uts'
let appId = ''
let busy = false
@@ -24,12 +24,25 @@ export function invoke(options: PayOptions, callback: PayCallback,
return
}
let valid = false
let payload: string = ''
try {
const info = options.payInfo
const values = [info.appid, info.partnerid, info.prepayid, info.package, info.noncestr, info.timestamp, info.sign]
valid = appId.length > 0 && info.appid == appId && info.package == 'Sign=DYPay'
values.forEach((value: string) => { if (value.trim().length == 0) valid = false })
if (!/^[0-9]{1,10}$/.test(info.timestamp) || info.noncestr.length > 32) valid = false
// Swift infers an optional from the generated PayInfo! property unless
// the local type is explicit. Check before narrowing on both platforms.
const candidate: PayInfo | null = options.payInfo
if (candidate != null) {
const info: PayInfo = candidate as PayInfo
const values = new Array<string>(info.appid ?? '', info.partnerid ?? '', info.prepayid ?? '',
info.package ?? '', info.noncestr ?? '', info.timestamp ?? '', info.sign ?? '')
valid = appId.length > 0 && values[0] == appId && values[3] == 'Sign=DYPay'
values.forEach((value: string) => { if (value.trim().length == 0) valid = false })
if (!/^[0-9]{1,10}$/.test(values[5]) || values[4].length > 32) valid = false
// UTS JSON.stringify returns string | null. Serialize before taking the
// payment lock, and never pass a nullable value to a native String API.
if (valid) {
payload = JSON.stringify(info) ?? ''
if (payload.length == 0) valid = false
}
}
} catch (e) { valid = false }
if (!valid) {
const result: PayResult = { resultCode: '-1', errorMsg: '支付参数不完整或 AppID 不匹配,请重新下单', needsQuery: false }
@@ -61,7 +74,7 @@ export function invoke(options: PayOptions, callback: PayCallback,
callback(result)
}
try {
nativePay(JSON.stringify(options.payInfo), options.showLoading ?? true, complete)
nativePay(payload, options.showLoading ?? true, complete)
} catch (e) {
complete('{"resultCode":"2","errorMsg":"无法调用支付 SDK,请查询订单后重试"}')
}