广告平台(站长下代理使用)
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

56 rindas
1.2 KiB

  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/db/implement"
  5. "applet/app/db/model"
  6. "applet/app/lib/auth"
  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. OpenId: "",
  24. UnionId: "",
  25. }
  26. }
  27. return user.(*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.DBs[GetMasterId(c)])
  46. admin, err := adminDb.GetAdmin(mc.AdmId)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return admin, nil
  51. }