base_common.go 4.0 KB

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