package svc import ( "applet/app/e" "applet/app/lib/auth" "applet/app/provider" "code.fnuoos.com/EggPlanet/egg_models.git/src/model" "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum" "errors" "github.com/gin-gonic/gin" "strings" "time" ) func GetUser(c *gin.Context) *model.User { user, _ := c.Get("user") if user == nil { return &model.User{ Id: 0, Phone: "", UnionId: "", OpenId: "", Nickname: "", Avatar: "", Password: "", Passcode: "", Level: 0, InviteTotal: 0, State: 0, LastLoginIp: "", Sex: 0, ParentUid: 0, SystemInviteCode: "", CustomInviteCode: "", Memo: "", IsRealName: 0, RegisterType: 0, CreateAt: time.Now().Format("2006-01-02 15:04:05"), UpdateAt: time.Now().Format("2006-01-02 15:04:05"), } } return user.(*model.User) } func CheckUser(c *gin.Context) (user *model.User, newToken string, err error) { token := c.GetHeader("Authorization") if token == "" { return user, newToken, errors.New("token not exist") } // 1、按空格分割 parts := strings.SplitN(token, " ", 2) if !(len(parts) == 2 && parts[0] == "Bearer") { return user, newToken, errors.New("token format error") } // 2、parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它 mc, newToken, err := auth.ParseToken(parts[1]) if err != nil { return user, newToken, err } // 3、获取用户信息 userInfoProvider := provider.GetUserInfoProvider() userInfo, err := userInfoProvider.GetUserInfo(mc.Uid) if err != nil { return user, newToken, err } // 4、检验账号是否未激活或被冻结 if userInfo.State == int(enum.UserStateForFreeze) { return user, newToken, errors.New(e.MsgFlags[e.ERR_USER_IS_BAN]) } return userInfo, newToken, nil }