sessions.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. "errors"
  10. "net/url"
  11. "strconv"
  12. "github.com/kataras/iris/v12"
  13. "go.mod/comm"
  14. "go.mod/conf"
  15. "go.mod/services"
  16. )
  17. func SessionsAuth(ctx iris.Context) {
  18. // 获得cookie
  19. cookie, err := ctx.Request().Cookie("cm")
  20. if err != nil {
  21. ctx.JSON(iris.Map{"code": 1, "msg": ""})
  22. return
  23. }
  24. // 格式化
  25. params, err := url.ParseQuery(cookie.Value)
  26. if err != nil {
  27. ctx.JSON(iris.Map{"code": 1, "msg": ""})
  28. return
  29. }
  30. // 获得用户信息--TODO 存放redis
  31. // 解密用户标识
  32. identityId, err := getDecryptId(params.Get("identity"))
  33. if err != nil {
  34. ctx.JSON(iris.Map{"code": 1, "msg": "账号发生异常1"})
  35. return
  36. }
  37. // 数字证书
  38. digitalToken := comm.CreateSign(conf.CookieSecret + strconv.Itoa(identityId))
  39. // 解密副标识
  40. attachedIdentityId, err := getDecryptId(params.Get("attachedIdentity"))
  41. if err != nil {
  42. ctx.JSON(iris.Map{"code": 1, "msg": "账号发生异常2"})
  43. return
  44. }
  45. npaSer := services.NewProjectAccountService()
  46. accountInfo := npaSer.Get(identityId, attachedIdentityId)
  47. if accountInfo.Id == "0" {
  48. ctx.JSON(iris.Map{"code": 1, "msg": "账号不存在"})
  49. return
  50. }
  51. ctx.Values().Set("accountId", identityId)
  52. ctx.Values().Set("projectId", attachedIdentityId)
  53. ctx.Values().Set("account", accountInfo)
  54. // 设置viewData
  55. //ctx.ViewData("Account", accountInfo)
  56. // 比对数字证书
  57. if digitalToken != params.Get("digitalToken") {
  58. ctx.JSON(iris.Map{"code": 1, "msg": "账号异常3"})
  59. return
  60. }
  61. // TODO 分布式session
  62. //通过后执行下一步
  63. ctx.Next()
  64. }
  65. // 获得解密后的ID
  66. func getDecryptId(id string) (int, error) {
  67. id, err := comm.AesDecrypt(id, conf.CookieSecret)
  68. if err != nil {
  69. return 0, errors.New("ID 解析错误")
  70. }
  71. idInt, err := strconv.Atoi(id)
  72. if err != nil {
  73. return 0, errors.New("ID 转换错误")
  74. }
  75. return idInt, nil
  76. }
  77. // 登陆态-认证
  78. // func SessionsAuth(ctx iris.Context) {
  79. // // 获得cookie
  80. // cookie, err := ctx.Request().Cookie("cm")
  81. // if err != nil {
  82. // comm.Redirect(ctx.ResponseWriter(), "/login")
  83. // }
  84. // // 格式化
  85. // params, err := url.ParseQuery(cookie.Value)
  86. // if err != nil {
  87. // comm.Redirect(ctx.ResponseWriter(), "/login")
  88. // }
  89. // // 解密用户标识
  90. // identity, err := comm.AesDecrypt(params.Get("identity"), conf.CookieSecret)
  91. // if err != nil {
  92. // comm.Redirect(ctx.ResponseWriter(), "/login")
  93. // }
  94. // digitalToken := comm.CreateSign(conf.CookieSecret + identity)
  95. // // 获得用户信息--TODO 存放redis
  96. // npaSer := services.NewProjectAccountService()
  97. // identityId, err := strconv.Atoi(identity)
  98. // if err != nil {
  99. // comm.Redirect(ctx.ResponseWriter(), "/login")
  100. // }
  101. // // TODO 项目ID的获得
  102. // accountInfo := npaSer.Get(identityId, 2)
  103. // if accountInfo.Id == "0" {
  104. // comm.Redirect(ctx.ResponseWriter(), "/login")
  105. // }
  106. // ctx.Values().Set("accountId", identity)
  107. // projectId, err := comm.AesDecrypt(accountInfo.ProjectId, conf.SignSecret)
  108. // if err != nil {
  109. // comm.Redirect(ctx.ResponseWriter(), "/login")
  110. // }
  111. // ctx.Values().Set("projectId", projectId)
  112. // // 设置viewData
  113. // ctx.ViewData("Account", accountInfo)
  114. // // npaDao := dao.NewProjectAccountDao(datasource.InstanceDbMaster())
  115. // // identityId, err := strconv.Atoi(identity)
  116. // // if err != nil {
  117. // // comm.Redirect(ctx.ResponseWriter(), "/login")
  118. // // }
  119. // // accountInfo := npaDao.Get(identityId)
  120. // // if accountInfo.Id == 0 {
  121. // // comm.Redirect(ctx.ResponseWriter(), "/login")
  122. // // }
  123. // // ctx.Values().Set("accountId", identity)
  124. // // 设置viewData
  125. // // accountView := viewmodels.ProjectAccount{}
  126. // // accountView.Account = accountInfo.Account
  127. // // accountView.Name = accountInfo.Name
  128. // // accountView.Company = accountInfo.Company
  129. // // accountView.Role = accountInfo.Role
  130. // // accountView.Mobile = accountInfo.Mobile
  131. // // accountView.Telephone = accountInfo.Telephone
  132. // // ctx.ViewData("Account", accountView)
  133. // // 比对数字证书
  134. // if digitalToken != params.Get("digitalToken") {
  135. // comm.Redirect(ctx.ResponseWriter(), "/login")
  136. // }
  137. // // TODO 分布式session
  138. // //通过后执行下一步
  139. // ctx.Next()
  140. // }