base_auth.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "eta/eta_chart_lib/models"
  5. "eta/eta_chart_lib/utils"
  6. "fmt"
  7. "github.com/beego/beego/v2/server/web"
  8. "net/http"
  9. "net/url"
  10. "strings"
  11. )
  12. type BaseAuthController struct {
  13. web.Controller
  14. }
  15. func (c *BaseAuthController) Prepare() {
  16. fmt.Println("enter prepare")
  17. method := c.Ctx.Input.Method()
  18. uri := c.Ctx.Input.URI()
  19. ip := c.Ctx.Input.IP()
  20. fmt.Println("Url:", uri+";ip:"+ip)
  21. if method != "HEAD" {
  22. if method == "POST" || method == "GET" {
  23. //authorization := this.Ctx.Input.Header("authorization")
  24. //if authorization == "" {
  25. // authorization = this.Ctx.Input.Header("Authorization")
  26. //}
  27. //if authorization == "" {
  28. // newAuthorization := this.GetString("authorization")
  29. // account := this.GetString("account")
  30. // authorization = "authorization=" + newAuthorization + ";account=" + account
  31. // if newAuthorization == "" {
  32. // newAuthorization = this.GetString("Authorization")
  33. // if account == "" {
  34. // account = this.GetString("Account")
  35. // }
  36. // authorization = "authorization=" + newAuthorization + ";account=" + account
  37. // }
  38. //}
  39. //fmt.Println("authorization##################")
  40. //fmt.Println(authorization)
  41. //fmt.Println("authorization############")
  42. //if authorization == "" {
  43. // strArr := strings.Split(uri, "?")
  44. // for k, v := range strArr {
  45. // fmt.Println(k, v)
  46. // }
  47. // if len(strArr) > 1 {
  48. // authorization := strArr[1]
  49. // authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  50. // fmt.Println(authorization)
  51. // }
  52. //}
  53. //fmt.Println("authorization------------")
  54. //fmt.Println(authorization)
  55. //fmt.Println("authorization-------------")
  56. //if authorization == "" {
  57. // this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新授权!", ErrMsg: "请重新授权:Token is empty or account is empty"}, false, false)
  58. // this.StopRun()
  59. // return
  60. //}
  61. //authorizationArr := strings.Split(authorization, ";")
  62. //tokenStr := authorizationArr[0]
  63. //tokenArr := strings.Split(tokenStr, "=")
  64. //token := tokenArr[1]
  65. //
  66. //accountStr := authorizationArr[1]
  67. //accountArr := strings.Split(accountStr, "=")
  68. //account := accountArr[1]
  69. //
  70. //fmt.Println("token:", token)
  71. //fmt.Println("account:", account)
  72. ////校验token是否合法
  73. //// JWT校验Token和Account
  74. //if !utils.CheckToken(account, token) {
  75. // fmt.Println("CheckToken Err")
  76. // this.JSON(models.BaseResponse{Ret: 408, Msg: "鉴权失败,请重新登录!", ErrMsg: "登录失效,请重新登陆!,CheckToken Fail"}, false, false)
  77. // this.StopRun()
  78. // return
  79. //}
  80. //
  81. //fmt.Println("GetUserByToken")
  82. //session, err := system.GetSysSessionByToken(token)
  83. //if err != nil {
  84. // if err.Error() == utils.ErrNoRow() {
  85. // this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "Token 信息已变更:Token: " + token}, false, false)
  86. // this.StopRun()
  87. // return
  88. // }
  89. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  90. // this.StopRun()
  91. // return
  92. //}
  93. //if session == nil {
  94. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "sesson is empty "}, false, false)
  95. // this.StopRun()
  96. // return
  97. //}
  98. //if time.Now().After(session.ExpiredTime) {
  99. // this.JSON(models.BaseResponse{Ret: 408, Msg: "请重新登录!", ErrMsg: "获取用户信息异常,Eerr:" + err.Error()}, false, false)
  100. // this.StopRun()
  101. // return
  102. //}
  103. //admin, err := system.GetSysUserById(session.SysUserId)
  104. //if err != nil {
  105. // if err.Error() == utils.ErrNoRow() {
  106. // this.JSON(models.BaseResponse{Ret: 408, Msg: "信息已变更,请重新登陆!", ErrMsg: "获取admin 信息失败 " + strconv.Itoa(session.SysUserId)}, false, false)
  107. // this.StopRun()
  108. // return
  109. // }
  110. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "获取admin信息异常,Eerr:" + err.Error()}, false, false)
  111. // this.StopRun()
  112. // return
  113. //}
  114. //if admin == nil {
  115. // this.JSON(models.BaseResponse{Ret: 408, Msg: "网络异常,请稍后重试!", ErrMsg: "admin is empty "}, false, false)
  116. // this.StopRun()
  117. // return
  118. //}
  119. //admin.RoleTypeCode = GetSysUserRoleTypeCode(admin.RoleTypeCode)
  120. //this.SysUser = admin
  121. } else {
  122. c.JSON(models.BaseResponse{Ret: 408, Msg: "请求异常,请联系客服!", ErrMsg: "POST之外的请求,暂不支持"}, false, false)
  123. c.StopRun()
  124. return
  125. }
  126. }
  127. }
  128. func (c *BaseAuthController) ServeJSON(encoding ...bool) {
  129. var (
  130. hasIndent = false
  131. hasEncoding = false
  132. )
  133. if web.BConfig.RunMode == web.PROD {
  134. hasIndent = false
  135. }
  136. if len(encoding) > 0 && encoding[0] == true {
  137. hasEncoding = true
  138. }
  139. if c.Data["json"] == nil {
  140. go utils.SendEmail("异常提醒:", "接口:"+"URI:"+c.Ctx.Input.URI()+";无返回值", utils.EmailSendToUsers)
  141. return
  142. }
  143. baseRes := c.Data["json"].(*models.BaseResponse)
  144. if baseRes != nil && baseRes.Ret != 200 && baseRes.Ret != 408 && baseRes.IsSendEmail {
  145. body, _ := json.Marshal(baseRes)
  146. var requestBody string
  147. method := c.Ctx.Input.Method()
  148. if method == "GET" {
  149. requestBody = c.Ctx.Request.RequestURI
  150. } else {
  151. requestBody, _ = url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  152. }
  153. go utils.SendEmail(utils.APPNAME+"【"+utils.RunMode+"】"+"失败提醒", "URI:"+c.Ctx.Input.URI()+"<br/> "+"Params"+requestBody+" <br/>"+"ErrMsg:"+baseRes.ErrMsg+";<br/>Msg:"+baseRes.Msg+";<br/> Body:"+string(body)+"<br/>", utils.EmailSendToUsers)
  154. }
  155. c.JSON(c.Data["json"], hasIndent, hasEncoding)
  156. }
  157. func (c *BaseAuthController) JSON(data interface{}, hasIndent bool, coding bool) error {
  158. c.Ctx.Output.Header("Content-Type", "application/json; charset=utf-8")
  159. var content []byte
  160. var err error
  161. if hasIndent {
  162. content, err = json.MarshalIndent(data, "", " ")
  163. } else {
  164. content, err = json.Marshal(data)
  165. }
  166. if err != nil {
  167. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  168. return err
  169. }
  170. ip := c.Ctx.Input.IP()
  171. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  172. if err != nil {
  173. requestBody = string(c.Ctx.Input.RequestBody)
  174. }
  175. if requestBody == "" {
  176. requestBody = c.Ctx.Input.URI()
  177. }
  178. c.logUri(content, requestBody, ip)
  179. if coding {
  180. content = []byte(utils.StringsToJSON(string(content)))
  181. }
  182. return c.Ctx.Output.Body(content)
  183. }
  184. func (c *BaseAuthController) logUri(content []byte, requestBody, ip string) {
  185. authorization := ""
  186. method := c.Ctx.Input.Method()
  187. uri := c.Ctx.Input.URI()
  188. fmt.Println("Url:", uri)
  189. if method != "HEAD" {
  190. if method == "POST" || method == "GET" {
  191. authorization = c.Ctx.Input.Header("authorization")
  192. if authorization == "" {
  193. authorization = c.Ctx.Input.Header("Authorization")
  194. }
  195. if authorization == "" {
  196. newAuthorization := c.GetString("authorization")
  197. if newAuthorization != `` {
  198. authorization = "authorization=" + newAuthorization
  199. } else {
  200. newAuthorization = c.GetString("Authorization")
  201. authorization = "authorization=" + newAuthorization
  202. }
  203. } else {
  204. if strings.Contains(authorization, ";") {
  205. authorization = strings.Replace(authorization, ";", "$", 1)
  206. }
  207. }
  208. if authorization == "" {
  209. strArr := strings.Split(uri, "?")
  210. for k, v := range strArr {
  211. fmt.Println(k, v)
  212. }
  213. if len(strArr) > 1 {
  214. authorization = strArr[1]
  215. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  216. fmt.Println(authorization)
  217. }
  218. }
  219. }
  220. }
  221. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  222. return
  223. }