base_pc_not_auth.go 4.7 KB

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