sessions.go 4.4 KB

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