修改参数缓存bug
This commit is contained in:
@@ -9,6 +9,10 @@
|
||||
|
||||
const STORAGE_KEY = 'app_launch_payment_data'
|
||||
const APP_QUERY_KEY = 'app_query_params'
|
||||
/** 页面缓存的缴费条目(arrearsPayment 页展示用) */
|
||||
const PAYMENT_ITEM_KEY = 'arrearsPayment'
|
||||
/** 日照银行支付小程序返回的原始结果 */
|
||||
const PAYMENT_RESULT_KEY = 'payment_return_result'
|
||||
|
||||
/**
|
||||
* 需要转为数组的参数字段(逗号分隔字符串 → 数组)
|
||||
@@ -73,6 +77,8 @@ function normalizeBillParams(obj) {
|
||||
export function captureAppLaunchData(onShowOptions) {
|
||||
let data = null
|
||||
let queryData = null
|
||||
// 参数来源:current=本次进入 / enter=当前进入 / launch=冷启动兜底(热启动时仍是首次冷启动的旧值)
|
||||
let dataSource = null
|
||||
|
||||
const tryGetData = (options, label) => {
|
||||
if (!options) return null
|
||||
@@ -106,6 +112,7 @@ export function captureAppLaunchData(onShowOptions) {
|
||||
// 热启动再次从 App 进入时,getLaunchOptionsSync 仍返回首次冷启动的旧值,
|
||||
// 若先读它会导致新 token 被旧 token 覆盖,因此这里以本次 options 为准。
|
||||
data = tryGetData(onShowOptions, '本次进入')
|
||||
if (data) dataSource = 'current'
|
||||
|
||||
// 兜底:本次 options 未取到数据时,依次尝试当前进入参数、冷启动参数。
|
||||
// 注意:不能以 !onShowOptions 判断,onLaunch/onShow 始终会传入 options 对象,
|
||||
@@ -113,14 +120,20 @@ export function captureAppLaunchData(onShowOptions) {
|
||||
if (!data) {
|
||||
try {
|
||||
data = tryGetData(wx.getEnterOptionsSync?.() || {}, '当前进入(getEnterOptionsSync)')
|
||||
if (data) dataSource = 'enter'
|
||||
} catch (e) { console.error('getEnterOptionsSync 失败:', e) }
|
||||
}
|
||||
if (!data) {
|
||||
try {
|
||||
data = tryGetData(wx.getLaunchOptionsSync?.() || {}, '冷启动(getLaunchOptionsSync)')
|
||||
if (data) dataSource = 'launch'
|
||||
} catch (e) { console.error('getLaunchOptionsSync 失败:', e) }
|
||||
}
|
||||
|
||||
// getLaunchOptionsSync 返回的是首次冷启动的参数,热启动再次从 App 进入时仍是旧值。
|
||||
// 用它恢复缴费参数会把上一次的金额/billIds 覆盖进来,因此只允许它恢复登录态。
|
||||
const isStaleSource = dataSource === 'launch'
|
||||
|
||||
if (data) {
|
||||
// 恢复登录态:只要 App 传入了 token/userId 就恢复,不依赖支付字段名
|
||||
if (data.token) {
|
||||
@@ -133,7 +146,10 @@ export function captureAppLaunchData(onShowOptions) {
|
||||
}
|
||||
|
||||
// 存在支付参数时才作为 App 支付进入处理
|
||||
if (data.payAmount || data.acct || data.custId || data.billId || data.billIds) {
|
||||
const hasPaymentParams = !!(data.payAmount || data.acct || data.custId || data.billId || data.billIds)
|
||||
if (hasPaymentParams && isStaleSource) {
|
||||
console.log('冷启动兜底参数为旧值,跳过缴费参数恢复')
|
||||
} else if (hasPaymentParams) {
|
||||
// 归一化 billId/billIds:单个 id 用 billId,多个用 billIds,统一转为 billIds 数组
|
||||
normalizeBillParams(data)
|
||||
uni.setStorageSync(STORAGE_KEY, data)
|
||||
@@ -144,8 +160,9 @@ export function captureAppLaunchData(onShowOptions) {
|
||||
// 归一化 queryData 中的 billId/billIds
|
||||
normalizeBillParams(queryData)
|
||||
|
||||
// 单独保存参数供接口使用(优先 queryData,fallback 到 data,即 extraData 来源)
|
||||
const queryParams = queryData || data
|
||||
// 单独保存参数供接口使用(优先 queryData,fallback 到 data,即 extraData 来源)。
|
||||
// 冷启动兜底来源不写入,避免拿着上一次的账单去支付。
|
||||
const queryParams = isStaleSource ? null : (queryData || data)
|
||||
if (queryParams) {
|
||||
uni.setStorageSync(APP_QUERY_KEY, queryParams)
|
||||
console.log('已存储 App 接口参数:', JSON.stringify(queryParams))
|
||||
@@ -179,6 +196,23 @@ export function clearAppQueryParams() {
|
||||
uni.removeStorageSync(APP_QUERY_KEY)
|
||||
}
|
||||
|
||||
/**
|
||||
* 清理全部缴费缓存(App 传入参数 / 接口参数 / 页面条目 / 支付返回结果)
|
||||
* 用于返回 App 前、以及再次从 App 进入时重置上一次的数据,
|
||||
* 避免小程序热启动 onLoad 不执行导致继续展示上一次的金额。
|
||||
*/
|
||||
export function clearPaymentCache() {
|
||||
uni.removeStorageSync(STORAGE_KEY)
|
||||
uni.removeStorageSync(APP_QUERY_KEY)
|
||||
uni.removeStorageSync(PAYMENT_RESULT_KEY)
|
||||
uni.removeStorageSync(PAYMENT_ITEM_KEY)
|
||||
}
|
||||
|
||||
/** 清除页面缓存的缴费条目 */
|
||||
export function clearPaymentItem() {
|
||||
uni.removeStorageSync(PAYMENT_ITEM_KEY)
|
||||
}
|
||||
|
||||
/** 组装 paymentItem */
|
||||
export function buildPaymentItemFromApp(appData) {
|
||||
if (!appData) return null
|
||||
@@ -193,22 +227,31 @@ export function buildPaymentItemFromApp(appData) {
|
||||
* 本小程序在 App.onShow 中通过 referrerInfo.extraData 接收(场景值 1038)。
|
||||
* 这里只负责捕获/读取/清除原始返回数据,成功/失败判定见 resolvePaymentResult。
|
||||
*/
|
||||
const PAYMENT_RESULT_KEY = 'payment_return_result'
|
||||
|
||||
/**
|
||||
* 判断 extraData 是否为 App 跳转参数(而非支付小程序返回)。
|
||||
* App 跳转必然带登录凭证或缴费参数,支付返回只带支付结果字段。
|
||||
*/
|
||||
function isAppLaunchExtraData(extraData) {
|
||||
if (!extraData || typeof extraData !== 'object') return false
|
||||
return !!(extraData.token || extraData.userId || extraData.payAmount || extraData.acct ||
|
||||
extraData.custId || extraData.billId || extraData.billIds)
|
||||
}
|
||||
|
||||
/**
|
||||
* 捕获支付返回结果(在 App.onShow 中调用)
|
||||
* 返回 true 表示本次进入是支付返回,调用方(App.vue)据此跳过 App 跳转参数捕获 ——
|
||||
* 否则兜底参数会把上一次的账单写回缓存,缴费页会误判为「再次从 App 进入」并重置成待缴费状态。
|
||||
* 注意:App 冷/热启动进入时 extraData 是支付参数(payAmount/billIds 等),
|
||||
* 并非支付返回,需跳过,避免覆盖真实结果。
|
||||
*/
|
||||
export function capturePaymentResult(onShowOptions) {
|
||||
if (!onShowOptions) return
|
||||
if (!onShowOptions) return false
|
||||
const extraData = onShowOptions?.referrerInfo?.extraData
|
||||
if (!extraData) return
|
||||
if (!extraData) return false
|
||||
|
||||
// 含支付参数字段说明是 App 跳转进入(冷/热启动),不是支付返回,跳过
|
||||
if (typeof extraData === 'object' && (extraData.payAmount || extraData.billId || extraData.billIds || extraData.acct || extraData.custId)) {
|
||||
return
|
||||
}
|
||||
// 含登录凭证/缴费参数字段说明是 App 跳转进入(冷/热启动),不是支付返回
|
||||
if (isAppLaunchExtraData(extraData)) return false
|
||||
|
||||
let parsed = extraData
|
||||
if (typeof extraData === 'string') {
|
||||
@@ -218,8 +261,11 @@ export function capturePaymentResult(onShowOptions) {
|
||||
parsed = parseQueryString(extraData)
|
||||
}
|
||||
}
|
||||
if (isAppLaunchExtraData(parsed)) return false
|
||||
|
||||
console.log('捕获到支付返回结果:', JSON.stringify(parsed))
|
||||
uni.setStorageSync(PAYMENT_RESULT_KEY, parsed)
|
||||
return true
|
||||
}
|
||||
|
||||
/** 读取支付返回结果(读取后建议调用 clearPaymentReturnResult 清除) */
|
||||
|
||||
Reference in New Issue
Block a user