79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
/**
|
||
* App 跳转参数处理
|
||
* 花开物业 App 通过 path query string 将 token、支付参数传入本中间小程序。
|
||
*
|
||
* App 传入示例:
|
||
* pageSubPack/payment-list/arrearsPayment?token=xxx&userId=xxx&payAmount=10&acct=xxx&custId=xxx&sign=xxx&...
|
||
*/
|
||
|
||
const STORAGE_KEY = 'app_launch_payment_data'
|
||
|
||
/**
|
||
* 从启动参数中获取 App 传入的全部数据(支付参数 + token + userId)
|
||
*/
|
||
export function captureAppLaunchData(onShowOptions) {
|
||
let data = null
|
||
|
||
const tryGetData = (options, label) => {
|
||
if (!options) return null
|
||
console.log(label, JSON.stringify(options))
|
||
if (options.query && Object.keys(options.query).length > 0) {
|
||
return options.query
|
||
}
|
||
if (options?.referrerInfo?.extraData) {
|
||
return options.referrerInfo.extraData
|
||
}
|
||
return null
|
||
}
|
||
|
||
// 冷启动
|
||
try {
|
||
data = tryGetData(wx.getLaunchOptionsSync?.() || {}, '冷启动')
|
||
} catch (e) { console.error('getLaunchOptionsSync 失败:', e) }
|
||
|
||
// 热启动
|
||
if (!data) {
|
||
data = tryGetData(onShowOptions, '热启动')
|
||
}
|
||
|
||
if (data && (data.payAmount || data.acct || data.custId)) {
|
||
// 恢复登录态
|
||
if (data.token) {
|
||
uni.setStorageSync('token', data.token)
|
||
console.log('已恢复 token')
|
||
}
|
||
if (data.userId) {
|
||
uni.setStorageSync('userId', data.userId)
|
||
console.log('已恢复 userId')
|
||
}
|
||
|
||
uni.setStorageSync(STORAGE_KEY, data)
|
||
console.log('已存储 App 支付数据:', JSON.stringify(data))
|
||
return data
|
||
}
|
||
|
||
return null
|
||
}
|
||
|
||
/** 检查是否已登录(token 存在) */
|
||
export function isLoggedIn() {
|
||
return !!uni.getStorageSync('token')
|
||
}
|
||
|
||
/** 获取 App 传入的支付数据 */
|
||
export function getAppPaymentData() {
|
||
try { return uni.getStorageSync(STORAGE_KEY) || null } catch (e) { return null }
|
||
}
|
||
|
||
/** 清除 */
|
||
export function clearAppPaymentData() {
|
||
uni.removeStorageSync(STORAGE_KEY)
|
||
}
|
||
|
||
/** 组装 paymentItem */
|
||
export function buildPaymentItemFromApp(appData) {
|
||
if (!appData) return null
|
||
const amount = Number(appData.payAmount || 0).toFixed(2)
|
||
return { ...appData, type: '缴费', rechargeAmount: amount }
|
||
}
|