From 719cd14269372e07840bc8be3f238129a3e1bad0 Mon Sep 17 00:00:00 2001 From: liangzai <2440983361@qq.com> Date: Sat, 2 Aug 2025 19:58:14 +0800 Subject: [PATCH] =?UTF-8?q?add=20=E5=90=88=E5=90=8C=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domains/user/entities/contract_info.go | 5 + scripts/README_admin_token.md | 93 +++++++++++ scripts/README_token_generator.md | 72 ++++++++ scripts/generate_admin_token_simple.go | 56 +++++++ scripts/init_product_api_configs.go | 157 ------------------ 5 files changed, 226 insertions(+), 157 deletions(-) create mode 100644 scripts/README_admin_token.md create mode 100644 scripts/README_token_generator.md create mode 100644 scripts/generate_admin_token_simple.go delete mode 100644 scripts/init_product_api_configs.go diff --git a/internal/domains/user/entities/contract_info.go b/internal/domains/user/entities/contract_info.go index c258543..b9ce8b8 100644 --- a/internal/domains/user/entities/contract_info.go +++ b/internal/domains/user/entities/contract_info.go @@ -13,6 +13,7 @@ type ContractType string const ( ContractTypeCooperation ContractType = "cooperation" // 合作协议 + ContractTypeReSign ContractType = "resign" // 补签协议 ) // ContractInfo 合同信息聚合根 @@ -228,6 +229,8 @@ func (c *ContractInfo) GetContractTypeName() string { switch c.ContractType { case ContractTypeCooperation: return "合作协议" + case ContractTypeReSign: + return "补签协议" default: return "未知类型" } @@ -265,6 +268,8 @@ func isValidContractType(contractType ContractType) bool { switch contractType { case ContractTypeCooperation: return true + case ContractTypeReSign: + return true default: return false } diff --git a/scripts/README_admin_token.md b/scripts/README_admin_token.md new file mode 100644 index 0000000..aba057b --- /dev/null +++ b/scripts/README_admin_token.md @@ -0,0 +1,93 @@ +# 管理员Token生成脚本使用说明 + +## 概述 + +`generate_admin_token_simple.go` 是一个用于生成管理员JWT token的脚本,方便管理员登录用户账号进行查看和调试。 + +## 功能特性 + +- 支持通过环境变量自定义用户信息 +- 自动加载项目配置文件 +- 生成标准的JWT token +- 提供详细的使用说明和示例 + +## 使用方法 + +### 直接运行 + +```bash +cd tyapi-server-gin +go run scripts/generate_admin_token_simple.go +``` + +### 固定用户信息 + +脚本中已预设了以下用户信息: +- 用户ID: `admin_user_001` +- 手机号: `13800138000` +- 邮箱: `admin@example.com` +- 用户类型: `admin` + +如需修改用户信息,请直接编辑脚本中的相应变量。 + +## 输出示例 + +``` +=== 管理员Token生成成功 === +用户ID: admin_user_001 +手机号: 13800138000 +邮箱: admin@example.com +用户类型: admin +过期时间: 24h0m0s + +=== Token === +eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... + +=== 使用说明 === +在API请求的Authorization头部中使用: +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... + +=== 示例curl命令 === +curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." http://localhost:8080/api/v1/user/profile +``` + +## 使用场景 + +1. **管理员调试**: 管理员需要以特定用户身份登录系统进行调试 +2. **用户问题排查**: 管理员需要查看特定用户的数据和操作 +3. **API测试**: 开发人员需要测试不同用户类型的API权限 +4. **系统维护**: 在系统维护时需要以特定用户身份执行操作 + +## 安全注意事项 + +⚠️ **重要提醒**: +- 此脚本仅用于开发和调试环境 +- 生产环境中请勿使用此脚本 +- 生成的token具有完整的用户权限,请妥善保管 +- 使用完毕后请及时删除或过期token + +## 编译为可执行文件 + +如果需要频繁使用,可以编译为可执行文件: + +```bash +# 编译 +go build -o bin/generate-admin-token scripts/generate_admin_token_simple.go + +# 使用 +./bin/generate-admin-token +``` + +## 故障排除 + +### 1. 配置文件找不到 +确保在项目根目录下运行脚本,或者确保 `config.yaml` 文件存在。 + +### 2. 权限错误 +确保脚本有读取配置文件的权限。 + +### 3. 依赖问题 +确保所有Go依赖都已正确安装: +```bash +go mod tidy +``` \ No newline at end of file diff --git a/scripts/README_token_generator.md b/scripts/README_token_generator.md new file mode 100644 index 0000000..ac4abca --- /dev/null +++ b/scripts/README_token_generator.md @@ -0,0 +1,72 @@ +# Token生成器使用说明 + +## 概述 + +为了方便管理员登录用户账号进行查看和调试,我们创建了一个简单的JWT token生成脚本。 + +## 文件说明 + +- `generate_admin_token_simple.go` - 生成管理员token的脚本 + +## 使用方法 + +### 生成管理员Token + +```bash +cd tyapi-server-gin +go run scripts/generate_admin_token_simple.go +``` + +### 预设用户信息 + +脚本中已预设了以下管理员信息: +- 用户ID: `admin_user_001` +- 手机号: `13800138000` +- 邮箱: `admin@example.com` +- 用户类型: `admin` + +### 输出示例 + +``` +=== 管理员Token生成成功 === +用户ID: admin_user_001 +手机号: 13800138000 +邮箱: admin@example.com +用户类型: admin +过期时间: 168h0m0s + +=== Token === +eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... + +=== 使用说明 === +在API请求的Authorization头部中使用: +Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9... + +=== 示例curl命令 === +curl -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." http://localhost:8080/api/v1/user/profile +``` + +## 使用场景 + +1. **管理员调试**: 管理员需要以特定用户身份登录系统进行调试 +2. **用户问题排查**: 管理员需要查看特定用户的数据和操作 +3. **API测试**: 开发人员需要测试不同用户类型的API权限 + +## 安全提醒 + +⚠️ **重要提醒**: +- 此脚本仅用于开发和调试环境 +- 生产环境中请勿使用此脚本 +- 生成的token具有完整的用户权限,请妥善保管 + +## 修改用户信息 + +如需修改用户信息,请直接编辑 `generate_admin_token_simple.go` 文件中的相应变量: + +```go +// 固定的用户信息 +userID := "admin_user_001" +phone := "13800138000" +email := "admin@example.com" +userType := "admin" +``` \ No newline at end of file diff --git a/scripts/generate_admin_token_simple.go b/scripts/generate_admin_token_simple.go new file mode 100644 index 0000000..dcb9af3 --- /dev/null +++ b/scripts/generate_admin_token_simple.go @@ -0,0 +1,56 @@ +package main + +import ( + "fmt" + "log" + + "tyapi-server/internal/config" + "tyapi-server/internal/shared/middleware" + + "go.uber.org/zap" +) + +func main() { + // 固定的用户信息 + userID := "8dd1036f-3555-4b04-95de-053baf2414e4" + phone := "15550440999" + email := "" + userType := "user" + + // 加载配置文件 + cfg, err := config.LoadConfig() + if err != nil { + log.Fatalf("加载配置文件失败: %v", err) + } + + // 创建logger + logger, err := zap.NewProduction() + if err != nil { + log.Fatalf("创建logger失败: %v", err) + } + defer logger.Sync() + + // 创建JWT认证中间件 + jwtAuth := middleware.NewJWTAuthMiddleware(cfg, logger) + + // 生成token + token, err := jwtAuth.GenerateToken(userID, phone, email, userType) + if err != nil { + log.Fatalf("生成token失败: %v", err) + } + + // 输出结果 + fmt.Println("=== 管理员Token生成成功 ===") + fmt.Printf("用户ID: %s\n", userID) + fmt.Printf("手机号: %s\n", phone) + fmt.Printf("邮箱: %s\n", email) + fmt.Printf("用户类型: %s\n", userType) + fmt.Printf("过期时间: %s\n", cfg.JWT.ExpiresIn.String()) + fmt.Println("\n=== Token ===") + fmt.Println(token) + fmt.Println("\n=== 使用说明 ===") + fmt.Println("在API请求的Authorization头部中使用:") + fmt.Printf("Authorization: Bearer %s\n", token) + fmt.Println("\n=== 示例curl命令 ===") + fmt.Printf("curl -H \"Authorization: Bearer %s\" http://localhost:8080/api/v1/user/profile\n", token) +} \ No newline at end of file diff --git a/scripts/init_product_api_configs.go b/scripts/init_product_api_configs.go deleted file mode 100644 index 44e8b1c..0000000 --- a/scripts/init_product_api_configs.go +++ /dev/null @@ -1,157 +0,0 @@ -package main - -import ( - "encoding/json" - "log" - "tyapi-server/internal/config" - "tyapi-server/internal/domains/product/entities" - "tyapi-server/internal/infrastructure/database" - - "github.com/google/uuid" -) - -func main() { - // 加载配置 - cfg, err := config.LoadConfig() - if err != nil { - log.Fatalf("加载配置失败: %v", err) - } - - // 连接数据库 - db, err := database.NewConnection(database.Config{ - Host: cfg.Database.Host, - Port: cfg.Database.Port, - User: cfg.Database.User, - Password: cfg.Database.Password, - Name: cfg.Database.Name, - SSLMode: cfg.Database.SSLMode, - Timezone: cfg.Database.Timezone, - MaxOpenConns: cfg.Database.MaxOpenConns, - MaxIdleConns: cfg.Database.MaxIdleConns, - ConnMaxLifetime: cfg.Database.ConnMaxLifetime, - }) - if err != nil { - log.Fatalf("连接数据库失败: %v", err) - } - - // 获取所有产品 - var products []entities.Product - if err := db.Find(&products).Error; err != nil { - log.Fatalf("获取产品失败: %v", err) - } - log.Printf("找到 %d 个产品", len(products)) - - for _, product := range products { - // 检查是否已有API配置 - var count int64 - db.Model(&entities.ProductApiConfig{}).Where("product_id = ?", product.ID).Count(&count) - if count > 0 { - log.Printf("产品已有API配置,跳过: %s (%s)", product.Name, product.Code) - continue - } - - // 创建默认请求参数 - requestParams := []entities.RequestParam{ - { - Name: "姓名", - Field: "name", - Type: "text", - Required: true, - Description: "用户真实姓名", - Example: "张三", - Validation: "^[\\u4e00-\\u9fa5]{2,4}$", - }, - { - Name: "身份证号", - Field: "id_card", - Type: "idcard", - Required: true, - Description: "用户身份证号码", - Example: "110101199001011234", - Validation: "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$", - }, - { - Name: "手机号", - Field: "mobile", - Type: "phone", - Required: true, - Description: "用户手机号码", - Example: "13800138000", - Validation: "^1[3-9]\\d{9}$", - }, - } - - // 创建默认响应字段 - responseFields := []entities.ResponseField{ - { - Name: "结果", - Path: "result", - Type: "string", - Required: true, - Description: "查询结果", - Example: "success", - }, - { - Name: "状态码", - Path: "code", - Type: "number", - Required: true, - Description: "响应状态码", - Example: "200", - }, - { - Name: "消息", - Path: "message", - Type: "string", - Required: true, - Description: "响应消息", - Example: "查询成功", - }, - { - Name: "数据", - Path: "data", - Type: "object", - Required: false, - Description: "查询结果数据", - Example: "{}", - }, - } - - // 创建默认响应示例 - responseExample := map[string]interface{}{ - "result": "success", - "code": 200, - "message": "查询成功", - "data": map[string]interface{}{ - "name": "张三", - "id_card": "110101199001011234", - "mobile": "13800138000", - "status": "正常", - "query_time": "2024-01-01 12:00:00", - }, - } - - // 序列化为JSON - requestParamsJSON, _ := json.Marshal(requestParams) - responseFieldsJSON, _ := json.Marshal(responseFields) - responseExampleJSON, _ := json.Marshal(responseExample) - - // 创建API配置 - apiConfig := entities.ProductApiConfig{ - ID: uuid.New().String(), - ProductID: product.ID, - RequestParams: string(requestParamsJSON), - ResponseFields: string(responseFieldsJSON), - ResponseExample: string(responseExampleJSON), - } - - // 保存到数据库 - if err := db.Create(&apiConfig).Error; err != nil { - log.Printf("创建产品API配置失败: %s, 错误: %v", product.Name, err) - } else { - log.Printf("成功创建产品API配置: %s (%s)", product.Name, product.Code) - } - } - - log.Println("产品API配置初始化完成") -} \ No newline at end of file