前端服务共享

This commit is contained in:
2025-12-11 14:21:36 +08:00
parent fa3dbe0496
commit 5ee9770747
46 changed files with 3732 additions and 1782 deletions

View File

@@ -0,0 +1,254 @@
# Platform 应用配置说明
## AES 加密配置
### 密钥配置
**配置文件**`src/config/index.ts`
```typescript
export const AES_SECRET_KEY = '1234567890qwer'
```
**注意事项**
1. ✅ 密钥已配置为 `1234567890qwer`,与后端保持一致
2. ⚠️ 该密钥与后端 `application.yml` 中的 `security.aes.secret-key` 必须相同
3. 🔒 生产环境应从环境变量或配置中心获取,不要硬编码
### 对应后端配置
**Gateway** (`gateway/src/main/resources/application.yml`):
```yaml
security:
aes:
secret-key: 1234567890qwer
```
**Auth Service** (`auth/src/main/resources/application.yml`):
```yaml
security:
aes:
secret-key: 1234567890qwer
```
## 使用示例
### 1. 登录时加密密码
```typescript
import { authAPI } from '@shared/api/auth'
import { getAesInstance } from '@shared/utils'
async function handleLogin(username: string, password: string) {
try {
// 1. 获取 AES 加密实例
const aes = getAesInstance()
// 2. 加密密码
const encryptedPassword = await aes.encryptPassword(password)
// 3. 发送登录请求
const response = await authAPI.login({
username,
password: encryptedPassword, // 使用加密后的密码
loginType: 'password'
})
if (response.data.success) {
console.log('登录成功')
// 保存 token 等操作
}
} catch (error) {
console.error('登录失败:', error)
}
}
```
### 2. 注册时加密手机号和密码
```typescript
import { authAPI } from '@shared/api/auth'
import { getAesInstance } from '@shared/utils'
async function handleRegister(phone: string, password: string, smsCode: string, sessionId: string) {
try {
const aes = getAesInstance()
// 加密敏感信息
const encryptedPhone = await aes.encryptPhone(phone)
const encryptedPassword = await aes.encryptPassword(password)
// 发送注册请求
const response = await authAPI.register({
registerType: 'phone',
phone: encryptedPhone,
password: encryptedPassword,
confirmPassword: encryptedPassword,
smsCode,
smsSessionId: sessionId
})
if (response.data.success) {
console.log('注册成功')
}
} catch (error) {
console.error('注册失败:', error)
}
}
```
### 3. 数据脱敏显示
```typescript
import { AesUtils } from '@shared/utils'
// 显示脱敏手机号
const phone = '13812345678'
const maskedPhone = AesUtils.maskPhone(phone)
console.log(maskedPhone) // 输出138****5678
// 显示脱敏身份证号
const idCard = '110101199001011234'
const maskedIdCard = AesUtils.maskIdCard(idCard)
console.log(maskedIdCard) // 输出110101********1234
```
## 初始化流程
### 应用启动时自动初始化
**文件**`src/main.ts`
```typescript
import { AES_SECRET_KEY } from './config'
import { initAesEncrypt } from '@shared/utils'
async function initApp() {
// 初始化 AES 加密工具
await initAesEncrypt(AES_SECRET_KEY)
// ... 其他初始化操作
}
initApp()
```
### 初始化状态检查
```typescript
import { getAesInstance } from '@shared/utils'
try {
const aes = getAesInstance()
console.log('✅ AES 加密工具已初始化')
} catch (error) {
console.error('❌ AES 加密工具未初始化:', error)
}
```
## 加密流程图
```
用户输入密码
前端 AES 加密 (1234567890qwer)
发送加密后的密码
Gateway (不解密,直接转发)
Auth Service 接收
AES 解密 (1234567890qwer)
BCrypt 再次加密
存入数据库
```
## 安全建议
### 开发环境
- ✅ 使用固定密钥 `1234567890qwer`
- ✅ 密钥在代码中配置
### 生产环境
- 🔒 从环境变量获取密钥
- 🔒 使用配置中心Nacos
- 🔒 定期轮换密钥
- 🔒 密钥长度至少 32 字符
### 示例:从环境变量获取
```typescript
// 生产环境配置
export const AES_SECRET_KEY = process.env.VUE_APP_AES_SECRET_KEY || '1234567890qwer'
```
## 故障排查
### 问题:登录时提示"密码错误"
**可能原因**:前后端密钥不一致
**排查步骤**
1. 检查前端配置:`src/config/index.ts` 中的 `AES_SECRET_KEY`
2. 检查后端配置:`application.yml` 中的 `security.aes.secret-key`
3. 确保两者完全一致
**解决方案**
```bash
# 前端
export const AES_SECRET_KEY = '1234567890qwer'
# 后端
security:
aes:
secret-key: 1234567890qwer
```
### 问题:"AES 加密工具未初始化"
**原因**`initAesEncrypt()` 未被调用
**解决**:检查 `main.ts` 中是否正确调用初始化函数
### 问题:加密后的数据无法解密
**可能原因**
1. 密钥不正确
2. 数据被篡改
3. Base64 编码问题
**调试方法**
```typescript
const aes = getAesInstance()
const original = 'test123'
const encrypted = await aes.encrypt(original)
const decrypted = await aes.decrypt(encrypted)
console.log(original === decrypted) // 应该输出 true
```
## API 参考
### 配置项
| 配置项 | 类型 | 默认值 | 说明 |
|--------|------|--------|------|
| `AES_SECRET_KEY` | `string` | `'1234567890qwer'` | AES 加密密钥 |
| `API_BASE_URL` | `string` | `'http://localhost:8180'` | API 基础地址 |
| `APP_CONFIG.name` | `string` | `'泰豪电源 AI 数智化平台'` | 应用名称 |
| `APP_CONFIG.version` | `string` | `'1.0.0'` | 应用版本 |
### 环境变量
| 变量名 | 说明 | 示例 |
|--------|------|------|
| `VITE_API_BASE_URL` | API 基础地址 | `https://api.example.com` |
| `VUE_APP_AES_SECRET_KEY` | AES 密钥(生产) | `your-secret-key-32-chars-long` |
## 更多信息
- AES 加密工具详细文档:`@shared/utils/crypto/README.md`
- Auth API 文档:`@shared/api/auth/auth.ts`
- 后端 AES 实现:`urbanLifelineServ/common/common-utils/src/main/java/org/xyzh/common/utils/crypto/AesEncryptUtil.java`

View File

@@ -0,0 +1,24 @@
/**
* Platform 应用配置
*/
/**
* AES 加密密钥(与后端保持一致)
* 对应后端配置security.aes.secret-key
* Base64 编码的 32 字节密钥256 位)
*/
export const AES_SECRET_KEY = 'MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI=' // Base64 编码,解码后是 "12345678901234567890123456789012" (32字节)
/**
* API 基础地址
*/
export const API_BASE_URL = (import.meta as any).env?.VITE_API_BASE_URL || 'http://localhost:8180'
/**
* 应用配置
*/
export const APP_CONFIG = {
name: '泰豪电源 AI 数智化平台',
version: '1.0.0',
copyright: '泰豪电源'
}