base_common.go 4.9 KB

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