base_auth.go 8.1 KB

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