From 69e49bfe9f484587bea3ee8eb96df73d1643a584 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E5=AF=BB=E6=AC=A2?= Date: Mon, 5 Feb 2024 17:28:46 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E6=96=B0=E5=A2=9E=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E6=89=80=E6=9C=89=E8=A7=92=E8=89=B2=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/admin/role/role.go | 26 ++++++++++++++++++++++++++ model/param/role/select.go | 8 ++++++++ router/admin/role.go | 13 +++++++++++++ router/admin/route.go | 1 + service/role/select.go | 20 ++++++++++++++++++++ 5 files changed, 68 insertions(+) create mode 100644 model/param/role/select.go create mode 100644 router/admin/role.go 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 +}