base_auth.go 5.2 KB

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