base_auth.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "github.com/astaxie/beego"
  9. "hongze/hongze_api/models"
  10. "hongze/hongze_api/utils"
  11. "rdluck_tools/log"
  12. )
  13. var apiLog *log.Log
  14. func init() {
  15. apiLog = log.Init("20060102.api")
  16. }
  17. type BaseAuthController struct {
  18. beego.Controller
  19. User *models.WxUserItem
  20. }
  21. func (this *BaseAuthController) Prepare() {
  22. fmt.Println("enter prepare")
  23. method := this.Ctx.Input.Method()
  24. uri := this.Ctx.Input.URI()
  25. fmt.Println("Url:", uri)
  26. if method != "HEAD" {
  27. if method == "POST" || method == "GET" {
  28. authorization := this.Ctx.Input.Header("Authorization")
  29. fmt.Println("authorization:", authorization)
  30. if authorization == "" {
  31. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
  32. this.StopRun()
  33. return
  34. }
  35. //authorizationArr := strings.Split(authorization, ",")
  36. //tokenStr := authorizationArr[0]
  37. //tokenArr := strings.Split(tokenStr, "=")
  38. //token := tokenArr[1]
  39. //
  40. //accountStr := authorizationArr[1]
  41. //accountArr := strings.Split(accountStr, "=")
  42. //account := accountArr[1]
  43. //
  44. //fmt.Println("token:", token)
  45. //fmt.Println("account:", account)
  46. ////校验token是否合法
  47. //// JWT校验Token和Account
  48. //if !utils.CheckToken(account, token) {
  49. // fmt.Println("CheckToken Err")
  50. // this.JSON(models.BaseResponse{Ret: 408, Msg: "鉴权失败,请重新登录!", ErrMsg: "登录失效,请重新登陆!,CheckToken Fail"}, false, false)
  51. // this.StopRun()
  52. // return
  53. //}
  54. //
  55. //fmt.Println("GetUserByToken")
  56. session, err := models.GetSessionByToken(authorization)
  57. if err != nil {
  58. if err.Error() == utils.ErrNoRow() {
  59. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
  60. this.StopRun()
  61. return
  62. }
  63. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  64. this.StopRun()
  65. return
  66. }
  67. if session == nil {
  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. 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. } else {
  90. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  91. this.StopRun()
  92. return
  93. }
  94. }
  95. }
  96. func (c *BaseAuthController) 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("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  109. return
  110. }
  111. baseRes := c.Data["json"].(*models.BaseResponse)
  112. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 {
  113. body, _ := json.Marshal(baseRes)
  114. go utils.SendEmail(utils.APPNAME+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body), utils.EmailSendToUsers)
  115. }
  116. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  117. }
  118. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  119. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  120. var content []byte
  121. var err error
  122. if hasIndent {
  123. content, err = json.MarshalIndent(data, "", " ")
  124. } else {
  125. content, err = json.Marshal(data)
  126. }
  127. if err != nil {
  128. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  129. return err
  130. }
  131. ip := c.Ctx.Input.IP()
  132. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  133. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  134. if coding {
  135. content = []byte(utils.StringsToJSON(string(content)))
  136. }
  137. return c.Ctx.Output.Body(content)
  138. }