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

68 regels
1.7 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. //TODO::兼容open
  31. appSecret := c.GetHeader("AppSecret")
  32. if appSecret != "" {
  33. //TODO::暂时给激活鸟写死
  34. if appSecret == "2F125D59EE826535D7E84E407A13C107" {
  35. // 获取admin
  36. adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
  37. admin, err := adminDb.GetAdmin(1001)
  38. if err != nil {
  39. return nil, "", err
  40. }
  41. return admin, "", nil
  42. }
  43. }
  44. return nil, "", errors.New("token not exist")
  45. }
  46. // 按空格分割
  47. parts := strings.SplitN(token, " ", 2)
  48. if !(len(parts) == 2 && parts[0] == "Bearer") {
  49. return nil, "", errors.New("token format error")
  50. }
  51. // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  52. mc, newToken, err := auth.ParseToken(parts[1])
  53. if err != nil {
  54. return nil, "", err
  55. }
  56. // 获取admin
  57. adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
  58. admin, err := adminDb.GetAdmin(mc.AdmId)
  59. if err != nil {
  60. return nil, "", err
  61. }
  62. return admin, newToken, nil
  63. }