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

@@ -6,6 +6,7 @@ import (
"strings"
"time"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/file"
"ems.agt/src/framework/utils/parse"
@@ -36,6 +37,18 @@ type SysPostController struct {
func (s *SysPostController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.sysPostService.SelectPostPage(querys)
rows := data["rows"].([]model.SysPost)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
converI18n := func(language string, arr *[]model.SysPost) {
for i := range *arr {
(*arr)[i].PostName = i18n.TKey(language, (*arr)[i].PostName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
c.JSON(200, result.Ok(data))
}
@@ -43,13 +56,17 @@ func (s *SysPostController) List(c *gin.Context) {
//
// GET /:postId
func (s *SysPostController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
postId := c.Param("postId")
if postId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
data := s.sysPostService.SelectPostById(postId)
if data.PostID == postId {
// 处理多语言
data.PostName = i18n.TKey(language, data.PostName)
data.Remark = i18n.TKey(language, data.Remark)
c.JSON(200, result.OkData(data))
return
}
@@ -60,10 +77,11 @@ func (s *SysPostController) Info(c *gin.Context) {
//
// POST /
func (s *SysPostController) Add(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysPost
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.PostID != "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -71,7 +89,7 @@ func (s *SysPostController) Add(c *gin.Context) {
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, "")
if !uniqueuPostName {
// 岗位新增【%s】失败岗位名称已存在
msg := fmt.Sprintf("Job addition [%s] failed, job name already exists", body.PostName)
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -80,7 +98,7 @@ func (s *SysPostController) Add(c *gin.Context) {
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, "")
if !uniquePostCode {
// 岗位新增【%s】失败岗位编码已存在
msg := fmt.Sprintf("Job addition [%s] failed, job code already exists", body.PostCode)
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -98,10 +116,11 @@ func (s *SysPostController) Add(c *gin.Context) {
//
// PUT /
func (s *SysPostController) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysPost
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.PostID == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -109,7 +128,7 @@ func (s *SysPostController) Edit(c *gin.Context) {
post := s.sysPostService.SelectPostById(body.PostID)
if post.PostID != body.PostID {
// 没有可访问岗位数据!
c.JSON(200, result.ErrMsg("There is no accessible post data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "post.noData")))
return
}
@@ -117,7 +136,7 @@ func (s *SysPostController) Edit(c *gin.Context) {
uniqueuPostName := s.sysPostService.CheckUniquePostName(body.PostName, body.PostID)
if !uniqueuPostName {
// 岗位修改【%s】失败岗位名称已存在
msg := fmt.Sprintf("Post modification [%s] failed, post name already exists", body.PostName)
msg := i18n.TTemplate(language, "post.errNameExists", map[string]any{"name": body.PostName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -126,7 +145,7 @@ func (s *SysPostController) Edit(c *gin.Context) {
uniquePostCode := s.sysPostService.CheckUniquePostCode(body.PostCode, body.PostID)
if !uniquePostCode {
// 岗位修改【%s】失败岗位编码已存在
msg := fmt.Sprintf("Post modification [%s] failed, post code already exists", body.PostCode)
msg := i18n.TTemplate(language, "post.errCodeExists", map[string]any{"name": body.PostCode})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -144,9 +163,10 @@ func (s *SysPostController) Edit(c *gin.Context) {
//
// DELETE /:postIds
func (s *SysPostController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
postIds := c.Param("postIds")
if postIds == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
@@ -161,7 +181,7 @@ func (s *SysPostController) 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))
}
@@ -169,33 +189,43 @@ func (s *SysPostController) Remove(c *gin.Context) {
//
// POST /export
func (s *SysPostController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制
querys := ctx.BodyJSONMap(c)
data := s.sysPostService.SelectPostPage(querys)
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.SysPost)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysPost) {
for i := range *arr {
(*arr)[i].PostName = i18n.TKey(language, (*arr)[i].PostName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
// 导出文件名称
fileName := fmt.Sprintf("post_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
// 第一行表头标题
headerCells := map[string]string{
"A1": "PostID",
"B1": "PostCode",
"C1": "PostName",
"D1": "PostSort",
"E1": "Status",
"A1": i18n.TKey(language, "post.export.id"),
"B1": i18n.TKey(language, "post.export.code"),
"C1": i18n.TKey(language, "post.export.name"),
"D1": i18n.TKey(language, "post.export.sort"),
"E1": i18n.TKey(language, "post.export.status"),
}
// 从第二行开始的数据
dataCells := make([]map[string]any, 0)
for i, row := range rows {
idx := strconv.Itoa(i + 2)
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.PostID,