refactor: api层尽可能屏蔽gin框架相关代码

This commit is contained in:
meilin.huang
2024-02-24 16:30:29 +08:00
parent 7e7f02b502
commit b56b0187cf
44 changed files with 639 additions and 595 deletions

View File

@@ -10,7 +10,6 @@ import (
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/contextx"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/model"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
@@ -60,8 +59,7 @@ func (a *Account) GetPermissions(rc *req.Ctx) {
}
func (a *Account) ChangePassword(rc *req.Ctx) {
form := new(form.AccountChangePasswordForm)
ginx.BindJsonAndValid(rc.GinCtx, form)
form := req.BindJsonAndValid(rc, new(form.AccountChangePasswordForm))
originOldPwd, err := cryptox.DefaultRsaDecrypt(form.OldPassword, true)
biz.ErrIsNilAppendErr(err, "解密旧密码错误: %s")
@@ -98,7 +96,7 @@ func (a *Account) AccountInfo(rc *req.Ctx) {
// 更新个人账号信息
func (a *Account) UpdateAccount(rc *req.Ctx) {
updateAccount := ginx.BindJsonAndCopyTo[*entity.Account](rc.GinCtx, new(form.AccountUpdateForm), new(entity.Account))
updateAccount := req.BindJsonAndCopyTo[*entity.Account](rc, new(form.AccountUpdateForm), new(entity.Account))
// 账号id为登录者账号
updateAccount.Id = rc.GetLoginAccount().Id
@@ -122,8 +120,8 @@ func (a *Account) UpdateAccount(rc *req.Ctx) {
// @router /accounts [get]
func (a *Account) Accounts(rc *req.Ctx) {
condition := &entity.Account{}
condition.Username = rc.GinCtx.Query("username")
res, err := a.AccountApp.GetPageList(condition, ginx.GetPageParam(rc.GinCtx), new([]vo.AccountManageVO))
condition.Username = rc.F.Query("username")
res, err := a.AccountApp.GetPageList(condition, rc.F.GetPageParam(), new([]vo.AccountManageVO))
biz.ErrIsNil(err)
rc.ResData = res
}
@@ -131,7 +129,7 @@ func (a *Account) Accounts(rc *req.Ctx) {
// @router /accounts
func (a *Account) SaveAccount(rc *req.Ctx) {
form := &form.AccountCreateForm{}
account := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Account))
account := req.BindJsonAndCopyTo(rc, form, new(entity.Account))
form.Password = "*****"
rc.ReqParam = form
@@ -150,12 +148,10 @@ func (a *Account) SaveAccount(rc *req.Ctx) {
}
func (a *Account) ChangeStatus(rc *req.Ctx) {
g := rc.GinCtx
account := &entity.Account{}
account.Id = uint64(ginx.PathParamInt(g, "id"))
account.Id = uint64(rc.F.PathParamInt("id"))
status := entity.AccountStatus(int8(ginx.PathParamInt(g, "status")))
status := entity.AccountStatus(int8(rc.F.PathParamInt("status")))
biz.ErrIsNil(entity.AccountStatusEnum.Valid(status))
account.Status = status
@@ -164,7 +160,7 @@ func (a *Account) ChangeStatus(rc *req.Ctx) {
}
func (a *Account) DeleteAccount(rc *req.Ctx) {
idsStr := ginx.PathParam(rc.GinCtx, "id")
idsStr := rc.F.PathParam("id")
rc.ReqParam = idsStr
ids := strings.Split(idsStr, ",")
@@ -177,7 +173,7 @@ func (a *Account) DeleteAccount(rc *req.Ctx) {
// 获取账号角色信息列表
func (a *Account) AccountRoles(rc *req.Ctx) {
rc.ResData = a.getAccountRoles(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
rc.ResData = a.getAccountRoles(uint64(rc.F.PathParamInt("id")))
}
func (a *Account) getAccountRoles(accountId uint64) []*vo.AccountRoleVO {
@@ -221,23 +217,21 @@ func (a *Account) getAccountRoles(accountId uint64) []*vo.AccountRoleVO {
func (a *Account) AccountResources(rc *req.Ctx) {
var resources vo.ResourceManageVOList
// 获取账号菜单资源
biz.ErrIsNil(a.ResourceApp.GetAccountResources(uint64(ginx.PathParamInt(rc.GinCtx, "id")), &resources))
biz.ErrIsNil(a.ResourceApp.GetAccountResources(uint64(rc.F.PathParamInt("id")), &resources))
rc.ResData = resources.ToTrees(0)
}
// 关联账号角色
func (a *Account) RelateRole(rc *req.Ctx) {
var form form.AccountRoleForm
ginx.BindJsonAndValid(rc.GinCtx, &form)
form := req.BindJsonAndValid(rc, new(form.AccountRoleForm))
rc.ReqParam = form
biz.ErrIsNil(a.RoleApp.RelateAccountRole(rc.MetaCtx, form.Id, form.RoleId, consts.AccountRoleRelateType(form.RelateType)))
}
// 重置otp秘钥
func (a *Account) ResetOtpSecret(rc *req.Ctx) {
account := &entity.Account{OtpSecret: "-"}
accountId := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
accountId := uint64(rc.F.PathParamInt("id"))
account.Id = accountId
rc.ReqParam = collx.Kvs("accountId", accountId)
biz.ErrIsNil(a.AccountApp.Update(rc.MetaCtx, account))

View File

@@ -5,7 +5,6 @@ import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
)
@@ -14,16 +13,15 @@ type Config struct {
}
func (c *Config) Configs(rc *req.Ctx) {
g := rc.GinCtx
condition := &entity.Config{Key: g.Query("key")}
condition := &entity.Config{Key: rc.F.Query("key")}
condition.Permission = rc.GetLoginAccount().Username
res, err := c.ConfigApp.GetPageList(condition, ginx.GetPageParam(g), new([]entity.Config))
res, err := c.ConfigApp.GetPageList(condition, rc.F.GetPageParam(), new([]entity.Config))
biz.ErrIsNil(err)
rc.ResData = res
}
func (c *Config) GetConfigValueByKey(rc *req.Ctx) {
key := rc.GinCtx.Query("key")
key := rc.F.Query("key")
biz.NotEmpty(key, "key不能为空")
config := c.ConfigApp.GetConfig(key)
@@ -38,7 +36,7 @@ func (c *Config) GetConfigValueByKey(rc *req.Ctx) {
func (c *Config) SaveConfig(rc *req.Ctx) {
form := &form.ConfigForm{}
config := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Config))
config := req.BindJsonAndCopyTo(rc, form, new(entity.Config))
rc.ReqParam = form
biz.ErrIsNil(c.ConfigApp.Save(rc.MetaCtx, config))
}

View File

@@ -7,7 +7,6 @@ import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/collx"
)
@@ -23,15 +22,14 @@ func (r *Resource) GetAllResourceTree(rc *req.Ctx) {
}
func (r *Resource) GetById(rc *req.Ctx) {
res, err := r.ResourceApp.GetById(new(entity.Resource), uint64(ginx.PathParamInt(rc.GinCtx, "id")))
res, err := r.ResourceApp.GetById(new(entity.Resource), uint64(rc.F.PathParamInt("id")))
biz.ErrIsNil(err, "该资源不存在")
rc.ResData = res
}
func (r *Resource) SaveResource(rc *req.Ctx) {
g := rc.GinCtx
form := new(form.ResourceForm)
entity := ginx.BindJsonAndCopyTo(g, form, new(entity.Resource))
entity := req.BindJsonAndCopyTo(rc, form, new(entity.Resource))
rc.ReqParam = form
@@ -43,19 +41,19 @@ func (r *Resource) SaveResource(rc *req.Ctx) {
}
func (r *Resource) DelResource(rc *req.Ctx) {
biz.ErrIsNil(r.ResourceApp.Delete(rc.MetaCtx, uint64(ginx.PathParamInt(rc.GinCtx, "id"))))
biz.ErrIsNil(r.ResourceApp.Delete(rc.MetaCtx, uint64(rc.F.PathParamInt("id"))))
}
func (r *Resource) ChangeStatus(rc *req.Ctx) {
rid := uint64(ginx.PathParamInt(rc.GinCtx, "id"))
status := int8(ginx.PathParamInt(rc.GinCtx, "status"))
rid := uint64(rc.F.PathParamInt("id"))
status := int8(rc.F.PathParamInt("status"))
rc.ReqParam = collx.Kvs("id", rid, "status", status)
biz.ErrIsNil(r.ResourceApp.ChangeStatus(rc.MetaCtx, rid, status))
}
func (r *Resource) Sort(rc *req.Ctx) {
var rs []form.ResourceForm
rc.GinCtx.ShouldBindJSON(&rs)
rc.F.BindJSON(&rs)
rc.ReqParam = rs
for _, v := range rs {

View File

@@ -6,7 +6,6 @@ import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
"mayfly-go/pkg/utils/anyx"
"mayfly-go/pkg/utils/collx"
@@ -20,10 +19,9 @@ type Role struct {
}
func (r *Role) Roles(rc *req.Ctx) {
g := rc.GinCtx
cond, pageParam := ginx.BindQueryAndPage(g, new(entity.RoleQuery))
cond, pageParam := req.BindQueryAndPage(rc, new(entity.RoleQuery))
notIdsStr := g.Query("notIds")
notIdsStr := rc.F.Query("notIds")
if notIdsStr != "" {
cond.NotIds = collx.ArrayMap[string, uint64](strings.Split(notIdsStr, ","), func(val string) uint64 {
return uint64(anyx.ConvInt(val))
@@ -38,7 +36,7 @@ func (r *Role) Roles(rc *req.Ctx) {
// 保存角色信息
func (r *Role) SaveRole(rc *req.Ctx) {
form := &form.RoleForm{}
role := ginx.BindJsonAndCopyTo(rc.GinCtx, form, new(entity.Role))
role := req.BindJsonAndCopyTo(rc, form, new(entity.Role))
rc.ReqParam = form
r.RoleApp.SaveRole(rc.MetaCtx, role)
@@ -46,7 +44,7 @@ func (r *Role) SaveRole(rc *req.Ctx) {
// 删除角色及其资源关联关系
func (r *Role) DelRole(rc *req.Ctx) {
idsStr := ginx.PathParam(rc.GinCtx, "id")
idsStr := rc.F.PathParam("id")
rc.ReqParam = collx.Kvs("ids", idsStr)
ids := strings.Split(idsStr, ",")
@@ -59,23 +57,20 @@ func (r *Role) DelRole(rc *req.Ctx) {
// 获取角色关联的资源id数组用于分配资源时回显已拥有的资源
func (r *Role) RoleResourceIds(rc *req.Ctx) {
rc.ResData = r.RoleApp.GetRoleResourceIds(uint64(ginx.PathParamInt(rc.GinCtx, "id")))
rc.ResData = r.RoleApp.GetRoleResourceIds(uint64(rc.F.PathParamInt("id")))
}
// 查看角色关联的资源树信息
func (r *Role) RoleResource(rc *req.Ctx) {
g := rc.GinCtx
var resources vo.ResourceManageVOList
r.RoleApp.GetRoleResources(uint64(ginx.PathParamInt(g, "id")), &resources)
r.RoleApp.GetRoleResources(uint64(rc.F.PathParamInt("id")), &resources)
rc.ResData = resources.ToTrees(0)
}
// 保存角色资源
func (r *Role) SaveResource(rc *req.Ctx) {
var form form.RoleResourceForm
ginx.BindJsonAndValid(rc.GinCtx, &form)
req.BindJsonAndValid(rc, &form)
rc.ReqParam = form
// 将,拼接的字符串进行切割并转换
@@ -89,9 +84,8 @@ func (r *Role) SaveResource(rc *req.Ctx) {
// 查看角色关联的用户
func (r *Role) RoleAccount(rc *req.Ctx) {
g := rc.GinCtx
cond, pageParam := ginx.BindQueryAndPage[*entity.RoleAccountQuery](g, new(entity.RoleAccountQuery))
cond.RoleId = uint64(ginx.PathParamInt(g, "id"))
cond, pageParam := req.BindQueryAndPage[*entity.RoleAccountQuery](rc, new(entity.RoleAccountQuery))
cond.RoleId = uint64(rc.F.PathParamInt("id"))
var accounts []*vo.AccountRoleVO
res, err := r.RoleApp.GetRoleAccountPage(cond, pageParam, &accounts)
biz.ErrIsNil(err)

View File

@@ -4,7 +4,6 @@ import (
"mayfly-go/internal/sys/application"
"mayfly-go/internal/sys/domain/entity"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/ginx"
"mayfly-go/pkg/req"
)
@@ -13,7 +12,7 @@ type Syslog struct {
}
func (r *Syslog) Syslogs(rc *req.Ctx) {
queryCond, page := ginx.BindQueryAndPage[*entity.SysLogQuery](rc.GinCtx, new(entity.SysLogQuery))
queryCond, page := req.BindQueryAndPage[*entity.SysLogQuery](rc, new(entity.SysLogQuery))
res, err := r.SyslogApp.GetPageList(queryCond, page, new([]entity.SysLog), "create_time DESC")
biz.ErrIsNil(err)
rc.ResData = res