base_auth.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "strconv"
  8. "strings"
  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. }
  22. func (this *BaseAuthController) Prepare() {
  23. fmt.Println("enter prepare")
  24. method := this.Ctx.Input.Method()
  25. uri := this.Ctx.Input.URI()
  26. fmt.Println("Url:", uri)
  27. if method != "HEAD" {
  28. if method == "POST" || method == "GET" {
  29. authorization := this.Ctx.Input.Header("Authorization")
  30. fmt.Println("authorization:", authorization)
  31. if authorization == "" {
  32. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
  33. this.StopRun()
  34. return
  35. }
  36. authorizationArr := strings.Split(authorization, ",")
  37. tokenStr := authorizationArr[0]
  38. tokenArr := strings.Split(tokenStr, "=")
  39. token := tokenArr[1]
  40. accountStr := authorizationArr[1]
  41. accountArr := strings.Split(accountStr, "=")
  42. account := accountArr[1]
  43. fmt.Println("token:", token)
  44. fmt.Println("account:", account)
  45. //校验token是否合法
  46. // JWT校验Token和Account
  47. if !utils.CheckToken(account, token) {
  48. fmt.Println("CheckToken Err")
  49. this.JSON(models.BaseResponse{Ret: 408, Msg: "鉴权失败,请重新登录!", ErrMsg: "登录失效,请重新登陆!,CheckToken Fail"}, false, false)
  50. this.StopRun()
  51. return
  52. }
  53. fmt.Println("GetUserByToken")
  54. session, err := models.GetSessionByToken(token)
  55. if err != nil {
  56. if err.Error() == utils.ErrNoRow() {
  57. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + token}, false, false)
  58. this.StopRun()
  59. return
  60. }
  61. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  62. this.StopRun()
  63. return
  64. }
  65. if session == nil {
  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. if err != nil {
  72. if err.Error() == utils.ErrNoRow() {
  73. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  74. this.StopRun()
  75. return
  76. }
  77. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取wx_user信息异常,Eerr:" + err.Error()}, false, false)
  78. this.StopRun()
  79. return
  80. }
  81. if wxUser == nil {
  82. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  83. this.StopRun()
  84. return
  85. }
  86. this.User = wxUser
  87. } else {
  88. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  89. this.StopRun()
  90. return
  91. }
  92. }
  93. }
  94. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  95. var (
  96. hasIndent = false
  97. hasEncoding = false
  98. )
  99. if beego.BConfig.RunMode == beego.PROD {
  100. hasIndent = false
  101. }
  102. if len(encoding) > 0 && encoding[0] == true {
  103. hasEncoding = true
  104. }
  105. if c.Data["json"] == nil {
  106. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  107. return
  108. }
  109. baseRes := c.Data["json"].(*models.BaseResponse)
  110. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 {
  111. body, _ := json.Marshal(baseRes)
  112. go utils.SendEmail(utils.APPNAME+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body), utils.EmailSendToUsers)
  113. }
  114. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  115. }
  116. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  117. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  118. var content []byte
  119. var err error
  120. if hasIndent {
  121. content, err = json.MarshalIndent(data, "", " ")
  122. } else {
  123. content, err = json.Marshal(data)
  124. }
  125. if err != nil {
  126. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  127. return err
  128. }
  129. ip := c.Ctx.Input.IP()
  130. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  131. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  132. if coding {
  133. content = []byte(utils.StringsToJSON(string(content)))
  134. }
  135. return c.Ctx.Output.Body(content)
  136. }