feat: 通用模块多语言

This commit is contained in:
TsMask
2023-11-20 18:53:25 +08:00
parent a5139cf29c
commit 506866e082
5 changed files with 97 additions and 45 deletions

View File

@@ -4,8 +4,10 @@ import (
"ems.agt/src/framework/config"
commonConstants "ems.agt/src/framework/constants/common"
tokenConstants "ems.agt/src/framework/constants/token"
ctxUtils "ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
tokenUtils "ems.agt/src/framework/utils/token"
"ems.agt/src/framework/vo"
"ems.agt/src/framework/vo/result"
libAccount "ems.agt/src/lib_features/account"
commonModel "ems.agt/src/modules/common/model"
@@ -34,15 +36,16 @@ type AccountController struct {
//
// POST /login
func (s *AccountController) Login(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var loginBody commonModel.LoginBody
if err := c.ShouldBindJSON(&loginBody); err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 当前请求信息
ipaddr, location := ctxUtils.IPAddrLocation(c)
os, browser := ctxUtils.UaOsBrowser(c)
ipaddr, location := ctx.IPAddrLocation(c)
os, browser := ctx.UaOsBrowser(c)
// 校验验证码
err := s.accountService.ValidateCaptcha(
@@ -51,19 +54,19 @@ func (s *AccountController) Login(c *gin.Context) {
)
// 根据错误信息,创建系统访问记录
if err != nil {
msg := err.Error() + " " + loginBody.Code
msg := i18n.TKey(language, err.Error())
s.sysLogLoginService.CreateSysLogLogin(
loginBody.Username, commonConstants.STATUS_NO, msg,
loginBody.Username, commonConstants.STATUS_NO, msg+loginBody.Code,
ipaddr, location, os, browser,
)
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, result.ErrMsg(msg))
return
}
// 登录用户信息
loginUser, err := s.accountService.LoginByUsername(loginBody.Username, loginBody.Password)
if err != nil {
c.JSON(200, result.ErrMsg(err.Error()))
c.JSON(200, result.ErrMsg(i18n.TKey(language, err.Error())))
return
}
@@ -75,8 +78,9 @@ func (s *AccountController) Login(c *gin.Context) {
} else {
s.accountService.UpdateLoginDateAndIP(&loginUser)
// 登录成功
msg := i18n.TKey(language, "app.common.loginSuccess")
s.sysLogLoginService.CreateSysLogLogin(
loginBody.Username, commonConstants.STATUS_YES, "Login Successful",
loginBody.Username, commonConstants.STATUS_YES, msg,
ipaddr, location, os, browser,
)
}
@@ -93,9 +97,10 @@ func (s *AccountController) Login(c *gin.Context) {
//
// GET /getInfo
func (s *AccountController) Info(c *gin.Context) {
loginUser, err := ctxUtils.LoginUser(c)
language := ctx.AcceptLanguage(c)
loginUser, err := ctx.LoginUser(c)
if err != nil {
c.JSON(401, result.CodeMsg(401, err.Error()))
c.JSON(401, result.CodeMsg(401, i18n.TKey(language, err.Error())))
return
}
@@ -103,6 +108,12 @@ func (s *AccountController) Info(c *gin.Context) {
isAdmin := config.IsAdmin(loginUser.UserID)
roles, perms := s.accountService.RoleAndMenuPerms(loginUser.UserID, isAdmin)
loginUser.User.NickName = i18n.TKey(language, loginUser.User.NickName)
loginUser.User.Remark = i18n.TKey(language, loginUser.User.Remark)
loginUser.User.Dept.DeptName = i18n.TKey(language, loginUser.User.Dept.DeptName)
for ri := range loginUser.User.Roles {
loginUser.User.Roles[ri].RoleName = i18n.TKey(language, loginUser.User.Roles[ri].RoleName)
}
c.JSON(200, result.OkData(map[string]any{
"user": loginUser.User,
"roles": roles,
@@ -114,11 +125,25 @@ func (s *AccountController) Info(c *gin.Context) {
//
// GET /getRouters
func (s *AccountController) Router(c *gin.Context) {
userID := ctxUtils.LoginUserToUserID(c)
userID := ctx.LoginUserToUserID(c)
// 前端路由,管理员拥有所有
isAdmin := config.IsAdmin(userID)
buildMenus := s.accountService.RouteMenus(userID, isAdmin)
// 闭包函数处理多语言
language := ctx.AcceptLanguage(c)
var converI18n func(language string, arr *[]vo.Router)
converI18n = func(language string, arr *[]vo.Router) {
for i := range *arr {
(*arr)[i].Meta.Title = i18n.TKey(language, (*arr)[i].Meta.Title)
if len((*arr)[i].Children) > 0 {
converI18n(language, &(*arr)[i].Children)
}
}
}
converI18n(language, &buildMenus)
c.JSON(200, result.OkData(buildMenus))
}
@@ -126,21 +151,25 @@ func (s *AccountController) Router(c *gin.Context) {
//
// POST /logout
func (s *AccountController) Logout(c *gin.Context) {
tokenStr := ctxUtils.Authorization(c)
language := ctx.AcceptLanguage(c)
msg := i18n.TKey(language, "app.common.logoutSuccess")
tokenStr := ctx.Authorization(c)
if tokenStr != "" {
// 存在token时记录退出信息
userName := tokenUtils.Remove(tokenStr)
if userName != "" {
// 当前请求信息
ipaddr, location := ctxUtils.IPAddrLocation(c)
os, browser := ctxUtils.UaOsBrowser(c)
ipaddr, location := ctx.IPAddrLocation(c)
os, browser := ctx.UaOsBrowser(c)
// 创建系统访问记录 退出成功
s.sysLogLoginService.CreateSysLogLogin(
userName, commonConstants.STATUS_YES, "Exit successful",
userName, commonConstants.STATUS_YES, msg,
ipaddr, location, os, browser,
)
}
}
c.JSON(200, result.OkMsg("Exit successful"))
c.JSON(200, result.OkMsg(msg))
}