This commit is contained in:
oneao committed 2025-11-06 17:32:51 +08:00
1 parent 6849579652
commit d60be4a75b
24 files changed
+463 -179

No files matched your search

@@ -15,9 +15,9 @@ type CustomClaims struct {
// TokenResult 用于返回验证结果
type TokenResult struct {
Claims *CustomClaims
Valid bool
Expired bool
Claims *CustomClaims
IsValid bool
IsExpired bool
}
// CreateAccessToken 创建 Access Token,只返回 token 和 error
@@ -61,7 +61,7 @@ func createToken(userID int, expiry time.Duration) (string, error) {
// VerifyToken 验证 token 并返回结构化结果
func VerifyToken(tokenString string) TokenResult {
result := TokenResult{Valid: false}
result := TokenResult{IsValid: false, IsExpired: false}
token, _ := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return []byte(config.App.JWT.Secret), nil
@@ -73,9 +73,9 @@ func VerifyToken(tokenString string) TokenResult {
}
result.Claims = claims
result.Valid = true
result.IsValid = true
if claims.ExpiresAt != nil && time.Now().After(claims.ExpiresAt.Time) {
result.Expired = true
result.IsExpired = true
}
return result
@@ -36,25 +36,25 @@ func NotEmpty(data map[string]interface{}, fields ...string) error {
// collectErrors 递归收集字段错误
func collectErrors(v interface{}, path string, errs map[string]string) {
if v == nil {
errs[path] = fmt.Sprintf("字段 %s 不能为空", path)
errs[path] = "字段不能为空"
return
}
switch val := v.(type) {
case string:
if strings.TrimSpace(val) == "" {
errs[path] = fmt.Sprintf("字段 %s 不能为空", path)
errs[path] = "字段不能为空"
}
case []interface{}:
if len(val) == 0 {
errs[path] = fmt.Sprintf("字段 %s 不能为空", path)
errs[path] = "字段不能为空"
}
for i, elem := range val {
collectErrors(elem, fmt.Sprintf("%s[%d]", path, i), errs)
}
case map[string]interface{}:
if len(val) == 0 {
errs[path] = fmt.Sprintf("字段 %s 不能为空", path)
errs[path] = "字段不能为空"
}
for k, elem := range val {
collectErrors(elem, fmt.Sprintf("%s.%s", path, k), errs)