base_auth.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "hongze/hongze_mfyx_gzh/services"
  7. "net/http"
  8. "net/url"
  9. "strconv"
  10. "hongze/hongze_mfyx_gzh/models"
  11. "hongze/hongze_mfyx_gzh/utils"
  12. "github.com/rdlucklib/rdluck_tools/log"
  13. )
  14. var apiLog *log.Log
  15. func init() {
  16. if utils.RunMode == "release" {
  17. logDir := `/data/rdlucklog/hongze_mfyx_gzh`
  18. apiLog = log.Init("20060102.api", logDir)
  19. } else {
  20. apiLog = log.Init("20060102.api")
  21. }
  22. }
  23. type BaseAuthController struct {
  24. web.Controller
  25. User *models.WxUserItem
  26. Token string
  27. }
  28. func (this *BaseAuthController) Prepare() {
  29. fmt.Println("enter prepare")
  30. method := this.Ctx.Input.Method()
  31. uri := this.Ctx.Input.URI()
  32. fmt.Println("Url:", uri)
  33. if method != "HEAD" {
  34. if method == "POST" || method == "GET" {
  35. authorization := this.Ctx.Input.Header("Authorization")
  36. if authorization == "" {
  37. authorization = this.GetString("Authorization")
  38. }
  39. this.Token = authorization
  40. if authorization == "" {
  41. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
  42. this.StopRun()
  43. return
  44. }
  45. session, err := models.GetSessionByToken(authorization)
  46. if err != nil {
  47. if err.Error() == utils.ErrNoRow() {
  48. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
  49. this.StopRun()
  50. return
  51. }
  52. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  53. this.StopRun()
  54. return
  55. }
  56. if session == nil {
  57. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
  58. this.StopRun()
  59. return
  60. }
  61. wxUser, err := services.GetWxUserItemByOpenId2(session.OpenId)
  62. if err != nil && err.Error() != utils.ErrNoRow() {
  63. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取信息失败 " + strconv.Itoa(session.UserId) + "err: " + err.Error()}, false, false)
  64. this.StopRun()
  65. return
  66. }
  67. if wxUser == nil {
  68. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  69. this.StopRun()
  70. return
  71. }
  72. this.User = wxUser
  73. } else {
  74. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  75. this.StopRun()
  76. return
  77. }
  78. }
  79. }
  80. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  81. var (
  82. hasIndent = false
  83. hasEncoding = false
  84. )
  85. if web.BConfig.RunMode == web.PROD {
  86. hasIndent = false
  87. }
  88. if len(encoding) > 0 && encoding[0] == true {
  89. hasEncoding = true
  90. }
  91. if c.Data["json"] == nil {
  92. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  93. return
  94. }
  95. baseRes := c.Data["json"].(*models.BaseResponse)
  96. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  97. body, _ := json.Marshal(baseRes)
  98. msgBody := "URI:" + c.Ctx.Input.URI() + "<br/> ErrMsg:" + baseRes.ErrMsg + ";<br/>Msg:" + baseRes.Msg + ";<br/> Body:" + string(body) + ";<br/>" + c.Token
  99. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", msgBody, utils.EmailSendToUsers)
  100. }
  101. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  102. }
  103. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  104. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  105. var content []byte
  106. var err error
  107. if hasIndent {
  108. content, err = json.MarshalIndent(data, "", " ")
  109. } else {
  110. content, err = json.Marshal(data)
  111. }
  112. if err != nil {
  113. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  114. return err
  115. }
  116. ip := c.Ctx.Input.IP()
  117. requestBody, _ := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  118. apiLog.Println("请求地址:", c.Ctx.Input.URI(), "Authorization:", c.Ctx.Input.Header("Authorization"), "RequestBody:", requestBody, "ResponseBody", string(content), "IP:", ip)
  119. if coding {
  120. content = []byte(utils.StringsToJSON(string(content)))
  121. }
  122. return c.Ctx.Output.Body(content)
  123. }