base_auth_v2.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 BaseAuthV2Controller struct {
  13. web.Controller
  14. Lang string `description:"当前语言类型,中文:zh;英文:en;默认:zh"`
  15. }
  16. func (c *BaseAuthV2Controller) 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. authorization := c.Ctx.Input.Header("authorization")
  32. if authorization == "" {
  33. c.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
  34. c.StopRun()
  35. return
  36. }
  37. checkAuthorization := utils.MD5(utils.APP_NAME_EN + utils.Md5Key)
  38. fmt.Println(checkAuthorization)
  39. if authorization != checkAuthorization {
  40. c.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
  41. c.StopRun()
  42. return
  43. }
  44. } else {
  45. c.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  46. c.StopRun()
  47. return
  48. }
  49. }
  50. }
  51. func (c *BaseAuthV2Controller) ServeJSON(encoding ...bool) {
  52. var (
  53. hasIndent = false
  54. hasEncoding = false
  55. )
  56. if web.BConfig.RunMode == web.PROD {
  57. hasIndent = false
  58. }
  59. if len(encoding) > 0 && encoding[0] == true {
  60. hasEncoding = true
  61. }
  62. if c.Data["json"] == nil {
  63. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  64. return
  65. }
  66. baseRes := c.Data["json"].(*models.BaseResponse)
  67. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  68. body, _ := json.Marshal(baseRes)
  69. var requestBody string
  70. method := c.Ctx.Input.Method()
  71. if method == "GET" {
  72. requestBody = c.Ctx.Request.RequestURI
  73. } else {
  74. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  75. }
  76. 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)
  77. }
  78. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  79. }
  80. func (c *BaseAuthV2Controller) JSON(data interface{}, hasIndent bool, coding bool) error {
  81. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  82. var content []byte
  83. var err error
  84. if hasIndent {
  85. content, err = json.MarshalIndent(data, "", " ")
  86. } else {
  87. content, err = json.Marshal(data)
  88. }
  89. if err != nil {
  90. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  91. return err
  92. }
  93. ip := c.Ctx.Input.IP()
  94. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  95. if err != nil {
  96. requestBody = string(c.Ctx.Input.RequestBody)
  97. }
  98. if requestBody == "" {
  99. requestBody = c.Ctx.Input.URI()
  100. }
  101. c.logUri(content, requestBody, ip)
  102. if coding {
  103. content = []byte(utils.StringsToJSON(string(content)))
  104. }
  105. return c.Ctx.Output.Body(content)
  106. }
  107. func (c *BaseAuthV2Controller) logUri(content []byte, requestBody, ip string) {
  108. authorization := ""
  109. method := c.Ctx.Input.Method()
  110. uri := c.Ctx.Input.URI()
  111. fmt.Println("Url:", uri)
  112. if method != "HEAD" {
  113. if method == "POST" || method == "GET" {
  114. authorization = c.Ctx.Input.Header("authorization")
  115. if authorization == "" {
  116. authorization = c.Ctx.Input.Header("Authorization")
  117. }
  118. if authorization == "" {
  119. newAuthorization := c.GetString("authorization")
  120. if newAuthorization != `` {
  121. authorization = "authorization=" + newAuthorization
  122. } else {
  123. newAuthorization = c.GetString("Authorization")
  124. authorization = "authorization=" + newAuthorization
  125. }
  126. } else {
  127. if strings.Contains(authorization, ";") {
  128. authorization = strings.Replace(authorization, ";", "$", 1)
  129. }
  130. }
  131. if authorization == "" {
  132. strArr := strings.Split(uri, "?")
  133. for k, v := range strArr {
  134. fmt.Println(k, v)
  135. }
  136. if len(strArr) > 1 {
  137. authorization = strArr[1]
  138. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  139. fmt.Println(authorization)
  140. }
  141. }
  142. }
  143. }
  144. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  145. return
  146. }