feat: 系统模块多语言
This commit is contained in:
@@ -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"
|
||||
@@ -39,6 +40,21 @@ type SysDictDataController struct {
|
||||
func (s *SysDictDataController) List(c *gin.Context) {
|
||||
querys := ctx.QueryMap(c)
|
||||
data := s.sysDictDataService.SelectDictDataPage(querys)
|
||||
|
||||
rows := data["rows"].([]model.SysDictData)
|
||||
// 闭包函数处理多语言
|
||||
language := ctx.AcceptLanguage(c)
|
||||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||||
for i := range *arr {
|
||||
if strings.Contains((*arr)[i].DictType, "i18n") {
|
||||
continue
|
||||
}
|
||||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
c.JSON(200, result.Ok(data))
|
||||
}
|
||||
|
||||
@@ -46,13 +62,17 @@ func (s *SysDictDataController) List(c *gin.Context) {
|
||||
//
|
||||
// GET /:dictCode
|
||||
func (s *SysDictDataController) Info(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
dictCode := c.Param("dictCode")
|
||||
if dictCode == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
data := s.sysDictDataService.SelectDictDataByCode(dictCode)
|
||||
if data.DictCode == dictCode {
|
||||
// 处理多语言
|
||||
data.DictLabel = i18n.TKey(language, data.DictLabel)
|
||||
data.Remark = i18n.TKey(language, data.Remark)
|
||||
c.JSON(200, result.OkData(data))
|
||||
return
|
||||
}
|
||||
@@ -63,10 +83,11 @@ func (s *SysDictDataController) Info(c *gin.Context) {
|
||||
//
|
||||
// POST /
|
||||
func (s *SysDictDataController) Add(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.SysDictData
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DictCode != "" {
|
||||
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -74,7 +95,7 @@ func (s *SysDictDataController) Add(c *gin.Context) {
|
||||
sysDictType := s.sysDictTypeService.SelectDictTypeByType(body.DictType)
|
||||
if sysDictType.DictType != body.DictType {
|
||||
// 没有可访问字典类型数据!
|
||||
c.JSON(200, result.ErrMsg("There is no accessible dictionary type data!"))
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -82,16 +103,7 @@ func (s *SysDictDataController) Add(c *gin.Context) {
|
||||
uniqueDictLabel := s.sysDictDataService.CheckUniqueDictLabel(body.DictType, body.DictLabel, "")
|
||||
if !uniqueDictLabel {
|
||||
// 数据新增【%s】失败,该字典类型下标签名已存在
|
||||
msg := fmt.Sprintf("Data addition [%s] failed, tag name already exists under this dictionary type", body.DictLabel)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典键值唯一
|
||||
uniqueDictValue := s.sysDictDataService.CheckUniqueDictValue(body.DictType, body.DictValue, "")
|
||||
if !uniqueDictValue {
|
||||
// 数据新增【%s】失败,该字典类型下标签值已存在
|
||||
msg := fmt.Sprintf("Data addition [%s] failed, tagged value already exists under this dictionary type", body.DictValue)
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DictLabel})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
@@ -109,10 +121,11 @@ func (s *SysDictDataController) Add(c *gin.Context) {
|
||||
//
|
||||
// PUT /
|
||||
func (s *SysDictDataController) Edit(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
var body model.SysDictData
|
||||
err := c.ShouldBindBodyWith(&body, binding.JSON)
|
||||
if err != nil || body.DictCode == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -120,7 +133,7 @@ func (s *SysDictDataController) Edit(c *gin.Context) {
|
||||
sysDictType := s.sysDictTypeService.SelectDictTypeByType(body.DictType)
|
||||
if sysDictType.DictType != body.DictType {
|
||||
// 没有可访问字典类型数据!
|
||||
c.JSON(200, result.ErrMsg("There is no accessible dictionary type data!"))
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictType.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -128,7 +141,7 @@ func (s *SysDictDataController) Edit(c *gin.Context) {
|
||||
SysDictDataController := s.sysDictDataService.SelectDictDataByCode(body.DictCode)
|
||||
if SysDictDataController.DictCode != body.DictCode {
|
||||
// 没有可访问字典编码数据!
|
||||
c.JSON(200, result.ErrMsg("There is no accessible dictionary-encoded data!"))
|
||||
c.JSON(200, result.ErrMsg(i18n.TKey(language, "dictData.noData")))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -136,16 +149,7 @@ func (s *SysDictDataController) Edit(c *gin.Context) {
|
||||
uniqueDictLabel := s.sysDictDataService.CheckUniqueDictLabel(body.DictType, body.DictLabel, body.DictCode)
|
||||
if !uniqueDictLabel {
|
||||
// 数据修改【%s】失败,该字典类型下标签名已存在
|
||||
msg := fmt.Sprintf("Data modification [%s] failed, tag name already exists under this dictionary type", body.DictLabel)
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
|
||||
// 检查字典键值唯一
|
||||
uniqueDictValue := s.sysDictDataService.CheckUniqueDictValue(body.DictType, body.DictValue, body.DictCode)
|
||||
if !uniqueDictValue {
|
||||
// 数据修改【%s】失败,该字典类型下标签值已存在
|
||||
msg := fmt.Sprintf("Data modification [%s] failed, tagged value already exists under this dictionary type", body.DictValue)
|
||||
msg := i18n.TTemplate(language, "dictType.errLabelExists", map[string]any{"name": body.DictLabel})
|
||||
c.JSON(200, result.ErrMsg(msg))
|
||||
return
|
||||
}
|
||||
@@ -163,9 +167,10 @@ func (s *SysDictDataController) Edit(c *gin.Context) {
|
||||
//
|
||||
// DELETE /:dictCodes
|
||||
func (s *SysDictDataController) Remove(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
dictCodes := c.Param("dictCodes")
|
||||
if dictCodes == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
// 处理字符转id数组后去重
|
||||
@@ -181,7 +186,7 @@ func (s *SysDictDataController) Remove(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
// 删除成功:%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))
|
||||
}
|
||||
|
||||
@@ -189,13 +194,24 @@ func (s *SysDictDataController) Remove(c *gin.Context) {
|
||||
//
|
||||
// GET /type/:dictType
|
||||
func (s *SysDictDataController) DictType(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
dictType := c.Param("dictType")
|
||||
if dictType == "" {
|
||||
c.JSON(400, result.CodeMsg(400, "parameter error"))
|
||||
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
|
||||
return
|
||||
}
|
||||
|
||||
data := s.sysDictDataService.SelectDictDataByType(dictType)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
converI18n(language, &data)
|
||||
|
||||
c.JSON(200, result.OkData(data))
|
||||
}
|
||||
|
||||
@@ -203,34 +219,44 @@ func (s *SysDictDataController) DictType(c *gin.Context) {
|
||||
//
|
||||
// POST /export
|
||||
func (s *SysDictDataController) Export(c *gin.Context) {
|
||||
language := ctx.AcceptLanguage(c)
|
||||
// 查询结果,根据查询条件结果,单页最大值限制
|
||||
querys := ctx.BodyJSONMap(c)
|
||||
data := s.sysDictDataService.SelectDictDataPage(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.SysDictData)
|
||||
|
||||
// 闭包函数处理多语言
|
||||
converI18n := func(language string, arr *[]model.SysDictData) {
|
||||
for i := range *arr {
|
||||
(*arr)[i].DictLabel = i18n.TKey(language, (*arr)[i].DictLabel)
|
||||
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
|
||||
}
|
||||
}
|
||||
converI18n(language, &rows)
|
||||
|
||||
// 导出文件名称
|
||||
fileName := fmt.Sprintf("dict_data_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
|
||||
// 第一行表头标题
|
||||
headerCells := map[string]string{
|
||||
"A1": "DictCode",
|
||||
"B1": "DictSort",
|
||||
"C1": "DictLabel",
|
||||
"D1": "DictValue",
|
||||
"E1": "DictType",
|
||||
"F1": "Status",
|
||||
"A1": i18n.TKey(language, "dictData.export.code"),
|
||||
"B1": i18n.TKey(language, "dictData.export.sort"),
|
||||
"C1": i18n.TKey(language, "dictData.export.label"),
|
||||
"D1": i18n.TKey(language, "dictData.export.value"),
|
||||
"E1": i18n.TKey(language, "dictData.export.type"),
|
||||
"F1": i18n.TKey(language, "dictData.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.DictCode,
|
||||
|
||||
Reference in New Issue
Block a user