init
This commit is contained in:
70
pkg/util/172/api_result.go
Normal file
70
pkg/util/172/api_result.go
Normal file
@ -0,0 +1,70 @@
|
||||
package _72
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ApiResponse 定义响应体的结构体
|
||||
type ApiResponse[T any] struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Errs *string `json:"errs"`
|
||||
Data T `json:"data"`
|
||||
}
|
||||
|
||||
// 优化后的 generateMD5Sign 方法
|
||||
func generateMD5SignV2(params interface{}, secret string) string {
|
||||
// 获取结构体中的字段信息
|
||||
var strBuilder strings.Builder
|
||||
|
||||
// 获取参数类型
|
||||
v := reflect.ValueOf(params)
|
||||
|
||||
// 遍历结构体中的字段
|
||||
for i := 0; i < v.NumField(); i++ {
|
||||
// 获取字段名和值
|
||||
field := v.Type().Field(i)
|
||||
jsonTag := field.Tag.Get("json") // 获取 json 标签
|
||||
value := v.Field(i)
|
||||
// 如果字段是嵌套结构体,跳过
|
||||
if value.Kind() == reflect.Struct {
|
||||
continue
|
||||
}
|
||||
// 获取字段值
|
||||
fieldValue := value.String()
|
||||
|
||||
// 排除 user_id 和空字符串字段
|
||||
if field.Name != "UserID" && field.Name != "UserSign" {
|
||||
// 拼接字段名和值
|
||||
strBuilder.WriteString(fmt.Sprintf("%s=%s&", jsonTag, fieldValue))
|
||||
}
|
||||
}
|
||||
|
||||
// 使用反射获取字段并拼接
|
||||
timestamp := v.FieldByName("Timestamp")
|
||||
if timestamp.IsValid() {
|
||||
timestampTag, _ := v.Type().FieldByName("Timestamp")
|
||||
strBuilder.WriteString(fmt.Sprintf("%s=%s&", timestampTag.Tag.Get("json"), timestamp.String()))
|
||||
}
|
||||
userID := v.FieldByName("UserID")
|
||||
if userID.IsValid() {
|
||||
userIDTag, _ := v.Type().FieldByName("UserID")
|
||||
strBuilder.WriteString(fmt.Sprintf("%s=%s", userIDTag.Tag.Get("json"), userID.String()))
|
||||
}
|
||||
|
||||
// 拼接 secret 到最后
|
||||
strBuilder.WriteString(secret)
|
||||
|
||||
// 获取拼接后的字符串
|
||||
concatenatedStr := strBuilder.String()
|
||||
|
||||
fmt.Println(concatenatedStr)
|
||||
// 计算 MD5 哈希
|
||||
hash := md5.New()
|
||||
hash.Write([]byte(concatenatedStr))
|
||||
return hex.EncodeToString(hash.Sum(nil))
|
||||
}
|
Reference in New Issue
Block a user