base_common.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_mini_crm_ht/models"
  5. "eta/eta_mini_crm_ht/utils"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "github.com/beego/beego/v2/server/web"
  11. )
  12. type BaseCommonController struct {
  13. web.Controller
  14. }
  15. func (c *BaseCommonController) Prepare() {
  16. var requestBody string
  17. method := c.Ctx.Input.Method()
  18. if method == "GET" {
  19. requestBody = c.Ctx.Request.RequestURI
  20. } else {
  21. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  22. }
  23. ip := c.Ctx.Input.IP()
  24. utils.ApiLog.Info("uri:%s, requestBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  25. }
  26. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  27. var (
  28. hasIndent = false
  29. hasEncoding = false
  30. )
  31. if web.BConfig.RunMode == web.PROD {
  32. hasIndent = false
  33. }
  34. if len(encoding) > 0 && encoding[0] == true {
  35. hasEncoding = true
  36. }
  37. if c.Data["json"] == nil {
  38. utils.ApiLog.Notice("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值")
  39. return
  40. }
  41. baseRes := c.Data["json"].(*models.BaseResponse)
  42. if baseRes != nil && !baseRes.Success {
  43. utils.ApiLog.Info("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg)
  44. }
  45. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  46. }
  47. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  48. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  49. desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  50. c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  51. // 设置Cookie为HTTPOnly
  52. c.Ctx.SetCookie("", "", -1, "/", "", false, true, "")
  53. var content []byte
  54. var err error
  55. if hasIndent {
  56. content, err = json.MarshalIndent(data, "", " ")
  57. } else {
  58. content, err = json.Marshal(data)
  59. }
  60. if err != nil {
  61. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  62. return err
  63. }
  64. ip := c.Ctx.Input.IP()
  65. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  66. c.logUri(content, requestBody, ip)
  67. params := c.Ctx.Input.Params()
  68. fmt.Println("params")
  69. fmt.Println(params)
  70. // 如果不是debug分支的话,那么需要加密返回
  71. if utils.RunMode != "debug" {
  72. content = utils.DesBase64Encrypt(content, utils.DesKey)
  73. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  74. content = []byte(`"` + string(content) + `"`)
  75. }
  76. if coding {
  77. content = []byte(utils.StringsToJSON(string(content)))
  78. }
  79. return c.Ctx.Output.Body(content)
  80. }
  81. func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
  82. authorization := ""
  83. method := c.Ctx.Input.Method()
  84. uri := c.Ctx.Input.URI()
  85. if method != "HEAD" {
  86. if method == "POST" || method == "GET" {
  87. authorization = c.Ctx.Input.Header("authorization")
  88. if authorization == "" {
  89. authorization = c.Ctx.Input.Header("Authorization")
  90. }
  91. if authorization == "" {
  92. newAuthorization := c.GetString("authorization")
  93. if newAuthorization != `` {
  94. authorization = "authorization=" + newAuthorization
  95. } else {
  96. newAuthorization = c.GetString("Authorization")
  97. authorization = "authorization=" + newAuthorization
  98. }
  99. } else {
  100. if strings.Contains(authorization, ";") {
  101. authorization = strings.Replace(authorization, ";", "$", 1)
  102. }
  103. }
  104. if authorization == "" {
  105. strArr := strings.Split(uri, "?")
  106. for k, v := range strArr {
  107. fmt.Println(k, v)
  108. }
  109. if len(strArr) > 1 {
  110. authorization = strArr[1]
  111. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  112. fmt.Println(authorization)
  113. }
  114. }
  115. }
  116. }
  117. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  118. return
  119. }