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

3 months ago
3 months ago
1 month ago
3 months ago
4 weeks ago
4 weeks ago
4 weeks ago
1 month ago
3 months ago
1 month ago
3 months ago
1 month ago
3 months ago
1 month ago
3 months ago
1 month ago
3 months ago
1 month ago
3 months ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. implement2 "code.fnuoos.com/zhimeng/model.git/src/super/implement"
  8. "errors"
  9. "github.com/gin-gonic/gin"
  10. "strings"
  11. )
  12. func GetUser(c *gin.Context) *model.Admin {
  13. user, _ := c.Get("admin")
  14. if user == nil {
  15. return &model.Admin{
  16. AdmId: 0,
  17. Username: "",
  18. Password: "",
  19. State: 0,
  20. IsSuperAdministrator: 0,
  21. Memo: "",
  22. CreateAt: "",
  23. UpdateAt: "",
  24. }
  25. }
  26. return user.(*model.Admin)
  27. }
  28. func CheckUser(c *gin.Context) (*model.Admin, string, error) {
  29. token := c.GetHeader("Authorization")
  30. if token == "" {
  31. //TODO::兼容open
  32. appSecret := c.GetHeader("AppSecret")
  33. if appSecret != "" {
  34. userListDb := implement2.NewUserListDb(db.Db)
  35. userList, err := userListDb.GetUserListByOpenAppSecret(appSecret)
  36. if err != nil {
  37. return nil, "", err
  38. }
  39. if userList != nil {
  40. // 获取admin
  41. adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
  42. admin, err := adminDb.GetAdmin(1001)
  43. if err != nil {
  44. return nil, "", err
  45. }
  46. c.Set("is_open", true)
  47. return admin, "", nil
  48. }
  49. }
  50. return nil, "", errors.New("token not exist")
  51. }
  52. // 按空格分割
  53. parts := strings.SplitN(token, " ", 2)
  54. if !(len(parts) == 2 && parts[0] == "Bearer") {
  55. return nil, "", errors.New("token format error")
  56. }
  57. // parts[1]是获取到的tokenString,我们使用之前定义好的解析JWT的函数来解析它
  58. mc, newToken, err := auth.ParseToken(parts[1])
  59. if err != nil {
  60. return nil, "", err
  61. }
  62. // 获取admin
  63. adminDb := implement.NewAdminDb(db.DBs[GetMasterId(c)])
  64. admin, err := adminDb.GetAdmin(mc.AdmId)
  65. if err != nil {
  66. return nil, "", err
  67. }
  68. return admin, newToken, nil
  69. }