base_auth.go 3.6 KB

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