蛋蛋星球-客户端
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_auth.go 1.9 KiB

1 주 전
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. package svc
  2. import (
  3. "applet/app/e"
  4. "applet/app/lib/auth"
  5. "applet/app/provider"
  6. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  7. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  8. "errors"
  9. "github.com/gin-gonic/gin"
  10. "strings"
  11. "time"
  12. )
  13. func GetUser(c *gin.Context) *model.User {
  14. user, _ := c.Get("user")
  15. if user == nil {
  16. return &model.User{
  17. Id: 0,
  18. Phone: "",
  19. UnionId: "",
  20. OpenId: "",
  21. Nickname: "",
  22. Avatar: "",
  23. Password: "",
  24. Passcode: "",
  25. Level: 0,
  26. InviteTotal: 0,
  27. State: 0,
  28. LastLoginIp: "",
  29. Sex: 0,
  30. ParentUid: 0,
  31. SystemInviteCode: "",
  32. CustomInviteCode: "",
  33. Memo: "",
  34. IsRealName: 0,
  35. RegisterType: 0,
  36. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  37. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  38. }
  39. }
  40. return user.(*model.User)
  41. }
  42. func CheckUser(c *gin.Context) (user *model.User, newToken string, err error) {
  43. token := c.GetHeader("Authorization")
  44. if token == "" {
  45. return user, newToken, errors.New("token not exist")
  46. }
  47. // 1、按空格分割
  48. parts := strings.SplitN(token, " ", 2)
  49. if !(len(parts) == 2 && parts[0] == "Bearer") {
  50. return user, newToken, errors.New("token format error")
  51. }
  52. // 2、parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  53. mc, newToken, err := auth.ParseToken(parts[1])
  54. if err != nil {
  55. return user, newToken, err
  56. }
  57. // 3、获取用户信息
  58. userInfoProvider := provider.GetUserInfoProvider()
  59. userInfo, err := userInfoProvider.GetUserInfo(mc.Uid)
  60. if err != nil {
  61. return user, newToken, err
  62. }
  63. // 4、检验账号是否未激活或被冻结
  64. if userInfo.State == int(enum.UserStateForFreeze) {
  65. return user, newToken, errors.New(e.MsgFlags[e.ERR_USER_IS_BAN])
  66. }
  67. return userInfo, newToken, nil
  68. }