base_auth_v2.go 8.5 KB

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