feat: 系统模块多语言

This commit is contained in:
TsMask
2023-11-20 18:55:33 +08:00
parent d52945c946
commit 5604bd9b9d
23 changed files with 957 additions and 527 deletions

View File

@@ -7,7 +7,7 @@ import (
"time"
"ems.agt/src/framework/constants/admin"
"ems.agt/src/framework/constants/roledatascope"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/file"
"ems.agt/src/framework/utils/parse"
@@ -21,8 +21,9 @@ import (
// 实例化控制层 SysRoleController 结构体
var NewSysRole = &SysRoleController{
sysRoleService: service.NewSysRoleImpl,
sysUserService: service.NewSysUserImpl,
sysRoleService: service.NewSysRoleImpl,
sysUserService: service.NewSysUserImpl,
sysDictDataService: service.NewSysDictDataImpl,
}
// 角色信息
@@ -33,6 +34,8 @@ type SysRoleController struct {
sysRoleService service.ISysRole
// 用户服务
sysUserService service.ISysUser
// 字典数据服务
sysDictDataService service.ISysDictData
}
// 角色列表
@@ -42,6 +45,18 @@ func (s *SysRoleController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
data := s.sysRoleService.SelectRolePage(querys, dataScopeSQL)
rows := data["rows"].([]model.SysRole)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
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))
}
@@ -49,13 +64,16 @@ func (s *SysRoleController) List(c *gin.Context) {
//
// 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, "parameter error"))
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
}
@@ -66,10 +84,11 @@ func (s *SysRoleController) Info(c *gin.Context) {
//
// 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, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -77,7 +96,7 @@ func (s *SysRoleController) Add(c *gin.Context) {
uniqueRoleName := s.sysRoleService.CheckUniqueRoleName(body.RoleName, "")
if !uniqueRoleName {
// 角色新增【%s】失败角色名称已存在
msg := fmt.Sprintf("Character addition [%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
}
@@ -86,7 +105,7 @@ func (s *SysRoleController) Add(c *gin.Context) {
uniqueRoleKey := s.sysRoleService.CheckUniqueRoleKey(body.RoleKey, "")
if !uniqueRoleKey {
// 角色新增【%s】失败角色键值已存在
msg := fmt.Sprintf("Character addition [%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
}
@@ -104,17 +123,18 @@ func (s *SysRoleController) Add(c *gin.Context) {
//
// 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, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查是否管理员角色
if body.RoleID == admin.ROLE_ID {
// 不允许操作管理员角色
c.JSON(200, result.ErrMsg("The administrator role is not allowed to operate"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
return
}
@@ -122,15 +142,16 @@ func (s *SysRoleController) Edit(c *gin.Context) {
role := s.sysRoleService.SelectRoleById(body.RoleID)
if role.RoleID != body.RoleID {
// 没有可访问角色数据!
c.JSON(200, result.ErrMsg("There is no accessible role data!"))
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)
// 操作角色【%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
}
@@ -139,7 +160,8 @@ func (s *SysRoleController) Edit(c *gin.Context) {
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 := 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
}
@@ -157,9 +179,10 @@ func (s *SysRoleController) Edit(c *gin.Context) {
//
// 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, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
@@ -173,7 +196,7 @@ func (s *SysRoleController) Remove(c *gin.Context) {
for _, id := range uniqueIDs {
if id == admin.ROLE_ID {
// 不允许操作管理员角色
c.JSON(200, result.ErrMsg("The administrator role is not allowed to operate"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
return
}
}
@@ -182,7 +205,7 @@ func (s *SysRoleController) Remove(c *gin.Context) {
c.JSON(200, result.ErrMsg(err.Error()))
return
}
msg := fmt.Sprintf("Deleted successfully: %d", rows)
msg := i18n.TTemplate(language, "app.common.deleteSuccess", map[string]any{"num": rows})
c.JSON(200, result.OkMsg(msg))
}
@@ -190,6 +213,7 @@ func (s *SysRoleController) Remove(c *gin.Context) {
//
// PUT /changeStatus
func (s *SysRoleController) Status(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 角色ID
RoleID string `json:"roleId" binding:"required"`
@@ -198,14 +222,14 @@ func (s *SysRoleController) Status(c *gin.Context) {
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查是否管理员角色
if body.RoleID == admin.ROLE_ID {
// 不允许操作管理员角色
c.JSON(200, result.ErrMsg("The administrator role is not allowed to operate"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
return
}
@@ -213,14 +237,14 @@ func (s *SysRoleController) Status(c *gin.Context) {
role := s.sysRoleService.SelectRoleById(body.RoleID)
if role.RoleID != body.RoleID {
// 没有可访问角色数据!
c.JSON(200, result.ErrMsg("There is no accessible role data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
return
}
// 与旧值相等不变更
if role.Status == body.Status {
// 变更状态与旧值相等!
c.JSON(200, result.ErrMsg("The change status is equal to the old value!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.statusEq")))
return
}
@@ -243,6 +267,7 @@ func (s *SysRoleController) Status(c *gin.Context) {
//
// PUT /dataScope
func (s *SysRoleController) DataScope(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 角色ID
RoleID string `json:"roleId"`
@@ -255,14 +280,14 @@ func (s *SysRoleController) DataScope(c *gin.Context) {
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查是否管理员角色
if body.RoleID == admin.ROLE_ID {
// 不允许操作管理员角色
c.JSON(200, result.ErrMsg("The administrator role is not allowed to operate"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "app.common.errOperateRole")))
return
}
@@ -270,7 +295,7 @@ func (s *SysRoleController) DataScope(c *gin.Context) {
role := s.sysRoleService.SelectRoleById(body.RoleID)
if role.RoleID != body.RoleID {
// 没有可访问角色数据!
c.JSON(200, result.ErrMsg("There is no accessible role data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
return
}
@@ -295,10 +320,11 @@ func (s *SysRoleController) DataScope(c *gin.Context) {
//
// 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, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -306,7 +332,7 @@ func (s *SysRoleController) AuthUserAllocatedList(c *gin.Context) {
role := s.sysRoleService.SelectRoleById(roleId.(string))
if role.RoleID != roleId {
// 没有可访问角色数据!
c.JSON(200, result.ErrMsg("There is no accessible role data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
return
}
@@ -319,6 +345,7 @@ func (s *SysRoleController) AuthUserAllocatedList(c *gin.Context) {
//
// PUT /authUser/checked
func (s *SysRoleController) AuthUserChecked(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 角色ID
RoleID string `json:"roleId" binding:"required"`
@@ -329,7 +356,7 @@ func (s *SysRoleController) AuthUserChecked(c *gin.Context) {
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -344,7 +371,7 @@ func (s *SysRoleController) AuthUserChecked(c *gin.Context) {
// 检查是否存在
role := s.sysRoleService.SelectRoleById(body.RoleID)
if role.RoleID != body.RoleID {
c.JSON(200, result.ErrMsg("There is no accessible role data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "role.noData")))
return
}
@@ -365,41 +392,56 @@ func (s *SysRoleController) AuthUserChecked(c *gin.Context) {
//
// POST /export
func (s *SysRoleController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制
querys := ctx.BodyJSONMap(c)
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
data := s.sysRoleService.SelectRolePage(querys, dataScopeSQL)
if data["total"].(int64) == 0 {
// 导出数据记录为空
c.JSON(200, result.ErrMsg("Export data record is empty"))
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": "RoleID",
"B1": "RoleName",
"C1": "RoleKey",
"D1": "RoleSort",
"E1": "DataScope",
"F1": "Status",
"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.dataScope"),
"F1": i18n.TKey(language, "role.export.status"),
}
// 读取系统角色数据范围字典数据
dictSysRoleDatascope := s.sysDictDataService.SelectDictDataByType("sys_role_datascope")
// 从第二行开始的数据
dataCells := make([]map[string]any, 0)
for i, row := range rows {
idx := strconv.Itoa(i + 2)
// 数据范围
dataScope := "Empty"
if v, ok := roledatascope.RoleDataScope[row.DataScope]; ok {
dataScope = v
dataScope := row.DataScope
for _, v := range dictSysRoleDatascope {
if row.DataScope == v.DictValue {
dataScope = i18n.TKey(language, v.DictLabel)
break
}
}
// 角色状态
statusValue := "deactivate"
statusValue := i18n.TKey(language, "dictData.disable")
if row.Status == "1" {
statusValue = "normalcy"
statusValue = i18n.TKey(language, "dictData.normal")
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.RoleID,