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

@@ -1,11 +1,12 @@
package controller
import (
"fmt"
"strings"
"ems.agt/src/framework/constants/common"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/vo"
"ems.agt/src/framework/vo/result"
"ems.agt/src/modules/system/model"
"ems.agt/src/modules/system/service"
@@ -31,6 +32,7 @@ type SysDeptController struct {
//
// GET /list
func (s *SysDeptController) List(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
// 部门ID
DeptID string `json:"deptId"`
@@ -43,7 +45,7 @@ func (s *SysDeptController) List(c *gin.Context) {
}
err := c.ShouldBindQuery(&querys)
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -55,6 +57,15 @@ func (s *SysDeptController) List(c *gin.Context) {
}
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
data := s.sysDeptService.SelectDeptList(SysDeptController, dataScopeSQL)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysDept) {
for i := range *arr {
(*arr)[i].DeptName = i18n.TKey(language, (*arr)[i].DeptName)
}
}
converI18n(language, &data)
c.JSON(200, result.OkData(data))
}
@@ -62,13 +73,16 @@ func (s *SysDeptController) List(c *gin.Context) {
//
// GET /:deptId
func (s *SysDeptController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
deptId := c.Param("deptId")
if deptId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
data := s.sysDeptService.SelectDeptById(deptId)
if data.DeptID == deptId {
// 处理多语言
data.DeptName = i18n.TKey(language, data.DeptName)
c.JSON(200, result.OkData(data))
return
}
@@ -79,10 +93,11 @@ func (s *SysDeptController) Info(c *gin.Context) {
//
// POST /
func (s *SysDeptController) Add(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysDept
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.DeptID != "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -91,18 +106,18 @@ func (s *SysDeptController) Add(c *gin.Context) {
deptParent := s.sysDeptService.SelectDeptById(body.ParentID)
if deptParent.DeptID != body.ParentID {
// 没有可访问部门数据!
c.JSON(200, result.ErrMsg("There is no accessible sectoral data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
return
}
if deptParent.Status == common.STATUS_NO {
// 上级部门【%s】停用不允许新增
msg := fmt.Sprintf("Upper division [%s] deactivated, no new additions allowed", deptParent.DeptName)
msg := i18n.TTemplate(language, "dept.errParentStatus", map[string]any{"name": deptParent.DeptName})
c.JSON(200, result.ErrMsg(msg))
return
}
if deptParent.DelFlag == common.STATUS_YES {
// 上级部门【%s】已删除不允许新增
msg := fmt.Sprintf("The parent department [%s] has been deleted and is not allowed to be added", deptParent.DeptName)
msg := i18n.TTemplate(language, "dept.errParentDelFlag", map[string]any{"name": deptParent.DeptName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -115,7 +130,7 @@ func (s *SysDeptController) Add(c *gin.Context) {
uniqueDeptName := s.sysDeptService.CheckUniqueDeptName(body.DeptName, body.ParentID, "")
if !uniqueDeptName {
// 部门新增【%s】失败部门名称已存在
msg := fmt.Sprintf("Department add [%s] failed, department name already exists", body.DeptName)
msg := i18n.TTemplate(language, "dept.errNameExists", map[string]any{"name": body.DeptName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -133,17 +148,18 @@ func (s *SysDeptController) Add(c *gin.Context) {
//
// PUT /
func (s *SysDeptController) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysDept
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.DeptID == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 上级部门不能选自己
if body.DeptID == body.ParentID {
// 部门修改【%s】失败上级部门不能是自己
msg := fmt.Sprintf("Departmental modification [%s] failed, the parent department cannot be itself", body.DeptName)
msg := i18n.TTemplate(language, "dept.errParentID", map[string]any{"name": body.DeptName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -152,7 +168,7 @@ func (s *SysDeptController) Edit(c *gin.Context) {
deptInfo := s.sysDeptService.SelectDeptById(body.DeptID)
if deptInfo.DeptID != body.DeptID {
// 没有可访问部门数据!
c.JSON(200, result.ErrMsg("There is no accessible sectoral data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
return
}
// 父级ID不为0是要检查
@@ -160,7 +176,7 @@ func (s *SysDeptController) Edit(c *gin.Context) {
deptParent := s.sysDeptService.SelectDeptById(body.ParentID)
if deptParent.DeptID != body.ParentID {
// 没有可访问部门数据!
c.JSON(200, result.ErrMsg("There is no accessible sectoral data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
return
}
}
@@ -169,7 +185,7 @@ func (s *SysDeptController) Edit(c *gin.Context) {
uniqueDeptName := s.sysDeptService.CheckUniqueDeptName(body.DeptName, body.ParentID, body.DeptID)
if !uniqueDeptName {
// 部门修改【%s】失败部门名称已存在
msg := fmt.Sprintf("Department modification [%s] failed, department name already exists", body.DeptName)
msg := i18n.TTemplate(language, "dept.errNameExists", map[string]any{"name": body.DeptName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -179,7 +195,7 @@ func (s *SysDeptController) Edit(c *gin.Context) {
hasChild := s.sysDeptService.HasChildByDeptId(body.DeptID)
if hasChild > 0 {
// 该部门包含未停用的子部门数量:%d
msg := fmt.Sprintf("Number of subsectors not deactivated included in this sector: %d", hasChild)
msg := i18n.TTemplate(language, "dept.errHasChildUse", map[string]any{"num": hasChild})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -198,9 +214,10 @@ func (s *SysDeptController) Edit(c *gin.Context) {
//
// DELETE /:deptId
func (s *SysDeptController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
deptId := c.Param("deptId")
if deptId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -208,7 +225,7 @@ func (s *SysDeptController) Remove(c *gin.Context) {
dept := s.sysDeptService.SelectDeptById(deptId)
if dept.DeptID != deptId {
// 没有可访问部门数据!
c.JSON(200, result.ErrMsg("There is no accessible sectoral data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dept.noData")))
return
}
@@ -216,7 +233,7 @@ func (s *SysDeptController) Remove(c *gin.Context) {
hasChild := s.sysDeptService.HasChildByDeptId(deptId)
if hasChild > 0 {
// 不允许删除,存在子部门数:%d
msg := fmt.Sprintf("Deletion not allowed, number of subsectors present: %d", hasChild)
msg := i18n.TTemplate(language, "dept.errHasChildUse", map[string]any{"num": hasChild})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -225,7 +242,7 @@ func (s *SysDeptController) Remove(c *gin.Context) {
existUser := s.sysDeptService.CheckDeptExistUser(deptId)
if existUser > 0 {
// 不允许删除,部门已分配给用户数:%d
msg := fmt.Sprintf("Deletions are not allowed and the department has been assigned to the number of users: %d", existUser)
msg := i18n.TTemplate(language, "dept.errHasUserUse", map[string]any{"num": existUser})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -233,7 +250,7 @@ func (s *SysDeptController) Remove(c *gin.Context) {
rows := s.sysDeptService.DeleteDeptById(deptId)
if rows > 0 {
// 删除成功:%d
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
}
@@ -244,9 +261,10 @@ func (s *SysDeptController) Remove(c *gin.Context) {
//
// GET /list/exclude/:deptId
func (s *SysDeptController) ExcludeChild(c *gin.Context) {
language := ctx.AcceptLanguage(c)
deptId := c.Param("deptId")
if deptId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -265,6 +283,7 @@ func (s *SysDeptController) ExcludeChild(c *gin.Context) {
}
}
if !(dept.DeptID == deptId || hasAncestor) {
dept.DeptName = i18n.TKey(language, dept.DeptName)
filtered = append(filtered, dept)
}
}
@@ -275,6 +294,7 @@ func (s *SysDeptController) ExcludeChild(c *gin.Context) {
//
// GET /treeSelect
func (s *SysDeptController) TreeSelect(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var querys struct {
// 部门ID
DeptID string `json:"deptId"`
@@ -287,7 +307,7 @@ func (s *SysDeptController) TreeSelect(c *gin.Context) {
}
err := c.ShouldBindQuery(&querys)
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -298,23 +318,50 @@ func (s *SysDeptController) TreeSelect(c *gin.Context) {
Status: querys.Status,
}
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
data := s.sysDeptService.SelectDeptTreeSelect(SysDeptController, dataScopeSQL)
c.JSON(200, result.OkData(data))
deptTreeSelect := s.sysDeptService.SelectDeptTreeSelect(SysDeptController, dataScopeSQL)
// 闭包函数处理多语言
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, &deptTreeSelect)
c.JSON(200, result.OkData(deptTreeSelect))
}
// 部门树结构列表(指定角色)
//
// GET /roleDeptTreeSelect/:roleId
func (s *SysDeptController) RoleDeptTreeSelect(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
}
dataScopeSQL := ctx.LoginUserToDataScopeSQL(c, "d", "")
deptTreeSelect := s.sysDeptService.SelectDeptTreeSelect(model.SysDept{}, dataScopeSQL)
checkedKeys := s.sysDeptService.SelectDeptListByRoleId(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, &deptTreeSelect)
c.JSON(200, result.OkData(map[string]any{
"depts": deptTreeSelect,
"checkedKeys": checkedKeys,