499 lines
14 KiB
Go
499 lines
14 KiB
Go
package controller
|
||
|
||
import (
|
||
"fmt"
|
||
"strconv"
|
||
"strings"
|
||
"time"
|
||
|
||
"be.ems/src/framework/constants/admin"
|
||
"be.ems/src/framework/i18n"
|
||
"be.ems/src/framework/utils/ctx"
|
||
"be.ems/src/framework/utils/date"
|
||
"be.ems/src/framework/utils/file"
|
||
"be.ems/src/framework/utils/parse"
|
||
"be.ems/src/framework/vo/result"
|
||
"be.ems/src/modules/system/model"
|
||
"be.ems/src/modules/system/service"
|
||
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/gin-gonic/gin/binding"
|
||
)
|
||
|
||
// 实例化控制层 SysRoleController 结构体
|
||
var NewSysRole = &SysRoleController{
|
||
sysRoleService: service.NewSysRoleImpl,
|
||
sysUserService: service.NewSysUserImpl,
|
||
sysDictDataService: service.NewSysDictData,
|
||
}
|
||
|
||
// 角色信息
|
||
//
|
||
// PATH /system/role
|
||
type SysRoleController struct {
|
||
// 角色服务
|
||
sysRoleService service.ISysRole
|
||
// 用户服务
|
||
sysUserService service.ISysUser
|
||
sysDictDataService *service.SysDictData // 字典数据服务
|
||
}
|
||
|
||
// 角色列表
|
||
//
|
||
// GET /list
|
||
//
|
||
// @Tags system/role
|
||
// @Accept json
|
||
// @Produce json
|
||
// @Param roleName query string false "roleName"
|
||
// @Param pageNum query number true "pageNum" default(1)
|
||
// @Param pageSize query number true "pageSize" default(10)
|
||
// @Success 200 {object} object "Response Results"
|
||
// @Security TokenAuth
|
||
// @Summary Role Information List
|
||
// @Description Role Information List
|
||
// @Router /system/role/list [get]
|
||
func (s *SysRoleController) List(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
querys := ctx.QueryMap(c)
|
||
// 多语言值转key查询
|
||
if v, ok := querys["roleName"]; ok && v != "" {
|
||
querys["roleName"] = i18n.TFindKeyPrefix(language, "role", v.(string))
|
||
}
|
||
|
||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||
data := s.sysRoleService.SelectRolePage(querys, dataScopeSQL)
|
||
rows := data["rows"].([]model.SysRole)
|
||
|
||
// 闭包函数处理多语言
|
||
converI18n := func(language string, arr *[]model.SysRole) {
|
||
for i := range *arr {
|
||
(*arr)[i].RoleName = i18n.TKey(language, (*arr)[i].RoleName)
|
||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||
}
|
||
}
|
||
converI18n(language, &rows)
|
||
|
||
c.JSON(200, result.Ok(data))
|
||
}
|
||
|
||
// 角色信息详情
|
||
//
|
||
// GET /:roleId
|
||
func (s *SysRoleController) Info(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
roleId := c.Param("roleId")
|
||
if roleId == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
data := s.sysRoleService.SelectRoleById(roleId)
|
||
if data.RoleID == roleId {
|
||
data.RoleName = i18n.TKey(language, data.RoleName)
|
||
data.Remark = i18n.TKey(language, data.Remark)
|
||
c.JSON(200, result.OkData(data))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 角色信息新增
|
||
//
|
||
// POST /
|
||
func (s *SysRoleController) Add(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body model.SysRole
|
||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||
if err != nil || body.RoleID != "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 判断角色名称是否唯一
|
||
uniqueRoleName := s.sysRoleService.CheckUniqueRoleName(body.RoleName, "")
|
||
if !uniqueRoleName {
|
||
// 角色新增【%s】失败,角色名称已存在
|
||
msg := i18n.TTemplate(language, "role.errNameExists", map[string]any{"name": body.RoleName})
|
||
c.JSON(200, result.ErrMsg(msg))
|
||
return
|
||
}
|
||
|
||
// 判断角色键值是否唯一
|
||
uniqueRoleKey := s.sysRoleService.CheckUniqueRoleKey(body.RoleKey, "")
|
||
if !uniqueRoleKey {
|
||
// 角色新增【%s】失败,角色键值已存在
|
||
msg := i18n.TTemplate(language, "role.errKeyExists", map[string]any{"name": body.RoleName})
|
||
c.JSON(200, result.ErrMsg(msg))
|
||
return
|
||
}
|
||
|
||
body.CreateBy = ctx.LoginUserToUserName(c)
|
||
insertId := s.sysRoleService.InsertRole(body)
|
||
if insertId != "" {
|
||
c.JSON(200, result.Ok(nil))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 角色信息修改
|
||
//
|
||
// PUT /
|
||
func (s *SysRoleController) Edit(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body model.SysRole
|
||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||
if err != nil || body.RoleID == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 检查是否管理员角色
|
||
if body.RoleID == admin.ROLE_ID {
|
||
// 不允许操作管理员角色
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||
return
|
||
}
|
||
|
||
// 检查是否存在
|
||
roleInfo := s.sysRoleService.SelectRoleById(body.RoleID)
|
||
if roleInfo.RoleID != body.RoleID {
|
||
// 没有可访问角色数据!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||
return
|
||
}
|
||
|
||
// 判断角色名称是否唯一
|
||
uniqueRoleName := s.sysRoleService.CheckUniqueRoleName(body.RoleName, body.RoleID)
|
||
if !uniqueRoleName {
|
||
// 操作角色【%s】失败,角色名称已存在
|
||
// msg := fmt.Sprintf("Character modification [%s] failed, character name already exists", body.RoleName)
|
||
msg := i18n.TTemplate(language, "role.errNameExists", map[string]any{"name": body.RoleName})
|
||
c.JSON(200, result.ErrMsg(msg))
|
||
return
|
||
}
|
||
|
||
// 判断角色键值是否唯一
|
||
uniqueRoleKey := s.sysRoleService.CheckUniqueRoleKey(body.RoleKey, body.RoleID)
|
||
if !uniqueRoleKey {
|
||
// 角色修改【%s】失败,角色键值已存在
|
||
// msg := fmt.Sprintf("Character modification [%s] failed, character key already exists", body.RoleName)
|
||
msg := i18n.TTemplate(language, "role.errKeyExists", map[string]any{"name": body.RoleName})
|
||
c.JSON(200, result.ErrMsg(msg))
|
||
return
|
||
}
|
||
|
||
// 多语言非原始值
|
||
i18nValue := i18n.TKey(language, roleInfo.RoleName)
|
||
if i18nValue != roleInfo.RoleName {
|
||
i18n.UpdateKeyValue(language, roleInfo.RoleName, body.RoleName)
|
||
body.RoleName = roleInfo.RoleName
|
||
}
|
||
// 多语言非原始值
|
||
i18nValue2 := i18n.TKey(language, roleInfo.Remark)
|
||
if i18nValue2 != roleInfo.Remark {
|
||
i18n.UpdateKeyValue(language, roleInfo.Remark, body.Remark)
|
||
body.Remark = roleInfo.Remark
|
||
}
|
||
|
||
body.UpdateBy = ctx.LoginUserToUserName(c)
|
||
rows := s.sysRoleService.UpdateRole(body)
|
||
if rows > 0 {
|
||
c.JSON(200, result.Ok(nil))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 角色信息删除
|
||
//
|
||
// DELETE /:roleIds
|
||
func (s *SysRoleController) Remove(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
roleIds := c.Param("roleIds")
|
||
if roleIds == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
// 处理字符转id数组后去重
|
||
ids := strings.Split(roleIds, ",")
|
||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||
if len(uniqueIDs) <= 0 {
|
||
c.JSON(200, result.Err(nil))
|
||
return
|
||
}
|
||
// 检查是否管理员角色
|
||
for _, id := range uniqueIDs {
|
||
if id == admin.ROLE_ID {
|
||
// 不允许操作管理员角色
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||
return
|
||
}
|
||
}
|
||
rows, err := s.sysRoleService.DeleteRoleByIds(uniqueIDs)
|
||
if err != nil {
|
||
c.JSON(200, result.ErrMsg(err.Error()))
|
||
return
|
||
}
|
||
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
|
||
c.JSON(200, result.OkMsg(msg))
|
||
}
|
||
|
||
// 角色状态变更
|
||
//
|
||
// PUT /changeStatus
|
||
func (s *SysRoleController) Status(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body struct {
|
||
// 角色ID
|
||
RoleID string `json:"roleId" binding:"required"`
|
||
// 状态
|
||
Status string `json:"status" binding:"required"`
|
||
}
|
||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||
if err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 检查是否管理员角色
|
||
if body.RoleID == admin.ROLE_ID {
|
||
// 不允许操作管理员角色
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||
return
|
||
}
|
||
|
||
// 检查是否存在
|
||
role := s.sysRoleService.SelectRoleById(body.RoleID)
|
||
if role.RoleID != body.RoleID {
|
||
// 没有可访问角色数据!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||
return
|
||
}
|
||
|
||
// 与旧值相等不变更
|
||
if role.Status == body.Status {
|
||
// 变更状态与旧值相等!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.statusEq")))
|
||
return
|
||
}
|
||
|
||
// 更新状态不刷新缓存
|
||
userName := ctx.LoginUserToUserName(c)
|
||
SysRoleController := model.SysRole{
|
||
RoleID: body.RoleID,
|
||
Status: body.Status,
|
||
UpdateBy: userName,
|
||
}
|
||
rows := s.sysRoleService.UpdateRole(SysRoleController)
|
||
if rows > 0 {
|
||
c.JSON(200, result.Ok(nil))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 角色数据权限修改
|
||
//
|
||
// PUT /dataScope
|
||
func (s *SysRoleController) DataScope(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body struct {
|
||
// 角色ID
|
||
RoleID string `json:"roleId"`
|
||
// 部门组(数据权限)
|
||
DeptIds []string `json:"deptIds"`
|
||
// 数据范围(1:全部数据权限 2:自定数据权限 3:本部门数据权限 4:本部门及以下数据权限 5:仅本人数据权限)
|
||
DataScope string `json:"dataScope"`
|
||
// 部门树选择项是否关联显示(0:父子不互相关联显示 1:父子互相关联显示)
|
||
DeptCheckStrictly string `json:"deptCheckStrictly"`
|
||
}
|
||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||
if err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 检查是否管理员角色
|
||
if body.RoleID == admin.ROLE_ID {
|
||
// 不允许操作管理员角色
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
|
||
return
|
||
}
|
||
|
||
// 检查是否存在
|
||
role := s.sysRoleService.SelectRoleById(body.RoleID)
|
||
if role.RoleID != body.RoleID {
|
||
// 没有可访问角色数据!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||
return
|
||
}
|
||
|
||
// 更新数据权限
|
||
userName := ctx.LoginUserToUserName(c)
|
||
SysRoleController := model.SysRole{
|
||
RoleID: body.RoleID,
|
||
DeptIds: body.DeptIds,
|
||
DataScope: body.DataScope,
|
||
DeptCheckStrictly: body.DeptCheckStrictly,
|
||
UpdateBy: userName,
|
||
}
|
||
rows := s.sysRoleService.AuthDataScope(SysRoleController)
|
||
if rows > 0 {
|
||
c.JSON(200, result.Ok(nil))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 角色分配用户列表
|
||
//
|
||
// GET /authUser/allocatedList
|
||
func (s *SysRoleController) AuthUserAllocatedList(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
querys := ctx.QueryMap(c)
|
||
roleId, ok := querys["roleId"]
|
||
if !ok || roleId == "" {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 检查是否存在
|
||
role := s.sysRoleService.SelectRoleById(roleId.(string))
|
||
if role.RoleID != roleId {
|
||
// 没有可访问角色数据!
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||
return
|
||
}
|
||
|
||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "u")
|
||
data := s.sysUserService.SelectAllocatedPage(querys, dataScopeSQL)
|
||
c.JSON(200, result.Ok(data))
|
||
}
|
||
|
||
// 角色分配选择授权
|
||
//
|
||
// PUT /authUser/checked
|
||
func (s *SysRoleController) AuthUserChecked(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
var body struct {
|
||
// 角色ID
|
||
RoleID string `json:"roleId" binding:"required"`
|
||
// 用户ID组
|
||
UserIDs string `json:"userIds" binding:"required"`
|
||
// 选择操作 添加true 取消false
|
||
Checked bool `json:"checked"`
|
||
}
|
||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||
if err != nil {
|
||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||
return
|
||
}
|
||
|
||
// 处理字符转id数组后去重
|
||
ids := strings.Split(body.UserIDs, ",")
|
||
uniqueIDs := parse.RemoveDuplicates(ids)
|
||
if len(uniqueIDs) <= 0 {
|
||
c.JSON(200, result.Err(nil))
|
||
return
|
||
}
|
||
|
||
// 检查是否存在
|
||
role := s.sysRoleService.SelectRoleById(body.RoleID)
|
||
if role.RoleID != body.RoleID {
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
|
||
return
|
||
}
|
||
|
||
var rows int64
|
||
if body.Checked {
|
||
rows = s.sysRoleService.InsertAuthUsers(body.RoleID, uniqueIDs)
|
||
} else {
|
||
rows = s.sysRoleService.DeleteAuthUsers(body.RoleID, uniqueIDs)
|
||
}
|
||
if rows > 0 {
|
||
c.JSON(200, result.Ok(nil))
|
||
return
|
||
}
|
||
c.JSON(200, result.Err(nil))
|
||
}
|
||
|
||
// 导出角色信息
|
||
//
|
||
// POST /export
|
||
func (s *SysRoleController) Export(c *gin.Context) {
|
||
language := ctx.AcceptLanguage(c)
|
||
// 查询结果,根据查询条件结果,单页最大值限制
|
||
querys := ctx.BodyJSONMap(c)
|
||
querys["pageNum"] = 1
|
||
querys["pageSize"] = 10000
|
||
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
|
||
data := s.sysRoleService.SelectRolePage(querys, dataScopeSQL)
|
||
if parse.Number(data["total"]) == 0 {
|
||
// 导出数据记录为空
|
||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.exportEmpty")))
|
||
return
|
||
}
|
||
rows := data["rows"].([]model.SysRole)
|
||
|
||
// 闭包函数处理多语言
|
||
converI18n := func(language string, arr *[]model.SysRole) {
|
||
for i := range *arr {
|
||
(*arr)[i].RoleName = i18n.TKey(language, (*arr)[i].RoleName)
|
||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||
}
|
||
}
|
||
converI18n(language, &rows)
|
||
|
||
// 导出文件名称
|
||
fileName := fmt.Sprintf("role_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||
// 第一行表头标题
|
||
headerCells := map[string]string{
|
||
"A1": i18n.TKey(language, "role.export.id"),
|
||
"B1": i18n.TKey(language, "role.export.name"),
|
||
"C1": i18n.TKey(language, "role.export.key"),
|
||
"D1": i18n.TKey(language, "role.export.sort"),
|
||
"E1": i18n.TKey(language, "role.export.status"),
|
||
"F1": i18n.TKey(language, "role.export.time"),
|
||
// "F1": i18n.TKey(language, "role.export.dataScope"),
|
||
}
|
||
// 读取系统角色数据范围字典数据
|
||
// dictSysRoleDatascope := s.sysDictDataService.SelectDictDataByType("sys_role_datascope")
|
||
// 从第二行开始的数据
|
||
dataCells := make([]map[string]any, 0)
|
||
for i, row := range rows {
|
||
idx := strconv.Itoa(i + 2)
|
||
// 数据范围
|
||
// dataScope := row.DataScope
|
||
// for _, v := range dictSysRoleDatascope {
|
||
// if row.DataScope == v.DictValue {
|
||
// dataScope = i18n.TKey(language, v.DictLabel)
|
||
// break
|
||
// }
|
||
// }
|
||
// 角色状态
|
||
statusValue := i18n.TKey(language, "dictData.disable")
|
||
if row.Status == "1" {
|
||
statusValue = i18n.TKey(language, "dictData.normal")
|
||
}
|
||
dataCells = append(dataCells, map[string]any{
|
||
"A" + idx: row.RoleID,
|
||
"B" + idx: row.RoleName,
|
||
"C" + idx: row.RoleKey,
|
||
"D" + idx: row.RoleSort,
|
||
"E" + idx: statusValue,
|
||
"F" + idx: date.ParseDateToStr(row.CreateTime, date.YYYY_MM_DDTHH_MM_SSZ),
|
||
// "F" + idx: dataScope,
|
||
})
|
||
}
|
||
|
||
// 导出数据表格
|
||
saveFilePath, err := file.WriteSheet(headerCells, dataCells, fileName, "")
|
||
if err != nil {
|
||
c.JSON(200, result.ErrMsg(err.Error()))
|
||
return
|
||
}
|
||
|
||
c.FileAttachment(saveFilePath, fileName)
|
||
}
|