蛋蛋星球 后台端
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

1 kuukausi sitten
1 kuukausi sitten
1 kuukausi sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. Memo: "",
  22. CreateAt: time.Now().Format("2006-01-02 15:04:05"),
  23. UpdateAt: time.Now().Format("2006-01-02 15:04:05"),
  24. }
  25. }
  26. return admin.(*model.Admin)
  27. }
  28. func CheckUser(c *gin.Context) (*model.Admin, error) {
  29. token := c.GetHeader("Authorization")
  30. if token == "" {
  31. return nil, errors.New("token not exist")
  32. }
  33. // 按空格分割
  34. parts := strings.SplitN(token, " ", 2)
  35. if !(len(parts) == 2 && parts[0] == "Bearer") {
  36. return nil, errors.New("token format error")
  37. }
  38. // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  39. mc, err := auth.ParseToken(parts[1])
  40. if err != nil {
  41. return nil, err
  42. }
  43. // 获取admin
  44. adminDb := implement.NewAdminDb(db.Db)
  45. user, err := adminDb.GetAdmin(mc.AdmId)
  46. if err != nil {
  47. return nil, err
  48. }
  49. return user, nil
  50. }