逻辑完善,登录接口返回用户角色代码

This commit is contained in:
李寻欢 2024-02-01 20:39:54 +08:00
parent 379e2bdb54
commit 0aae8e36eb
2 changed files with 14 additions and 3 deletions

View File

@ -12,6 +12,7 @@ import (
"wechat-robot/internal/database" "wechat-robot/internal/database"
"wechat-robot/model/entity" "wechat-robot/model/entity"
userService "wechat-robot/service/adminuser" userService "wechat-robot/service/adminuser"
"wechat-robot/service/role"
"wechat-robot/utils" "wechat-robot/utils"
) )
@ -64,7 +65,8 @@ func ExtensionFields(ti oauth2.TokenInfo) (fieldsValue map[string]any) {
_ = database.Client.Take(&ui, "id = ?", ti.GetUserID()).Error _ = database.Client.Take(&ui, "id = ?", ti.GetUserID()).Error
fieldsValue["username"] = ui.Username fieldsValue["username"] = ui.Username
fieldsValue["id"] = ui.Id fieldsValue["id"] = ui.Id
fieldsValue["roles"] = []string{"admin"} // 获取用户的角色代码
fieldsValue["roles"] = role.GetCodesByUserId(ti.GetUserID())
return return
} }

View File

@ -10,7 +10,16 @@ import (
// @param userId string 用户Id // @param userId string 用户Id
// @return flag bool 是否是超级管理员用户 // @return flag bool 是否是超级管理员用户
func CheckIsSuperAdminUser(userId string) (flag bool) { func CheckIsSuperAdminUser(userId string) (flag bool) {
var codes []string // 获取用户角色代码
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"). database.Client.Table("t_role AS tr").
Joins("LEFT JOIN t_admin_user_role AS tur ON tur.role_id = tr.id"). Joins("LEFT JOIN t_admin_user_role AS tur ON tur.role_id = tr.id").
@ -18,5 +27,5 @@ func CheckIsSuperAdminUser(userId string) (flag bool) {
Where("tur.user_id = ?", userId). Where("tur.user_id = ?", userId).
Where("tr.is_del IS FALSE"). Where("tr.is_del IS FALSE").
Take(&codes) Take(&codes)
return slices.Contains(codes, "admin") return
} }