base_auth.go 7.9 KB

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