base_auth.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. 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 err.Error() == utils.ErrNoRow() {
  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 err.Error() == utils.ErrNoRow() {
  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. desEncrypt := utils.DesBase64Encrypt([]byte(utils.DesKey), utils.DesKeySalt)
  169. c.Ctx.Output.Header("Dk", string(desEncrypt)) // des3加解密key
  170. var content []byte
  171. var err error
  172. if hasIndent {
  173. content, err = json.MarshalIndent(data, "", " ")
  174. } else {
  175. content, err = json.Marshal(data)
  176. }
  177. if err != nil {
  178. http.Error(c.Ctx.Output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  179. return err
  180. }
  181. ip := c.Ctx.Input.IP()
  182. requestBody, err := url.QueryUnescape(string(c.Ctx.Input.RequestBody))
  183. if err != nil {
  184. requestBody = string(c.Ctx.Input.RequestBody)
  185. }
  186. if requestBody == "" {
  187. requestBody = c.Ctx.Input.URI()
  188. }
  189. c.logUri(content, requestBody, ip)
  190. //if coding {
  191. // content = []byte(utils.StringsToJSON(string(content)))
  192. //}
  193. //if utils.RunMode != "debug" {
  194. // content = utils.DesBase64Encrypt(content, utils.DesKey)
  195. // // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  196. // content = []byte(`"` + string(content) + `"`)
  197. //}
  198. content = utils.DesBase64Encrypt(content, utils.DesKey)
  199. // get请求时,不加双引号就获取不到数据,不知道什么原因,所以还是在前后加上双引号吧
  200. content = []byte(`"` + string(content) + `"`)
  201. if coding {
  202. content = []byte(utils.StringsToJSON(string(content)))
  203. }
  204. return c.Ctx.Output.Body(content)
  205. }
  206. func (c *BaseAuthController) logUri(content []byte, requestBody, ip string) {
  207. authorization := ""
  208. method := c.Ctx.Input.Method()
  209. uri := c.Ctx.Input.URI()
  210. fmt.Println("Url:", uri)
  211. if method != "HEAD" {
  212. if method == "POST" || method == "GET" {
  213. authorization = c.Ctx.Input.Header("authorization")
  214. if authorization == "" {
  215. authorization = c.Ctx.Input.Header("Authorization")
  216. }
  217. if authorization == "" {
  218. newAuthorization := c.GetString("authorization")
  219. if newAuthorization != `` {
  220. authorization = "authorization=" + newAuthorization
  221. } else {
  222. newAuthorization = c.GetString("Authorization")
  223. authorization = "authorization=" + newAuthorization
  224. }
  225. } else {
  226. if strings.Contains(authorization, ";") {
  227. authorization = strings.Replace(authorization, ";", "$", 1)
  228. }
  229. }
  230. if authorization == "" {
  231. strArr := strings.Split(uri, "?")
  232. for k, v := range strArr {
  233. fmt.Println(k, v)
  234. }
  235. if len(strArr) > 1 {
  236. authorization = strArr[1]
  237. authorization = strings.Replace(authorization, "Authorization", "authorization", -1)
  238. fmt.Println(authorization)
  239. }
  240. }
  241. }
  242. }
  243. utils.ApiLog.Info("uri:%s, authorization:%s, requestBody:%s, responseBody:%s, ip:%s", c.Ctx.Input.URI(), authorization, requestBody, content, ip)
  244. return
  245. }