feat(admin): Add user management and upgrade to module permission system

Features - User Management (Phase 4.1):
- Database: Add user_modules table for fine-grained module permissions
- Database: Add 4 user permissions (view/create/edit/delete) to role_permissions
- Backend: UserService (780 lines) - CRUD with tenant isolation
- Backend: UserController + UserRoutes (648 lines) - 13 API endpoints
- Backend: Batch import users from Excel
- Frontend: UserListPage (412 lines) - list/filter/search/pagination
- Frontend: UserFormPage (341 lines) - create/edit with module config
- Frontend: UserDetailPage (393 lines) - details/tenant/module management
- Frontend: 3 modal components (592 lines) - import/assign/configure
- API: GET/POST/PUT/DELETE /api/admin/users/* endpoints

Architecture Upgrade - Module Permission System:
- Backend: Add getUserModules() method in auth.service
- Backend: Login API returns modules array in user object
- Frontend: AuthContext adds hasModule() method
- Frontend: Navigation filters modules based on user.modules
- Frontend: RouteGuard checks requiredModule instead of requiredVersion
- Frontend: Remove deprecated version-based permission system
- UX: Only show accessible modules in navigation (clean UI)
- UX: Smart redirect after login (avoid 403 for regular users)

Fixes:
- Fix UTF-8 encoding corruption in ~100 docs files
- Fix pageSize type conversion in userService (String to Number)
- Fix authUser undefined error in TopNavigation
- Fix login redirect logic with role-based access check
- Update Git commit guidelines v1.2 with UTF-8 safety rules

Database Changes:
- CREATE TABLE user_modules (user_id, tenant_id, module_code, is_enabled)
- ADD UNIQUE CONSTRAINT (user_id, tenant_id, module_code)
- INSERT 4 permissions + role assignments
- UPDATE PUBLIC tenant with 8 module subscriptions

Technical:
- Backend: 5 new files (~2400 lines)
- Frontend: 10 new files (~2500 lines)
- Docs: 1 development record + 2 status updates + 1 guideline update
- Total: ~4900 lines of code

Status: User management 100% complete, module permission system operational
This commit is contained in:
2026-01-16 13:42:10 +08:00
parent 98d862dbd4
commit 66255368b7
560 changed files with 70424 additions and 52353 deletions

View File

@@ -2,47 +2,47 @@
## 📌 背景
鍦ㄤ换鍔?9锛堝悗绔<E68297>唬鐮佸垎灞傦級鎵ц<E98EB5>杩囩▼涓<E296BC>紝鐢变簬 PowerShell 鎵归噺鎿嶄綔瀵艰嚧浜嗕腑鏂囩紪鐮侀棶棰橈紝寮曞彂浜嗗<E6B59C><EFBFBD>枃浠剁殑瀛楃<E7809B>涓叉湭闂<E6B9AD>悎銆佹敞閲婁唬鐮佸悎骞剁瓑涓ラ噸閿欒<E996BF>锛屽<E9949B>鑷存湇鍔″櫒鏃犳硶鍚<E7A1B6>姩銆?
在任务19后端代码分层执行过程中由于 PowerShell 批量操作导致了中文编码问题,引发了多个文件的字符串未闭合、注释代码合并等严重错误,导致服务器无法启动。
## 馃攳 涔辩爜浜х敓鐨勬牴鏈<E789B4>師鍥?
## 🔍 乱码产生的根本原因
### 1. PowerShell 鎵归噺鎿嶄綔鐨勯櫡闃?
### 1. PowerShell 批量操作的陷阱
```powershell
# 鉂?閿欒<E996BF>绀轰緥锛堜細瀵艰嚧涔辩爜锛?
# ❌ 错误示例(会导致乱码)
Get-Content file.ts -Raw -Encoding UTF8 |
Set-Content file.ts -NoNewline -Encoding UTF8
```
**<EFBFBD><EFBFBD>锛?*
**问题:**
- `-NoNewline` 会导致行合并
- Windows PowerShell 鐨?UTF-8 澶勭悊涓嶅畬鍠?
- <EFBFBD>兘寮曞叆鎴栫Щ闄?BOM (Byte Order Mark)
- Windows PowerShell UTF-8 处理不完善
- 可能引入或移除 BOM (Byte Order Mark)
### 2. 缂栫爜涓嶄竴鑷?
### 2. 编码不一致
- UTF-8 with BOM vs UTF-8 without BOM
- Windows (CRLF) vs Unix (LF) 琛屽熬绗?
- Windows (CRLF) vs Unix (LF) 行尾符
- 不同编辑器的默认编码设置
## 鉁?瑙喅鏂规<E98F82>
## ✅ 解决方案
### 鏂规<EFBFBD>涓€锛氶」鐩<EFBFBD>厤缃<EFBFBD>枃浠?
### 方案一:项目配置文件
#### 1. `.editorconfig`锛堝凡鍒涘缓锛?
#### 1. `.editorconfig`(已创建)
<EFBFBD>繚鎵€鏈夌紪杈戝櫒缁熶竴浣跨敤锛?
确保所有编辑器统一使用:
- 编码UTF-8 without BOM
- 行尾符LF
- 缂╄繘锛?绌烘牸
- 缩进2空格
#### 2. `.gitattributes`锛堝凡鍒涘缓锛?
#### 2. `.gitattributes`(已创建)
闃叉<EFBFBD> Git <EFBFBD>姩杞<EFBFBD>崲琛屽熬绗﹀<EFBFBD>鑷寸殑闂<EFBFBD><EFBFBD>銆?
防止 Git 自动转换行尾符导致的问题。
### 鏂规<EFBFBD>浜岋細VSCode 宸ヤ綔鍖鸿<EFBFBD>缃?
### 方案二:VSCode 工作区设置
鍦ㄩ」鐩<EFBFBD>牴鐩<EFBFBD>綍鍒涘缓 `.vscode/settings.json`锛?
在项目根目录创建 `.vscode/settings.json`
```json
{
@@ -56,23 +56,23 @@ Get-Content file.ts -Raw -Encoding UTF8 |
### 方案三:团队规范
#### 寮哄埗瑙勫垯锛?
#### 强制规则:
1. **禁止使用 PowerShell 批量修改代码文件**
- 改用 Node.js 脚本
- 浣跨敤 IDE 鐨勯噸鏋勫姛鑳?
- 使用 IDE 的重构功能
2. **所有代码文件必须:**
- UTF-8 without BOM
- LF 琛屽熬绗?
- 鏂囦欢鏈<EFBFBD>熬鏈夌┖琛?
- LF 行尾符
- 文件末尾有空行
3. **批量操作前必须:**
- Git 鎻愪氦褰撳墠鐘舵€?
- 鍦ㄧ嫭绔嬪垎鏀<EFBFBD>搷浣?
- 閫愭<EFBFBD>楠岃瘉锛岃€岄潪涓€娆℃€у叏閮ㄤ慨鏀?
- Git 提交当前状态
- 在独立分支操作
- 逐步验证,而非一次性全部修改
## 馃洜锔?瀹夊叏鐨勬壒閲忎慨鏀规柟妗?
## 🛠️ 安全的批量修改方案
### 使用 Node.js 脚本(推荐)
@@ -85,7 +85,7 @@ function safeReplace(filePath, searchValue, replaceValue) {
// 使用 UTF-8 读取
let content = fs.readFileSync(filePath, 'utf8');
// <EFBFBD><EFBFBD>闇€瑕佷慨鏀?
// 确认需要修改
if (!content.includes(searchValue)) {
return false;
}
@@ -96,19 +96,19 @@ function safeReplace(filePath, searchValue, replaceValue) {
replaceValue
);
// 鍐欏洖锛堜繚鎸佸師鏈夎<EFBFBD>灏剧<EFBFBD>锛?
// 写回(保持原有行尾符)
fs.writeFileSync(filePath, newContent, 'utf8');
console.log(`鉁?淇<>敼鎴愬姛: ${path.basename(filePath)}`);
console.log(`✅ 修改成功: ${path.basename(filePath)}`);
return true;
} catch (error) {
console.error(`鉂?淇<>敼澶辫触: ${filePath}`, error.message);
console.error(`❌ 修改失败: ${filePath}`, error.message);
return false;
}
}
```
### 浣跨敤 TypeScript 缂栬緫鍣?API锛堟渶瀹夊叏锛?
### 使用 TypeScript 编辑器 API最安全
```typescript
// 使用 ts-morph 库进行安全的代码重构
@@ -150,29 +150,29 @@ git restore . # 或从手动备份恢复
### 4. 验证
```bash
# 缂栬瘧妫€鏌?
# 编译检查
npm run build
# 启动测试
npm run dev
```
## 馃摑 妫€鏌ユ竻鍗?
## 📝 检查清单
在提交代码前,确保:
- [ ] 鎵€鏈?`.ts` 鏂囦欢缂栫爜涓?UTF-8
- [ ] 所有 `.ts` 文件编码为 UTF-8
- [ ] 没有 BOM 标记
- [ ] 浣跨敤 LF 琛屽熬绗?
- [ ] 娌℃湁涔辩爜瀛楃<EFBFBD>锛堬拷锛?
- [ ] 使用 LF 行尾符
- [ ] 没有乱码字符<EFBFBD>
- [ ] 字符串都正确闭合
- [ ] 注释没有和代码行合并
- [ ] 缂栬瘧閫氳繃锛坄npm run build`锛?
- [ ] 鏈嶅姟鍣ㄥ彲浠ュ惎鍔<EFBFBD>`npm run dev`锛?
- [ ] 编译通过(`npm run build`
- [ ] 服务器可以启动(`npm run dev`
## 🔧 实用工具命令
### 妫€娴嬫枃浠剁紪鐮?
### 检测文件编码
```bash
file -bi <filename> # Linux/Mac
```
@@ -182,11 +182,11 @@ file -bi <filename> # Linux/Mac
iconv -f GBK -t UTF-8 file.txt > file_utf8.txt
```
### 妫€鏌?BOM
### 检查 BOM
```bash
# Linux/Mac
head -c 3 file.txt | od -A n -t x1
# 濡傛灉杈撳嚭 ef bb bf锛屽垯鏈?BOM
# 如果输出 ef bb bf,则有 BOM
```
### 移除 BOM
@@ -197,16 +197,16 @@ sed -i '1s/^\xEF\xBB\xBF//' file.txt
## 💡 经验教训
1. **鏋舵瀯浼樺厛锛屽伐鍏峰叾娆?*
- 鐨勬灦鏋勮<EFBFBD>璁″彲浠ュ噺灏戞壒閲忎慨鏀圭殑闇€姹?
1. **架构优先,工具其次**
- 正确的架构设计可以减少批量修改的需求
2. **灏忔<EFBFBD><EFBFBD>窇锛岄<EFBFBD>绻侀獙璇?*
- 姣忎慨鏀逛竴涓<EFBFBD>枃浠跺氨娴嬭瘯涓€娆?
- 涓嶈<EFBFBD>涓€娆℃€т慨鏀规墍鏈夋枃浠?
2. **小步快跑,频繁验证**
- 每修改一个文件就测试一次
- 不要一次性修改所有文件
3. **<EFBFBD>姩鍖栨祴璇曟槸鏈€鍚庨槻绾?*
- 缂栬瘧妫€鏌?
- Linter 妫€鏌?
3. **自动化测试是最后防线**
- 编译检查
- Linter 检查
- 单元测试
4. **备份是安全的保障**
@@ -214,7 +214,7 @@ sed -i '1s/^\xEF\xBB\xBF//' file.txt
- 手动备份关键目录
- 云端同步
## 馃摎 鍙傝€冭祫鏂?
## 📚 参考资料
- [EditorConfig 官方文档](https://editorconfig.org/)
- [Git Attributes 文档](https://git-scm.com/docs/gitattributes)
@@ -223,10 +223,10 @@ sed -i '1s/^\xEF\xBB\xBF//' file.txt
---
**鏂囨。鐗堟湰锛?* 1.0
**鍒涘缓鏃ユ湡锛?* 2025-11-14
**文档版本:** 1.0
**创建日期:** 2025-11-14
**最后更新:** 2025-11-14
**缁存姢浜哄憳锛?* AI Assistant
**维护人员:** AI Assistant