base_not_auth.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "hongze/hongze_api/services"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  9. "github.com/beego/beego/v2/server/web"
  10. "hongze/hongze_api/models"
  11. "hongze/hongze_api/utils"
  12. )
  13. type BaseNotAuthController struct {
  14. web.Controller
  15. User *models.WxUserItem
  16. Token string
  17. }
  18. func (this *BaseNotAuthController) Prepare() {
  19. var requestBody string
  20. method := this.Ctx.Input.Method()
  21. if method == "GET" {
  22. requestBody = this.Ctx.Request.RequestURI
  23. } else {
  24. requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
  25. }
  26. ip := this.Ctx.Input.IP()
  27. apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
  28. authorization := this.Ctx.Input.Header("Authorization")
  29. if authorization == "" {
  30. cookie := this.Ctx.GetCookie("rddp_access_token")
  31. utils.FileLog.Info("authorization:%s,cookie:%s", authorization, cookie)
  32. authorization = cookie
  33. }
  34. uri := this.Ctx.Input.URI()
  35. utils.FileLog.Info("URI:%s", uri)
  36. if strings.Contains(uri, "/api/wechat/login") {
  37. authorization = ""
  38. }
  39. if authorization != "" {
  40. session, err := models.GetSessionByToken(authorization)
  41. if err != nil {
  42. if err.Error() == utils.ErrNoRow() {
  43. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
  44. this.StopRun()
  45. return
  46. }
  47. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  48. this.StopRun()
  49. return
  50. }
  51. if session == nil {
  52. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
  53. this.StopRun()
  54. return
  55. }
  56. //wxUser, err := models.GetWxUserItemByUserId(session.UserId)
  57. wxUser, err := services.GetWxUserItemByOpenId(session.OpenId)
  58. if err != nil {
  59. //没有找到记录
  60. if err.Error() == utils.ErrNoRow() {
  61. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  62. this.StopRun()
  63. return
  64. }
  65. //用户openid没有绑定用户
  66. if err != services.ERR_USER_NOT_BIND {
  67. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  68. this.StopRun()
  69. return
  70. }
  71. }
  72. if wxUser == nil {
  73. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  74. this.StopRun()
  75. return
  76. }
  77. this.User = wxUser
  78. }
  79. this.Token = authorization
  80. }
  81. func (c *BaseNotAuthController) ServeJSON(encoding ...bool) {
  82. var (
  83. hasIndent = false
  84. hasEncoding = false
  85. )
  86. if web.BConfig.RunMode == web.PROD {
  87. hasIndent = false
  88. }
  89. if len(encoding) > 0 && encoding[0] == true {
  90. hasEncoding = true
  91. }
  92. if c.Data["json"] == nil {
  93. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  94. return
  95. }
  96. baseRes := c.Data["json"].(*models.BaseResponse)
  97. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  98. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  99. }
  100. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  101. }
  102. func (c *BaseNotAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  103. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  104. var content []byte
  105. var err error
  106. if hasIndent {
  107. content, err = json.MarshalIndent(data, "", " ")
  108. } else {
  109. content, err = json.Marshal(data)
  110. }
  111. if err != nil {
  112. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  113. return err
  114. }
  115. ip := c.Ctx.Input.IP()
  116. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  117. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  118. if coding {
  119. content = []byte(utils.StringsToJSON(string(content)))
  120. }
  121. // 数据加密
  122. if services.CheckEncryption() {
  123. content = utils.DesBase64Encrypt(content)
  124. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  125. content = []byte(`"` + string(content) + `"`)
  126. }
  127. return c.Ctx.Output.Body(content)
  128. }