1
0

feat: 多语言处理tkey

This commit is contained in:
TsMask
2023-11-21 14:42:37 +08:00
parent f490d9af0f
commit d5d3feb5fc
61 changed files with 1381 additions and 757 deletions

View File

@@ -1,13 +1,13 @@
package controller
import (
"fmt"
"ems.agt/src/framework/config"
"ems.agt/src/framework/constants/common"
"ems.agt/src/framework/constants/menu"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/regular"
"ems.agt/src/framework/vo"
"ems.agt/src/framework/vo/result"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/service"
@@ -46,6 +46,21 @@ func (s *SysMenuController) List(c *gin.Context) {
userId = "*"
}
data := s.sysMenuService.SelectMenuList(query, userId)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
var converI18n func(language string, arr *[]model.SysMenu)
converI18n = func(language string, arr *[]model.SysMenu) {
for i := range *arr {
(*arr)[i].MenuName = i18n.TKey(language, (*arr)[i].MenuName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
if len((*arr)[i].Children) > 0 {
converI18n(language, &(*arr)[i].Children)
}
}
}
converI18n(language, &data)
c.JSON(200, result.OkData(data))
}
@@ -53,13 +68,17 @@ func (s *SysMenuController) List(c *gin.Context) {
//
// GET /:menuId
func (s *SysMenuController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
menuId := c.Param("menuId")
if menuId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
data := s.sysMenuService.SelectMenuById(menuId)
if data.MenuID == menuId {
// 处理多语言
data.MenuName = i18n.TKey(language, data.MenuName)
data.Remark = i18n.TKey(language, data.Remark)
c.JSON(200, result.OkData(data))
return
}
@@ -70,10 +89,11 @@ func (s *SysMenuController) Info(c *gin.Context) {
//
// POST /
func (s *SysMenuController) Add(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysMenu
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.MenuID != "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -82,7 +102,7 @@ func (s *SysMenuController) Add(c *gin.Context) {
uniqueNenuPath := s.sysMenuService.CheckUniqueMenuPath(body.Path, body.ParentID, "")
if !uniqueNenuPath {
// 菜单新增【%s】失败菜单路由地址已存在
msg := fmt.Sprintf("Menu add [%s] failed, menu routing address already exists", body.MenuName)
msg := i18n.TTemplate(language, "menu.errPathExists", map[string]any{"name": body.MenuName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -92,15 +112,15 @@ func (s *SysMenuController) Add(c *gin.Context) {
uniqueNenuName := s.sysMenuService.CheckUniqueMenuName(body.MenuName, body.ParentID, "")
if !uniqueNenuName {
// 菜单新增【%s】失败菜单名称已存在
msg := fmt.Sprintf("Menu add [%s] failed, menu name already exists", body.MenuName)
msg := i18n.TTemplate(language, "menu.errNameExists", map[string]any{"name": body.MenuName})
c.JSON(200, result.ErrMsg(msg))
return
}
// 外链菜单需要符合网站http(s)开头
if body.IsFrame == common.STATUS_NO && !regular.ValidHttp(body.Path) {
// 菜单新增【%s】失败,非内部地址必须以http(s)://开头
msg := fmt.Sprintf("Menu adds [%s] failure, non-internal addresses must start with http(s)://", body.MenuName)
// 操作菜单【{name}】失败,非内部地址以http(s)://开头
msg := i18n.TTemplate(language, "menu.errFramePath", map[string]any{"name": body.MenuName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -118,17 +138,18 @@ func (s *SysMenuController) Add(c *gin.Context) {
//
// PUT /
func (s *SysMenuController) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysMenu
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.MenuID == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 上级菜单不能选自己
if body.MenuID == body.ParentID {
// 菜单修改【%s】失败上级菜单不能选择自己
msg := fmt.Sprintf("Menu modification [%s] fails, the parent menu cannot select itself", body.MenuName)
msg := i18n.TTemplate(language, "menu.errPathExists", map[string]any{"name": body.MenuName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -136,21 +157,22 @@ func (s *SysMenuController) Edit(c *gin.Context) {
// 检查数据是否存在
menuInfo := s.sysMenuService.SelectMenuById(body.MenuID)
if menuInfo.MenuID != body.MenuID {
// 没有可访问菜单数据
c.JSON(200, result.ErrMsg("No accessible menu data"))
// 没有可访问菜单数据
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.noData")))
return
}
// 父级ID不为0是要检查
if body.ParentID != "0" {
menuParent := s.sysMenuService.SelectMenuById(body.ParentID)
if menuParent.MenuID != body.ParentID {
c.JSON(200, result.ErrMsg("No accessible menu data"))
// 没有可访问菜单数据!
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.noData")))
return
}
// 禁用菜单时检查父菜单是否使用
if body.Status == common.STATUS_YES && menuParent.Status == common.STATUS_NO {
// 上级菜单未启用!
c.JSON(200, result.ErrMsg("The parent menu is not enabled!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.errParentStatus")))
return
}
}
@@ -160,7 +182,7 @@ func (s *SysMenuController) Edit(c *gin.Context) {
uniqueNenuPath := s.sysMenuService.CheckUniqueMenuPath(body.Path, body.ParentID, body.MenuID)
if !uniqueNenuPath {
// 菜单修改【%s】失败菜单路由地址已存在
msg := fmt.Sprintf("Menu modification [%s] failed, menu routing address already exists", body.MenuName)
msg := i18n.TTemplate(language, "menu.errPathExists", map[string]any{"name": body.MenuName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -170,7 +192,7 @@ func (s *SysMenuController) Edit(c *gin.Context) {
uniqueNenuName := s.sysMenuService.CheckUniqueMenuName(body.MenuName, body.ParentID, body.MenuID)
if !uniqueNenuName {
// 菜单修改【%s】失败菜单名称已存在
msg := fmt.Sprintf("Menu modification [%s] failed, menu name already exists", body.MenuName)
msg := i18n.TTemplate(language, "menu.errNameExists", map[string]any{"name": body.MenuName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -178,7 +200,7 @@ func (s *SysMenuController) Edit(c *gin.Context) {
// 外链菜单需要符合网站http(s)开头
if body.IsFrame == common.STATUS_NO && !regular.ValidHttp(body.Path) {
// 菜单修改【%s】失败非内部地址必须以http(s)://开头
msg := fmt.Sprintf("Menu change [%s] failed, non-internal address must start with http(s)://", body.MenuName)
msg := i18n.TTemplate(language, "menu.errFramePath", map[string]any{"name": body.MenuName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -187,8 +209,8 @@ func (s *SysMenuController) Edit(c *gin.Context) {
if body.Status == common.STATUS_NO {
hasStatus := s.sysMenuService.HasChildByMenuIdAndStatus(body.MenuID, common.STATUS_YES)
if hasStatus > 0 {
// 不允许禁用,存在使用子菜单数:%d
msg := fmt.Sprintf("Disabling is not allowed, number of submenus present for use: %d", hasStatus)
// 操作菜单【%s】失败,存在使用子菜单数:%d
msg := i18n.TTemplate(language, "menu.errHasChildUse", map[string]any{"name": body.MenuName, "num": hasStatus})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -207,9 +229,10 @@ func (s *SysMenuController) Edit(c *gin.Context) {
//
// DELETE /:menuId
func (s *SysMenuController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
menuId := c.Param("menuId")
if menuId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -217,7 +240,7 @@ func (s *SysMenuController) Remove(c *gin.Context) {
menu := s.sysMenuService.SelectMenuById(menuId)
if menu.MenuID != menuId {
// 没有可访问菜单数据!
c.JSON(200, result.ErrMsg("There is no accessible menu data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "menu.noData")))
return
}
@@ -225,7 +248,7 @@ func (s *SysMenuController) Remove(c *gin.Context) {
hasChild := s.sysMenuService.HasChildByMenuIdAndStatus(menuId, "")
if hasChild > 0 {
// 不允许删除,存在子菜单数:%d
msg := fmt.Sprintf("Deletion not allowed, number of submenus present: %d", hasChild)
msg := i18n.TTemplate(language, "menu.errHasChildUse", map[string]any{"name": menu.MenuName, "num": hasChild})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -234,14 +257,14 @@ func (s *SysMenuController) Remove(c *gin.Context) {
existRole := s.sysMenuService.CheckMenuExistRole(menuId)
if existRole > 0 {
// 不允许删除,菜单已分配给角色数:%d
msg := fmt.Sprintf("Deletion not allowed, number of roles assigned to the menu: %d", existRole)
msg := i18n.TTemplate(language, "menu.errHasRoleUse", map[string]any{"name": menu.MenuName, "num": existRole})
c.JSON(200, result.ErrMsg(msg))
return
}
rows := s.sysMenuService.DeleteMenuById(menuId)
if rows > 0 {
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))
return
}
@@ -265,6 +288,20 @@ func (s *SysMenuController) TreeSelect(c *gin.Context) {
userId = "*"
}
data := s.sysMenuService.SelectMenuTreeSelectByUserId(query, userId)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
var converI18n func(language string, arr *[]vo.TreeSelect)
converI18n = func(language string, arr *[]vo.TreeSelect) {
for i := range *arr {
(*arr)[i].Label = i18n.TKey(language, (*arr)[i].Label)
if len((*arr)[i].Children) > 0 {
converI18n(language, &(*arr)[i].Children)
}
}
}
converI18n(language, &data)
c.JSON(200, result.OkData(data))
}
@@ -273,9 +310,10 @@ func (s *SysMenuController) TreeSelect(c *gin.Context) {
//
// GET /roleMenuTreeSelect/:roleId
func (s *SysMenuController) RoleMenuTreeSelect(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
}
@@ -293,6 +331,19 @@ func (s *SysMenuController) RoleMenuTreeSelect(c *gin.Context) {
}
menuTreeSelect := s.sysMenuService.SelectMenuTreeSelectByUserId(query, userId)
checkedKeys := s.sysMenuService.SelectMenuListByRoleId(roleId)
// 闭包函数处理多语言
var converI18n func(language string, arr *[]vo.TreeSelect)
converI18n = func(language string, arr *[]vo.TreeSelect) {
for i := range *arr {
(*arr)[i].Label = i18n.TKey(language, (*arr)[i].Label)
if len((*arr)[i].Children) > 0 {
converI18n(language, &(*arr)[i].Children)
}
}
}
converI18n(language, &menuTreeSelect)
c.JSON(200, result.OkData(map[string]any{
"menus": menuTreeSelect,
"checkedKeys": checkedKeys,