base_auth.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "github.com/beego/beego/v2/server/web"
  8. "hongze/hongze_edb_lib/models"
  9. "hongze/hongze_edb_lib/utils"
  10. "github.com/rdlucklib/rdluck_tools/log"
  11. )
  12. var apiLog *log.Log
  13. func init() {
  14. if utils.RunMode == "release" {
  15. logDir := `/data/rdlucklog/` + utils.APP_NAME_EN
  16. apiLog = log.Init("20060102.api", logDir)
  17. } else {
  18. apiLog = log.Init("20060102.api")
  19. }
  20. }
  21. type BaseAuthController struct {
  22. web.Controller
  23. }
  24. func (this *BaseAuthController) Prepare() {
  25. fmt.Println("enter prepare")
  26. method := this.Ctx.Input.Method()
  27. uri := this.Ctx.Input.URI()
  28. fmt.Println("Url:", uri)
  29. if method != "HEAD" {
  30. if method == "POST" {
  31. authorization := this.Ctx.Input.Header("authorization")
  32. if authorization == "" {
  33. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:authorization is empty "}, false, false)
  34. this.StopRun()
  35. return
  36. }
  37. checkAuthorization := utils.MD5(utils.APP_NAME_EN + utils.Md5Key)
  38. if authorization != checkAuthorization {
  39. this.JSON(models.BaseResponse{Ret: 408, Msg: "签名错误!", ErrMsg: "签名错误:authorization is err "}, false, false)
  40. this.StopRun()
  41. return
  42. }
  43. } else {
  44. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  45. this.StopRun()
  46. return
  47. }
  48. } else {
  49. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "method:" + method}, false, false)
  50. this.StopRun()
  51. return
  52. }
  53. }
  54. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  55. var (
  56. hasIndent = false
  57. hasEncoding = false
  58. )
  59. if web.BConfig.RunMode == web.PROD {
  60. hasIndent = false
  61. }
  62. if len(encoding) > 0 && encoding[0] == true {
  63. hasEncoding = true
  64. }
  65. if c.Data["json"] == nil {
  66. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  67. return
  68. }
  69. baseRes := c.Data["json"].(*models.BaseResponse)
  70. if baseRes != nil && baseRes.Ret != 408 {
  71. body, _ := json.Marshal(baseRes)
  72. var requestBody string
  73. method := c.Ctx.Input.Method()
  74. if method == "GET" {
  75. requestBody = c.Ctx.Request.RequestURI
  76. } else {
  77. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  78. }
  79. if baseRes.Ret != 200 && baseRes.IsSendEmail {
  80. go utils.SendEmail(utils.APP_NAME_CN+"【"+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)
  81. }
  82. }
  83. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  84. }
  85. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  86. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  87. var content []byte
  88. var err error
  89. if hasIndent {
  90. content, err = json.MarshalIndent(data, "", " ")
  91. } else {
  92. content, err = json.Marshal(data)
  93. }
  94. if err != nil {
  95. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  96. return err
  97. }
  98. ip := c.Ctx.Input.IP()
  99. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  100. if err != nil {
  101. requestBody = string(c.Ctx.Input.RequestBody)
  102. }
  103. if requestBody == "" {
  104. requestBody = c.Ctx.Input.URI()
  105. }
  106. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  107. if coding {
  108. content = []byte(utils.StringsToJSON(string(content)))
  109. }
  110. return c.Ctx.Output.Body(content)
  111. }