This commit is contained in:
2026-02-02 18:26:50 +08:00
13 changed files with 116 additions and 30 deletions

View File

@@ -61,6 +61,7 @@ type (
Nickname string `json:"nickname"` // 昵称 Nickname string `json:"nickname"` // 昵称
Info string `json:"info"` // 备注信息 Info string `json:"info"` // 备注信息
Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否
Disable int64 `json:"disable"` // 是否封禁 0-可用 1-禁用
CreateTime string `json:"create_time"` // 创建时间 CreateTime string `json:"create_time"` // 创建时间
UpdateTime string `json:"update_time"` // 更新时间 UpdateTime string `json:"update_time"` // 更新时间
} }
@@ -77,6 +78,7 @@ type (
Nickname string `json:"nickname"` // 昵称 Nickname string `json:"nickname"` // 昵称
Info string `json:"info"` // 备注信息 Info string `json:"info"` // 备注信息
Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否
Disable int64 `json:"disable"` // 是否封禁 0-可用 1-禁用
CreateTime string `json:"create_time"` // 创建时间 CreateTime string `json:"create_time"` // 创建时间
UpdateTime string `json:"update_time"` // 更新时间 UpdateTime string `json:"update_time"` // 更新时间
} }
@@ -103,6 +105,7 @@ type (
Nickname *string `json:"nickname,optional"` // 昵称 Nickname *string `json:"nickname,optional"` // 昵称
Info *string `json:"info,optional"` // 备注信息 Info *string `json:"info,optional"` // 备注信息
Inside *int64 `json:"inside,optional"` // 是否内部用户 1-是 0-否 Inside *int64 `json:"inside,optional"` // 是否内部用户 1-是 0-否
Disable *int64 `json:"disable,optional"` // 是否封禁 0-可用 1-禁用
} }
// 更新响应 // 更新响应

View File

@@ -770,7 +770,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.UserAuthInterceptor}, []rest.Middleware{serverCtx.UserDisableInterceptor, serverCtx.UserAuthInterceptor},
[]rest.Route{ []rest.Route{
{ {
Method: http.MethodGet, Method: http.MethodGet,
@@ -810,7 +810,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.UserAuthInterceptor}, []rest.Middleware{serverCtx.UserDisableInterceptor, serverCtx.UserAuthInterceptor},
[]rest.Route{ []rest.Route{
{ {
Method: http.MethodPost, Method: http.MethodPost,
@@ -830,7 +830,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.UserAuthInterceptor}, []rest.Middleware{serverCtx.UserDisableInterceptor, serverCtx.UserAuthInterceptor},
[]rest.Route{ []rest.Route{
{ {
Method: http.MethodGet, Method: http.MethodGet,
@@ -880,7 +880,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor}, []rest.Middleware{serverCtx.AuthInterceptor, serverCtx.UserDisableInterceptor},
[]rest.Route{ []rest.Route{
{ {
Method: http.MethodPost, Method: http.MethodPost,
@@ -987,7 +987,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.UserAuthInterceptor}, []rest.Middleware{serverCtx.UserDisableInterceptor, serverCtx.UserAuthInterceptor},
[]rest.Route{ []rest.Route{
{ {
Method: http.MethodPost, Method: http.MethodPost,
@@ -1039,7 +1039,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor}, []rest.Middleware{serverCtx.AuthInterceptor, serverCtx.UserDisableInterceptor},
[]rest.Route{ []rest.Route{
{ {
// query service agent // query service agent
@@ -1059,7 +1059,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.UserAuthInterceptor}, []rest.Middleware{serverCtx.UserDisableInterceptor, serverCtx.UserAuthInterceptor},
[]rest.Route{ []rest.Route{
{ {
// query service // query service
@@ -1075,7 +1075,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.UserAuthInterceptor}, []rest.Middleware{serverCtx.UserDisableInterceptor, serverCtx.UserAuthInterceptor},
[]rest.Route{ []rest.Route{
{ {
// 生成分享链接 // 生成分享链接
@@ -1179,7 +1179,7 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
server.AddRoutes( server.AddRoutes(
rest.WithMiddlewares( rest.WithMiddlewares(
[]rest.Middleware{serverCtx.AuthInterceptor}, []rest.Middleware{serverCtx.AuthInterceptor, serverCtx.UserDisableInterceptor},
[]rest.Route{ []rest.Route{
{ {
// 绑定手机号 // 绑定手机号

View File

@@ -43,6 +43,7 @@ func (l *AdminGetPlatformUserDetailLogic) AdminGetPlatformUserDetail(req *types.
Nickname: "", Nickname: "",
Info: user.Info, Info: user.Info,
Inside: user.Inside, Inside: user.Inside,
Disable: user.Disable,
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"), CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"), UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"),
} }

View File

@@ -31,7 +31,12 @@ func NewAdminGetPlatformUserListLogic(ctx context.Context, svcCtx *svc.ServiceCo
func (l *AdminGetPlatformUserListLogic) AdminGetPlatformUserList(req *types.AdminGetPlatformUserListReq) (resp *types.AdminGetPlatformUserListResp, err error) { func (l *AdminGetPlatformUserListLogic) AdminGetPlatformUserList(req *types.AdminGetPlatformUserListReq) (resp *types.AdminGetPlatformUserListResp, err error) {
builder := l.svcCtx.UserModel.SelectBuilder() builder := l.svcCtx.UserModel.SelectBuilder()
if req.Mobile != "" { if req.Mobile != "" {
builder = builder.Where("mobile = ?", req.Mobile) // 数据库手机号为密文,需加密明文手机号后再查询
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, l.svcCtx.Config.Encrypt.SecretKey)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机号加密失败: %v", err)
}
builder = builder.Where("mobile = ?", encryptedMobile)
} }
if req.Nickname != "" { if req.Nickname != "" {
builder = builder.Where("nickname = ?", req.Nickname) builder = builder.Where("nickname = ?", req.Nickname)
@@ -72,6 +77,7 @@ func (l *AdminGetPlatformUserListLogic) AdminGetPlatformUserList(req *types.Admi
Nickname: "", Nickname: "",
Info: user.Info, Info: user.Info,
Inside: user.Inside, Inside: user.Inside,
Disable: user.Disable,
CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"), CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"),
UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"), UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"),
} }

View File

@@ -52,6 +52,12 @@ func (l *AdminUpdatePlatformUserLogic) AdminUpdatePlatformUser(req *types.AdminU
} }
user.Inside = *req.Inside user.Inside = *req.Inside
} }
if req.Disable != nil {
if *req.Disable != 1 && *req.Disable != 0 {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "封禁状态错误: %d", *req.Disable)
}
user.Disable = *req.Disable
}
if req.Password != nil { if req.Password != nil {
user.Password = sql.NullString{String: *req.Password, Valid: *req.Password != ""} user.Password = sql.NullString{String: *req.Password, Valid: *req.Password != ""}
} }

View File

@@ -0,0 +1,60 @@
package middleware
import (
"net/http"
"tyc-server/app/main/model"
"tyc-server/common/ctxdata"
"tyc-server/common/result"
"tyc-server/common/xerr"
"github.com/zeromicro/go-zero/rest/httpx"
)
// UserDisableInterceptorMiddleware 检查用户是否被封禁,封禁用户直接拦截并返回错误
type UserDisableInterceptorMiddleware struct {
UserModel model.UserModel
}
// NewUserDisableInterceptorMiddleware 创建用户封禁检查中间件
func NewUserDisableInterceptorMiddleware(userModel model.UserModel) *UserDisableInterceptorMiddleware {
return &UserDisableInterceptorMiddleware{
UserModel: userModel,
}
}
// Handle 处理请求:当用户已登录且被封禁时,直接返回错误
func (m *UserDisableInterceptorMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
claims, err := ctxdata.GetClaimsFromCtx(r.Context())
if err != nil {
// 无登录信息,放行(由后续认证中间件处理)
next(w, r)
return
}
// 管理员不检查 user 表的 disable 字段
if claims.UserType == model.UserTypeAdmin {
next(w, r)
return
}
// 查询用户是否被封禁
user, err := m.UserModel.FindOne(r.Context(), claims.UserId)
if err != nil {
// 用户不存在(如管理员账号)或查询失败,放行
next(w, r)
return
}
// 1 表示已禁用
if user.Disable == 1 {
errcode := xerr.USER_DISABLED
errmsg := xerr.MapErrMsg(errcode)
httpx.WriteJson(w, http.StatusOK, result.Error(errcode, errmsg))
return
}
next(w, r)
}
}

View File

@@ -21,9 +21,10 @@ type ServiceContext struct {
Redis *redis.Redis Redis *redis.Redis
// 中间件 // 中间件
AuthInterceptor rest.Middleware AuthInterceptor rest.Middleware
UserAuthInterceptor rest.Middleware UserAuthInterceptor rest.Middleware
AdminAuthInterceptor rest.Middleware UserDisableInterceptor rest.Middleware
AdminAuthInterceptor rest.Middleware
// 用户相关模型 // 用户相关模型
UserModel model.UserModel UserModel model.UserModel
@@ -224,8 +225,9 @@ func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{ return &ServiceContext{
Config: c, Config: c,
Redis: redisClient, Redis: redisClient,
AuthInterceptor: middleware.NewAuthInterceptorMiddleware(c).Handle, AuthInterceptor: middleware.NewAuthInterceptorMiddleware(c).Handle,
UserAuthInterceptor: middleware.NewUserAuthInterceptorMiddleware().Handle, UserAuthInterceptor: middleware.NewUserAuthInterceptorMiddleware().Handle,
UserDisableInterceptor: middleware.NewUserDisableInterceptorMiddleware(userModel).Handle,
AdminAuthInterceptor: middleware.NewAdminAuthInterceptorMiddleware(c, AdminAuthInterceptor: middleware.NewAdminAuthInterceptorMiddleware(c,
adminUserModel, adminUserRoleModel, adminRoleModel, adminApiModel, adminRoleApiModel).Handle, adminUserModel, adminUserRoleModel, adminRoleModel, adminApiModel, adminRoleApiModel).Handle,

View File

@@ -568,6 +568,7 @@ type AdminGetPlatformUserDetailResp struct {
Nickname string `json:"nickname"` // 昵称 Nickname string `json:"nickname"` // 昵称
Info string `json:"info"` // 备注信息 Info string `json:"info"` // 备注信息
Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否
Disable int64 `json:"disable"` // 是否封禁 0-可用 1-禁用
CreateTime string `json:"create_time"` // 创建时间 CreateTime string `json:"create_time"` // 创建时间
UpdateTime string `json:"update_time"` // 更新时间 UpdateTime string `json:"update_time"` // 更新时间
} }
@@ -949,12 +950,13 @@ type AdminUpdateOrderResp struct {
} }
type AdminUpdatePlatformUserReq struct { type AdminUpdatePlatformUserReq struct {
Id int64 `path:"id"` // 用户ID Id int64 `path:"id"` // 用户ID
Mobile *string `json:"mobile,optional"` // 手机号 Mobile *string `json:"mobile,optional"` // 手机号
Password *string `json:"password,optional"` // 密码 Password *string `json:"password,optional"` // 密码
Nickname *string `json:"nickname,optional"` // 昵称 Nickname *string `json:"nickname,optional"` // 昵称
Info *string `json:"info,optional"` // 备注信息 Info *string `json:"info,optional"` // 备注信息
Inside *int64 `json:"inside,optional"` // 是否内部用户 1-是 0-否 Inside *int64 `json:"inside,optional"` // 是否内部用户 1-是 0-否
Disable *int64 `json:"disable,optional"` // 是否封禁 0-可用 1-禁用
} }
type AdminUpdatePlatformUserResp struct { type AdminUpdatePlatformUserResp struct {
@@ -1861,6 +1863,7 @@ type PlatformUserListItem struct {
Nickname string `json:"nickname"` // 昵称 Nickname string `json:"nickname"` // 昵称
Info string `json:"info"` // 备注信息 Info string `json:"info"` // 备注信息
Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否
Disable int64 `json:"disable"` // 是否封禁 0-可用 1-禁用
CreateTime string `json:"create_time"` // 创建时间 CreateTime string `json:"create_time"` // 创建时间
UpdateTime string `json:"update_time"` // 更新时间 UpdateTime string `json:"update_time"` // 更新时间
} }

View File

@@ -68,6 +68,7 @@ type (
Nickname sql.NullString `db:"nickname"` Nickname sql.NullString `db:"nickname"`
Info string `db:"info"` Info string `db:"info"`
Inside int64 `db:"inside"` Inside int64 `db:"inside"`
Disable int64 `db:"disable"` // 0可用 1禁用
} }
) )
@@ -83,11 +84,11 @@ func (m *defaultUserModel) Insert(ctx context.Context, session sqlx.Session, dat
tycUserIdKey := fmt.Sprintf("%s%v", cacheTycUserIdPrefix, data.Id) tycUserIdKey := fmt.Sprintf("%s%v", cacheTycUserIdPrefix, data.Id)
tycUserMobileKey := fmt.Sprintf("%s%v", cacheTycUserMobilePrefix, data.Mobile) tycUserMobileKey := fmt.Sprintf("%s%v", cacheTycUserMobilePrefix, data.Mobile)
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet) query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, userRowsExpectAutoSet)
if session != nil { if session != nil {
return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside) return session.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside, data.Disable)
} }
return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside) return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside, data.Disable)
}, tycUserIdKey, tycUserMobileKey) }, tycUserIdKey, tycUserMobileKey)
} }
@@ -138,9 +139,9 @@ func (m *defaultUserModel) Update(ctx context.Context, session sqlx.Session, new
return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { return m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userRowsWithPlaceHolder) query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, userRowsWithPlaceHolder)
if session != nil { if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id) return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id)
} }
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id) return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id)
}, tycUserIdKey, tycUserMobileKey) }, tycUserIdKey, tycUserMobileKey)
} }
@@ -161,9 +162,9 @@ func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.S
sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { sqlResult, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) {
query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, userRowsWithPlaceHolder) query := fmt.Sprintf("update %s set %s where `id` = ? and version = ? ", m.table, userRowsWithPlaceHolder)
if session != nil { if session != nil {
return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion) return session.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id, oldVersion)
} }
return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Id, oldVersion) return conn.ExecCtx(ctx, query, newData.DeleteTime, newData.DelState, newData.Version, newData.Mobile, newData.Password, newData.Nickname, newData.Info, newData.Inside, newData.Disable, newData.Id, oldVersion)
}, tycUserIdKey, tycUserMobileKey) }, tycUserIdKey, tycUserMobileKey)
if err != nil { if err != nil {
return err return err

View File

@@ -16,6 +16,7 @@ const PARAM_VERIFICATION_ERROR uint32 = 100007
const CUSTOM_ERROR uint32 = 100008 const CUSTOM_ERROR uint32 = 100008
const USER_NOT_FOUND uint32 = 100009 const USER_NOT_FOUND uint32 = 100009
const USER_NEED_BIND_MOBILE uint32 = 100010 const USER_NEED_BIND_MOBILE uint32 = 100010
const USER_DISABLED uint32 = 100011 // 用户已被封禁
const LOGIN_FAILED uint32 = 200001 const LOGIN_FAILED uint32 = 200001
const LOGIC_QUERY_WAIT uint32 = 200002 const LOGIC_QUERY_WAIT uint32 = 200002

View File

@@ -11,6 +11,7 @@ func init() {
message[TOKEN_GENERATE_ERROR] = "生成token失败" message[TOKEN_GENERATE_ERROR] = "生成token失败"
message[DB_ERROR] = "系统维护升级中,请稍后再试" message[DB_ERROR] = "系统维护升级中,请稍后再试"
message[DB_UPDATE_AFFECTED_ZERO_ERROR] = "更新数据影响行数为0" message[DB_UPDATE_AFFECTED_ZERO_ERROR] = "更新数据影响行数为0"
message[USER_DISABLED] = "您的账号已被封禁,如有疑问请联系客服"
} }
func MapErrMsg(errcode uint32) string { func MapErrMsg(errcode uint32) string {

View File

@@ -37,7 +37,7 @@ $tables = @(
# "query_cleanup_log" # "query_cleanup_log"
# "query_cleanup_detail" # "query_cleanup_detail"
# "query_cleanup_config" # "query_cleanup_config"
# "user" "user"
# "user_auth" # "user_auth"
# "user_temp" # "user_temp"
# "example" # "example"
@@ -54,7 +54,7 @@ $tables = @(
# "admin_promotion_link_stats_total" # "admin_promotion_link_stats_total"
# "admin_promotion_link_stats_history" # "admin_promotion_link_stats_history"
# "admin_promotion_order" # "admin_promotion_order"
"query_user_record" # "query_user_record"
) )

View File

@@ -0,0 +1,2 @@
-- 为用户表添加 disable 字段0 可用1 禁用,默认 0
ALTER TABLE `user` ADD COLUMN `disable` tinyint NOT NULL DEFAULT 0 COMMENT '0可用 1禁用' AFTER `inside`;