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,19 @@ type SysConfigController struct {
func (s *SysConfigController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.sysConfigService.SelectConfigPage(querys)
rows := data["rows"].([]model.SysConfig)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
converI18n := func(language string, arr *[]model.SysConfig) {
for i := range *arr {
(*arr)[i].ConfigName = i18n.TKey(language, (*arr)[i].ConfigName)
(*arr)[i].ConfigValue = i18n.TKey(language, (*arr)[i].ConfigValue)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
c.JSON(200, result.Ok(data))
}
@@ -43,13 +57,19 @@ func (s *SysConfigController) List(c *gin.Context) {
//
// GET /:configId
func (s *SysConfigController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
configId := c.Param("configId")
if configId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
data := s.sysConfigService.SelectConfigById(configId)
if data.ConfigID == configId {
// 处理多语言
data.ConfigName = i18n.TKey(language, data.ConfigName)
data.ConfigValue = i18n.TKey(language, data.ConfigValue)
data.Remark = i18n.TKey(language, data.Remark)
c.JSON(200, result.OkData(data))
return
}
@@ -60,10 +80,11 @@ func (s *SysConfigController) Info(c *gin.Context) {
//
// POST /
func (s *SysConfigController) Add(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysConfig
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.ConfigID != "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -71,7 +92,7 @@ func (s *SysConfigController) Add(c *gin.Context) {
uniqueConfigKey := s.sysConfigService.CheckUniqueConfigKey(body.ConfigKey, "")
if !uniqueConfigKey {
// 参数配置新增【%s】失败参数键名已存在
msg := fmt.Sprintf("Parameter configuration add [%s] failed, parameter key name already exists", body.ConfigKey)
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -89,10 +110,11 @@ func (s *SysConfigController) Add(c *gin.Context) {
//
// PUT /
func (s *SysConfigController) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysConfig
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.ConfigID == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -100,7 +122,7 @@ func (s *SysConfigController) Edit(c *gin.Context) {
uniqueConfigKey := s.sysConfigService.CheckUniqueConfigKey(body.ConfigKey, body.ConfigID)
if !uniqueConfigKey {
// 参数配置修改【%s】失败参数键名已存在
msg := fmt.Sprintf("Parameter configuration modification [%s] failed, parameter key name already exists", body.ConfigKey)
msg := i18n.TTemplate(language, "config.errKeyExists", map[string]any{"name": body.ConfigKey})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -109,7 +131,7 @@ func (s *SysConfigController) Edit(c *gin.Context) {
config := s.sysConfigService.SelectConfigById(body.ConfigID)
if config.ConfigID != body.ConfigID {
// 没有可访问参数配置数据!
c.JSON(200, result.ErrMsg("There is no accessible parameter configuration data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.noData")))
return
}
@@ -126,9 +148,10 @@ func (s *SysConfigController) Edit(c *gin.Context) {
//
// DELETE /:configIds
func (s *SysConfigController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
configIds := c.Param("configIds")
if configIds == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
@@ -140,10 +163,10 @@ func (s *SysConfigController) Remove(c *gin.Context) {
}
rows, err := s.sysConfigService.DeleteConfigByIds(uniqueIDs)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, result.ErrMsg(i18n.TKey(language, 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))
}
@@ -159,9 +182,10 @@ func (s *SysConfigController) RefreshCache(c *gin.Context) {
//
// GET /configKey/:configKey
func (s *SysConfigController) ConfigKey(c *gin.Context) {
language := ctx.AcceptLanguage(c)
configKey := c.Param("configKey")
if configKey == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
key := s.sysConfigService.SelectConfigValueByKey(configKey)
@@ -176,33 +200,45 @@ func (s *SysConfigController) ConfigKey(c *gin.Context) {
//
// POST /export
func (s *SysConfigController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制
querys := ctx.BodyJSONMap(c)
data := s.sysConfigService.SelectConfigPage(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.SysConfig)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysConfig) {
for i := range *arr {
(*arr)[i].ConfigName = i18n.TKey(language, (*arr)[i].ConfigName)
(*arr)[i].ConfigValue = i18n.TKey(language, (*arr)[i].ConfigValue)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
// 导出文件名称
fileName := fmt.Sprintf("config_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
// 第一行表头标题
headerCells := map[string]string{
"A1": "ConfigID",
"B1": "ConfigName",
"C1": "ConfigKey",
"D1": "ConfigValue",
"E1": "Type",
"A1": i18n.TKey(language, "config..export.id"),
"B1": i18n.TKey(language, "config..export.name"),
"C1": i18n.TKey(language, "config..export.key"),
"D1": i18n.TKey(language, "config..export.value"),
"E1": i18n.TKey(language, "config..export.type"),
"F1": i18n.TKey(language, "config..export.remark"),
}
// 从第二行开始的数据
dataCells := make([]map[string]any, 0)
for i, row := range rows {
idx := strconv.Itoa(i + 2)
typeValue := "clogged"
typeValue := i18n.TKey(language, "dictData.no")
if row.ConfigType == "Y" {
typeValue = "be"
typeValue = i18n.TKey(language, "dictData.yes")
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.ConfigID,
@@ -210,6 +246,7 @@ func (s *SysConfigController) Export(c *gin.Context) {
"C" + idx: row.ConfigKey,
"D" + idx: row.ConfigValue,
"E" + idx: typeValue,
"F" + idx: row.Remark,
})
}
@@ -227,12 +264,13 @@ func (s *SysConfigController) Export(c *gin.Context) {
//
// PUT /changeConfigValue
func (s *SysConfigController) ConfigValue(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
Key string `json:"key" binding:"required"`
Value string `json:"value" binding:"required"`
}
if err := c.ShouldBindBodyWith(&body, binding.JSON); err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -240,14 +278,14 @@ func (s *SysConfigController) ConfigValue(c *gin.Context) {
info := s.sysConfigService.SelectConfigByKey(body.Key)
if info.ConfigKey != body.Key {
// 无效 key
c.JSON(200, result.ErrMsg("Invalid key"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errKey")))
return
}
// 与旧值相等不变更
if info.ConfigValue == body.Value {
// 变更状态与旧值相等!
c.JSON(200, result.ErrMsg("The change status is equal to the old value!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "config.errValueEq")))
return
}
info.ConfigValue = body.Value