liufumingren 8f1a8b7ddb feat(miniprogram): 新增 uni-app + Vue3 + TS 微信小程序
- 登录:微信一键登录(code2Session)+ 账号登录双 tab;个人中心为整页登录,其它页走抽屉
- 协议:登录前必须阅读并同意,版本号随登录上报落库
- 不强制登录:启动不弹窗,点功能或遇到 401 才提示(符合小程序审核)
- 页面:首页统计 / 扫码记录 / 个人中心;H5 预览端口 7304

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-21 00:47:16 +08:00

30 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 微信小程序登录 code
*
* 真机:`uni.login({ provider:'weixin' })` 拿到一次性 code,后端换 openid。
* H5 / 开发者工具预览:`uni.login` 不可用,配置 `VITE_WX_MOCK_OPENID` 后走后端 mock
* (code 传 `mock:<openid>`,同一值固定同一个账号),仅用于本地联调。
*/
export async function getWechatLoginCode(): Promise<string> {
try {
const code = await new Promise<string>((resolve, reject) => {
uni.login({
provider: 'weixin',
success: (res) => {
if (res.code) resolve(res.code);
else reject(new Error('未获取到微信登录 code'));
},
fail: (err) => reject(new Error(err.errMsg || '微信登录失败')),
});
});
if (code) return code;
} catch {
/* 落到下面的 mock / 报错 */
}
const mock = String(import.meta.env.VITE_WX_MOCK_OPENID || '').trim();
if (mock) return `mock:${mock}`;
throw new Error('当前环境不支持微信登录,请使用账号登录');
}