base_auth.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/astaxie/beego"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "hongze/hongze_cygx/models"
  10. "hongze/hongze_cygx/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. Token string
  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. this.Token=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. session, err := models.GetSessionByToken(authorization)
  37. if err != nil {
  38. if err.Error() == utils.ErrNoRow() {
  39. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
  40. this.StopRun()
  41. return
  42. }
  43. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  44. this.StopRun()
  45. return
  46. }
  47. if session == nil {
  48. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
  49. this.StopRun()
  50. return
  51. }
  52. wxUser, err := models.GetWxUserItemByUserId(session.UserId)
  53. if err != nil {
  54. if err.Error() == utils.ErrNoRow() {
  55. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  56. this.StopRun()
  57. return
  58. }
  59. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取wx_user信息异常,Eerr:" + err.Error()}, false, false)
  60. this.StopRun()
  61. return
  62. }
  63. if wxUser == nil {
  64. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  65. this.StopRun()
  66. return
  67. }
  68. this.User = wxUser
  69. } else {
  70. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  71. this.StopRun()
  72. return
  73. }
  74. }
  75. }
  76. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  77. var (
  78. hasIndent = false
  79. hasEncoding = false
  80. )
  81. if beego.BConfig.RunMode == beego.PROD {
  82. hasIndent = false
  83. }
  84. if len(encoding) > 0 && encoding[0] == true {
  85. hasEncoding = true
  86. }
  87. if c.Data["json"] == nil {
  88. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  89. return
  90. }
  91. baseRes := c.Data["json"].(*models.BaseResponse)
  92. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  93. body, _ := json.Marshal(baseRes)
  94. 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)
  95. }
  96. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  97. }
  98. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  99. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  100. var content []byte
  101. var err error
  102. if hasIndent {
  103. content, err = json.MarshalIndent(data, "", " ")
  104. } else {
  105. content, err = json.Marshal(data)
  106. }
  107. if err != nil {
  108. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  109. return err
  110. }
  111. ip := c.Ctx.Input.IP()
  112. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  113. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  114. if coding {
  115. content = []byte(utils.StringsToJSON(string(content)))
  116. }
  117. return c.Ctx.Output.Body(content)
  118. }