base_common.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hongze_web_mfyx/models"
  7. "hongze/hongze_web_mfyx/utils"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. )
  12. type BaseCommonController struct {
  13. web.Controller
  14. //User *models.WxUserItem
  15. Token string
  16. }
  17. func (this *BaseCommonController) Prepare() {
  18. var requestBody string
  19. method := this.Ctx.Input.Method()
  20. if method == "GET" {
  21. requestBody = this.Ctx.Request.RequestURI
  22. } else {
  23. requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
  24. }
  25. ip := this.Ctx.Input.IP()
  26. //apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
  27. authorization := this.Ctx.Input.Header("Authorization")
  28. if authorization == "" {
  29. cookie := this.Ctx.GetCookie("rddp_access_token")
  30. //utils.FileLog.Info("authorization:%s,cookie:%s", authorization, cookie)
  31. authorization = cookie
  32. }
  33. uri := this.Ctx.Input.URI()
  34. //utils.FileLog.Info("URI:%s", uri)
  35. if strings.Contains(uri, "/api/wechat/login") {
  36. authorization = ""
  37. }
  38. utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", this.Ctx.Input.URI(), requestBody, ip)
  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. //if err != nil {
  58. // if err.Error() == utils.ErrNoRow() {
  59. // this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  60. // this.StopRun()
  61. // return
  62. // }
  63. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取wx_user信息异常,Eerr:" + err.Error()}, false, false)
  64. // this.StopRun()
  65. // return
  66. //}
  67. //if wxUser == nil {
  68. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  69. // this.StopRun()
  70. // return
  71. //}
  72. //this.User = wxUser
  73. //}
  74. //this.Token = authorization
  75. }
  76. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  77. var (
  78. hasIndent = false
  79. hasEncoding = false
  80. )
  81. if web.BConfig.RunMode == web.PROD {
  82. hasIndent = false
  83. }
  84. if len(encoding) > 0 && encoding[0] == true {
  85. hasEncoding = true
  86. }
  87. if c.Data["json"] == nil {
  88. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  89. return
  90. }
  91. baseRes := c.Data["json"].(*models.BaseResponse)
  92. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  93. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  94. }
  95. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  96. }
  97. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  98. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  99. var content []byte
  100. var err error
  101. if hasIndent {
  102. content, err = json.MarshalIndent(data, "", " ")
  103. } else {
  104. content, err = json.Marshal(data)
  105. }
  106. if err != nil {
  107. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  108. return err
  109. }
  110. ip := c.Ctx.Input.IP()
  111. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  112. //apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  113. if coding {
  114. content = []byte(utils.StringsToJSON(string(content)))
  115. }
  116. c.logUri(content, requestBody, ip)
  117. return c.Ctx.Output.Body(content)
  118. }
  119. func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
  120. authorization := ""
  121. method := c.Ctx.Input.Method()
  122. uri := c.Ctx.Input.URI()
  123. //fmt.Println("Url:", uri)
  124. if method != "HEAD" {
  125. if method == "POST" || method == "GET" {
  126. authorization = c.Ctx.Input.Header("authorization")
  127. if authorization == "" {
  128. authorization = c.Ctx.Input.Header("Authorization")
  129. }
  130. if authorization == "" {
  131. newAuthorization := c.GetString("authorization")
  132. if newAuthorization != `` {
  133. authorization = "authorization=" + newAuthorization
  134. } else {
  135. newAuthorization = c.GetString("Authorization")
  136. authorization = "authorization=" + newAuthorization
  137. }
  138. } else {
  139. if strings.Contains(authorization, ";") {
  140. authorization = strings.Replace(authorization, ";", "$", 1)
  141. }
  142. }
  143. if authorization == "" {
  144. strArr := strings.Split(uri, "?")
  145. for k, v := range strArr {
  146. fmt.Println(k, v)
  147. }
  148. if len(strArr) > 1 {
  149. authorization = strArr[1]
  150. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  151. fmt.Println(authorization)
  152. }
  153. }
  154. }
  155. }
  156. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  157. return
  158. }