|
|
@@ -27,27 +27,27 @@ func GetUser(c *gin.Context) *model.Admin { |
|
|
|
return user.(*model.Admin) |
|
|
|
} |
|
|
|
|
|
|
|
func CheckUser(c *gin.Context) (*model.Admin, error) { |
|
|
|
func CheckUser(c *gin.Context) (*model.Admin, string, error) { |
|
|
|
token := c.GetHeader("Authorization") |
|
|
|
if token == "" { |
|
|
|
return nil, errors.New("token not exist") |
|
|
|
return nil, "", errors.New("token not exist") |
|
|
|
} |
|
|
|
// 按空格分割 |
|
|
|
parts := strings.SplitN(token, " ", 2) |
|
|
|
if !(len(parts) == 2 && parts[0] == "Bearer") { |
|
|
|
return nil, errors.New("token format error") |
|
|
|
return nil, "", errors.New("token format error") |
|
|
|
} |
|
|
|
// parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它 |
|
|
|
mc, err := auth.ParseToken(parts[1]) |
|
|
|
mc, newToken, err := auth.ParseToken(parts[1]) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, "", err |
|
|
|
} |
|
|
|
|
|
|
|
// 获取admin |
|
|
|
adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)]) |
|
|
|
admin, err := adminDb.GetAdmin(mc.AdmId) |
|
|
|
if err != nil { |
|
|
|
return nil, err |
|
|
|
return nil, "", err |
|
|
|
} |
|
|
|
return admin, nil |
|
|
|
return admin, newToken, nil |
|
|
|
} |