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

@@ -7,6 +7,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"
@@ -41,6 +42,18 @@ type SysJobController struct {
func (s *SysJobController) List(c *gin.Context) {
querys := ctx.QueryMap(c)
data := s.sysJobService.SelectJobPage(querys)
rows := data["rows"].([]model.SysJob)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
converI18n := func(language string, arr *[]model.SysJob) {
for i := range *arr {
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
c.JSON(200, result.Ok(data))
}
@@ -48,14 +61,18 @@ func (s *SysJobController) List(c *gin.Context) {
//
// GET /:jobId
func (s *SysJobController) Info(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobId := c.Param("jobId")
if jobId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
data := s.sysJobService.SelectJobById(jobId)
if data.JobID == jobId {
// 处理多语言
data.JobName = i18n.TKey(language, data.JobName)
data.Remark = i18n.TKey(language, data.Remark)
c.JSON(200, result.OkData(data))
return
}
@@ -66,17 +83,18 @@ func (s *SysJobController) Info(c *gin.Context) {
//
// POST /
func (s *SysJobController) Add(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysJob
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.JobID != "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查cron表达式格式
if parse.CronExpression(body.CronExpression) == 0 {
// 调度任务新增【%s】失败Cron表达式不正确
msg := fmt.Sprintf("Scheduling task add [%s] fails with incorrect Cron expression", body.JobName)
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -84,7 +102,7 @@ func (s *SysJobController) Add(c *gin.Context) {
// 检查任务调用传入参数是否json格式
if body.TargetParams != "" {
// 调度任务新增【%s】失败任务传入参数json字符串不正确
msg := fmt.Sprintf("Scheduling task add [%s] failed, task incoming parameter json string is incorrect", body.JobName)
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": body.JobName})
if len(body.TargetParams) < 7 {
c.JSON(200, result.ErrMsg(msg))
return
@@ -99,7 +117,7 @@ func (s *SysJobController) Add(c *gin.Context) {
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, "")
if !uniqueJob {
// 调度任务新增【%s】失败同任务组内有相同任务名称
msg := fmt.Sprintf("Scheduling tasks with new [%s] failures, with the same task name in the same task group", body.JobName)
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -117,17 +135,18 @@ func (s *SysJobController) Add(c *gin.Context) {
//
// PUT /
func (s *SysJobController) Edit(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body model.SysJob
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.JobID == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 检查cron表达式格式
if parse.CronExpression(body.CronExpression) == 0 {
// 调度任务修改【%s】失败Cron表达式不正确
msg := fmt.Sprintf("Scheduling task modification [%s] fails with incorrect Cron expression", body.JobName)
msg := i18n.TTemplate(language, "job.errCronExpression", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -135,7 +154,7 @@ func (s *SysJobController) Edit(c *gin.Context) {
// 检查任务调用传入参数是否json格式
if body.TargetParams != "" {
// 调度任务修改【%s】失败任务传入参数json字符串不正确
msg := fmt.Sprintf("Scheduling task modification [%s] failed, task incoming parameter json string is not correct", body.JobName)
msg := i18n.TTemplate(language, "job.errTargetParams", map[string]any{"name": body.JobName})
if len(body.TargetParams) < 7 {
c.JSON(200, result.ErrMsg(msg))
return
@@ -150,7 +169,7 @@ func (s *SysJobController) Edit(c *gin.Context) {
uniqueJob := s.sysJobService.CheckUniqueJobName(body.JobName, body.JobGroup, body.JobID)
if !uniqueJob {
// 调度任务修改【%s】失败同任务组内有相同任务名称
msg := fmt.Sprintf("Scheduling task modification [%s] failed with the same task name in the same task group", body.JobName)
msg := i18n.TTemplate(language, "job.errJobExists", map[string]any{"name": body.JobName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -168,9 +187,10 @@ func (s *SysJobController) Edit(c *gin.Context) {
//
// DELETE /:jobIds
func (s *SysJobController) Remove(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobIds := c.Param("jobIds")
if jobIds == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 处理字符转id数组后去重
@@ -185,7 +205,7 @@ func (s *SysJobController) 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))
}
@@ -193,6 +213,7 @@ func (s *SysJobController) Remove(c *gin.Context) {
//
// PUT /changeStatus
func (s *SysJobController) Status(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 任务ID
JobId string `json:"jobId" binding:"required"`
@@ -201,7 +222,7 @@ func (s *SysJobController) Status(c *gin.Context) {
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -209,14 +230,14 @@ func (s *SysJobController) Status(c *gin.Context) {
job := s.sysJobService.SelectJobById(body.JobId)
if job.JobID != body.JobId {
// 没有可访问调度任务数据!
c.JSON(200, result.ErrMsg("No accessible scheduling task data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
return
}
// 与旧值相等不变更
if job.Status == body.Status {
// 变更状态与旧值相等!
c.JSON(200, result.ErrMsg("Change status equals old value!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.statusEq")))
return
}
@@ -235,9 +256,10 @@ func (s *SysJobController) Status(c *gin.Context) {
//
// PUT /run/:jobId
func (s *SysJobController) Run(c *gin.Context) {
language := ctx.AcceptLanguage(c)
jobId := c.Param("jobId")
if jobId == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -245,7 +267,7 @@ func (s *SysJobController) Run(c *gin.Context) {
job := s.sysJobService.SelectJobById(jobId)
if job.JobID != jobId {
// 没有可访问调度任务数据!
c.JSON(200, result.ErrMsg("No accessible scheduling task data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "job.noData")))
return
}
@@ -269,30 +291,38 @@ func (s *SysJobController) ResetQueueJob(c *gin.Context) {
//
// POST /export
func (s *SysJobController) Export(c *gin.Context) {
language := ctx.AcceptLanguage(c)
// 查询结果,根据查询条件结果,单页最大值限制
querys := ctx.BodyJSONMap(c)
data := s.sysJobService.SelectJobPage(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.SysJob)
// 闭包函数处理多语言
converI18n := func(language string, arr *[]model.SysJob) {
for i := range *arr {
(*arr)[i].JobName = i18n.TKey(language, (*arr)[i].JobName)
(*arr)[i].Remark = i18n.TKey(language, (*arr)[i].Remark)
}
}
converI18n(language, &rows)
// 导出文件名称
fileName := fmt.Sprintf("job_export_%d_%d.xlsx", len(rows), time.Now().UnixMilli())
// 第一行表头标题
headerCells := map[string]string{
"A1": "JobID",
"B1": "JobName",
"C1": "JobGroupName",
"D1": "InvokeTarget",
"E1": "TargetParams",
"F1": "CronExpression",
"G1": "MisfirePolicy",
"H1": "Concurrent",
"I1": "Status",
"J1": "Remark",
"A1": i18n.TKey(language, "job.export.jobID"),
"B1": i18n.TKey(language, "job.export.jobName"),
"C1": i18n.TKey(language, "job.export.jobGroupName"),
"D1": i18n.TKey(language, "job.export.invokeTarget"),
"E1": i18n.TKey(language, "job.export.targetParams"),
"F1": i18n.TKey(language, "job.export.cronExpression"),
"G1": i18n.TKey(language, "job.export.status"),
"H1": i18n.TKey(language, "job.export.remark"),
}
// 读取任务组名字典数据
dictSysJobGroup := s.sysDictDataService.SelectDictDataByType("sys_job_group")
@@ -304,24 +334,15 @@ func (s *SysJobController) Export(c *gin.Context) {
sysJobGroup := ""
for _, v := range dictSysJobGroup {
if row.JobGroup == v.DictValue {
sysJobGroup = v.DictLabel
sysJobGroup = i18n.TKey(language, v.DictLabel)
break
}
}
misfirePolicy := "Abandon execution"
if row.MisfirePolicy == "1" {
misfirePolicy = "Execute immediately"
} else if row.MisfirePolicy == "2" {
misfirePolicy = "Execute once"
}
concurrent := "prohibit"
if row.Concurrent == "1" {
concurrent = "allow"
}
// 状态
statusValue := "fail"
statusValue := i18n.TKey(language, "dictData.fail")
if row.Status == "1" {
statusValue = "successes"
statusValue = i18n.TKey(language, "dictData.success")
}
dataCells = append(dataCells, map[string]any{
"A" + idx: row.JobID,
@@ -330,10 +351,8 @@ func (s *SysJobController) Export(c *gin.Context) {
"D" + idx: row.InvokeTarget,
"E" + idx: row.TargetParams,
"F" + idx: row.CronExpression,
"G" + idx: misfirePolicy,
"H" + idx: concurrent,
"I" + idx: statusValue,
"J" + idx: row.Remark,
"G" + idx: statusValue,
"H" + idx: row.Remark,
})
}