sessions.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. if accountInfo.Enable == 0 {
  55. ctx.RemoveCookie("cm")
  56. ctx.JSON(iris.Map{"code": 1, "msg": "账号被停用"})
  57. return
  58. }
  59. ctx.Values().Set("accountId", identityId)
  60. ctx.Values().Set("projectId", attachedIdentityId)
  61. ctx.Values().Set("account", accountInfo)
  62. // 设置viewData
  63. //ctx.ViewData("Account", accountInfo)
  64. // 比对数字证书
  65. if digitalToken != params.Get("digitalToken") {
  66. ctx.JSON(iris.Map{"code": 1, "msg": "账号异常3"})
  67. return
  68. }
  69. // TODO 分布式session
  70. //通过后执行下一步
  71. ctx.Next()
  72. }
  73. // 获得解密后的ID
  74. func getDecryptId(id string) (int, error) {
  75. id, err := comm.AesDecrypt(id, conf.CookieSecret)
  76. if err != nil {
  77. return 0, errors.New("ID 解析错误")
  78. }
  79. idInt, err := strconv.Atoi(id)
  80. if err != nil {
  81. return 0, errors.New("ID 转换错误")
  82. }
  83. return idInt, nil
  84. }
  85. // 登陆态-认证
  86. // func SessionsAuth(ctx iris.Context) {
  87. // // 获得cookie
  88. // cookie, err := ctx.Request().Cookie("cm")
  89. // if err != nil {
  90. // comm.Redirect(ctx.ResponseWriter(), "/login")
  91. // }
  92. // // 格式化
  93. // params, err := url.ParseQuery(cookie.Value)
  94. // if err != nil {
  95. // comm.Redirect(ctx.ResponseWriter(), "/login")
  96. // }
  97. // // 解密用户标识
  98. // identity, err := comm.AesDecrypt(params.Get("identity"), conf.CookieSecret)
  99. // if err != nil {
  100. // comm.Redirect(ctx.ResponseWriter(), "/login")
  101. // }
  102. // digitalToken := comm.CreateSign(conf.CookieSecret + identity)
  103. // // 获得用户信息--TODO 存放redis
  104. // npaSer := services.NewProjectAccountService()
  105. // identityId, err := strconv.Atoi(identity)
  106. // if err != nil {
  107. // comm.Redirect(ctx.ResponseWriter(), "/login")
  108. // }
  109. // // TODO 项目ID的获得
  110. // accountInfo := npaSer.Get(identityId, 2)
  111. // if accountInfo.Id == "0" {
  112. // comm.Redirect(ctx.ResponseWriter(), "/login")
  113. // }
  114. // ctx.Values().Set("accountId", identity)
  115. // projectId, err := comm.AesDecrypt(accountInfo.ProjectId, conf.SignSecret)
  116. // if err != nil {
  117. // comm.Redirect(ctx.ResponseWriter(), "/login")
  118. // }
  119. // ctx.Values().Set("projectId", projectId)
  120. // // 设置viewData
  121. // ctx.ViewData("Account", accountInfo)
  122. // // npaDao := dao.NewProjectAccountDao(datasource.InstanceDbMaster())
  123. // // identityId, err := strconv.Atoi(identity)
  124. // // if err != nil {
  125. // // comm.Redirect(ctx.ResponseWriter(), "/login")
  126. // // }
  127. // // accountInfo := npaDao.Get(identityId)
  128. // // if accountInfo.Id == 0 {
  129. // // comm.Redirect(ctx.ResponseWriter(), "/login")
  130. // // }
  131. // // ctx.Values().Set("accountId", identity)
  132. // // 设置viewData
  133. // // accountView := viewmodels.ProjectAccount{}
  134. // // accountView.Account = accountInfo.Account
  135. // // accountView.Name = accountInfo.Name
  136. // // accountView.Company = accountInfo.Company
  137. // // accountView.Role = accountInfo.Role
  138. // // accountView.Mobile = accountInfo.Mobile
  139. // // accountView.Telephone = accountInfo.Telephone
  140. // // ctx.ViewData("Account", accountView)
  141. // // 比对数字证书
  142. // if digitalToken != params.Get("digitalToken") {
  143. // comm.Redirect(ctx.ResponseWriter(), "/login")
  144. // }
  145. // // TODO 分布式session
  146. // //通过后执行下一步
  147. // ctx.Next()
  148. // }