广告平台(媒体使用)
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 1.3 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package svc
  2. import (
  3. "applet/app/lib/auth"
  4. db "code.fnuoos.com/zhimeng/model.git/src"
  5. "code.fnuoos.com/zhimeng/model.git/src/implement"
  6. "code.fnuoos.com/zhimeng/model.git/src/model"
  7. "errors"
  8. "github.com/gin-gonic/gin"
  9. "strings"
  10. )
  11. func GetUser(c *gin.Context) *model.Medium {
  12. user, _ := c.Get("admin")
  13. if user == nil {
  14. return &model.Medium{
  15. Id: 0,
  16. Username: "",
  17. Password: "",
  18. State: 0,
  19. IsSuperAdministrator: 0,
  20. Memo: "",
  21. CreateAt: "",
  22. UpdateAt: "",
  23. }
  24. }
  25. return user.(*model.Medium)
  26. }
  27. func CheckUser(c *gin.Context) (*model.Medium, error) {
  28. token := c.GetHeader("Authorization")
  29. if token == "" {
  30. return nil, errors.New("token not exist")
  31. }
  32. // 按空格分割
  33. parts := strings.SplitN(token, " ", 2)
  34. if !(len(parts) == 2 && parts[0] == "Bearer") {
  35. return nil, errors.New("token format error")
  36. }
  37. // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  38. mc, err := auth.ParseToken(parts[1])
  39. if err != nil {
  40. return nil, err
  41. }
  42. // 获取admin
  43. adminDb := implement.NewMediumDb(db.DBs[GetMasterId(c)])
  44. admin, err := adminDb.GetMedium(mc.AdmId)
  45. if err != nil {
  46. return nil, err
  47. }
  48. return admin, nil
  49. }