golang 的 rabbitmq 消费项目
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.
 
 
 

74 line
1.6 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/md"
  5. "applet/app/utils"
  6. "errors"
  7. "strings"
  8. "github.com/gin-gonic/gin"
  9. )
  10. // 因为在mw_auth已经做完所有校验, 因此在此不再做任何校验
  11. //GetUser is get user model
  12. func GetUser(c *gin.Context) *md.User {
  13. user, _ := c.Get("user")
  14. return user.(*md.User)
  15. }
  16. func GetUid(c *gin.Context) string {
  17. user, _ := c.Get("user")
  18. u := user.(*md.User)
  19. return utils.IntToStr(u.Info.Uid)
  20. }
  21. func CheckUser(c *gin.Context) (*md.User, error) {
  22. token := c.GetHeader("Authorization")
  23. if token == "" {
  24. return nil, errors.New("token not exist")
  25. }
  26. // 按空格分割
  27. parts := strings.SplitN(token, " ", 2)
  28. if !(len(parts) == 2 && parts[0] == "Bearer") {
  29. return nil, errors.New("token format error")
  30. }
  31. // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  32. mc, err := utils.ParseToken(parts[1])
  33. if err != nil {
  34. return nil, err
  35. }
  36. // 获取user
  37. u, err := db.UserFindByID(db.DBs[c.GetString("mid")], mc.UID)
  38. if err != nil {
  39. return nil, err
  40. }
  41. if u == nil {
  42. return nil, errors.New("token can not find user")
  43. }
  44. // 获取user profile
  45. up, err := db.UserProfileFindByID(db.DBs[c.GetString("mid")], mc.UID)
  46. if err != nil {
  47. return nil, err
  48. }
  49. // 获取user 等级
  50. ul, err := db.UserLevelByID(db.DBs[c.GetString("mid")], u.Level)
  51. if err != nil {
  52. return nil, err
  53. }
  54. // 获取用户标签
  55. tags, err := db.UserTagsByUid(db.DBs[c.GetString("mid")], mc.UID)
  56. if err != nil {
  57. return nil, err
  58. }
  59. user := &md.User{
  60. Info: u,
  61. Profile: up,
  62. Level: ul,
  63. Tags: tags,
  64. }
  65. return user, nil
  66. }