bd398f154a
- 新增 friendlyVpError() 工具函数,映射 VP 常见错误码 (pay cancel, access denied, SIGNATURE_INVALID 等) 为友好中文 - member.vue 两个 VP fail 回调使用 friendlyVpError 显示 原始: requestVirtualPayment:fail pay cancel 优化: 你已取消支付
73 lines
2.6 KiB
TypeScript
73 lines
2.6 KiB
TypeScript
/**
|
||
* 清除登录状态 —— token 过期 / 未授权时调用
|
||
*/
|
||
export function clearAuth() {
|
||
uni.removeStorageSync('token')
|
||
uni.removeStorageSync('userInfo')
|
||
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' })
|
||
// 延迟跳转,避免和当前页面操作冲突
|
||
setTimeout(() => {
|
||
uni.navigateTo({ url: '/pages/login/login' })
|
||
}, 800)
|
||
}
|
||
|
||
/**
|
||
* 检查 uni.request 的响应是否为 401,是则自动清除登录态
|
||
* 返回 true 表示已处理 401(调用方应停止后续逻辑)
|
||
*/
|
||
export function checkAuth(res: any): boolean {
|
||
if (res.statusCode === 401) {
|
||
clearAuth()
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
/**
|
||
* 微信虚拟支付(VP)错误消息 → 用户友好中文提示
|
||
* 覆盖 wx.requestVirtualPayment fail 回调中的常见错误码
|
||
*/
|
||
export function friendlyVpError(errMsg: string): string {
|
||
if (!errMsg) return '支付失败,请重试'
|
||
|
||
const map: Record<string, string> = {
|
||
'pay cancel': '你已取消支付',
|
||
'cancel': '你已取消支付',
|
||
'access denied': '支付权限不足,请联系客服',
|
||
'商户收款功能受限': '商户暂不支持收款,请联系客服',
|
||
'PRODUCT_ID_EMPTY': '商品信息错误',
|
||
'PRODUCT_NOT_EXIST': '商品不存在',
|
||
'PRODUCT_NOT_ONLINE': '商品未上架',
|
||
'PAY_SIG_INVALID': '支付签名异常,请重试',
|
||
'SIGNATURE_INVALID': '支付验证失败,请重试',
|
||
'BALANCE_NOT_ENOUGH': '余额不足',
|
||
'ORDER_NOT_EXIST': '订单不存在',
|
||
'ORDER_EXPIRED': '订单已过期,请重新下单',
|
||
'ORDER_PAYED': '订单已支付',
|
||
'ORDER_CANCELED': '订单已取消',
|
||
'PRICE_NOT_MATCH': '价格异常,请联系客服',
|
||
'TOKEN_NOT_EXIST': '登录态失效,请重新登录',
|
||
'TOKEN_EXPIRED': '登录态已过期,请重新登录',
|
||
'TOKEN_NOT_MATCH': '登录态不匹配,请重新登录',
|
||
'internal error': '支付异常,请稍后重试',
|
||
'system error': '系统繁忙,请稍后重试',
|
||
'payment limit exceeded': '已超出支付限额',
|
||
}
|
||
|
||
// 尝试精确匹配
|
||
const lower = errMsg.toLowerCase()
|
||
for (const [key, msg] of Object.entries(map)) {
|
||
if (lower.includes(key.toLowerCase())) return msg
|
||
}
|
||
|
||
// fallback:去前缀(requestVirtualPayment:fail xxx → xxx)
|
||
const cleaned = errMsg.replace(/^requestVirtualPayment:fail\s*/i, '').replace(/^requestPayment:fail\s*/i, '').trim()
|
||
if (cleaned && cleaned !== errMsg) {
|
||
for (const [key, msg] of Object.entries(map)) {
|
||
if (cleaned.toLowerCase().includes(key.toLowerCase())) return msg
|
||
}
|
||
}
|
||
|
||
return `支付失败(${cleaned || '未知错误'})`
|
||
}
|