Browse Source

add: 添加用户列表开放接口

zoie 3 months ago
parent
commit
04665648d1
5 changed files with 105 additions and 3 deletions
  1. 2 1
      conf/app.conf
  2. 3 0
      conf/config.go
  3. 14 1
      controllers/Account.go
  4. 4 1
      models/Account/Admin.go
  5. 82 0
      routers/openapi.go

+ 2 - 1
conf/app.conf

@@ -40,4 +40,5 @@ BoosUuid = "fa5be10f-5be1-42be-a8dc-8d142e006266"
 # 冷链验证 报告负责人
 VdelUuid = "1c13436c-4511-4030-8b26-baadad88445a"
 
-
+OpenApi_Key = "coldverify"
+OpenApi_Secret = "H3L9OPQR2VX8ZZYN7STKFG5JMWB1CV4D"

+ 3 - 0
conf/config.go

@@ -40,3 +40,6 @@ var WechatNews_GroupName, _ = beego.AppConfig.String("WechatNews_GroupName")
 var WechatNews_Url, _ = beego.AppConfig.String("WechatNews_Url")
 var BoosUuid, _ = beego.AppConfig.String("BoosUuid")
 var VdelUuid, _ = beego.AppConfig.String("VdelUuid")
+
+var OpenApi_Key, _ = beego.AppConfig.String("OpenApi_Key")
+var OpenApi_Secret, _ = beego.AppConfig.String("OpenApi_Secret")

+ 14 - 1
controllers/Account.go

@@ -349,7 +349,7 @@ func (c *AccountController) List_All() {
 	PowerMap := Account.UserPowerListToPowerMap(PowerList)
 
 	var r_jsons lib.R_JSONS
-	r_jsons.List = Account.Read_Admin_List_ALL_Power(User_r.T_Distributor_id, PowerMap)
+	r_jsons.List = Account.Read_Admin_List_ALL_Power(User_r.T_Distributor_id, "", PowerMap)
 	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
 	c.ServeJSON()
 	return
@@ -389,3 +389,16 @@ func (c *AccountController) UpPassword() {
 	c.ServeJSON()
 	return
 }
+
+// 获取管理员 关联ERP
+func (c *AccountController) List_All_For_ERP() {
+
+	PowerList := Account.Read_Power_List_ALL_1()
+	PowerMap := Account.UserPowerListToPowerMap(PowerList)
+	T_name := c.GetString("T_name")
+	var r_jsons lib.R_JSONS
+	r_jsons.List = Account.Read_Admin_List_ALL_Power("", T_name, PowerMap)
+	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
+	c.ServeJSON()
+	return
+}

+ 4 - 1
models/Account/Admin.go

@@ -181,7 +181,7 @@ func Read_Admin_List_ALL_1() (maps []Admin) {
 }
 
 // 获取全部列表
-func Read_Admin_List_ALL_Power(T_Distributor_id string, PowerMaps map[int]string) (maps []AdminPower_R) {
+func Read_Admin_List_ALL_Power(T_Distributor_id string, T_name string,PowerMaps map[int]string) (maps []AdminPower_R) {
 
 	o := orm.NewOrm()
 	var r []Admin
@@ -191,6 +191,9 @@ func Read_Admin_List_ALL_Power(T_Distributor_id string, PowerMaps map[int]string
 	} else {
 		qs = qs.Filter("T_Distributor_id", T_Distributor_id)
 	}
+	if len(T_name) > 0 {
+		qs = qs.Filter("T_name__icontains", T_name)
+	}
 	qs.Filter("T_State", 1).OrderBy("Id").All(&r)
 	// 转换
 	var Admin_Power_r []AdminPower_R

+ 82 - 0
routers/openapi.go

@@ -0,0 +1,82 @@
+package routers
+
+import (
+	"ColdVerify_server/conf"
+	"ColdVerify_server/controllers"
+	"ColdVerify_server/lib"
+	"ColdVerify_server/logs"
+	"crypto/hmac"
+	"crypto/sha256"
+	"encoding/hex"
+	"errors"
+	beego "github.com/beego/beego/v2/server/web"
+	"github.com/beego/beego/v2/server/web/context"
+	"strconv"
+	"time"
+)
+
+const apiKeyHeader = "X-API-KEY"
+const apiSignatureHeader = "X-API-SIGNATURE"
+const apiTimestampHeader = "X-API-TIMESTAMP"
+
+func isValidSignature(apiKey, signature, timestamp string) bool {
+	// 使用提供的 API Key 查找对应的 API Secret
+	if apiKey != conf.OpenApi_Key {
+		return false
+	}
+	secret := conf.OpenApi_Secret
+
+	// 计算签名,签名内容是 "apiKey + timestamp"
+	message := apiKey + timestamp
+	mac := hmac.New(sha256.New, []byte(secret))
+	mac.Write([]byte(message))
+	expectedSignature := hex.EncodeToString(mac.Sum(nil))
+	logs.Println("apiKey:", apiKey)
+	logs.Println("secret:", secret)
+	logs.Println("时间戳:", timestamp)
+	logs.Println("生成的签名:", expectedSignature)
+	logs.Println("传递的签名:", signature)
+	// 验证客户端提供的签名是否与预期签名匹配
+	return hmac.Equal([]byte(signature), []byte(expectedSignature))
+}
+func ApiKeyAuthMiddleware(ctx *context.Context) {
+	apiKey := ctx.Input.Query(apiKeyHeader)
+	signature := ctx.Input.Query(apiSignatureHeader)
+	timestamp := ctx.Input.Query(apiTimestampHeader)
+
+	// 检查 API Key, 签名和时间戳是否存在
+	if apiKey == "" || signature == "" || timestamp == "" {
+		err := errors.New("API Key, Signature, and Timestamp required")
+		data := lib.JSONS{Code: 202, Msg: err.Error()}
+		ctx.Output.JSON(data, true, false)
+		return
+	}
+
+	// 校验请求的签名是否有效
+	if !isValidSignature(apiKey, signature, timestamp) {
+		err := errors.New("Invalid Signature")
+		data := lib.JSONS{Code: 202, Msg: err.Error()}
+		ctx.Output.JSON(data, true, false)
+		return
+	}
+
+	// 校验时间戳是否在合理范围内(防止重放攻击)
+	sec, _ := strconv.ParseInt(timestamp, 10, 64)
+	reqTime := time.Unix(sec, 0)
+	if time.Since(reqTime) > 5*time.Minute {
+		err := errors.New("Request too old or invalid timestamp")
+		data := lib.JSONS{Code: 202, Msg: err.Error()}
+		ctx.Output.JSON(data, true, false)
+		return
+	}
+}
+
+func init() {
+	//beego.InsertFilter("/openapi", beego.BeforeRouter, ApiKeyAuthMiddleware)
+
+	ns := beego.NewNamespace("/openapi",
+		beego.NSBefore(ApiKeyAuthMiddleware),
+		beego.NSRouter("/user/list", &controllers.AccountController{}, "*:List_All_For_ERP"), // 管理员所有列表
+	)
+	beego.AddNamespace(ns)
+}