base_common.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_api/cache"
  5. "eta_gn/eta_api/models"
  6. "eta_gn/eta_api/services/alarm_msg"
  7. "eta_gn/eta_api/utils"
  8. "fmt"
  9. "github.com/beego/beego/v2/server/web"
  10. "net/http"
  11. "net/url"
  12. "strings"
  13. )
  14. type BaseCommonController struct {
  15. web.Controller
  16. Lang string `description:"当前语言类型,中文:zh;英文:en;默认:zh"`
  17. }
  18. func (c *BaseCommonController) Prepare() {
  19. var requestBody string
  20. method := c.Ctx.Input.Method()
  21. if method == "GET" {
  22. requestBody = c.Ctx.Request.RequestURI
  23. } else {
  24. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  25. }
  26. // 当前语言
  27. {
  28. lang := c.Ctx.Input.Header("Lang")
  29. if lang == "" {
  30. lang = utils.ZhLangVersion
  31. }
  32. c.Lang = lang
  33. }
  34. ip := c.Ctx.Input.IP()
  35. utils.ApiLog.Info("uri:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), requestBody, ip)
  36. }
  37. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  38. var (
  39. hasIndent = false
  40. hasEncoding = false
  41. )
  42. if web.BConfig.RunMode == web.PROD {
  43. hasIndent = false
  44. }
  45. if len(encoding) > 0 && encoding[0] == true {
  46. hasEncoding = true
  47. }
  48. if c.Data["json"] == nil {
  49. //go utils.SendEmail("异常提醒:"+utils.RunMode, "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  50. go alarm_msg.SendAlarmMsg("接口:URI:"+c.Ctx.Input.URI()+";无返回值", 3)
  51. return
  52. }
  53. baseRes := c.Data["json"].(*models.BaseResponse)
  54. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  55. //go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  56. go alarm_msg.SendAlarmMsg("失败提醒:URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, 3)
  57. }
  58. //新增uuid记录
  59. {
  60. urlPath := c.Ctx.Request.URL.Path
  61. if _, ok := AdminOperateRecordMap[urlPath]; !ok {
  62. var adminId int
  63. var realName, params, ip, uriStr string
  64. ip = c.Ctx.Input.IP()
  65. uuid := c.Ctx.Input.Header("Uuid")
  66. uri := c.Ctx.Input.URI()
  67. userAgent := c.Ctx.Input.Header("User-Agent")
  68. uriStr, _ = url.PathUnescape(uri)
  69. if uriStr != "" {
  70. uri = uriStr
  71. }
  72. if c.Ctx.Input.Method() == "GET" {
  73. paramsJson, _ := json.Marshal(c.Ctx.Input.Params())
  74. params = string(paramsJson)
  75. } else {
  76. params = string(c.Ctx.Input.RequestBody)
  77. }
  78. header := c.Ctx.Request.Header
  79. headerJson, _ := json.Marshal(header)
  80. headerStr := string(headerJson)
  81. go cache.AdminOperateRecord(adminId, realName, uuid, uri, params, ip, userAgent, headerStr)
  82. }
  83. }
  84. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  85. }
  86. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  87. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  88. desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  89. c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  90. // 设置Cookie为HTTPOnly
  91. c.Ctx.SetCookie("", "", -1, "/", "", false, true, "")
  92. var content []byte
  93. var err error
  94. if hasIndent {
  95. content, err = json.MarshalIndent(data, "", " ")
  96. } else {
  97. content, err = json.Marshal(data)
  98. }
  99. if err != nil {
  100. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  101. return err
  102. }
  103. ip := c.Ctx.Input.IP()
  104. params := c.Ctx.Input.Params()
  105. fmt.Println("params")
  106. fmt.Println(params)
  107. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  108. c.logUri(content, requestBody, ip)
  109. // 如果不是debug分支的话,那么需要加密返回
  110. if utils.RunMode != "debug" {
  111. content = utils.DesBase64Encrypt(content, utils.DesKey)
  112. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  113. content = []byte(`"` + string(content) + `"`)
  114. }
  115. if coding {
  116. content = []byte(utils.StringsToJSON(string(content)))
  117. }
  118. return c.Ctx.Output.Body(content)
  119. }
  120. func (c *BaseCommonController) logUri(respContent []byte, requestBody, ip string) {
  121. authorization := ""
  122. method := c.Ctx.Input.Method()
  123. uri := c.Ctx.Input.URI()
  124. //fmt.Println("Url:", uri)
  125. if method != "HEAD" {
  126. if method == "POST" || method == "GET" {
  127. authorization = c.Ctx.Input.Header("authorization")
  128. if authorization == "" {
  129. authorization = c.Ctx.Input.Header("Authorization")
  130. }
  131. if authorization == "" {
  132. newAuthorization := c.GetString("authorization")
  133. if newAuthorization != `` {
  134. authorization = "authorization=" + newAuthorization
  135. } else {
  136. newAuthorization = c.GetString("Authorization")
  137. authorization = "authorization=" + newAuthorization
  138. }
  139. } else {
  140. if strings.Contains(authorization, ";") {
  141. authorization = strings.Replace(authorization, ";", "$", 1)
  142. }
  143. }
  144. if authorization == "" {
  145. strArr := strings.Split(uri, "?")
  146. for k, v := range strArr {
  147. fmt.Println(k, v)
  148. }
  149. if len(strArr) > 1 {
  150. authorization = strArr[1]
  151. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  152. fmt.Println(authorization)
  153. }
  154. }
  155. }
  156. }
  157. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  158. return
  159. }