2024-01-19 12:06:30 +08:00
|
|
|
package role
|
|
|
|
|
|
|
|
import (
|
2024-02-05 17:28:46 +08:00
|
|
|
"database/sql"
|
2024-01-19 12:06:30 +08:00
|
|
|
"slices"
|
|
|
|
"wechat-robot/internal/database"
|
2024-02-05 17:28:46 +08:00
|
|
|
"wechat-robot/model/entity"
|
|
|
|
roleParam "wechat-robot/model/param/role"
|
2024-01-19 12:06:30 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// CheckIsSuperAdminUser
|
|
|
|
// @description: 检查是否是超级管理员用户
|
|
|
|
// @param userId string 用户Id
|
|
|
|
// @return flag bool 是否是超级管理员用户
|
|
|
|
func CheckIsSuperAdminUser(userId string) (flag bool) {
|
2024-02-01 20:39:54 +08:00
|
|
|
// 获取用户角色代码
|
|
|
|
codes := GetCodesByUserId(userId)
|
|
|
|
return slices.Contains(codes, "admin")
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCodesByUserId
|
|
|
|
// @description: 根据用户Id获取用户的角色代码
|
|
|
|
// @param userId string 用户Id
|
|
|
|
// @return codes []string 角色代码
|
|
|
|
func GetCodesByUserId(userId string) (codes []string) {
|
2024-01-19 12:06:30 +08:00
|
|
|
// 获取用户角色代码
|
|
|
|
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)
|
2024-02-01 20:39:54 +08:00
|
|
|
return
|
2024-01-19 12:06:30 +08:00
|
|
|
}
|
2024-02-05 17:28:46 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|