base_auth.go 5.6 KB

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