更新记录
1.7.0(2026-09-17) 下载此版本
- 新增资料更新接口:api 层
updateProfile({ nickname?, headimgurl? }), 对接POST /core/mobile/wonUser/updateProfile(server 端 WonUserController::updateProfile, 验证器只收这两个字段);返回新增类型WonUserProfile(won_user 模型 toArray,无账号快照)。 保存后建议getUserInfo(true)强刷 store,让「我的」等页面立即展示新资料。
1.6.0(2026-09-14) 下载此版本
- 渠道化重构:登录渠道独立成
channels/目录,为支付宝小程序、望潮客户端等 后续渠道预留清晰的扩展位(新渠道 = 新文件,不动其它代码):channels/wechat-mini.ts:wechatMiniLogin(wcappid?)—— 微信小程序一键登录 (原 mp-login.ts 的 mpWeixinLogin 更名,与 server 端点同名)channels/web-oauth.ts:webOauthLogin(params?)—— H5 网页授权(原 auth.ts 内联链路抽出)。命名中性:server 依 User-Agent 自动分流微信公众号/支付宝/望潮, 三平台 H5 共用本入口,不绑死微信- H5 URL 工具(login_code 提取/清理、当前页地址)独立为
commons/url.ts auth.ts瘦身为登录协调器:setupWcAuth / loginByCode / 刷新 / 用户信息 / 登出;login()变为纯分发(H5 → webOauthLogin,MP-WEIXIN → wechatMiniLogin)
- 对外导出变化:新增
wechatMiniLogin、webOauthLogin;移除mpWeixinLogin; api 层wechatMiniLogin(接口函数)不再从 index 导出,避免与渠道入口同名混淆 - 行为不变:token 管理、刷新、401 重试、wcappid 传参约定均与 1.5.0 一致
1.3.0(2026-08-21) 下载此版本
- 新增微信小程序登录分支(与 H5 同一 API,条件编译内部分流):
login()在 MP-WEIXIN 下走uni.login→ miniLogin 接口换 token,静默无跳转- 端点 URL 待后端确定(暂用占位,won-passion 项目参照是 /api/app/auth/login)
- token 管理 / 刷新 / 401 重试 / 用户信息两端共用
平台兼容性
uni-app(5.24)
| Vue2 | Vue3 | Chrome | Safari | app-vue | app-nvue | Android | iOS | 鸿蒙 |
|---|---|---|---|---|---|---|---|---|
| - | √ | - | - | - | - | - | - | - |
| 微信小程序 | 支付宝小程序 | 抖音小程序 | 百度小程序 | 快手小程序 | 京东小程序 | 鸿蒙元服务 | QQ小程序 | 飞书小程序 | 小红书小程序 | 快应用-华为 | 快应用-联盟 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| √ | - | - | - | - | - | - | - | - | - | - | - |
uni-app x(5.24)
| Chrome | Safari | Android | iOS | 鸿蒙 | 微信小程序 |
|---|---|---|---|---|---|
| - | - | - | - | - | - |
wc-auth
微信登录封装(H5 网页授权 + 微信小程序),基于 wc-request,对接 server 的
won_user 统一登录体系(/core/mobile/wonUser/*,见 server wonadmin 插件
WonLoginController)。token 管理(Pinia + storage 持久化)、过期预刷新、401 自动
重发重试,两端共用一套 API。
安装前置
wc-request≥ 2.1.0(token 供应器机制)、pinia- App 启动配置(main.ts):
import * as Pinia from 'pinia'
import { setupWcAuth } from '@/uni_modules/wc-auth/commons'
import { setBaseURL } from '@/uni_modules/wc-request/commons/request'
export function createApp() {
setBaseURL(import.meta.env.VITE_APP_API_BASE_URL || '')
const app = createSSRApp(App)
app.use(Pinia.createPinia())
// 登录接线:恢复 token、注册请求头供应器与 401 处理、处理回跳 login_code
setupWcAuth()
return { app, Pinia }
}
注意 setupWcAuth 必须在 createPinia 之后调用(依赖 store)。
项目配置(wcappid)
wcappid 标识项目在望潮用户中心登记的微信登录配置,每个项目不同。
插件不读取项目环境变量 —— 由调用方从项目 .env 读取后,以参数传给登录接口:
# uni/.env
VITE_APP_WCAPPID=wonCollectPayment # 本项目(望潮收款)的应用 ID
// 页面/业务代码(如登录页)
const wcappid = import.meta.env.VITE_APP_WCAPPID || undefined
await login({ wcappid }) // 自动分发:H5 网页授权 / 微信小程序均生效
// 或 wechatMiniLogin(wcappid) // 微信小程序一键登录独立入口
// 或 webOauthLogin({ wcappid }) // H5 网页授权独立入口
不传时登录请求不带该参数,走 server 默认应用。
使用
登录渠道一览(channels/,按平台各一个入口)
| 渠道入口 | 平台 | 链路 |
|---|---|---|
wechatMiniLogin(wcappid?) |
微信小程序 | uni.login code → POST /wonUser/wechatMiniLogin 静默换 token |
webOauthLogin(params?) |
H5(微信公众号 / 支付宝 / 望潮客户端内嵌,UA 自动分流) | getAuthorizeUrl 整页跳转 → server 回调 → login_code 回跳 → 自动兑换 |
login(params?) |
自动分发 | 按运行环境选上面两者(业务推荐默认入口) |
| (预留) | 支付宝小程序 / 望潮 APP 原生 / 手机号验证码 | server 端已备(phoneLogin 等),前端按需新增渠道文件 |
发起登录(未登录时,推荐统一入口)
import { login, useAuthStore } from '@/uni_modules/wc-auth/commons'
const store = useAuthStore()
if (!store.isLoggedIn) {
await login()
// H5 :整页跳转授权页(微信/支付宝/望潮按 UA 自动分流)→ 回跳本页自动兑换 token
// MP-WEIXIN :uni.login 拿 code → wechatMiniLogin 接口换 token(静默完成,无跳转)
}
两端的差异全部被条件编译封装在模块内部:
| 环节 | H5 | 小程序 |
|---|---|---|
| 获取凭证 | 授权页回跳 ?login_code= |
uni.login() 临时 code |
| 换 token | GET /wonUser/getTokenByLoginCode |
POST /wonUser/wechatMiniLogin(直接返回令牌) |
| 页面跳转 | 整页跳转两次(授权页→回跳) | 零跳转,静默 |
| 后续(token 管理/刷新/用户信息) | 完全一致 | 完全一致 |
指定渠道登录(独立入口,可选)
除统一入口 login() 外,各渠道可单独调用(login() 的对应分支内部就是它们):
import { wechatMiniLogin, webOauthLogin, useAuthStore } from '@/uni_modules/wc-auth/commons'
const store = useAuthStore()
if (!store.isLoggedIn) {
// 微信小程序「一键登录」按钮:uni.login 拿 code → 接口换 token → 写 store,
// 静默完成;并发连点只发一次请求;非微信小程序环境抛错
const token = await wechatMiniLogin(wcappid)
console.log(token.access_token)
// H5 网页授权(微信/支付宝/望潮按 UA 自动分流):整页跳转,本函数返回时页面即将离开;
// 回跳后 setupWcAuth 自动兑换 token,调用方无需处理回跳
await webOauthLogin({ wcappid })
}
getAuthorizeUrl 参数说明(GET query 传递;platform 由 server 依 User-Agent 自动判定,前端不传):
| 参数 | wc-auth 默认 | 说明 |
|---|---|---|
redirect_url |
当前页面地址 | 登录成功回跳地址(server 存 Redis,5 分钟有效;回跳恒为 login_code 模式) |
scope |
不传(server 默认 snsapi_base) |
授权作用域(需拉取昵称头像时传 snsapi_userinfo) |
wcappid |
不传(server 默认 tzWonTech) |
望潮用户中心应用 ID(项目级配置,调用方从项目 .env 读取传入,见「项目配置」) |
登录状态与用户信息
const store = useAuthStore()
store.isLoggedIn // 是否已登录(有 token 且未过期,响应式)
store.access_token // 当前令牌
// 用户信息(won_user 主体 + 当前登录账号快照),内存缓存于 store,登出自动清空
const user = await getUserInfo() // store 已有直接返回,没有则请求 /wonUser/info
await getUserInfo(true) // 强制刷新(资料修改后)
store.userInfo // 响应式读取,模板直接用
store.userInfo?.nickname // 昵称(won_user 主体字段)
store.userInfo?.account?.openid // 当前登录账号 openid
store.userInfo?.account?.platform // 渠道:1公众号 2支付宝 3微信小程序 4望潮
登出
import { logout } from '@/uni_modules/wc-auth/commons'
logout()
完整链路(setupWcAuth 之后自动运转的部分)
1. 请求自动带 Authorization: Bearer <access_token>(wc-request 供应器)
2. 预刷新:token 快过期(提前 10 分钟)→ 发请求前先刷新,避免注定 401 的往返
3. 401 自动重发:请求收到 401 → 自动刷新 token(并发去重)→ 重发原请求,
调用方完全无感(表单提交等场景不需要用户重点一次)
4. 微信回跳 URL 带 login_code → 静默兑换 token → 清理 URL 参数
5. refresh_token 也失效 → 清空 token(调用方按 isLoggedIn 引导重新 login)
行为约定
| 场景 | 行为 |
|---|---|
| token 存放 | Pinia store(id wc-auth)+ uni storage(key wc_auth_token) |
| 过期判断 | token_time + expires_in,提前 10 分钟视为过期 |
| 401 收到 | 自动刷新(去重);refresh_token 失效则清 token |
| login_code 兑换 | URL 参数(含 hash 路由 query)自动识别,成功后从 URL 移除 |
| 匿名接口 | isToken: false 的请求不带 Authorization(登录三接口自身即匿名) |
| 环境 | 授权跳转/URL 处理仅 H5;token 管理与刷新全端可用 |
范围说明
刻意不包含(保持简单):
- 微信 JSSDK 配置(wc-jssdk 的范畴)
- 支付宝小程序 / 望潮 APP 原生登录(需端能力如 my.getAuthCode,按需新增 channels/ 渠道文件;H5 侧支付宝/望潮已随 webOauthLogin 自动支持)
- 手机号验证码登录(server 端点 phoneLogin 已备,前端按需封装)
- 旧体系 /core/mobile/user/*(won_user 统一登录体系上线后不再对接)
接口一览(server 端点,won_user 统一登录体系)
| 函数 | 端点 |
|---|---|
getAuthorizeUrl(params) |
GET /core/mobile/wonUser/getAuthorizeUrl |
getTokenByLoginCode(code) |
GET /core/mobile/wonUser/getTokenByLoginCode |
refreshToken(token) |
POST /core/mobile/wonUser/refreshToken |
wechatMiniLogin(code, wcappid?) |
POST /core/mobile/wonUser/wechatMiniLogin |
requestUserInfo() |
GET /core/mobile/wonUser/info |

收藏人数:
下载插件并导入HBuilderX
下载插件ZIP
赞赏(0)
下载 7
赞赏 0
下载 12652932
赞赏 1954
赞赏
京公网安备:11010802035340号