caipin 4 年之前
父节点
当前提交
a8173768f2
共有 1 个文件被更改,包括 157 次插入0 次删除
  1. 157 0
      web/middleware/sessions_backstage.go

+ 157 - 0
web/middleware/sessions_backstage.go

@@ -0,0 +1,157 @@
+/*
+ * @description: session判断中间件
+ * @Author: CP
+ * @Date: 2020-08-28 14:17:23
+ * @FilePath: \construction_management\web\middleware\sessions_backstage.go
+ */
+package middleware
+
+import (
+	"errors"
+	"net/url"
+
+	"github.com/kataras/iris/v12"
+	"go.mod/comm"
+	"go.mod/conf"
+)
+
+func SessionsBackstageAuth(ctx iris.Context) {
+	// 获得cookie
+	cookie, err := ctx.Request().Cookie("cmBackstage")
+	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
+	// 解密用户标识
+	UserName, err := getDecryptValue(params.Get("identity"))
+	if err != nil {
+		ctx.JSON(iris.Map{"code": 1, "msg": "账号发生异常1"})
+		return
+	}
+	// 数字证书
+	digitalToken := comm.CreateSign(conf.CookieSecret + UserName)
+
+	// 解密副标识
+	Category, 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(UserName, 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("StaffName", UserName)
+	ctx.Values().Set("Category", Category)
+	// 设置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 getDecryptValue(id string) (string, error) {
+	value, err := comm.AesDecrypt(id, conf.CookieSecret)
+	if err != nil {
+		return "", errors.New("ID 解析错误")
+	}
+
+	return value, 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()
+// }