feat: i18n

This commit is contained in:
meilin.huang
2024-11-20 22:43:53 +08:00
parent 74ae031853
commit 99a746085b
308 changed files with 8177 additions and 3880 deletions

View File

@@ -8,6 +8,7 @@ import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/consts"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/internal/sys/imsg"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/contextx"
"mayfly-go/pkg/model"
@@ -15,7 +16,6 @@ import (
"mayfly-go/pkg/utils/collx"
"mayfly-go/pkg/utils/cryptox"
"mayfly-go/pkg/utils/structx"
"strconv"
"strings"
"time"
@@ -62,28 +62,30 @@ func (a *Account) GetPermissions(rc *req.Ctx) {
}
func (a *Account) ChangePassword(rc *req.Ctx) {
ctx := rc.MetaCtx
form := req.BindJsonAndValid(rc, new(form.AccountChangePasswordForm))
originOldPwd, err := cryptox.DefaultRsaDecrypt(form.OldPassword, true)
biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
biz.ErrIsNilAppendErr(err, "Wrong to decrypt old password: %s")
account := &entity.Account{Username: form.Username}
err = a.AccountApp.GetByCond(model.NewModelCond(account).Columns("Id", "Username", "Password", "Status"))
biz.ErrIsNil(err, "旧密码错误")
biz.IsTrue(cryptox.CheckPwdHash(originOldPwd, account.Password), "旧密码错误")
biz.IsTrue(account.IsEnable(), "该账号不可用")
biz.ErrIsNilI(ctx, err, imsg.ErrOldPasswordWrong)
biz.IsTrueI(ctx, cryptox.CheckPwdHash(originOldPwd, account.Password), imsg.ErrOldPasswordWrong)
biz.IsTrue(account.IsEnable(), "This account is not available")
originNewPwd, err := cryptox.DefaultRsaDecrypt(form.NewPassword, true)
biz.ErrIsNilAppendErr(err, "解密新密码错误: %s")
biz.IsTrue(utils.CheckAccountPasswordLever(originNewPwd), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
biz.ErrIsNilAppendErr(err, "Wrong to decrypt new password: %s")
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(originNewPwd), imsg.ErrAccountPasswordNotFollowRule)
updateAccount := new(entity.Account)
updateAccount.Id = account.Id
updateAccount.Password = cryptox.PwdHash(originNewPwd)
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, updateAccount), "更新账号密码失败")
biz.ErrIsNilAppendErr(a.AccountApp.Update(ctx, updateAccount), "failed to update account password: %s")
// 赋值loginAccount 主要用于记录操作日志,因为操作日志保存请求上下文没有该信息不保存日志
contextx.WithLoginAccount(rc.MetaCtx, &model.LoginAccount{
contextx.WithLoginAccount(ctx, &model.LoginAccount{
Id: account.Id,
Username: account.Username,
})
@@ -103,19 +105,20 @@ func (a *Account) UpdateAccount(rc *req.Ctx) {
// 账号id为登录者账号
updateAccount.Id = rc.GetLoginAccount().Id
ctx := rc.MetaCtx
if updateAccount.Password != "" {
biz.IsTrue(utils.CheckAccountPasswordLever(updateAccount.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(updateAccount.Password), imsg.ErrAccountPasswordNotFollowRule)
updateAccount.Password = cryptox.PwdHash(updateAccount.Password)
}
oldAcc, err := a.AccountApp.GetById(updateAccount.Id)
biz.ErrIsNil(err, "账号信息不存在")
biz.ErrIsNilAppendErr(err, "Account does not exist: %s")
// 账号创建十分钟内允许修改用户名兼容oauth2首次登录修改用户名否则不允许修改
if oldAcc.CreateTime.Add(10 * time.Minute).Before(time.Now()) {
// 禁止更新用户名,防止误传被更新
updateAccount.Username = ""
}
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, updateAccount))
biz.ErrIsNil(a.AccountApp.Update(ctx, updateAccount))
}
/** 后台账号操作 **/
@@ -149,7 +152,7 @@ func (a *Account) SimpleAccounts(rc *req.Ctx) {
func (a *Account) AccountDetail(rc *req.Ctx) {
accountId := uint64(rc.PathParamInt("id"))
account, err := a.AccountApp.GetById(accountId)
biz.ErrIsNil(err, "账号不存在")
biz.ErrIsNilAppendErr(err, "Account does not exist: %s")
accountvo := new(vo.SimpleAccountVO)
structx.Copy(accountvo, account)
@@ -164,17 +167,21 @@ func (a *Account) SaveAccount(rc *req.Ctx) {
form.Password = "*****"
rc.ReqParam = form
ctx := rc.MetaCtx
if account.Id == 0 {
biz.NotEmpty(account.Password, "password is required")
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(account.Password), imsg.ErrAccountPasswordNotFollowRule)
account.Password = cryptox.PwdHash(account.Password)
biz.ErrIsNil(a.AccountApp.Create(rc.MetaCtx, account))
} else {
if account.Password != "" {
biz.IsTrue(utils.CheckAccountPasswordLever(account.Password), "密码强度必须8位以上且包含字⺟⼤⼩写+数字+特殊符号")
biz.IsTrueI(ctx, utils.CheckAccountPasswordLever(account.Password), imsg.ErrAccountPasswordNotFollowRule)
account.Password = cryptox.PwdHash(account.Password)
}
// 更新操作不允许修改用户名、防止误传更新
account.Username = ""
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))
biz.ErrIsNil(a.AccountApp.Update(ctx, account))
}
}
@@ -196,9 +203,7 @@ func (a *Account) DeleteAccount(rc *req.Ctx) {
ids := strings.Split(idsStr, ",")
for _, v := range ids {
value, err := strconv.Atoi(v)
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
biz.ErrIsNilAppendErr(a.AccountApp.Delete(rc.MetaCtx, uint64(value)), "删除失败:%s")
biz.ErrIsNil(a.AccountApp.Delete(rc.MetaCtx, cast.ToUint64(v)))
}
}

View File

@@ -9,6 +9,6 @@ import (
func GenerateCaptcha(rc *req.Ctx) {
id, image, err := captcha.Generate()
biz.ErrIsNilAppendErr(err, "获取验证码错误: %s")
biz.ErrIsNilAppendErr(err, "failed to generate the CAPTCHA: %s")
rc.ResData = collx.M{"base64Captcha": image, "cid": id}
}

View File

@@ -5,7 +5,9 @@ import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/config"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
)
type Config struct {
@@ -22,7 +24,7 @@ func (c *Config) Configs(rc *req.Ctx) {
func (c *Config) GetConfigValueByKey(rc *req.Ctx) {
key := rc.Query("key")
biz.NotEmpty(key, "key不能为空")
biz.NotEmpty(key, "key cannot be empty")
config := c.ConfigApp.GetConfig(key)
// 判断是否为公开配置
@@ -40,3 +42,9 @@ func (c *Config) SaveConfig(rc *req.Ctx) {
rc.ReqParam = form
biz.ErrIsNil(c.ConfigApp.Save(rc.MetaCtx, config))
}
// GetServerConfig 获取当前系统启动配置
func (c *Config) GetServerConfig(rc *req.Ctx) {
conf := config.Conf
rc.ResData = collx.Kvs("i18n", conf.Server.Lang)
}

View File

@@ -2,7 +2,7 @@ package form
type AccountCreateForm struct {
Id uint64 `json:"id"`
Name string `json:"name" binding:"required,max=16" msg:"required=姓名不能为空,max=姓名最大长度不能超过16位"`
Name string `json:"name" binding:"required,max=16" msg:"required=name cannot be blank,max=The maximum length of a name cannot exceed 16 characters"`
Username string `json:"username" binding:"pattern=account_username"`
Password string `json:"password"`
}

View File

@@ -24,7 +24,7 @@ func (r *Resource) GetAllResourceTree(rc *req.Ctx) {
func (r *Resource) GetById(rc *req.Ctx) {
res, err := r.ResourceApp.GetById(uint64(rc.PathParamInt("id")))
biz.ErrIsNil(err, "该资源不存在")
biz.ErrIsNil(err, "The resource does not exist")
rc.ResData = res
}
@@ -63,3 +63,10 @@ func (r *Resource) Sort(rc *req.Ctx) {
r.ResourceApp.Sort(rc.MetaCtx, sortE)
}
}
// GetResourceRoles
func (r *Resource) GetResourceRoles(rc *req.Ctx) {
rrs, err := r.ResourceApp.GetResourceRoles(uint64(rc.PathParamInt("id")))
biz.ErrIsNil(err)
rc.ResData = rrs
}

View File

@@ -8,7 +8,6 @@ import (
"mayfly-go/pkg/biz"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
"strconv"
"strings"
"github.com/may-fly/cast"
@@ -50,9 +49,7 @@ func (r *Role) DelRole(rc *req.Ctx) {
ids := strings.Split(idsStr, ",")
for _, v := range ids {
value, err := strconv.Atoi(v)
biz.ErrIsNilAppendErr(err, "string类型转换为int异常: %s")
r.RoleApp.DeleteRole(rc.MetaCtx, uint64(value))
biz.ErrIsNil(r.RoleApp.DeleteRole(rc.MetaCtx, cast.ToUint64(v)))
}
}
@@ -79,7 +76,7 @@ func (r *Role) SaveResource(rc *req.Ctx) {
return cast.ToUint64(val)
})
biz.ErrIsNilAppendErr(r.RoleApp.SaveRoleResource(rc.MetaCtx, form.Id, newIds), "保存角色资源失败: %s")
biz.ErrIsNilAppendErr(r.RoleApp.SaveRoleResource(rc.MetaCtx, form.Id, newIds), "save role resource failed: %s")
}
// 查看角色关联的用户

View File

@@ -20,7 +20,7 @@ func (s *System) ConnectWs(g *gin.Context) {
defer func() {
if err := recover(); err != nil {
errInfo := anyx.ToString(err)
logx.Errorf("websocket连接失败: %s", errInfo)
logx.Errorf("websocket connect error: %s", errInfo)
if wsConn != nil {
wsConn.WriteMessage(websocket.TextMessage, []byte(errInfo))
wsConn.Close()
@@ -28,14 +28,14 @@ func (s *System) ConnectWs(g *gin.Context) {
}
}()
biz.ErrIsNilAppendErr(err, "%s")
biz.ErrIsNil(err)
clientId := g.Query("clientId")
biz.NotEmpty(clientId, "clientId不能为空")
biz.NotEmpty(clientId, "clientId cannot be empty")
// 权限校验
rc := req.NewCtxWithGin(g)
err = req.PermissionHandler(rc)
biz.ErrIsNil(err, "sys websocket没有权限连接")
biz.ErrIsNil(err, "sys-websocket connect without permission")
// 登录账号信息
la := rc.GetLoginAccount()