sessions.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * @description: session判断中间件
  3. * @Author: CP
  4. * @Date: 2020-08-28 14:17:23
  5. * @FilePath: \construction_management\web\middleware\sessions.go
  6. */
  7. package middleware
  8. import (
  9. "net/url"
  10. "strconv"
  11. "github.com/kataras/iris/v12"
  12. "go.mod/comm"
  13. "go.mod/conf"
  14. "go.mod/services"
  15. )
  16. func SessionsAuth(ctx iris.Context) {
  17. // 获得cookie
  18. cookie, err := ctx.Request().Cookie("cm")
  19. if err != nil {
  20. comm.Redirect(ctx.ResponseWriter(), "/login")
  21. }
  22. // 格式化
  23. params, err := url.ParseQuery(cookie.Value)
  24. if err != nil {
  25. comm.Redirect(ctx.ResponseWriter(), "/login")
  26. }
  27. // 解密用户标识
  28. identity := comm.AesDecrypt(params.Get("identity"), conf.CookieSecret)
  29. digitalToken := comm.CreateSign(conf.CookieSecret + identity)
  30. // 获得用户信息--TODO 存放redis
  31. npaSer := services.NewProjectAccountService()
  32. identityId, err := strconv.Atoi(identity)
  33. if err != nil {
  34. comm.Redirect(ctx.ResponseWriter(), "/login")
  35. }
  36. accountInfo := npaSer.Get(identityId)
  37. if accountInfo.Id == "0" {
  38. comm.Redirect(ctx.ResponseWriter(), "/login")
  39. }
  40. ctx.Values().Set("accountId", identity)
  41. // 设置viewData
  42. ctx.ViewData("Account", accountInfo)
  43. // npaDao := dao.NewProjectAccountDao(datasource.InstanceDbMaster())
  44. // identityId, err := strconv.Atoi(identity)
  45. // if err != nil {
  46. // comm.Redirect(ctx.ResponseWriter(), "/login")
  47. // }
  48. // accountInfo := npaDao.Get(identityId)
  49. // if accountInfo.Id == 0 {
  50. // comm.Redirect(ctx.ResponseWriter(), "/login")
  51. // }
  52. // ctx.Values().Set("accountId", identity)
  53. // 设置viewData
  54. // accountView := viewmodels.ProjectAccount{}
  55. // accountView.Account = accountInfo.Account
  56. // accountView.Name = accountInfo.Name
  57. // accountView.Company = accountInfo.Company
  58. // accountView.Role = accountInfo.Role
  59. // accountView.Mobile = accountInfo.Mobile
  60. // accountView.Telephone = accountInfo.Telephone
  61. // ctx.ViewData("Account", accountView)
  62. // 比对数字证书
  63. if digitalToken != params.Get("digitalToken") {
  64. comm.Redirect(ctx.ResponseWriter(), "/login")
  65. }
  66. // TODO 分布式session
  67. //通过后执行下一步
  68. ctx.Next()
  69. }