base_auth.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/beego/beego/v2/server/web"
  6. "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. "hongze/hongze_web_mfyx/models"
  11. "hongze/hongze_web_mfyx/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_clpt`
  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. inviteCompany := this.Ctx.Input.Header("From")
  37. if authorization == "" {
  38. authorization = this.GetString("Authorization")
  39. }
  40. this.Token = authorization
  41. if authorization == "" {
  42. this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
  43. this.StopRun()
  44. return
  45. }
  46. session, err := models.GetSessionByToken(authorization)
  47. if err != nil {
  48. if err.Error() == utils.ErrNoRow() {
  49. this.JSON(models.BaseResponse{Ret: 406, Msg: "您的登录状态已过期,请重新登录", ErrMsg: "Token 信息已变更:Token: " + authorization}, false, false)
  50. this.StopRun()
  51. return
  52. }
  53. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  54. this.StopRun()
  55. return
  56. }
  57. if session == nil {
  58. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
  59. this.StopRun()
  60. return
  61. }
  62. //如果用户的手机号为空,则不展示
  63. if session.Mobile == "" {
  64. nilWxUser := new(models.WxUserItem)
  65. this.User = nilWxUser
  66. } else {
  67. if session.SessionStatus == 1 {
  68. this.JSON(models.BaseResponse{Ret: 401, Msg: "您的账号在另一设备登录,当前设备已被迫下线。若不是您本人操作,请确保未泄露短信验证码或及时修改您的登录密码", ErrMsg: "sesson is empty "}, false, false)
  69. this.StopRun()
  70. return
  71. }
  72. wxUser, err := models.GetWxUserItemByMobile(session.Mobile)
  73. if err != nil && err.Error() != utils.ErrNoRow() {
  74. this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取信息失败 " + strconv.Itoa(session.UserId)}, false, false)
  75. this.StopRun()
  76. return
  77. }
  78. if wxUser == nil {
  79. this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  80. this.StopRun()
  81. return
  82. }
  83. this.User = wxUser
  84. }
  85. this.User.InviteCompany = inviteCompany
  86. } else {
  87. this.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  88. this.StopRun()
  89. return
  90. }
  91. }
  92. }
  93. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  94. var (
  95. hasIndent = false
  96. hasEncoding = false
  97. )
  98. if web.BConfig.RunMode == web.PROD {
  99. hasIndent = false
  100. }
  101. if len(encoding) > 0 && encoding[0] == true {
  102. hasEncoding = true
  103. }
  104. if c.Data["json"] == nil {
  105. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+"异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  106. return
  107. }
  108. baseRes := c.Data["json"].(*models.BaseResponse)
  109. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  110. body, _ := json.Marshal(baseRes)
  111. msgBody := "URI:" + c.Ctx.Input.URI() + "<br/> ErrMsg:" + baseRes.ErrMsg + ";<br/>Msg:" + baseRes.Msg + ";<br/> Body:" + string(body) + ";<br/>" + c.Token
  112. go utils.SendEmail(utils.APPNAME+" "+utils.RunMode+" 失败提醒", msgBody, 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. c.logUri(content, requestBody, ip)
  136. return c.Ctx.Output.Body(content)
  137. }
  138. func (c *BaseAuthController) logUri(respContent []byte, requestBody, ip string) {
  139. authorization := ""
  140. method := c.Ctx.Input.Method()
  141. uri := c.Ctx.Input.URI()
  142. //fmt.Println("Url:", uri)
  143. if method != "HEAD" {
  144. if method == "POST" || method == "GET" {
  145. authorization = c.Ctx.Input.Header("authorization")
  146. if authorization == "" {
  147. authorization = c.Ctx.Input.Header("Authorization")
  148. }
  149. if authorization == "" {
  150. newAuthorization := c.GetString("authorization")
  151. if newAuthorization != `` {
  152. authorization = "authorization=" + newAuthorization
  153. } else {
  154. newAuthorization = c.GetString("Authorization")
  155. authorization = "authorization=" + newAuthorization
  156. }
  157. } else {
  158. if strings.Contains(authorization, ";") {
  159. authorization = strings.Replace(authorization, ";", "$", 1)
  160. }
  161. }
  162. if authorization == "" {
  163. strArr := strings.Split(uri, "?")
  164. for k, v := range strArr {
  165. fmt.Println(k, v)
  166. }
  167. if len(strArr) > 1 {
  168. authorization = strArr[1]
  169. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  170. fmt.Println(authorization)
  171. }
  172. }
  173. }
  174. }
  175. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, respContent, ip)
  176. return
  177. }