base_common.go 4.1 KB

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