Files
dukang/apps/mini-user/src/lib/pay-ready.ts
T
jacy 5c55d3c385
CI / verify (pull_request) Has been cancelled
fix(mini-user): persist bind-phone session and stop login bounce
Phone bind discarded the new JWT after account merge, so the next page 401'd back to login. Also harden 401 race, location deny cache, unpaid repay CTA, and finishLoginNavigate.
2026-07-15 21:10:42 +08:00

47 lines
1.5 KiB
TypeScript

import { goLogin } from './auth-nav';
import { isLoggedIn } from './api';
import { ensureWechatAuthForPay } from './wechat-auth';
import { fetchClientConfig, fetchUserProfile, needsWechatAuthForPay } from './pay-wechat';
/**
* 支付前门禁:
* - 未登录 → 跳转登录页(微信授权即可,不强制手机号)
* - H5 微信内缺 openId → 尝试 OAuth(可能跳转微信授权页)
* - 小程序缺绑定 → 跳转登录页 needWechat
*/
export async function ensurePayReady(returnPath: string): Promise<boolean> {
if (!isLoggedIn()) {
goLogin(returnPath);
return false;
}
try {
const [config, profile] = await Promise.all([fetchClientConfig(), fetchUserProfile()]);
if (!needsWechatAuthForPay(config, profile)) {
return true;
}
if (process.env.TARO_ENV === 'h5') {
const auth = await ensureWechatAuthForPay();
if (auth.ok) return true;
// 旧版 needBindPhone 已不再返回;缺 openId 时走登录补微信绑定
if ('needBindPhone' in auth && auth.needBindPhone) {
goLogin(returnPath, { needWechat: '1' });
return false;
}
// redirecting:正在跳转微信 OAuth
return false;
}
goLogin(returnPath, { needWechat: '1' });
return false;
} catch (e) {
// 仅会话失效时踢回登录;网络/业务错误不误清登录态
const msg = e instanceof Error ? e.message : '';
if (/登录已过期|重新登录|401/.test(msg) || !isLoggedIn()) {
goLogin(returnPath);
}
return false;
}
}