diff --git a/app/main/api/data/authorization_docs/2026/02/auth_4_3098_20260204_132534.pdf b/app/main/api/data/authorization_docs/2026/02/auth_4_3098_20260204_132534.pdf new file mode 100644 index 0000000..3fb71a9 Binary files /dev/null and b/app/main/api/data/authorization_docs/2026/02/auth_4_3098_20260204_132534.pdf differ diff --git a/app/main/api/desc/admin/platform_user.api b/app/main/api/desc/admin/platform_user.api index 7ab5e63..e50216c 100644 --- a/app/main/api/desc/admin/platform_user.api +++ b/app/main/api/desc/admin/platform_user.api @@ -61,6 +61,7 @@ type ( Nickname string `json:"nickname"` // 昵称 Info string `json:"info"` // 备注信息 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 + Disable int64 `json:"disable"` // 是否封禁 0-可用 1-封禁 CreateTime string `json:"create_time"` // 创建时间 UpdateTime string `json:"update_time"` // 更新时间 } @@ -77,6 +78,7 @@ type ( Nickname string `json:"nickname"` // 昵称 Info string `json:"info"` // 备注信息 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 + Disable int64 `json:"disable"` // 是否封禁 0-可用 1-封禁 CreateTime string `json:"create_time"` // 创建时间 UpdateTime string `json:"update_time"` // 更新时间 } @@ -103,6 +105,7 @@ type ( Nickname *string `json:"nickname,optional"` // 昵称 Info *string `json:"info,optional"` // 备注信息 Inside *int64 `json:"inside,optional"` // 是否内部用户 1-是 0-否 + Disable *int64 `json:"disable,optional"` // 是否封禁 0-可用 1-封禁 } // 更新响应 diff --git a/app/main/api/internal/logic/admin_platform_user/admingetplatformuserdetaillogic.go b/app/main/api/internal/logic/admin_platform_user/admingetplatformuserdetaillogic.go index a9e6201..c5a4a88 100644 --- a/app/main/api/internal/logic/admin_platform_user/admingetplatformuserdetaillogic.go +++ b/app/main/api/internal/logic/admin_platform_user/admingetplatformuserdetaillogic.go @@ -43,6 +43,7 @@ func (l *AdminGetPlatformUserDetailLogic) AdminGetPlatformUserDetail(req *types. Nickname: "", Info: user.Info, Inside: user.Inside, + Disable: user.Disable, CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"), UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"), } diff --git a/app/main/api/internal/logic/admin_platform_user/admingetplatformuserlistlogic.go b/app/main/api/internal/logic/admin_platform_user/admingetplatformuserlistlogic.go index 7507da0..e2fe79c 100644 --- a/app/main/api/internal/logic/admin_platform_user/admingetplatformuserlistlogic.go +++ b/app/main/api/internal/logic/admin_platform_user/admingetplatformuserlistlogic.go @@ -30,8 +30,14 @@ func NewAdminGetPlatformUserListLogic(ctx context.Context, svcCtx *svc.ServiceCo func (l *AdminGetPlatformUserListLogic) AdminGetPlatformUserList(req *types.AdminGetPlatformUserListReq) (resp *types.AdminGetPlatformUserListResp, err error) { builder := l.svcCtx.UserModel.SelectBuilder() + secretKey := l.svcCtx.Config.Encrypt.SecretKey if req.Mobile != "" { - builder = builder.Where("mobile = ?", req.Mobile) + // 数据库存密文,筛选时用明文加密后再匹配 + encryptedMobile, encErr := crypto.EncryptMobile(req.Mobile, secretKey) + if encErr != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机号加密失败: %v", encErr) + } + builder = builder.Where("mobile = ?", encryptedMobile) } if req.Nickname != "" { builder = builder.Where("nickname = ?", req.Nickname) @@ -55,8 +61,6 @@ func (l *AdminGetPlatformUserListLogic) AdminGetPlatformUserList(req *types.Admi return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询用户分页失败: %v", err) } var items []types.PlatformUserListItem - secretKey := l.svcCtx.Config.Encrypt.SecretKey - for _, user := range users { mobile := user.Mobile if mobile.Valid { @@ -72,6 +76,7 @@ func (l *AdminGetPlatformUserListLogic) AdminGetPlatformUserList(req *types.Admi Nickname: "", Info: user.Info, Inside: user.Inside, + Disable: user.Disable, CreateTime: user.CreateTime.Format("2006-01-02 15:04:05"), UpdateTime: user.UpdateTime.Format("2006-01-02 15:04:05"), } diff --git a/app/main/api/internal/logic/admin_platform_user/adminupdateplatformuserlogic.go b/app/main/api/internal/logic/admin_platform_user/adminupdateplatformuserlogic.go index d4e6baa..9cf9143 100644 --- a/app/main/api/internal/logic/admin_platform_user/adminupdateplatformuserlogic.go +++ b/app/main/api/internal/logic/admin_platform_user/adminupdateplatformuserlogic.go @@ -52,6 +52,12 @@ func (l *AdminUpdatePlatformUserLogic) AdminUpdatePlatformUser(req *types.AdminU } 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 { user.Password = sql.NullString{String: *req.Password, Valid: *req.Password != ""} } diff --git a/app/main/api/internal/logic/query/queryservicelogic.go b/app/main/api/internal/logic/query/queryservicelogic.go index 1ac8a93..a5cfbe7 100644 --- a/app/main/api/internal/logic/query/queryservicelogic.go +++ b/app/main/api/internal/logic/query/queryservicelogic.go @@ -40,8 +40,8 @@ func NewQueryServiceLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Quer func (l *QueryServiceLogic) QueryService(req *types.QueryServiceReq) (resp *types.QueryServiceResp, err error) { if req.AgentIdentifier != "" { - return nil, errors.Wrapf(xerr.NewErrMsg("系统正在升级,查询服务暂不可用,恢复时间待定"), "查询服务暂停, AgentIdentifier: %s", req.AgentIdentifier) - // l.ctx = context.WithValue(l.ctx, "agentIdentifier", req.AgentIdentifier) + // return nil, errors.Wrapf(xerr.NewErrMsg("系统正在升级,查询服务暂不可用,恢复时间待定"), "查询服务暂停, AgentIdentifier: %s", req.AgentIdentifier) + l.ctx = context.WithValue(l.ctx, "agentIdentifier", req.AgentIdentifier) } else if req.App { l.ctx = context.WithValue(l.ctx, "app", req.App) } diff --git a/app/main/api/internal/logic/user/bindmobilelogic.go b/app/main/api/internal/logic/user/bindmobilelogic.go index 2d62599..95dbfb3 100644 --- a/app/main/api/internal/logic/user/bindmobilelogic.go +++ b/app/main/api/internal/logic/user/bindmobilelogic.go @@ -97,6 +97,10 @@ func (l *BindMobileLogic) BindMobile(req *types.BindMobileReq) (resp *types.Bind token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, model.UserTypeNormal) if err != nil { + causeErr := errors.Cause(err) + if _, ok := causeErr.(*xerr.CodeError); ok { + return nil, err + } return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "绑定手机号, 生成token失败: %+v", err) } now := time.Now().Unix() diff --git a/app/main/api/internal/logic/user/gettokenlogic.go b/app/main/api/internal/logic/user/gettokenlogic.go index d24b083..c133c98 100644 --- a/app/main/api/internal/logic/user/gettokenlogic.go +++ b/app/main/api/internal/logic/user/gettokenlogic.go @@ -2,9 +2,9 @@ package user import ( "context" + "time" "tydata-server/common/ctxdata" "tydata-server/common/xerr" - "time" "github.com/pkg/errors" @@ -35,6 +35,10 @@ func (l *GetTokenLogic) GetToken() (resp *types.MobileCodeLoginResp, err error) } token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, claims.UserId, claims.UserType) if err != nil { + causeErr := errors.Cause(err) + if _, ok := causeErr.(*xerr.CodeError); ok { + return nil, err + } return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "用户信息, %v", err) } // 获取当前时间戳 diff --git a/app/main/api/internal/logic/user/mobilecodeloginlogic.go b/app/main/api/internal/logic/user/mobilecodeloginlogic.go index 2775349..7c85572 100644 --- a/app/main/api/internal/logic/user/mobilecodeloginlogic.go +++ b/app/main/api/internal/logic/user/mobilecodeloginlogic.go @@ -68,7 +68,12 @@ func (l *MobileCodeLoginLogic) MobileCodeLogin(req *types.MobileCodeLoginReq) (r } token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, model.UserTypeNormal) if err != nil { - return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d", userID) + // 封禁等业务错误原样返回,便于前端展示「用户已被封禁」 + causeErr := errors.Cause(err) + if _, ok := causeErr.(*xerr.CodeError); ok { + return nil, err + } + return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "手机登录, 生成token失败 : %d, %v", userID, err) } // 获取当前时间戳 diff --git a/app/main/api/internal/logic/user/wxh5authlogic.go b/app/main/api/internal/logic/user/wxh5authlogic.go index 9f26bd4..8daee5e 100644 --- a/app/main/api/internal/logic/user/wxh5authlogic.go +++ b/app/main/api/internal/logic/user/wxh5authlogic.go @@ -2,13 +2,13 @@ package user import ( "context" - "tydata-server/app/main/model" - "tydata-server/common/xerr" "encoding/json" "fmt" "io" "net/http" "time" + "tydata-server/app/main/model" + "tydata-server/common/xerr" "github.com/pkg/errors" @@ -82,6 +82,10 @@ func (l *WxH5AuthLogic) WxH5Auth(req *types.WXH5AuthReq) (resp *types.WXH5AuthRe // Step 4: 生成JWT Token token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType) if err != nil { + causeErr := errors.Cause(err) + if _, ok := causeErr.(*xerr.CodeError); ok { + return nil, err + } return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成JWT token失败: %v", err) } diff --git a/app/main/api/internal/logic/user/wxminiauthlogic.go b/app/main/api/internal/logic/user/wxminiauthlogic.go index 415ab81..355d003 100644 --- a/app/main/api/internal/logic/user/wxminiauthlogic.go +++ b/app/main/api/internal/logic/user/wxminiauthlogic.go @@ -81,6 +81,10 @@ func (l *WxMiniAuthLogic) WxMiniAuth(req *types.WXMiniAuthReq) (resp *types.WXMi // 4. 生成JWT Token token, err := l.svcCtx.UserService.GeneralUserToken(l.ctx, userID, userType) if err != nil { + causeErr := errors.Cause(err) + if _, ok := causeErr.(*xerr.CodeError); ok { + return nil, err + } return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成JWT Token失败: %v", err) } diff --git a/app/main/api/internal/middleware/userauthinterceptormiddleware.go b/app/main/api/internal/middleware/userauthinterceptormiddleware.go index 7cab9a7..439fc8e 100644 --- a/app/main/api/internal/middleware/userauthinterceptormiddleware.go +++ b/app/main/api/internal/middleware/userauthinterceptormiddleware.go @@ -1,10 +1,10 @@ package middleware import ( + "net/http" "tydata-server/app/main/model" "tydata-server/common/ctxdata" "tydata-server/common/xerr" - "net/http" "github.com/pkg/errors" "github.com/zeromicro/go-zero/rest/httpx" diff --git a/app/main/api/internal/middleware/userdisablecheckmiddleware.go b/app/main/api/internal/middleware/userdisablecheckmiddleware.go new file mode 100644 index 0000000..5cea56b --- /dev/null +++ b/app/main/api/internal/middleware/userdisablecheckmiddleware.go @@ -0,0 +1,69 @@ +package middleware + +import ( + "net/http" + "strings" + + "tydata-server/app/main/api/internal/config" + "tydata-server/app/main/model" + jwtx "tydata-server/common/jwt" + "tydata-server/common/result" + "tydata-server/common/xerr" + + "github.com/zeromicro/go-zero/rest/httpx" +) + +// UserDisableCheckMiddleware 全局中间件:若当前用户已被封禁(disable=1),则拒绝请求并返回封禁原因 +type UserDisableCheckMiddleware struct { + Config config.Config + UserModel model.UserModel +} + +// NewUserDisableCheckMiddleware 创建封禁检查中间件 +func NewUserDisableCheckMiddleware(c config.Config, userModel model.UserModel) *UserDisableCheckMiddleware { + return &UserDisableCheckMiddleware{ + Config: c, + UserModel: userModel, + } +} + +// Handle 仅对携带用户 JWT 的请求做封禁校验;无 token 或 admin token 直接放行 +func (m *UserDisableCheckMiddleware) Handle(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + authHeader := r.Header.Get("Authorization") + if authHeader == "" { + next(w, r) + return + } + + token := strings.TrimPrefix(authHeader, "Bearer ") + if token == authHeader { + next(w, r) + return + } + + // 仅使用用户端 JWT 密钥解析;管理员 token 会解析失败,直接放行由后续中间件处理 + claims, err := jwtx.ParseJwtToken(token, m.Config.JwtAuth.AccessSecret) + if err != nil { + next(w, r) + return + } + + // 查封禁状态不走缓存,封禁后立即生效,已登录用户下次请求即被拒绝 + disable, err := m.UserModel.FindDisableByUserId(r.Context(), claims.UserId) + if err != nil { + next(w, r) + return + } + + // disable: 0 可用,1 封禁 + if disable == 1 { + errcode := xerr.USER_DISABLED + errmsg := xerr.MapErrMsg(errcode) + httpx.WriteJson(w, http.StatusForbidden, result.Error(errcode, errmsg)) + return + } + + next(w, r) + } +} diff --git a/app/main/api/internal/service/userService.go b/app/main/api/internal/service/userService.go index 493847d..94a8e06 100644 --- a/app/main/api/internal/service/userService.go +++ b/app/main/api/internal/service/userService.go @@ -2,12 +2,12 @@ package service import ( "context" + "database/sql" "tydata-server/app/main/api/internal/config" "tydata-server/app/main/model" "tydata-server/common/ctxdata" jwtx "tydata-server/common/jwt" "tydata-server/common/xerr" - "database/sql" "github.com/google/uuid" "github.com/pkg/errors" @@ -78,6 +78,17 @@ func (s *UserService) RegisterUUIDUser(ctx context.Context) (int64, error) { // generalUserToken 生成用户token func (s *UserService) GeneralUserToken(ctx context.Context, userID int64, userType int64) (string, error) { + // 正式用户签发 token 前校验是否被封禁,封禁用户不允许登录 + if userType == model.UserTypeNormal { + user, err := s.userModel.FindOne(ctx, userID) + if err != nil { + return "", err + } + if user != nil && user.Disable == 1 { + return "", errors.Wrapf(xerr.NewErrCode(xerr.USER_DISABLED), "用户已被封禁") + } + } + platform, err := ctxdata.GetPlatformFromCtx(ctx) if err != nil { return "", err diff --git a/app/main/api/internal/types/types.go b/app/main/api/internal/types/types.go index d7db89b..dc7aa6c 100644 --- a/app/main/api/internal/types/types.go +++ b/app/main/api/internal/types/types.go @@ -568,6 +568,7 @@ type AdminGetPlatformUserDetailResp struct { Nickname string `json:"nickname"` // 昵称 Info string `json:"info"` // 备注信息 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 + Disable int64 `json:"disable"` // 是否封禁 0-可用 1-封禁 CreateTime string `json:"create_time"` // 创建时间 UpdateTime string `json:"update_time"` // 更新时间 } @@ -949,6 +950,7 @@ type AdminUpdatePlatformUserReq struct { Nickname *string `json:"nickname,optional"` // 昵称 Info *string `json:"info,optional"` // 备注信息 Inside *int64 `json:"inside,optional"` // 是否内部用户 1-是 0-否 + Disable *int64 `json:"disable,optional"` // 是否封禁 0-可用 1-封禁 } type AdminUpdatePlatformUserResp struct { @@ -1854,6 +1856,7 @@ type PlatformUserListItem struct { Nickname string `json:"nickname"` // 昵称 Info string `json:"info"` // 备注信息 Inside int64 `json:"inside"` // 是否内部用户 1-是 0-否 + Disable int64 `json:"disable"` // 是否封禁 0-可用 1-封禁 CreateTime string `json:"create_time"` // 创建时间 UpdateTime string `json:"update_time"` // 更新时间 } diff --git a/app/main/api/main.go b/app/main/api/main.go index 08229fd..92a6385 100644 --- a/app/main/api/main.go +++ b/app/main/api/main.go @@ -68,6 +68,7 @@ func main() { server := rest.MustNewServer(c.RestConf) server.Use(middleware.GlobalSourceInterceptor) + server.Use(middleware.NewUserDisableCheckMiddleware(svcContext.Config, svcContext.UserModel).Handle) defer server.Stop() handler.RegisterHandlers(server, svcContext) diff --git a/app/main/model/userModel.go b/app/main/model/userModel.go index 8123712..fa7f612 100644 --- a/app/main/model/userModel.go +++ b/app/main/model/userModel.go @@ -1,8 +1,14 @@ package model import ( + "context" + "fmt" + "github.com/zeromicro/go-zero/core/stores/cache" + "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" + + "tydata-server/common/globalkey" ) var _ UserModel = (*customUserModel)(nil) @@ -12,6 +18,8 @@ type ( // and implement the added methods in customUserModel. UserModel interface { userModel + // FindDisableByUserId 查用户封禁状态(不走缓存,封禁中间件用) + FindDisableByUserId(ctx context.Context, id int64) (int64, error) } customUserModel struct { @@ -19,6 +27,20 @@ type ( } ) +// FindDisableByUserId 查用户 disable 字段,不走缓存,确保封禁后立即生效 +func (m *customUserModel) FindDisableByUserId(ctx context.Context, id int64) (int64, error) { + var disable int64 + query := fmt.Sprintf("SELECT `disable` FROM %s WHERE `id` = ? and del_state = ? limit 1", m.table) + err := m.QueryRowNoCacheCtx(ctx, &disable, query, id, globalkey.DelStateNo) + if err != nil { + if err == sqlc.ErrNotFound { + return 0, ErrNotFound + } + return 0, err + } + return disable, nil +} + // NewUserModel returns a model for the database table. func NewUserModel(conn sqlx.SqlConn, c cache.CacheConf) UserModel { return &customUserModel{ diff --git a/app/main/model/userModel_gen.go b/app/main/model/userModel_gen.go index 0590bb7..ed93f03 100644 --- a/app/main/model/userModel_gen.go +++ b/app/main/model/userModel_gen.go @@ -10,8 +10,6 @@ import ( "time" - "tydata-server/common/globalkey" - "github.com/Masterminds/squirrel" "github.com/pkg/errors" "github.com/zeromicro/go-zero/core/stores/builder" @@ -19,6 +17,7 @@ import ( "github.com/zeromicro/go-zero/core/stores/sqlc" "github.com/zeromicro/go-zero/core/stores/sqlx" "github.com/zeromicro/go-zero/core/stringx" + "tydata-server/common/globalkey" ) var ( @@ -27,8 +26,8 @@ var ( userRowsExpectAutoSet = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), ",") userRowsWithPlaceHolder = strings.Join(stringx.Remove(userFieldNames, "`id`", "`create_time`", "`update_time`"), "=?,") + "=?" - cacheHmUserIdPrefix = "cache:tydata:user:id:" - cacheHmUserMobilePrefix = "cache:tydata:user:mobile:" + cacheZncUserIdPrefix = "cache:znc:user:id:" + cacheZncUserMobilePrefix = "cache:znc:user:mobile:" ) type ( @@ -68,6 +67,7 @@ type ( Nickname sql.NullString `db:"nickname"` Info string `db:"info"` Inside int64 `db:"inside"` + Disable int64 `db:"disable"` // 0可用 1禁用 } ) @@ -80,21 +80,21 @@ func newUserModel(conn sqlx.SqlConn, c cache.CacheConf) *defaultUserModel { func (m *defaultUserModel) Insert(ctx context.Context, session sqlx.Session, data *User) (sql.Result, error) { data.DelState = globalkey.DelStateNo - hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id) - hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile) + zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, data.Id) + zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile) 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 { - 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) - }, hmUserIdKey, hmUserMobileKey) + return conn.ExecCtx(ctx, query, data.DeleteTime, data.DelState, data.Version, data.Mobile, data.Password, data.Nickname, data.Info, data.Inside, data.Disable) + }, zncUserIdKey, zncUserMobileKey) } func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) { - hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, id) + zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, id) var resp User - err := m.QueryRowCtx(ctx, &resp, hmUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { + err := m.QueryRowCtx(ctx, &resp, zncUserIdKey, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) error { query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, m.table) return conn.QueryRowCtx(ctx, v, query, id, globalkey.DelStateNo) }) @@ -109,9 +109,9 @@ func (m *defaultUserModel) FindOne(ctx context.Context, id int64) (*User, error) } func (m *defaultUserModel) FindOneByMobile(ctx context.Context, mobile sql.NullString) (*User, error) { - hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, mobile) + zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, mobile) var resp User - err := m.QueryRowIndexCtx(ctx, &resp, hmUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { + err := m.QueryRowIndexCtx(ctx, &resp, zncUserMobileKey, m.formatPrimary, func(ctx context.Context, conn sqlx.SqlConn, v interface{}) (i interface{}, e error) { query := fmt.Sprintf("select %s from %s where `mobile` = ? and del_state = ? limit 1", userRows, m.table) if err := conn.QueryRowCtx(ctx, &resp, query, mobile, globalkey.DelStateNo); err != nil { return nil, err @@ -133,15 +133,15 @@ func (m *defaultUserModel) Update(ctx context.Context, session sqlx.Session, new if err != nil { return nil, err } - hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id) - hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile) + zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, data.Id) + zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile) 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) 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) - }, hmUserIdKey, hmUserMobileKey) + 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) + }, zncUserIdKey, zncUserMobileKey) } func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.Session, newData *User) error { @@ -156,15 +156,15 @@ func (m *defaultUserModel) UpdateWithVersion(ctx context.Context, session sqlx.S if err != nil { return err } - hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, data.Id) - hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile) + zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, data.Id) + zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile) 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) 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) - }, hmUserIdKey, hmUserMobileKey) + 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) + }, zncUserIdKey, zncUserMobileKey) if err != nil { return err } @@ -387,19 +387,19 @@ func (m *defaultUserModel) Delete(ctx context.Context, session sqlx.Session, id return err } - hmUserIdKey := fmt.Sprintf("%s%v", cacheHmUserIdPrefix, id) - hmUserMobileKey := fmt.Sprintf("%s%v", cacheHmUserMobilePrefix, data.Mobile) + zncUserIdKey := fmt.Sprintf("%s%v", cacheZncUserIdPrefix, id) + zncUserMobileKey := fmt.Sprintf("%s%v", cacheZncUserMobilePrefix, data.Mobile) _, err = m.ExecCtx(ctx, func(ctx context.Context, conn sqlx.SqlConn) (result sql.Result, err error) { query := fmt.Sprintf("delete from %s where `id` = ?", m.table) if session != nil { return session.ExecCtx(ctx, query, id) } return conn.ExecCtx(ctx, query, id) - }, hmUserIdKey, hmUserMobileKey) + }, zncUserIdKey, zncUserMobileKey) return err } func (m *defaultUserModel) formatPrimary(primary interface{}) string { - return fmt.Sprintf("%s%v", cacheHmUserIdPrefix, primary) + return fmt.Sprintf("%s%v", cacheZncUserIdPrefix, primary) } func (m *defaultUserModel) queryPrimary(ctx context.Context, conn sqlx.SqlConn, v, primary interface{}) error { query := fmt.Sprintf("select %s from %s where `id` = ? and del_state = ? limit 1", userRows, m.table) diff --git a/common/xerr/errCode.go b/common/xerr/errCode.go index a461ca9..035cd1d 100644 --- a/common/xerr/errCode.go +++ b/common/xerr/errCode.go @@ -16,6 +16,7 @@ const PARAM_VERIFICATION_ERROR uint32 = 100007 const CUSTOM_ERROR uint32 = 100008 const USER_NOT_FOUND uint32 = 100009 const USER_NEED_BIND_MOBILE uint32 = 100010 +const USER_DISABLED uint32 = 100011 // 用户已被封禁 const LOGIN_FAILED uint32 = 200001 const LOGIC_QUERY_WAIT uint32 = 200002 diff --git a/common/xerr/errMsg.go b/common/xerr/errMsg.go index dc911f1..400722f 100644 --- a/common/xerr/errMsg.go +++ b/common/xerr/errMsg.go @@ -11,6 +11,7 @@ func init() { message[TOKEN_GENERATE_ERROR] = "生成token失败" message[DB_ERROR] = "系统维护升级中,请稍后再试" message[DB_UPDATE_AFFECTED_ZERO_ERROR] = "更新数据影响行数为0" + message[USER_DISABLED] = "用户已被封禁" } func MapErrMsg(errcode uint32) string { diff --git a/deploy/script/gen_models.ps1 b/deploy/script/gen_models.ps1 index 54d1e1a..7369bc2 100644 --- a/deploy/script/gen_models.ps1 +++ b/deploy/script/gen_models.ps1 @@ -1,7 +1,7 @@ # 设置输出编码为UTF-8 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 # 数据库连接信息 - 修改了URL格式 -$DB_URL = "tydata:5vg67b3UNHu8@(127.0.0.1:21001)/tydata" +$DB_URL = "znc:5vg67b3UNHu823@tcp(localhost:21101)/znc" $OUTPUT_DIR = "./model" $TEMPLATE_DIR = "../template" @@ -12,7 +12,7 @@ $tables = @( # "agent_audit", # "agent_closure", # "agent_commission" - "agent_wallet_transaction" + # "agent_wallet_transaction" # "agent_commission_deduction" # "agent_link", # "agent_membership_config", @@ -37,7 +37,7 @@ $tables = @( # "query_cleanup_log" # "query_cleanup_detail" # "query_cleanup_config" - # "user" + "user" # "user_auth" # "user_temp" # "example" @@ -60,5 +60,5 @@ $tables = @( # 为每个表生成模型 foreach ($table in $tables) { - goctl model mysql datasource -url="tydata:5vg67b3UNHu8@tcp(127.0.0.1:21001)/tydata" -table="$table" -dir="./model" --home="../template" -cache=true --style=goZero + goctl model mysql datasource -url="znc:5vg67b3UNHu823@tcp(localhost:21101)/znc" -table="$table" -dir="./model" --home="../template" -cache=true --style=goZero } diff --git a/deploy/sql/user_add_disable.sql b/deploy/sql/user_add_disable.sql new file mode 100644 index 0000000..97965a5 --- /dev/null +++ b/deploy/sql/user_add_disable.sql @@ -0,0 +1,2 @@ +-- 为用户表添加 disable 字段:0 可用,1 禁用,默认 0 +ALTER TABLE `user` ADD COLUMN `disable` tinyint NOT NULL DEFAULT 0 COMMENT '0可用 1禁用' AFTER `inside`; diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml index d7e6368..b9abf64 100644 --- a/docker-compose.dev.yml +++ b/docker-compose.dev.yml @@ -42,7 +42,7 @@ services: volumes: # 数据文件 - data files - ./data/znc/redis/data:/data:rw - command: "redis-server --requirepass 3m3WsgyCKWqz --appendonly yes" + command: "redis-server --requirepass 3m3WsgyCKWqz --appendonly yes" privileged: true restart: always networks: @@ -80,27 +80,6 @@ services: - mysql networks: - znc_net - - main: - container_name: znc_main - build: - context: . - dockerfile: app/main/api/Dockerfile - ports: - - "21105:8888" - volumes: - - ./data/authorization_docs:/app/data/authorization_docs:rw - environment: - - TZ=Asia/Shanghai - - ENV=development - command: sh -c "echo '等待 Redis 启动...' && until nc -z znc_redis 6379; do echo '等待 Redis 端口开放...'; sleep 1; done && echo 'Redis 端口已开放,等待服务完全就绪...' && sleep 10 && echo '启动应用' && ./main" - depends_on: - - mysql - - redis - networks: - - znc_net - restart: always - networks: znc_net: driver: bridge