base_common.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/beego/beego/v2/server/web"
  5. "hongze/hongze_cygx/models"
  6. "hongze/hongze_cygx/utils"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. )
  11. type BaseCommonController struct {
  12. web.Controller
  13. //User *models.WxUserItem
  14. Token string
  15. }
  16. func (this *BaseCommonController) Prepare() {
  17. var requestBody string
  18. method := this.Ctx.Input.Method()
  19. if method == "GET" {
  20. requestBody = this.Ctx.Request.RequestURI
  21. } else {
  22. requestBody, _ = url.QueryUnescape(string(this.Ctx.Input.RequestBody))
  23. }
  24. ip := this.Ctx.Input.IP()
  25. apiLog.Println("请求地址:", this.Ctx.Input.URI(), "RequestBody:", requestBody, "IP:", ip)
  26. authorization := this.Ctx.Input.Header("Authorization")
  27. if authorization == "" {
  28. cookie := this.Ctx.GetCookie("rddp_access_token")
  29. utils.FileLog.Info("authorization:%s,cookie:%s", authorization, cookie)
  30. authorization = cookie
  31. }
  32. uri := this.Ctx.Input.URI()
  33. utils.FileLog.Info("URI:%s", uri)
  34. if strings.Contains(uri, "/api/wechat/login") {
  35. authorization = ""
  36. }
  37. if authorization != "" {
  38. //session, err := models.GetSessionByToken(authorization)
  39. //if err != nil {
  40. // if err.Error() == utils.ErrNoRow() {
  41. // this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
  42. // this.StopRun()
  43. // return
  44. // }
  45. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  46. // this.StopRun()
  47. // return
  48. //}
  49. //if session == nil {
  50. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
  51. // this.StopRun()
  52. // return
  53. //}
  54. //wxUser, err := models.GetWxUserItemByUserId(session.UserId)
  55. //if err != nil {
  56. // if err.Error() == utils.ErrNoRow() {
  57. // this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  58. // this.StopRun()
  59. // return
  60. // }
  61. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取wx_user信息异常,Eerr:" + err.Error()}, false, false)
  62. // this.StopRun()
  63. // return
  64. //}
  65. //if wxUser == nil {
  66. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  67. // this.StopRun()
  68. // return
  69. //}
  70. //this.User = wxUser
  71. }
  72. //this.Token = authorization
  73. }
  74. func (c *BaseCommonController) ServeJSON(encoding ...bool) {
  75. var (
  76. hasIndent = false
  77. hasEncoding = false
  78. )
  79. if web.BConfig.RunMode == web.PROD {
  80. hasIndent = false
  81. }
  82. if len(encoding) > 0 && encoding[0] == true {
  83. hasEncoding = true
  84. }
  85. if c.Data["json"] == nil {
  86. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  87. return
  88. }
  89. baseRes := c.Data["json"].(*models.BaseResponse)
  90. if baseRes != nil && !baseRes.Success && baseRes.IsSendEmail {
  91. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", "URI:"+c.Ctx.Input.URI()+" ErrMsg:"+baseRes.ErrMsg+";Msg"+baseRes.Msg, utils.EmailSendToUsers)
  92. }
  93. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  94. }
  95. func (c *BaseCommonController) JSON(data interface{}, hasIndent bool, coding bool) error {
  96. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  97. var content []byte
  98. var err error
  99. if hasIndent {
  100. content, err = json.MarshalIndent(data, "", " ")
  101. } else {
  102. content, err = json.Marshal(data)
  103. }
  104. if err != nil {
  105. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  106. return err
  107. }
  108. ip := c.Ctx.Input.IP()
  109. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  110. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  111. if coding {
  112. content = []byte(utils.StringsToJSON(string(content)))
  113. }
  114. return c.Ctx.Output.Body(content)
  115. }