base_common.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hongze_clpt/models"
  7. "hongze/hongze_clpt/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. ip := this.Ctx.Input.IP()
  39. utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", this.Ctx.Input.URI(), requestBody, ip)
  40. }
  41. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  42. var (
  43. hasIndent = false
  44. hasEncoding = false
  45. )
  46. if web.BConfig.RunMode == web.PROD {
  47. hasIndent = false
  48. }
  49. if len(encoding) > 0 && encoding[0] == true {
  50. hasEncoding = true
  51. }
  52. if c.Data["json"] == nil {
  53. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  54. return
  55. }
  56. baseRes := c.Data["json"].(*models.BaseResponse)
  57. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  58. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  59. }
  60. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  61. }
  62. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  63. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  64. var content []byte
  65. var err error
  66. if hasIndent {
  67. content, err = json.MarshalIndent(data, "", " ")
  68. } else {
  69. content, err = json.Marshal(data)
  70. }
  71. if err != nil {
  72. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  73. return err
  74. }
  75. ip := c.Ctx.Input.IP()
  76. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  77. c.logUri(content, requestBody, ip)
  78. //apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  79. if coding {
  80. content = []byte(utils.StringsToJSON(string(content)))
  81. }
  82. return c.Ctx.Output.Body(content)
  83. }
  84. func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
  85. authorization := ""
  86. method := c.Ctx.Input.Method()
  87. uri := c.Ctx.Input.URI()
  88. //fmt.Println("Url:", uri)
  89. if method != "HEAD" {
  90. if method == "POST" || method == "GET" {
  91. authorization = c.Ctx.Input.Header("authorization")
  92. if authorization == "" {
  93. authorization = c.Ctx.Input.Header("Authorization")
  94. }
  95. if authorization == "" {
  96. newAuthorization := c.GetString("authorization")
  97. if newAuthorization != `` {
  98. authorization = "authorization=" + newAuthorization
  99. } else {
  100. newAuthorization = c.GetString("Authorization")
  101. authorization = "authorization=" + newAuthorization
  102. }
  103. } else {
  104. if strings.Contains(authorization, ";") {
  105. authorization = strings.Replace(authorization, ";", "$", 1)
  106. }
  107. }
  108. if authorization == "" {
  109. strArr := strings.Split(uri, "?")
  110. for k, v := range strArr {
  111. fmt.Println(k, v)
  112. }
  113. if len(strArr) > 1 {
  114. authorization = strArr[1]
  115. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  116. fmt.Println(authorization)
  117. }
  118. }
  119. }
  120. }
  121. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  122. return
  123. }