蛋蛋星球-客户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_auth.go 2.1 KiB

1 viikko sitten
1 viikko sitten
1 viikko sitten
4 päivää sitten
1 viikko sitten
5 päivää sitten
1 viikko sitten
1 viikko sitten
1 viikko sitten
1 viikko sitten
1 viikko sitten
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. userData := user.(*model.User)
  41. userData = UserImg(userData)
  42. return userData
  43. }
  44. func CheckUser(c *gin.Context) (user *model.User, newToken string, err error) {
  45. token := c.GetHeader("Authorization")
  46. if token == "" {
  47. return user, newToken, errors.New("token not exist")
  48. }
  49. // 1、按空格分割
  50. parts := strings.SplitN(token, " ", 2)
  51. if !(len(parts) == 2 && parts[0] == "Bearer") {
  52. return user, newToken, errors.New("token format error")
  53. }
  54. // 2、parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  55. mc, newToken, err := auth.ParseToken(parts[1])
  56. if err != nil {
  57. return user, newToken, err
  58. }
  59. // 3、获取用户信息
  60. //userInfoProvider := provider.GetUserInfoProvider()
  61. userInfoProvider := provider.NewUserInfoProvider()
  62. userInfo, err := userInfoProvider.GetUserInfo(mc.Uid)
  63. if err != nil {
  64. return user, newToken, err
  65. }
  66. userInfo, err = userInfoProvider.GetUserInfo(mc.Uid)
  67. if err != nil {
  68. return user, newToken, err
  69. }
  70. // 4、检验账号是否未激活或被冻结
  71. if userInfo.State == int(enum.UserStateForFreeze) {
  72. return user, newToken, errors.New(e.MsgFlags[e.ERR_USER_IS_BAN])
  73. }
  74. return userInfo, newToken, nil
  75. }