正在显示
1 个修改的文件
包含
8 行增加
和
34 行删除
@@ -2,52 +2,26 @@ package tool | @@ -2,52 +2,26 @@ package tool | ||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | jwt "github.com/golang-jwt/jwt/v4" | 4 | jwt "github.com/golang-jwt/jwt/v4" |
5 | - "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/config" | ||
6 | "time" | 5 | "time" |
7 | ) | 6 | ) |
8 | 7 | ||
9 | type UserToken struct { | 8 | type UserToken struct { |
10 | - UserId int64 `json:"userId"` | ||
11 | - CoachId int64 `json:"coach_id"` | ||
12 | - AdminId int64 `json:"adminId"` | ||
13 | - ClientType string `json:"clientType"` | ||
14 | - AccessShops []int64 `json:"accessShops"` | 9 | + UserId int64 `json:"userId"` |
10 | + AdminId int64 `json:"adminId"` | ||
11 | + CompanyId int64 `json:"companyId"` | ||
12 | + ClientType string `json:"clientType"` | ||
15 | } | 13 | } |
16 | 14 | ||
17 | -func (tk UserToken) GenerateToken(jwtConfig config.JwtAuth) (string, error) { | 15 | +func (tk UserToken) GenerateToken(secret string, expire int64) (string, error) { |
18 | claims := make(jwt.MapClaims) | 16 | claims := make(jwt.MapClaims) |
19 | - claims["exp"] = time.Now().Unix() + jwtConfig.Expire | 17 | + claims["exp"] = time.Now().Unix() + expire |
20 | claims["iat"] = time.Now().Unix() | 18 | claims["iat"] = time.Now().Unix() |
21 | claims["UserId"] = tk.UserId | 19 | claims["UserId"] = tk.UserId |
22 | - claims["CoachId"] = tk.CoachId | ||
23 | claims["AdminId"] = tk.AdminId | 20 | claims["AdminId"] = tk.AdminId |
21 | + claims["CompanyId"] = tk.CompanyId | ||
24 | claims["ClientType"] = tk.ClientType | 22 | claims["ClientType"] = tk.ClientType |
25 | - claims["AccessShops"] = tk.AccessShops | ||
26 | token := jwt.New(jwt.SigningMethodHS256) | 23 | token := jwt.New(jwt.SigningMethodHS256) |
27 | token.Claims = claims | 24 | token.Claims = claims |
28 | 25 | ||
29 | - return token.SignedString([]byte(jwtConfig.AccessSecret)) | ||
30 | -} | ||
31 | - | ||
32 | -func (tk *UserToken) ParseToken(jwtConfig config.JWT, str string) error { | ||
33 | - //tokenClaims, err := jwt.ParseWithClaims( | ||
34 | - // str, | ||
35 | - // tk, | ||
36 | - // func(token *jwt.Token) (interface{}, error) { | ||
37 | - // return []byte(jwtConfig.Secret), nil | ||
38 | - // }) | ||
39 | - //if err != nil { | ||
40 | - // return err | ||
41 | - //} | ||
42 | - //if claim, ok := tokenClaims.Claims.(*UserToken); ok && tokenClaims.Valid { | ||
43 | - // *tk = *claim | ||
44 | - // return nil | ||
45 | - //} | ||
46 | - //return errors.New("token 解析失败") | ||
47 | - return nil | ||
48 | -} | ||
49 | - | ||
50 | -// CheckUserInfo 如果UserToken有效 返回:true 否则返回false | ||
51 | -func (tk *UserToken) CheckUserInfo() bool { | ||
52 | - return !(tk.UserId > 100000000 || tk.UserId <= 0) | 26 | + return token.SignedString([]byte(secret)) |
53 | } | 27 | } |
-
请 注册 或 登录 后发表评论