base_common.go 4.1 KB

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