base_auth.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta_gn/eta_chart_lib/models"
  5. "eta_gn/eta_chart_lib/utils"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "github.com/beego/beego/v2/server/web"
  11. )
  12. type BaseAuthController struct {
  13. web.Controller
  14. Lang string `description:"当前语言类型,中文:zh;英文:en;默认:zh"`
  15. }
  16. func (c *BaseAuthController) Prepare() {
  17. fmt.Println("enter prepare")
  18. method := c.Ctx.Input.Method()
  19. uri := c.Ctx.Input.URI()
  20. ip := c.Ctx.Input.IP()
  21. fmt.Println("Url:", uri+";ip:"+ip)
  22. if method != "HEAD" {
  23. if method == "POST" || method == "GET" {
  24. {
  25. lang := c.Ctx.Input.Header("Lang")
  26. if lang == "" {
  27. lang = utils.ZhLangVersion
  28. }
  29. c.Lang = lang
  30. }
  31. } else {
  32. c.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  33. c.StopRun()
  34. return
  35. }
  36. }
  37. }
  38. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  39. var (
  40. hasIndent = false
  41. hasEncoding = false
  42. )
  43. if web.BConfig.RunMode == web.PROD {
  44. hasIndent = false
  45. }
  46. if len(encoding) > 0 && encoding[0] == true {
  47. hasEncoding = true
  48. }
  49. if c.Data["json"] == nil {
  50. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  51. return
  52. }
  53. baseRes := c.Data["json"].(*models.BaseResponse)
  54. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  55. body, _ := json.Marshal(baseRes)
  56. var requestBody string
  57. method := c.Ctx.Input.Method()
  58. if method == "GET" {
  59. requestBody = c.Ctx.Request.RequestURI
  60. } else {
  61. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  62. }
  63. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> "+"Params"+requestBody+" <br/>"+"ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body)+"<br/>", utils.EmailSendToUsers)
  64. }
  65. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  66. }
  67. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  68. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  69. var content []byte
  70. var err error
  71. if hasIndent {
  72. content, err = json.MarshalIndent(data, "", " ")
  73. } else {
  74. content, err = json.Marshal(data)
  75. }
  76. if err != nil {
  77. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  78. return err
  79. }
  80. ip := c.Ctx.Input.IP()
  81. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  82. if err != nil {
  83. requestBody = string(c.Ctx.Input.RequestBody)
  84. }
  85. if requestBody == "" {
  86. requestBody = c.Ctx.Input.URI()
  87. }
  88. c.logUri(content, requestBody, ip)
  89. if coding {
  90. content = []byte(utils.StringsToJSON(string(content)))
  91. }
  92. return c.Ctx.Output.Body(content)
  93. }
  94. func (c *BaseAuthController) logUri(content []byte, requestBody, ip string) {
  95. authorization := ""
  96. method := c.Ctx.Input.Method()
  97. uri := c.Ctx.Input.URI()
  98. fmt.Println("Url:", uri)
  99. if method != "HEAD" {
  100. if method == "POST" || method == "GET" {
  101. authorization = c.Ctx.Input.Header("authorization")
  102. if authorization == "" {
  103. authorization = c.Ctx.Input.Header("Authorization")
  104. }
  105. if authorization == "" {
  106. newAuthorization := c.GetString("authorization")
  107. if newAuthorization != `` {
  108. authorization = "authorization=" + newAuthorization
  109. } else {
  110. newAuthorization = c.GetString("Authorization")
  111. authorization = "authorization=" + newAuthorization
  112. }
  113. } else {
  114. if strings.Contains(authorization, ";") {
  115. authorization = strings.Replace(authorization, ";", "$", 1)
  116. }
  117. }
  118. if authorization == "" {
  119. strArr := strings.Split(uri, "?")
  120. for k, v := range strArr {
  121. fmt.Println(k, v)
  122. }
  123. if len(strArr) > 1 {
  124. authorization = strArr[1]
  125. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  126. fmt.Println(authorization)
  127. }
  128. }
  129. }
  130. }
  131. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  132. return
  133. }