8 changed files with 51 additions and 114 deletions
-
9link_homework/internal/cmd/cmd.go
-
4link_homework/internal/controller/auth/login.go
-
33link_homework/internal/controller/member.go
-
29link_homework/internal/logic/login/login.go
-
43link_homework/internal/logic/login/token.go
-
17link_homework/internal/service/login.go
-
8link_homework/manifest/config/config.yaml
-
22link_homework/utility/utility.go
@ -1,33 +0,0 @@ |
|||||
package member |
|
||||
|
|
||||
import ( |
|
||||
"github.com/gogf/gf/v2/net/ghttp" |
|
||||
"link_homework/internal/model/dto" |
|
||||
"link_homework/utility" |
|
||||
) |
|
||||
|
|
||||
// MemberController 处理与成员相关的操作
|
|
||||
type MemberController struct{} |
|
||||
|
|
||||
// GetJwcode 获取用户的 jwcode
|
|
||||
// MemberController.GetJwcode
|
|
||||
func (c *MemberController) GetJwcode(r *ghttp.Request) { |
|
||||
// 获取 token 参数
|
|
||||
token := r.Get("token").String() |
|
||||
if token == "" { |
|
||||
r.Response.WriteJsonExit(dto.Error("Token is required")) |
|
||||
return |
|
||||
} |
|
||||
|
|
||||
// 调用 GetJwcodeJSON 函数获取 jwcode
|
|
||||
result := utility.GetJwcodeJSON(token) |
|
||||
if result.Code != 200 { |
|
||||
r.Response.WriteJsonExit(result) // 直接返回错误结果
|
|
||||
return |
|
||||
} |
|
||||
|
|
||||
// 成功返回 jwcode
|
|
||||
r.Response.WriteJsonExit(dto.SuccessWithData(map[string]interface{}{ |
|
||||
"jwcode": result.Data, |
|
||||
})) |
|
||||
} |
|
@ -1,43 +0,0 @@ |
|||||
package login |
|
||||
|
|
||||
import ( |
|
||||
"context" |
|
||||
"errors" |
|
||||
"github.com/golang-jwt/jwt/v4" |
|
||||
"link_homework/internal/service" |
|
||||
"log" |
|
||||
) |
|
||||
|
|
||||
type sTokenLogic struct{} |
|
||||
|
|
||||
func NewTokenLogic() *sTokenLogic { |
|
||||
return &sTokenLogic{} |
|
||||
} |
|
||||
|
|
||||
func init() { |
|
||||
service.RegisterTokenLogic(&sTokenLogic{}) |
|
||||
} |
|
||||
|
|
||||
// ValidateToken 验证 JWT Token 的方法
|
|
||||
func (t *sTokenLogic) ValidateToken(ctx context.Context, tokenString string) (bool, error) { |
|
||||
// 解析 token
|
|
||||
token, err := jwt.ParseWithClaims(tokenString, &CustomClaims{}, func(token *jwt.Token) (interface{}, error) { |
|
||||
return SecretKey, nil |
|
||||
}) |
|
||||
|
|
||||
if err != nil { |
|
||||
return false, errors.New("Token 无效: " + err.Error()) |
|
||||
} |
|
||||
|
|
||||
// 如果 token 有效,返回 true
|
|
||||
if token.Valid { |
|
||||
// 提取 CustomClaims 并打印 username
|
|
||||
if claims, ok := token.Claims.(*CustomClaims); ok { |
|
||||
log.Printf("解析到的用户名: %s", claims.Username) // 打印解析到的用户名
|
|
||||
} |
|
||||
return true, nil |
|
||||
} |
|
||||
|
|
||||
// 如果 token 无效
|
|
||||
return false, errors.New("Token 验证失败") |
|
||||
} |
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue