base_auth.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package controllers
  2. import (
  3. "fmt"
  4. "hongze/hongze_mobile_admin/models/custom"
  5. "hongze/hongze_mobile_admin/models/tables/h5_admin_session"
  6. "hongze/hongze_mobile_admin/services"
  7. "hongze/hongze_mobile_admin/utils"
  8. "net/url"
  9. "strconv"
  10. "strings"
  11. )
  12. // BaseAuth 需要授权token的基类
  13. type BaseAuth struct {
  14. BaseCommon
  15. AdminWx *custom.AdminWx `description:"管理员信息"`
  16. Token string `description:"用户token"`
  17. Session *h5_admin_session.H5AdminSession `description:"用户session"`
  18. }
  19. func (c *BaseAuth) Prepare() {
  20. var requestBody string
  21. method := c.Ctx.Input.Method()
  22. if method == "GET" {
  23. requestBody = c.Ctx.Request.RequestURI
  24. } else {
  25. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  26. }
  27. authorization := c.Ctx.Input.Header("Authorization")
  28. fmt.Println("requestBody:", requestBody)
  29. ip := c.Ctx.Input.IP()
  30. fmt.Println("ip:", ip)
  31. //apiLog.Println("请求地址:", c.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip, ";authorization:", authorization)
  32. //if authorization == "" {
  33. // cookie := c.Ctx.GetCookie("rddp_access_token")
  34. // utils.FileLog.Info("authorization:%s,cookie:%s", authorization, cookie)
  35. // authorization = cookie
  36. //}
  37. uri := c.Ctx.Input.URI()
  38. utils.FileLog.Info("URI:%s", uri)
  39. //if strings.Contains(uri, "/h5adminapi/wechat/login") {
  40. // authorization = ""
  41. //}
  42. if authorization == "" {
  43. c.TokenMsgError("请求异常,没有携带token!", "请求异常,没有携带token ")
  44. return
  45. }
  46. session, err := h5_admin_session.GetSessionByToken(authorization)
  47. if err != nil {
  48. if err.Error() == utils.ErrNoRow() {
  49. c.TokenMsgError("信息已变更,请重新登陆!", "Token 信息已变更:Token: "+authorization)
  50. return
  51. }
  52. c.TokenMsgError("网络异常,请稍后重试!", "获取用户信息异常,Err:"+err.Error())
  53. return
  54. }
  55. if session == nil {
  56. c.TokenMsgError("网络异常,请稍后重试!", "session is empty")
  57. return
  58. }
  59. c.Session = session
  60. var adminWx *custom.AdminWx
  61. if session.OpenId != "" {
  62. tmpAdminWx, tmpErr := services.GetAdminUserItemByOpenId(session.OpenId)
  63. adminWx = tmpAdminWx
  64. err = tmpErr
  65. } else {
  66. c.TokenMsgError("数据异常!", "session is empty")
  67. return
  68. }
  69. //wxUser, err := models.GetWxUserItemByUserId(session.UserId)
  70. //wxUser, err := services.GetWxUserItemByOpenId(session.OpenId)
  71. if err != nil {
  72. //没有找到记录
  73. if err.Error() == utils.ErrNoRow() {
  74. c.TokenMsgError("信息已变更,请重新登陆!", "获取admin 信息失败 "+strconv.Itoa(session.AdminId))
  75. return
  76. }
  77. //错误信息,账户未绑定,跳转到绑定页面
  78. if err == services.ERR_ADMIN_NOT_BIND {
  79. //如果不是登录接口
  80. if !strings.Contains(uri, "/h5adminapi/admin/login") {
  81. c.BindMsgError("账户未绑定!", "获取admin_wx信息异常,Err:"+err.Error())
  82. return
  83. }
  84. } else {
  85. //其他错误信息
  86. c.TokenMsgError("网络异常,请稍后重试!", "获取admin_wx信息异常,Err:"+err.Error())
  87. return
  88. }
  89. }
  90. if adminWx == nil {
  91. c.TokenMsgError("网络异常,请稍后重试!", "admin is empty")
  92. return
  93. }
  94. if adminWx.AdminId > 0 && adminWx.Enabled != 1 {
  95. c.BindMsgError("账号已被禁用,请联系管理员!", "账号已被禁用,强制退出")
  96. return
  97. }
  98. c.AdminWx = adminWx
  99. c.Token = authorization
  100. }