功能:新增微信小程序一键登录

This commit is contained in:
xiaowang
2026-04-02 13:20:28 +08:00
parent 8ac7deffca
commit 4f5ef4fedd
32 changed files with 1899 additions and 65 deletions
+22 -3
View File
@@ -1,11 +1,11 @@
# 项目架构分析
# 项目架构分析
## 1. 项目结构
### 1.1 目录结构
```
├── Frontend/ # 前端项目
├── YumiNail-Frontend/ # YumiNail 主业务前端项目
│ ├── src/
│ │ ├── api/ # API请求封装
│ │ ├── router/ # 路由配置
@@ -21,6 +21,9 @@
│ ├── controller/ # 控制器
│ ├── service/ # 业务逻辑
│ └── utils/ # 工具类
├── YumiNail-Calendar-Frontend/ # 美甲师日历表独立前端项目
├── YumiNail-Calendar-Gateway/ # 美甲师日历表独立网关
├── YumiNail-Calendar-Core/ # 美甲师日历表独立后端
└── MessageHub/ # 消息推送服务
```
@@ -45,6 +48,12 @@
- 对于管理员路由,会检查adminAccessToken
- 对于需要权限的页面,会检查用户是否拥有对应权限
4. **小程序自动登录**:
- `WeChatProgram/app.js` 会在本地没有有效 session 时自动执行 `wx.login -> /api/user/wechat-mini/auto-login`
- 若返回 `SUCCESS`,会直接写入 accessToken、refreshToken 和当前用户门店信息
- 若返回 `SELECT_SHOP`,会把一次性 ticket 缓存在小程序全局态,等进入登录页后再弹出独立门店选择层
- `WeChatProgram/utils/navigation.js` 的 `ensureLogin` 会等待这段启动期自动登录 promise 结束,再决定是否跳转登录页,避免先跳页后又自动登录成功的闪烁
### 2.2 后端处理流程
1. **网关过滤**:
@@ -56,6 +65,8 @@
- AuthInterceptor会进一步验证token的有效性
- 验证token是否过期、是否为accessToken类型
- 从token中提取用户信息并设置到请求属性中
- 微信小程序登录由 `WechatMiniAuthService` 统一编排:先调用微信 `jscode2session` / 手机号接口,再决定是自动登录、补选门店还是回退到传统登录
- `SELECT_SHOP` 场景会把一次性 ticket 存入 Redis,TTL 为 5 分钟;用户在登录页确认门店后再调用 `/api/user/wechat-mini/confirm-login` 完成收口
- 后台账号密码登录会强制校验 `shopId`,防止仅靠前端“选店铺”表单限制被绕过
- 当系统内尚无店铺时,仅平台管理员可跳过 `shopId` 进入后台;此时运行时权限会被收紧到 `shop:view` 与 `shop:create`
- 订单创建时还会校验购物车归属、同店铺/同美甲师约束、预约时间有效性、门店营业时间以及美甲师已有预约冲突
@@ -70,7 +81,7 @@
### 3.1 前端白名单
在`Frontend/src/api/index.ts`中定义:
在`YumiNail-Frontend/src/api/index.ts`中定义:
```typescript
const isWhitelistedPath = (url: string) => {
@@ -80,12 +91,15 @@ const isWhitelistedPath = (url: string) => {
url?.includes('/user/refresh-token') ||
url?.includes('/user/send-verification-code') ||
url?.includes('/user/phone-login') ||
url?.includes('/user/wechat-mini/') ||
url?.includes('/shop/list/simple') ||
url?.includes('/admin/auth/login') ||
url?.includes('/admin/auth/refresh')
}
```
小程序端对应的 `WeChatProgram/utils/request.js` 也需要把 `/user/wechat-mini/` 视为免鉴权白名单,否则冷启动自动登录会被误判为 401。
### 3.2 网关白名单
在`YumiNail-Gateway/src/main/resources/application.yaml`中通过配置集中维护:
@@ -100,6 +114,7 @@ yuminail:
- /api/user/refresh-token
- /api/user/send-verification-code
- /api/user/phone-login
- /api/user/wechat-mini/**
- /api/user/reset-password-by-phone
- /api/shop/list/simple
- /api/health/**
@@ -121,6 +136,7 @@ registry.addInterceptor(authInterceptor)
"/user/refresh-token",
"/user/send-verification-code",
"/user/phone-login",
"/user/wechat-mini/**",
"/user/reset-password-by-phone",
"/shop/list/simple",
"/admin/auth/login",
@@ -133,6 +149,8 @@ registry.addInterceptor(authInterceptor)
### 3.4 白名单补充说明
- 账号检测链路 `GET /api/user/check/account/{account}`、`GET /api/user/check/phone/{phone}`、`GET /api/user/check/login-account/{account}` 必须同时在前端、网关、本地 Core 拦截器以及 Nacos `YumiNail-Dev` 白名单中保持一致。
- 微信小程序登录链路 `POST /api/user/wechat-mini/auto-login`、`POST /api/user/wechat-mini/phone-login`、`POST /api/user/wechat-mini/confirm-login` 也必须同时在小程序请求层、Gateway 本地默认白名单、Gateway 配置白名单、Core 拦截器白名单与 Nacos `YumiNail-Dev` whitelist 中保持一致。
- 微信开放平台配置通过 Nacos `YumiNail-Dev` 下的 `yuminail.wechat.mini.*` 维护,至少包括 `app-id`、`app-secret`、`code2session-url`、`phone-number-url` 和 `access-token-buffer-seconds`。
- 白名单只开放登录、注册、验证码、刷新 token、店铺简表、健康检查与账号检测接口;其余用户资产、订单、购物车、后台管理接口仍必须携带 token。
## 4. 权限控制体系
@@ -641,3 +659,4 @@ registry.addInterceptor(authInterceptor)
该项目采用了现代化的前后端分离架构,具有完善的认证和授权机制,以及良好的安全设计。通过多层白名单保护和双Token认证,确保了系统的安全性。同时,清晰的代码组织和合理的服务拆分,为系统的可维护性和扩展性奠定了基础。
在未来的发展中,可以通过集中管理白名单、增强权限管理、加强安全性和完善监控日志等方面进一步提升系统的可靠性和安全性。