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

@@ -1,11 +1,10 @@
package controller
import (
"fmt"
"ems.agt/src/framework/config"
"ems.agt/src/framework/constants/admin"
"ems.agt/src/framework/constants/uploadsubpath"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/crypto"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/file"
@@ -46,9 +45,10 @@ type SysProfileController struct {
//
// GET /
func (s *SysProfileController) Info(c *gin.Context) {
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
}
@@ -56,20 +56,26 @@ func (s *SysProfileController) Info(c *gin.Context) {
roleGroup := []string{}
roles := s.sysRoleService.SelectRoleListByUserId(loginUser.UserID)
for _, role := range roles {
roleGroup = append(roleGroup, role.RoleName)
roleGroup = append(roleGroup, i18n.TKey(language, role.RoleName))
}
isAdmin := config.IsAdmin(loginUser.UserID)
if isAdmin {
roleGroup = append(roleGroup, "Administrator")
roleGroup = append(roleGroup, i18n.TKey(language, "role.admin"))
}
// 查询用户所属岗位组
postGroup := []string{}
posts := s.sysPostService.SelectPostListByUserId(loginUser.UserID)
for _, post := range posts {
postGroup = append(postGroup, post.PostName)
postGroup = append(postGroup, i18n.TKey(language, post.PostName))
}
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,
"roleGroup": parse.RemoveDuplicates(roleGroup),
@@ -81,6 +87,7 @@ func (s *SysProfileController) Info(c *gin.Context) {
//
// PUT /
func (s *SysProfileController) UpdateProfile(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 昵称
NickName string `json:"nickName" binding:"required"`
@@ -93,14 +100,14 @@ func (s *SysProfileController) UpdateProfile(c *gin.Context) {
}
err := c.ShouldBindBodyWith(&body, binding.JSON)
if err != nil || body.Sex == "" {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 登录用户信息
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
}
userId := loginUser.UserID
@@ -112,13 +119,13 @@ func (s *SysProfileController) UpdateProfile(c *gin.Context) {
uniquePhone := s.sysUserService.CheckUniquePhone(body.PhoneNumber, userId)
if !uniquePhone {
// 修改用户【%s】失败手机号码已存在
msg := fmt.Sprintf("Failed to modify user [%s], cell phone number already exists", userName)
msg := i18n.TTemplate(language, "user.errPhoneExists", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
return
}
} else {
// 修改用户【%s】失败手机号码格式错误
msg := fmt.Sprintf("Failed to modify user [%s], wrong format of cell phone number", userName)
msg := i18n.TTemplate(language, "user.errPhoneFormat", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -130,13 +137,13 @@ func (s *SysProfileController) UpdateProfile(c *gin.Context) {
uniqueEmail := s.sysUserService.CheckUniqueEmail(body.Email, userId)
if !uniqueEmail {
// 修改用户【%s】失败邮箱已存在
msg := fmt.Sprintf("Failed to modify user [%s], mailbox already exists", userName)
msg := i18n.TTemplate(language, "user.errEmailExists", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
return
}
} else {
// 修改用户【%s】失败邮箱格式错误
msg := fmt.Sprintf("Failed to modify user [%s], mailbox format error", userName)
msg := i18n.TTemplate(language, "user.errEmailFormat", map[string]any{"name": userName})
c.JSON(200, result.ErrMsg(msg))
return
}
@@ -169,14 +176,14 @@ func (s *SysProfileController) UpdateProfile(c *gin.Context) {
c.JSON(200, result.Ok(nil))
return
}
// 上传图片异常
c.JSON(200, result.ErrMsg("Upload Image Exception"))
c.JSON(200, result.Err(nil))
}
// 个人重置密码
//
// PUT /updatePwd
func (s *SysProfileController) UpdatePwd(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var body struct {
// 旧密码
OldPassword string `json:"oldPassword" binding:"required"`
@@ -185,14 +192,14 @@ func (s *SysProfileController) UpdatePwd(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
}
// 登录用户信息
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
}
userId := loginUser.UserID
@@ -202,7 +209,7 @@ func (s *SysProfileController) UpdatePwd(c *gin.Context) {
user := s.sysUserService.SelectUserById(userId)
if user.UserID != userId {
// 没有可访问用户数据!
c.JSON(200, result.ErrMsg("There is no accessible user data!"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.noData")))
return
}
@@ -210,13 +217,13 @@ func (s *SysProfileController) UpdatePwd(c *gin.Context) {
oldCompare := crypto.BcryptCompare(body.OldPassword, user.Password)
if !oldCompare {
// 修改密码失败,旧密码错误
c.JSON(200, result.ErrMsg("Failed to change password, old password is wrong"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.errPasswdOld")))
return
}
newCompare := crypto.BcryptCompare(body.NewPassword, user.Password)
if newCompare {
// 新密码不能与旧密码相同
c.JSON(200, result.ErrMsg("The new password cannot be the same as the old one"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "user.errPasswdEqOld")))
return
}
@@ -238,9 +245,10 @@ func (s *SysProfileController) UpdatePwd(c *gin.Context) {
//
// POST /avatar
func (s *SysProfileController) Avatar(c *gin.Context) {
language := ctx.AcceptLanguage(c)
formFile, err := c.FormFile("file")
if err != nil {
c.JSON(400, result.CodeMsg(400, "parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
@@ -254,7 +262,7 @@ func (s *SysProfileController) Avatar(c *gin.Context) {
// 登录用户信息
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
}