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

@@ -2,7 +2,8 @@ package controller
import (
commonConstants "ems.agt/src/framework/constants/common"
ctxUtils "ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/i18n"
"ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/regular"
"ems.agt/src/framework/vo/result"
commonModel "ems.agt/src/modules/common/model"
@@ -32,32 +33,33 @@ type RegisterController struct {
//
// GET /register
func (s *RegisterController) Register(c *gin.Context) {
language := ctx.AcceptLanguage(c)
var registerBody commonModel.RegisterBody
if err := c.ShouldBindJSON(&registerBody); err != nil {
c.JSON(400, result.ErrMsg("parameter error"))
c.JSON(400, result.CodeMsg(400, i18n.TKey(language, "app.common.err400")))
return
}
// 判断必传参数
if !regular.ValidUsername(registerBody.Username) {
// 账号不能以数字开头可包含大写小写字母数字且不少于5位
c.JSON(200, result.ErrMsg("The account cannot start with a number and can contain uppercase and lowercase letters, numbers, and no less than 5 digits"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "register.errUsername")))
return
}
if !regular.ValidPassword(registerBody.Password) {
// 登录密码至少包含大小写字母、数字、特殊符号且不少于6位
c.JSON(200, result.ErrMsg("The login password should contain at least uppercase and lowercase letters, numbers, special symbols, and no less than 6 digits"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "register.errPasswd")))
return
}
if registerBody.Password != registerBody.ConfirmPassword {
// 用户确认输入密码不一致
c.JSON(200, result.ErrMsg("The user confirmed that the input password is inconsistent"))
c.JSON(200, result.ErrMsg(i18n.TKey(language, "register.errPasswdNotEq")))
return
}
// 当前请求信息
ipaddr, location := ctxUtils.IPAddrLocation(c)
os, browser := ctxUtils.UaOsBrowser(c)
ipaddr, location := ctx.IPAddrLocation(c)
os, browser := ctx.UaOsBrowser(c)
// 校验验证码
err := s.registerService.ValidateCaptcha(
@@ -77,13 +79,13 @@ func (s *RegisterController) Register(c *gin.Context) {
userID, err := s.registerService.ByUserName(registerBody.Username, registerBody.Password, registerBody.UserType)
if err == nil {
msg := registerBody.Username + " registered success " + userID
msg := i18n.TTemplate(language, "register.successMsg", map[string]any{"name": registerBody.Username, "id": userID})
s.sysLogLoginService.CreateSysLogLogin(
registerBody.Username, commonConstants.STATUS_YES, msg,
ipaddr, location, os, browser,
)
// 注册成功
c.JSON(200, result.OkMsg("registered success"))
c.JSON(200, result.OkMsg(i18n.TKey(language, "register.success")))
return
}
c.JSON(200, result.ErrMsg(err.Error()))