Files
crmeb-mp/.hermes/note-backend-jwt-changes.md

62 lines
1.4 KiB
Markdown
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.
# 后端 JWT Token 签发 — 需要添加的修改
以 CRMEB 后端 PHP 为例,找到签发 token 的方法(通常在类似以下位置):
## 1. 查找签发位置
CRMEB 后端搜索 `JWT::encode` 或 `makeToken` / `createToken`:
- `crmeb/app/services/system/SystemAuthService.php`
- `crmeb/app/services/other/CacheTokenService.php`
- 或 `crmeb/app/services/wechat/...`
## 2. 修改 Payload
原始代码可能类似:
```php
$payload = [
'iss' => 'crmeb',
'iat' => time(),
'exp' => time() + 86400 * 7,
'id' => $user->id,
];
$token = JWT::encode($payload, $key, 'HS256');
```
改为(添加 uid、phone、openid):
```php
$payload = [
'iss' => 'crmeb',
'iat' => time(),
'exp' => time() + 86400 * 7,
'uid' => $user->id, // ← 添加:用户 ID
'phone' => $user->phone ?? '', // ← 添加:手机号
'openid' => $user->openid ?? '', // ← 添加:微信 openid
];
$token = JWT::encode($payload, $key, 'HS256');
```
## 3. 验证
修改后登录任意用户,token 形如:
```
eyJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjEyMywicGhvbmUiOiIxMzgwMDAwMDAwMCIsIm9wZW5pZCI6Im8xMjNfIn0.xxxx
```
在浏览器 DevTools 或 https://jwt.io 解码 payload 应看到:
```json
{
"uid": 123,
"phone": "13800000000",
"openid": "o123_xxx",
"exp": 1748000000,
"iss": "crmeb",
"iat": 1747395200
}
```
前端 `parseJwt()` 就能正确读取这些字段。