添加首页

This commit is contained in:
zhouyanli
2026-08-15 17:57:18 +08:00
parent 491c3a320a
commit 1ab7b22221
6 changed files with 275 additions and 56 deletions

View File

@@ -11,7 +11,12 @@ const STORAGE_KEY = 'app_launch_payment_data'
const APP_QUERY_KEY = 'app_query_params'
/**
* 解析查询字符串为对象
* 需要转为数组的参数字段(逗号分隔字符串 → 数组)
*/
const ARRAY_PARAMS = ['billIds']
/**
* 解析查询字符串为对象,数组字段自动拆分
*/
function parseQueryString(str) {
if (!str || typeof str !== 'string') return null
@@ -21,11 +26,47 @@ function parseQueryString(str) {
if (idx === -1) return
const key = item.substring(0, idx)
const val = item.substring(idx + 1)
params[key] = decodeURIComponent(val || '')
const decoded = decodeURIComponent(val || '')
// 同名参数合并为数组;已知数组字段按逗号拆分
if (key in params) {
if (Array.isArray(params[key])) {
params[key].push(decoded)
} else {
params[key] = [params[key], decoded]
}
} else {
params[key] = ARRAY_PARAMS.includes(key) && decoded.includes(',')
? decoded.split(',').map(v => v.trim()).filter(Boolean)
: decoded
}
})
return params
}
/**
* 归一化 billId/billIds:单个 id 用 billId,多个用 billIds,统一转为 billIds 数组
*/
function normalizeBillParams(obj) {
if (!obj) return
// 先解码再拆分为数组
if (typeof obj.billIds === 'string') {
const decoded = decodeURIComponent(obj.billIds)
obj.billIds = decoded.includes(',')
? decoded.split(',').map(v => v.trim()).filter(Boolean)
: [decoded]
}
// billId 存在且 billIds 不存在时,包装为数组
if (obj.billId && !obj.billIds) {
const decoded = decodeURIComponent(
typeof obj.billId === 'string' ? obj.billId : String(obj.billId)
)
obj.billIds = decoded.includes(',')
? decoded.split(',').map(v => v.trim()).filter(Boolean)
: [decoded]
delete obj.billId
}
}
/**
* 从启动参数中获取 App 传入的全部数据(支付参数 + token + userId)
*/
@@ -71,7 +112,10 @@ export function captureAppLaunchData(onShowOptions) {
data = tryGetData(onShowOptions, '热启动')
}
if (data && (data.payAmount || data.acct || data.custId || data.billId)) {
if (data && (data.payAmount || data.acct || data.custId || data.billId || data.billIds)) {
// 归一化 billId/billIds:单个 id 用 billId,多个用 billIds,统一转为 billIds 数组
normalizeBillParams(data)
// 恢复登录态
if (data.token) {
uni.setStorageSync('token', data.token)
@@ -86,6 +130,9 @@ export function captureAppLaunchData(onShowOptions) {
console.log('已存储 App 支付数据:', JSON.stringify(data))
}
// 归一化 queryData 中的 billId/billIds
normalizeBillParams(queryData)
// 单独保存参数供接口使用(优先 queryData,fallback 到 data,即 extraData 来源)
const queryParams = queryData || data
if (queryParams) {