蛋蛋星球-客户端
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

80 wiersze
2.1 KiB

  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. userInfoProvider := provider.NewUserInfoProvider()
  60. userInfo, err := userInfoProvider.GetUserInfo(mc.Uid)
  61. if err != nil {
  62. return user, newToken, err
  63. }
  64. userInfo, err = userInfoProvider.GetUserInfo(mc.Uid)
  65. if err != nil {
  66. return user, newToken, err
  67. }
  68. // 4、检验账号是否未激活或被冻结
  69. if userInfo.State == int(enum.UserStateForFreeze) {
  70. return user, newToken, errors.New(e.MsgFlags[e.ERR_USER_IS_BAN])
  71. }
  72. return userInfo, newToken, nil
  73. }