You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
507 lines
16 KiB
507 lines
16 KiB
package systemuser
|
|
|
|
import (
|
|
"encoding/json"
|
|
"strconv"
|
|
"time"
|
|
|
|
"gin_server_admin/commonus"
|
|
"gin_server_admin/global"
|
|
"gin_server_admin/model/common/response"
|
|
"gin_server_admin/model/systemuser"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// 角色相关
|
|
// 入口
|
|
func (s *SystemRoleApi) Index(c *gin.Context) {
|
|
outPut := commonus.MapOut()
|
|
response.Result(0, outPut, "角色管理入口获取成功", c)
|
|
}
|
|
|
|
/*
|
|
角色列表
|
|
*/
|
|
func (s *SystemRoleApi) SystemRoleList(c *gin.Context) {
|
|
var requestData menuList
|
|
c.ShouldBindJSON(&requestData)
|
|
|
|
isTrue, SysAdminCont := commonus.AdminClientIdentity()
|
|
if isTrue != true {
|
|
response.Result(101, isTrue, "您的身份令牌已经失效!请重新登录获取身份令牌!", c)
|
|
return
|
|
}
|
|
|
|
if requestData.PageSize == 0 {
|
|
requestData.PageSize = 20
|
|
}
|
|
if requestData.Page <= 0 {
|
|
requestData.Page = 1
|
|
}
|
|
offSetPage := commonus.CalculatePages(requestData.Page, requestData.PageSize)
|
|
|
|
gormDb := global.GVA_DB_Master.Model(&systemuser.SystemRole{})
|
|
|
|
if SysAdminCont.Group != "1" {
|
|
gormDb = gormDb.Where("`r_group` = ?", SysAdminCont.Group)
|
|
}
|
|
if SysAdminCont.AttriBute != "0" {
|
|
gormDb = gormDb.Where("`r_attribute` = ?", SysAdminCont.AttriBute)
|
|
}
|
|
if requestData.Title != "" {
|
|
gormDb = gormDb.Where("(`r_title` LIKE ?)", "%"+requestData.Title+"%")
|
|
}
|
|
|
|
var total int64
|
|
totalErr := gormDb.Count(&total).Error
|
|
if totalErr != nil {
|
|
total = 0
|
|
}
|
|
var systemRoleList []systemuser.SystemRole
|
|
systemRoleListerr := gormDb.Limit(requestData.PageSize).Offset(offSetPage).Order("r_id desc").Find(&systemRoleList).Error
|
|
if systemRoleListerr != nil {
|
|
response.Result(101, systemRoleListerr, "数据获取失败!", c)
|
|
return
|
|
}
|
|
var printRoleListMap []printRoleList
|
|
for _, val := range systemRoleList {
|
|
var printRoleCont printRoleList
|
|
printRoleCont.Id = val.Id
|
|
printRoleCont.Title = val.Title
|
|
printRoleCont.State = val.State
|
|
printRoleCont.Attribute = val.Attribute
|
|
printRoleCont.Gode = val.Gode
|
|
printRoleCont.Time = val.Time
|
|
printRoleCont.UserId = val.UserId
|
|
printRoleCont.Jurisdiction = val.Jurisdiction
|
|
printRoleCont.MenuOper = val.MenuOper
|
|
printRoleCont.Wand = val.Wand
|
|
printRoleCont.Group = val.Group
|
|
|
|
where := commonus.MapOut()
|
|
where["id"] = val.Group
|
|
orgCont, _ := commonus.GetNewOrgCont(where, "id", "name")
|
|
printRoleCont.GroupName = orgCont.Name
|
|
|
|
whereDepart := commonus.MapOut()
|
|
whereDepart["id"] = val.Attribute
|
|
orgContDepart, _ := commonus.GetNewOrgCont(whereDepart, "id", "name")
|
|
printRoleCont.BranchFactoryName = orgContDepart.Name
|
|
|
|
// groupIsTrue, groupCont := commonus.GetGroupCont(val.Group)
|
|
// if groupIsTrue == true {
|
|
// printRoleCont.GroupName = groupCont.Name
|
|
// }
|
|
// bfIsTrue, bfCont := commonus.GetBranchFactory(val.Attribute)
|
|
// if bfIsTrue == true {
|
|
// printRoleCont.BranchFactoryName = bfCont.Name
|
|
// }
|
|
json.Unmarshal([]byte(val.Jurisdiction), &printRoleCont.JurisdictionPower)
|
|
json.Unmarshal([]byte(val.MenuOper), &printRoleCont.MenuoperPower)
|
|
printRoleListMap = append(printRoleListMap, printRoleCont)
|
|
}
|
|
countSum := len(printRoleListMap)
|
|
printData := commonus.OutPutList(total, int64(countSum), requestData.Page, requestData.PageSize, printRoleListMap)
|
|
response.Result(0, printData, "查询成功!", c)
|
|
}
|
|
|
|
// 获取角色详细内容
|
|
func GetAdminRoleInfo(id int64) (isTrue bool, adminRoleInfo systemuser.SystemRole) {
|
|
isTrue = false
|
|
err := global.GVA_DB_Master.Where("r_id = ?", id).First(&adminRoleInfo).Error
|
|
if err == nil {
|
|
isTrue = true
|
|
}
|
|
return
|
|
}
|
|
|
|
// 添加系统角色
|
|
func (s *SystemRoleApi) AddSystemRole(c *gin.Context) {
|
|
isTrue, SysAdminCont := commonus.AdminClientIdentity()
|
|
if isTrue != true {
|
|
response.Result(101, isTrue, "您的身份令牌已经失效!请重新登录获取身份令牌!", c)
|
|
return
|
|
}
|
|
userKey, userKeyErr := strconv.ParseInt(SysAdminCont.UserKey, 10, 64)
|
|
groupId, groupIdErr := strconv.ParseInt(SysAdminCont.Group, 10, 64)
|
|
if groupIdErr != nil && userKeyErr != nil {
|
|
response.Result(102, isTrue, "您的身份令牌已经失效!请重新登录获取身份令牌!", c)
|
|
return
|
|
}
|
|
var requestData addSystemRoleType
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
response.Result(103, requestData, "参数错误!请重新提交", c)
|
|
return
|
|
}
|
|
if requestData.Name == "" {
|
|
response.Result(104, requestData, "请输入角色名称!", c)
|
|
return
|
|
}
|
|
if requestData.Group == 0 {
|
|
response.Result(105, requestData, "请选择角色归属!", c)
|
|
return
|
|
}
|
|
var systemRoleCont systemuser.SystemRole
|
|
systemRoleCont.Title = requestData.Name
|
|
systemRoleCont.State = 1
|
|
systemRoleCont.Time = time.Now().Unix()
|
|
systemRoleCont.UserId = userKey
|
|
|
|
godeVal := 1 //继承属性
|
|
if groupId == 1 {
|
|
|
|
systemRoleCont.MenuOper = `["7","8","9","10","11","12","17"]`
|
|
systemRoleCont.Jurisdiction = `["65","66","69","70","74","59","60","63"]`
|
|
systemRoleCont.Wand = 15
|
|
if requestData.Group > 1 {
|
|
systemRoleCont.Gode = 2
|
|
} else {
|
|
systemRoleCont.Gode = 1
|
|
}
|
|
switch requestData.Group {
|
|
case 1:
|
|
systemRoleCont.Attribute = int64(requestData.GroupSun)
|
|
systemRoleCont.Group = int64(requestData.Group)
|
|
case -1:
|
|
if requestData.GroupSun == 0 {
|
|
response.Result(106, requestData, "请选择角色归属!", c)
|
|
return
|
|
}
|
|
systemRoleCont.Attribute = 0
|
|
systemRoleCont.Group = int64(requestData.GroupSun)
|
|
default:
|
|
systemRoleCont.Attribute = int64(requestData.GroupSun)
|
|
systemRoleCont.Group = int64(requestData.Group)
|
|
}
|
|
} else {
|
|
godeVal = 2
|
|
systemRoleCont.Gode = godeVal
|
|
systemRoleCont.MenuOper = `["7","8","9","10","17","18","20","39","11","12","13","14","15","21","22","23","24","27","28","29","30","38","37","41","1","16","31","19","6","5"]`
|
|
systemRoleCont.Jurisdiction = `["65","66","67","68","69","70","71","72","73","74","75","76","77","78","79","80","81","59","60","61","62","63","64","48","49","50","51","52","53","54","55","56","57","58","21","22","23","24","25","27","82","83","84","85","86","28","29","30","31","32","33","34","35","36","37","38"]`
|
|
systemRoleCont.Wand = 86
|
|
systemRoleCont.Attribute = int64(requestData.GroupSun)
|
|
systemRoleCont.Group = int64(requestData.Group)
|
|
}
|
|
addErr := global.GVA_DB_Master.Create(&systemRoleCont).Error
|
|
if addErr != nil {
|
|
response.Result(106, requestData, "添加失败!", c)
|
|
} else {
|
|
response.Result(0, systemRoleCont, "添加成功!", c)
|
|
}
|
|
}
|
|
|
|
// 修改系统角色
|
|
func (s *SystemRoleApi) EiteSystemRole(c *gin.Context) {
|
|
isTrue, SysAdminCont := commonus.AdminClientIdentity()
|
|
if isTrue != true {
|
|
response.Result(101, isTrue, "您的身份令牌已经失效!请重新登录获取身份令牌!", c)
|
|
return
|
|
}
|
|
// _, userKeyErr := strconv.ParseInt(SysAdminCont.UserKey, 10, 64)
|
|
groupId, groupIdErr := strconv.ParseInt(SysAdminCont.Group, 10, 64)
|
|
if groupIdErr != nil {
|
|
response.Result(102, isTrue, "您的身份令牌已经失效!请重新登录获取身份令牌!", c)
|
|
return
|
|
}
|
|
var requestData eiteSystemRoleType
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
response.Result(103, requestData, "参数错误!请重新提交", c)
|
|
return
|
|
}
|
|
if requestData.Id == 0 {
|
|
response.Result(104, requestData, "参数错误!请重新提交!", c)
|
|
return
|
|
}
|
|
if requestData.Name == "" {
|
|
response.Result(105, requestData, "请输入角色名称!", c)
|
|
return
|
|
}
|
|
if requestData.Group == 0 {
|
|
response.Result(106, requestData, "请选择角色归属!", c)
|
|
return
|
|
}
|
|
eiteCont := commonus.MapOut()
|
|
eiteCont["r_title"] = requestData.Name
|
|
eiteCont["r_time"] = time.Now().Unix()
|
|
|
|
if groupId == 1 {
|
|
if requestData.Group > 1 {
|
|
eiteCont["r_gode"] = 2
|
|
} else {
|
|
eiteCont["r_gode"] = 1
|
|
}
|
|
switch requestData.Group {
|
|
case 1:
|
|
eiteCont["r_attribute"] = int64(requestData.GroupSun)
|
|
eiteCont["r_group"] = int64(requestData.Group)
|
|
case -1:
|
|
if requestData.GroupSun == 0 {
|
|
response.Result(106, requestData, "请选择角色归属!", c)
|
|
return
|
|
}
|
|
eiteCont["r_attribute"] = 0
|
|
eiteCont["r_group"] = int64(requestData.GroupSun)
|
|
default:
|
|
eiteCont["r_attribute"] = int64(requestData.GroupSun)
|
|
eiteCont["r_group"] = int64(requestData.Group)
|
|
}
|
|
} else {
|
|
eiteCont["r_attribute"] = int64(requestData.GroupSun)
|
|
eiteCont["r_group"] = int64(requestData.Group)
|
|
}
|
|
roleIsTrue, roleErr := eiteSystemRoleCont(requestData.Id, eiteCont)
|
|
// eiteErr := global.GVA_DB_Master.Model(&systemuser.SystemRole{}).Where("`r_id` = ?", requestData.Id).Updates(eiteCont).Error
|
|
if roleIsTrue != true {
|
|
response.Result(106, roleErr, "编辑失败!", c)
|
|
} else {
|
|
response.Result(0, eiteCont, "编辑成功!", c)
|
|
}
|
|
}
|
|
|
|
// 删除系统角色
|
|
func (s *SystemRoleApi) DelSystemRole(c *gin.Context) {
|
|
|
|
var requestData delSystemRoleType
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
response.Result(103, requestData, "参数错误!请重新提交", c)
|
|
return
|
|
}
|
|
if requestData.Id == 0 && requestData.OutId == "" {
|
|
response.Result(102, err, "参数错误!请重新提交!", c)
|
|
return
|
|
}
|
|
if requestData.OutId != "" {
|
|
idInt, inIntErr := strconv.ParseInt(requestData.OutId, 10, 64)
|
|
if inIntErr == nil {
|
|
requestData.Id = idInt
|
|
}
|
|
}
|
|
if requestData.State == 0 {
|
|
requestData.State = 1
|
|
}
|
|
saveData := commonus.MapOut()
|
|
saveData["r_set"] = requestData.State
|
|
saveData["r_time"] = time.Now().Unix()
|
|
if requestData.IsDel != 1 {
|
|
roleIsTrue, roleErr := eiteSystemRoleCont(requestData.Id, saveData)
|
|
if roleIsTrue != true {
|
|
response.Result(103, roleErr, "修改失败!", c)
|
|
return
|
|
}
|
|
response.Result(0, requestData, "修改成功!", c)
|
|
} else {
|
|
if requestData.State == 3 {
|
|
roleErr := global.GVA_DB_Master.Where("r_id = ?", requestData.Id).Delete(&systemuser.SystemRole{}).Error
|
|
if roleErr != nil {
|
|
response.Result(104, saveData, "删除失败!", c)
|
|
return
|
|
}
|
|
response.Result(0, saveData, "删除成功!", c)
|
|
} else {
|
|
roleIsTrue, roleErr := eiteSystemRoleCont(requestData.Id, saveData)
|
|
if roleIsTrue != true {
|
|
response.Result(103, roleErr, "修改失败!", c)
|
|
return
|
|
}
|
|
response.Result(0, requestData, "修改成功!", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
// 获取系统角色详情
|
|
func (s *SystemRoleApi) GetSystemRole(c *gin.Context) {
|
|
|
|
var requestData commonus.SetId
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
response.Result(103, requestData, "参数错误!请重新提交", c)
|
|
return
|
|
}
|
|
if requestData.Id == 0 && requestData.OutId == "" {
|
|
response.Result(102, err, "参数错误!请重新提交!", c)
|
|
return
|
|
}
|
|
if requestData.OutId != "" {
|
|
idInt, inIntErr := strconv.ParseInt(requestData.OutId, 10, 64)
|
|
if inIntErr == nil {
|
|
requestData.Id = idInt
|
|
}
|
|
}
|
|
var systemRoleCont systemuser.SystemRole
|
|
contErr := global.GVA_DB_Master.Where("`r_id` = ?", requestData.Id).First(&systemRoleCont).Error
|
|
if contErr != nil {
|
|
response.Result(102, err, "数据获取失败!", c)
|
|
return
|
|
}
|
|
var printRoleCont printRoleList
|
|
printRoleCont.Id = systemRoleCont.Id
|
|
printRoleCont.Title = systemRoleCont.Title
|
|
printRoleCont.State = systemRoleCont.State
|
|
printRoleCont.Attribute = systemRoleCont.Attribute
|
|
printRoleCont.Gode = systemRoleCont.Gode
|
|
printRoleCont.Time = systemRoleCont.Time
|
|
printRoleCont.UserId = systemRoleCont.UserId
|
|
printRoleCont.Jurisdiction = systemRoleCont.Jurisdiction
|
|
printRoleCont.MenuOper = systemRoleCont.MenuOper
|
|
printRoleCont.Wand = systemRoleCont.Wand
|
|
printRoleCont.Group = systemRoleCont.Group
|
|
|
|
where := commonus.MapOut()
|
|
where["id"] = systemRoleCont.Group
|
|
orgCont, _ := commonus.GetNewOrgCont(where, "id", "name")
|
|
printRoleCont.GroupName = orgCont.Name
|
|
|
|
whereDepart := commonus.MapOut()
|
|
whereDepart["id"] = systemRoleCont.Attribute
|
|
orgContDepart, _ := commonus.GetNewOrgCont(whereDepart, "id", "name")
|
|
printRoleCont.BranchFactoryName = orgContDepart.Name
|
|
|
|
// groupIsTrue, groupCont := commonus.GetGroupCont(systemRoleCont.Group)
|
|
// if groupIsTrue == true {
|
|
// printRoleCont.GroupName = groupCont.Name
|
|
// }
|
|
// bfIsTrue, bfCont := commonus.GetBranchFactory(systemRoleCont.Attribute)
|
|
// if bfIsTrue == true {
|
|
// printRoleCont.BranchFactoryName = bfCont.Name
|
|
// }
|
|
json.Unmarshal([]byte(systemRoleCont.Jurisdiction), &printRoleCont.JurisdictionPower)
|
|
json.Unmarshal([]byte(systemRoleCont.MenuOper), &printRoleCont.MenuoperPower)
|
|
response.Result(0, printRoleCont, "数据获取成功!", c)
|
|
}
|
|
|
|
// 编辑角色数据处理
|
|
func eiteSystemRoleCont(saveId int64, saveData map[string]interface{}) (isTrue bool, infoErr error) {
|
|
isTrue = false
|
|
infoErr = global.GVA_DB_Master.Model(&systemuser.SystemRole{}).Where("`r_id` = ?", saveId).Updates(saveData).Error
|
|
if infoErr != nil {
|
|
return
|
|
}
|
|
isTrue = true
|
|
return
|
|
}
|
|
|
|
// 获取角色权限配置清单
|
|
func (s *SystemRoleApi) GetPowerConfig(c *gin.Context) {
|
|
var requestData commonus.SetId
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
response.Result(103, requestData, "参数错误!请重新提交", c)
|
|
return
|
|
}
|
|
if requestData.Id == 0 && requestData.OutId == "" {
|
|
response.Result(102, err, "参数错误!请重新提交!", c)
|
|
return
|
|
}
|
|
if requestData.OutId != "" {
|
|
idInt, inIntErr := strconv.ParseInt(requestData.OutId, 10, 64)
|
|
if inIntErr == nil {
|
|
requestData.Id = idInt
|
|
}
|
|
}
|
|
var systemRoleCont systemuser.SystemRole
|
|
contErr := global.GVA_DB_Master.Where("`r_id` = ?", requestData.Id).First(&systemRoleCont).Error
|
|
if contErr != nil {
|
|
response.Result(102, err, "数据获取失败!", c)
|
|
return
|
|
}
|
|
_, datahhh := GetMenuOperation(systemRoleCont.MenuOper, systemRoleCont.Jurisdiction)
|
|
response.Result(0, datahhh, "数据获取失败!", c)
|
|
}
|
|
|
|
// 遍历确权
|
|
func GetMenuOperation(menuStr, menuOperStr string) (isTrue bool, sendData []powerConfigList) {
|
|
isTrue = false
|
|
var menuStrMap []string
|
|
var menuOperStrMap []string
|
|
|
|
json.Unmarshal([]byte(menuStr), &menuStrMap)
|
|
json.Unmarshal([]byte(menuOperStr), &menuOperStrMap)
|
|
|
|
var systemMenuList []systemuser.SystemMenu
|
|
menuOperErr := global.GVA_DB_Master.Where("`m_steat` IN ?", []int{1, 2}).Order("m_sort asc").Order("m_id desc").Find(&systemMenuList).Error
|
|
if menuOperErr != nil {
|
|
return
|
|
}
|
|
|
|
sendData = GetMenuPowerThree(1, 0, systemMenuList, menuStrMap, menuOperStrMap)
|
|
return
|
|
}
|
|
|
|
// 递归无限树
|
|
func GetMenuPowerThree(jurisd int, parentId int64, threeData []systemuser.SystemMenu, menuStrMap, menuOperStrMap []string) []powerConfigList {
|
|
// treeList := []SystemMenuThree{}
|
|
treeListPower := []powerConfigList{}
|
|
for _, v := range threeData {
|
|
if v.ParentId == parentId {
|
|
var powerConfigCont powerConfigList
|
|
powerConfigCont.Id = v.Id
|
|
powerConfigCont.Title = v.Title
|
|
powerConfigCont.IsTrue = JudeInArray(v.Id, menuStrMap)
|
|
child := GetMenuPowerThree(jurisd, v.Id, threeData, menuStrMap, menuOperStrMap)
|
|
powerConfigCont.Child = child
|
|
isTrue, menuOper := MenuOperation(jurisd, v.Id)
|
|
if isTrue == true {
|
|
var operList []powerCont
|
|
for _, oper_val := range menuOper {
|
|
var operCont powerCont
|
|
operCont.Id = oper_val.OperId
|
|
operCont.Title = oper_val.OperTitle
|
|
operCont.IsTrue = JudeInArray(oper_val.OperId, menuOperStrMap)
|
|
operList = append(operList, operCont)
|
|
}
|
|
powerConfigCont.MenuOperation = operList
|
|
}
|
|
|
|
treeListPower = append(treeListPower, powerConfigCont)
|
|
}
|
|
}
|
|
return treeListPower
|
|
}
|
|
|
|
// 判断值是否在数组中
|
|
func JudeInArray(id int64, ary []string) (isTrue bool) {
|
|
isTrue = false
|
|
for _, val := range ary {
|
|
valint64, err := strconv.ParseInt(val, 10, 64)
|
|
if err == nil {
|
|
if id == valint64 {
|
|
isTrue = true
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// 配置角色权限
|
|
func (s *SystemRoleApi) SetPowerConfig(c *gin.Context) {
|
|
var requestData SetPowerCont
|
|
err := c.ShouldBindJSON(&requestData)
|
|
if err != nil {
|
|
response.Result(103, requestData, "参数错误!请重新提交", c)
|
|
return
|
|
}
|
|
if requestData.Id == 0 && requestData.OutId == "" {
|
|
response.Result(102, err, "参数错误!请重新提交!", c)
|
|
return
|
|
}
|
|
if requestData.OutId != "" {
|
|
idInt, inIntErr := strconv.ParseInt(requestData.OutId, 10, 64)
|
|
if inIntErr == nil {
|
|
requestData.Id = idInt
|
|
}
|
|
}
|
|
eiteCont := commonus.MapOut()
|
|
eiteCont["r_menu_oper"] = requestData.Menu
|
|
eiteCont["r_jurisdiction"] = requestData.MenuOper
|
|
eiteCont["r_time"] = time.Now().Unix()
|
|
roleIsTrue, roleErr := eiteSystemRoleCont(requestData.Id, eiteCont)
|
|
// eiteErr := global.GVA_DB_Master.Model(&systemuser.SystemRole{}).Where("`r_id` = ?", requestData.Id).Updates(eiteCont).Error
|
|
if roleIsTrue != true {
|
|
response.Result(106, roleErr, "编辑失败!", c)
|
|
} else {
|
|
response.Result(0, eiteCont, "编辑成功!", c)
|
|
}
|
|
}
|
|
|