新增获取所有角色接口

This commit is contained in:
李寻欢 2024-02-05 17:28:46 +08:00
parent 2f736e521c
commit 69e49bfe9f
5 changed files with 68 additions and 0 deletions

View File

@ -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()
}

View File

@ -0,0 +1,8 @@
package role
// GetAll
// @description: 获取所有角色
type GetAll struct {
Keyword string `json:"keyword" form:"keyword"` // 关键词
Code string `json:"code" form:"code"` // 角色代码
}

13
router/admin/role.go Normal file
View File

@ -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) // 获取所有角色
}

View File

@ -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")) // 角色相关
}

View File

@ -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
}