广告平台(站长使用)
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.
 
 
 
 
 

54 lines
1.3 KiB

  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.Admin {
  12. user, _ := c.Get("admin")
  13. if user == nil {
  14. return &model.Admin{
  15. AdmId: 0,
  16. Username: "",
  17. Password: "",
  18. State: 0,
  19. IsSuperAdministrator: 0,
  20. Memo: "",
  21. CreateAt: "",
  22. UpdateAt: "",
  23. }
  24. }
  25. return user.(*model.Admin)
  26. }
  27. func CheckUser(c *gin.Context) (*model.Admin, string, 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, newToken, err := auth.ParseToken(parts[1])
  39. if err != nil {
  40. return nil, "", err
  41. }
  42. // 获取admin
  43. adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
  44. admin, err := adminDb.GetAdmin(mc.AdmId)
  45. if err != nil {
  46. return nil, "", err
  47. }
  48. return admin, newToken, nil
  49. }