123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /*
- * @description: session判断中间件
- * @Author: CP
- * @Date: 2020-08-28 14:17:23
- * @FilePath: \construction_management\web\middleware\sessions.go
- */
- package middleware
- import (
- "errors"
- "net/url"
- "strconv"
- "github.com/kataras/iris/v12"
- "go.mod/comm"
- "go.mod/conf"
- "go.mod/dao"
- "go.mod/datasource"
- )
- func SessionsAuth(ctx iris.Context) {
- // 获得cookie
- cookie, err := ctx.Request().Cookie("cm")
- if err != nil {
- ctx.JSON(iris.Map{"code": 1, "msg": "请重新登陆"})
- return
- }
- // 格式化
- params, err := url.ParseQuery(cookie.Value)
- if err != nil {
- ctx.JSON(iris.Map{"code": 1, "msg": "请重新登陆"})
- return
- }
- // 获得用户信息--TODO 存放redis
- // 解密用户标识
- identityId, err := getDecryptId(params.Get("identity"))
- if err != nil {
- ctx.JSON(iris.Map{"code": 1, "msg": "账号发生异常1"})
- return
- }
- // 数字证书
- digitalToken := comm.CreateSign(conf.CookieSecret + strconv.Itoa(identityId))
- // 解密副标识
- attachedIdentityId, err := getDecryptId(params.Get("attachedIdentity"))
- if err != nil {
- ctx.JSON(iris.Map{"code": 1, "msg": "账号发生异常2"})
- return
- }
- projectAccountDao := dao.NewProjectAccountDao(datasource.InstanceDbMaster())
- accountInfo := projectAccountDao.Get(identityId, attachedIdentityId)
- // npaSer := services.NewProjectAccountService()
- // accountInfo := npaSer.Get(identityId, attachedIdentityId)
- if accountInfo.Id == 0 {
- ctx.JSON(iris.Map{"code": 1, "msg": "账号不存在"})
- return
- }
- if accountInfo.Enable == 0 {
- ctx.RemoveCookie("cm")
- ctx.JSON(iris.Map{"code": 1, "msg": "账号被停用"})
- return
- }
- ctx.Values().Set("accountId", identityId)
- ctx.Values().Set("projectId", attachedIdentityId)
- ctx.Values().Set("account", accountInfo)
- // 设置viewData
- //ctx.ViewData("Account", accountInfo)
- // 比对数字证书
- if digitalToken != params.Get("digitalToken") {
- ctx.JSON(iris.Map{"code": 1, "msg": "账号异常3"})
- return
- }
- // TODO 分布式session
- //通过后执行下一步
- ctx.Next()
- }
- // 获得解密后的ID
- func getDecryptId(id string) (int, error) {
- id, err := comm.AesDecrypt(id, conf.CookieSecret)
- if err != nil {
- return 0, errors.New("ID 解析错误")
- }
- idInt, err := strconv.Atoi(id)
- if err != nil {
- return 0, errors.New("ID 转换错误")
- }
- return idInt, nil
- }
- // 登陆态-认证
- // func SessionsAuth(ctx iris.Context) {
- // // 获得cookie
- // cookie, err := ctx.Request().Cookie("cm")
- // if err != nil {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // // 格式化
- // params, err := url.ParseQuery(cookie.Value)
- // if err != nil {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // // 解密用户标识
- // identity, err := comm.AesDecrypt(params.Get("identity"), conf.CookieSecret)
- // if err != nil {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // digitalToken := comm.CreateSign(conf.CookieSecret + identity)
- // // 获得用户信息--TODO 存放redis
- // npaSer := services.NewProjectAccountService()
- // identityId, err := strconv.Atoi(identity)
- // if err != nil {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // // TODO 项目ID的获得
- // accountInfo := npaSer.Get(identityId, 2)
- // if accountInfo.Id == "0" {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // ctx.Values().Set("accountId", identity)
- // projectId, err := comm.AesDecrypt(accountInfo.ProjectId, conf.SignSecret)
- // if err != nil {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // ctx.Values().Set("projectId", projectId)
- // // 设置viewData
- // ctx.ViewData("Account", accountInfo)
- // // npaDao := dao.NewProjectAccountDao(datasource.InstanceDbMaster())
- // // identityId, err := strconv.Atoi(identity)
- // // if err != nil {
- // // comm.Redirect(ctx.ResponseWriter(), "/login")
- // // }
- // // accountInfo := npaDao.Get(identityId)
- // // if accountInfo.Id == 0 {
- // // comm.Redirect(ctx.ResponseWriter(), "/login")
- // // }
- // // ctx.Values().Set("accountId", identity)
- // // 设置viewData
- // // accountView := viewmodels.ProjectAccount{}
- // // accountView.Account = accountInfo.Account
- // // accountView.Name = accountInfo.Name
- // // accountView.Company = accountInfo.Company
- // // accountView.Role = accountInfo.Role
- // // accountView.Mobile = accountInfo.Mobile
- // // accountView.Telephone = accountInfo.Telephone
- // // ctx.ViewData("Account", accountView)
- // // 比对数字证书
- // if digitalToken != params.Get("digitalToken") {
- // comm.Redirect(ctx.ResponseWriter(), "/login")
- // }
- // // TODO 分布式session
- // //通过后执行下一步
- // ctx.Next()
- // }
|