go-wxhelper/service/role/select.go

52 lines
1.4 KiB
Go

package role
import (
"database/sql"
"slices"
"wechat-robot/internal/database"
"wechat-robot/model/entity"
roleParam "wechat-robot/model/param/role"
)
// CheckIsSuperAdminUser
// @description: 检查是否是超级管理员用户
// @param userId string 用户Id
// @return flag bool 是否是超级管理员用户
func CheckIsSuperAdminUser(userId string) (flag bool) {
// 获取用户角色代码
codes := GetCodesByUserId(userId)
return slices.Contains(codes, "admin")
}
// GetCodesByUserId
// @description: 根据用户Id获取用户的角色代码
// @param userId string 用户Id
// @return codes []string 角色代码
func GetCodesByUserId(userId string) (codes []string) {
// 获取用户角色代码
database.Client.Table("t_role AS tr").
Joins("LEFT JOIN t_admin_user_role AS tur ON tur.role_id = tr.id").
Select("tr.code").
Where("tur.user_id = ?", userId).
Where("tr.is_del IS FALSE").
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
}