50 lines
1.5 KiB
Go
Executable File

package interceptor
import (
"bindbox-game/internal/code"
"bindbox-game/internal/pkg/core"
"bindbox-game/internal/repository/mysql/model"
"net/http"
)
// CheckBlacklist 检查用户是否在黑名单中
func (i *interceptor) CheckBlacklist() core.HandlerFunc {
return func(ctx core.Context) {
// 1. 获取当前用户 ID (需在 Token 认证后使用)
userID := ctx.SessionUserInfo().Id
if userID <= 0 {
// 如果没有用户信息,可能是不需要认证的接口误用了此中间件,或者 Token 解析失败
// 这里偏向于放行还是拦截需根据业务决定,一般拦截
ctx.AbortWithError(core.Error(http.StatusUnauthorized, code.AuthorizationError, "未授权访问"))
return
}
// 2. 查询用户状态
// 这里每次请求都查库,如果有性能问题后续可加 Redis 缓存
var status int32
err := i.db.GetDbR().Model(&model.Users{}).
Select("status").
Where("id = ?", userID).
Scan(&status).Error
if err != nil {
// 数据库错误,安全起见放行还是报错?建议报错
ctx.AbortWithError(core.Error(http.StatusInternalServerError, code.ServerError, "系统繁忙"))
return
}
// 3. 检查黑名单状态
if status == model.UserStatusBlacklist {
ctx.AbortWithError(core.Error(http.StatusForbidden, code.ForbiddenError, "账号异常,禁止操作"))
return
}
// 4. 用户被禁用
if status == model.UserStatusDisabled {
ctx.AbortWithError(core.Error(http.StatusForbidden, code.ForbiddenError, "账号已被禁用"))
return
}
}
}