diff --git a/api/admin/role/role.go b/api/admin/role/role.go index e829912..5d9238d 100644 --- a/api/admin/role/role.go +++ b/api/admin/role/role.go @@ -1 +1,27 @@ package role + +import ( + "github.com/gin-gonic/gin" + roleParam "wechat-robot/model/param/role" + "wechat-robot/pkg/response" + roleService "wechat-robot/service/role" +) + +// GetAll +// @description: 获取所有角色 +// @param ctx +func GetAll(ctx *gin.Context) { + var p roleParam.GetAll + if err := ctx.ShouldBind(&p); err != nil { + response.New(ctx).SetMsg("参数错误").SetError(err).Fail() + return + } + + records, err := roleService.GetAll(p) + if err != nil { + response.New(ctx).SetMsg("获取所有角色失败").SetError(err).Fail() + return + } + + response.New(ctx).SetData(records).Success() +} diff --git a/model/param/role/select.go b/model/param/role/select.go new file mode 100644 index 0000000..47ad65b --- /dev/null +++ b/model/param/role/select.go @@ -0,0 +1,8 @@ +package role + +// GetAll +// @description: 获取所有角色 +type GetAll struct { + Keyword string `json:"keyword" form:"keyword"` // 关键词 + Code string `json:"code" form:"code"` // 角色代码 +} diff --git a/router/admin/role.go b/router/admin/role.go new file mode 100644 index 0000000..c3e4a15 --- /dev/null +++ b/router/admin/role.go @@ -0,0 +1,13 @@ +package admin + +import ( + "github.com/gin-gonic/gin" + roleApi "wechat-robot/api/admin/role" +) + +// role +// @description: 角色管理 +// @param g +func role(g *gin.RouterGroup) { + g.GET("", roleApi.GetAll) // 获取所有角色 +} diff --git a/router/admin/route.go b/router/admin/route.go index c9b2818..a3ae5f0 100644 --- a/router/admin/route.go +++ b/router/admin/route.go @@ -15,4 +15,5 @@ func InitRoute(g *gin.RouterGroup) { menu(g.Group("/menu")) // 菜单相关 robot(g.Group("/robot")) // 机器人相关 aiAssistant(g.Group("/ai-assistant")) // AI助手相关 + role(g.Group("/role")) // 角色相关 } diff --git a/service/role/select.go b/service/role/select.go index b86d189..bddb9aa 100644 --- a/service/role/select.go +++ b/service/role/select.go @@ -1,8 +1,11 @@ package role import ( + "database/sql" "slices" "wechat-robot/internal/database" + "wechat-robot/model/entity" + roleParam "wechat-robot/model/param/role" ) // CheckIsSuperAdminUser @@ -29,3 +32,20 @@ func GetCodesByUserId(userId string) (codes []string) { Take(&codes) return } + +// GetAll +// @description: 获取所有角色 +// @param p roleParam.GetAll 查询参数 +// @return records []entity.Role 角色列表 +// @return err error 错误信息 +func GetAll(p roleParam.GetAll) (records []entity.Role, err error) { + tx := database.Client.Order("created_at desc") + if p.Keyword != "" { + tx.Where("name LIKE @keyword OR describe LIKE @keyword", sql.Named("keyword", "%"+p.Keyword+"%")) + } + if p.Code != "" { + tx.Where("code = ?", p.Code) + } + err = tx.Find(&records).Error + return +}