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

svc_auth.go 1.4 KiB

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