实现个人客户微信认证和短信验证功能

- 添加个人客户微信登录和手机验证码登录接口
- 实现个人客户设备、ICCID、手机号关联管理
- 添加短信发送服务(HTTP 客户端)
- 添加微信认证服务(含 mock 实现)
- 添加 JWT Token 生成和验证工具
- 创建数据库迁移脚本(personal_customer 关联表)
- 修复测试文件中的路由注册参数错误
- 重构 scripts 目录结构(分离独立脚本到子目录)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-10 11:42:38 +08:00
parent 1b9080e3ab
commit 9c6d4a3bd4
53 changed files with 4258 additions and 97 deletions

View File

@@ -4,22 +4,29 @@ import (
pkgGorm "github.com/break/junhong_cmp_fiber/pkg/gorm"
)
// Bootstrap 初始化所有业务组件并返回 Handlers
// BootstrapResult Bootstrap 初始化结果
type BootstrapResult struct {
Handlers *Handlers
Middlewares *Middlewares
}
// Bootstrap 初始化所有业务组件并返回 Handlers 和 Middlewares
// 这是应用启动时的主入口,负责编排所有组件的初始化流程
//
// 初始化顺序:
// 1. 初始化 Store 层(数据访问)
// 2. 注册 GORM Callbacks数据权限过滤等- 需要 AccountStore
// 3. 初始化 Service 层(业务逻辑)
// 4. 初始化 Handler 层HTTP 处理
// 4. 初始化 Middleware 层(中间件
// 5. 初始化 Handler 层HTTP 处理)
//
// 参数:
// - deps: 基础依赖DB, Redis, Logger
//
// 返回:
// - *Handlers: 所有 HTTP 处理器
// - *BootstrapResult: 包含 Handlers 和 Middlewares
// - error: 初始化错误
func Bootstrap(deps *Dependencies) (*Handlers, error) {
func Bootstrap(deps *Dependencies) (*BootstrapResult, error) {
// 1. 初始化 Store 层
stores := initStores(deps)
@@ -29,12 +36,18 @@ func Bootstrap(deps *Dependencies) (*Handlers, error) {
}
// 3. 初始化 Service 层
services := initServices(stores)
services := initServices(stores, deps)
// 4. 初始化 Handler
handlers := initHandlers(services)
// 4. 初始化 Middleware
middlewares := initMiddlewares(deps)
return handlers, nil
// 5. 初始化 Handler 层
handlers := initHandlers(services, deps)
return &BootstrapResult{
Handlers: handlers,
Middlewares: middlewares,
}, nil
}
// registerGORMCallbacks 注册 GORM Callbacks

View File

@@ -1,6 +1,8 @@
package bootstrap
import (
"github.com/break/junhong_cmp_fiber/internal/service/verification"
"github.com/break/junhong_cmp_fiber/pkg/auth"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
"gorm.io/gorm"
@@ -9,7 +11,9 @@ import (
// Dependencies 封装所有基础依赖
// 这些是应用启动时初始化的核心组件
type Dependencies struct {
DB *gorm.DB // PostgreSQL 数据库连接
Redis *redis.Client // Redis 客户端
Logger *zap.Logger // 应用日志器
DB *gorm.DB // PostgreSQL 数据库连接
Redis *redis.Client // Redis 客户端
Logger *zap.Logger // 应用日志器
JWTManager *auth.JWTManager // JWT 管理器
VerificationService *verification.Service // 验证码服务
}

View File

@@ -2,14 +2,16 @@ package bootstrap
import (
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
"github.com/break/junhong_cmp_fiber/internal/handler/app"
)
// initHandlers 初始化所有 Handler 实例
func initHandlers(svc *services) *Handlers {
func initHandlers(svc *services, deps *Dependencies) *Handlers {
return &Handlers{
Account: admin.NewAccountHandler(svc.Account),
Role: admin.NewRoleHandler(svc.Role),
Permission: admin.NewPermissionHandler(svc.Permission),
Account: admin.NewAccountHandler(svc.Account),
Role: admin.NewRoleHandler(svc.Role),
Permission: admin.NewPermissionHandler(svc.Permission),
PersonalCustomer: app.NewPersonalCustomerHandler(svc.PersonalCustomer, deps.Logger),
// TODO: 新增 Handler 在此初始化
}
}

View File

@@ -0,0 +1,23 @@
package bootstrap
import (
"github.com/break/junhong_cmp_fiber/internal/middleware"
"github.com/break/junhong_cmp_fiber/pkg/auth"
"github.com/break/junhong_cmp_fiber/pkg/config"
)
// initMiddlewares 初始化所有中间件
func initMiddlewares(deps *Dependencies) *Middlewares {
// 获取全局配置
cfg := config.Get()
// 创建 JWT Manager
jwtManager := auth.NewJWTManager(cfg.JWT.SecretKey, cfg.JWT.TokenDuration)
// 创建个人客户认证中间件
personalAuthMiddleware := middleware.NewPersonalAuthMiddleware(jwtManager, deps.Logger)
return &Middlewares{
PersonalAuth: personalAuthMiddleware,
}
}

View File

@@ -3,24 +3,27 @@ package bootstrap
import (
accountSvc "github.com/break/junhong_cmp_fiber/internal/service/account"
permissionSvc "github.com/break/junhong_cmp_fiber/internal/service/permission"
personalCustomerSvc "github.com/break/junhong_cmp_fiber/internal/service/personal_customer"
roleSvc "github.com/break/junhong_cmp_fiber/internal/service/role"
)
// services 封装所有 Service 实例
// 注意:此结构体不导出,仅在 bootstrap 包内部使用
type services struct {
Account *accountSvc.Service
Role *roleSvc.Service
Permission *permissionSvc.Service
Account *accountSvc.Service
Role *roleSvc.Service
Permission *permissionSvc.Service
PersonalCustomer *personalCustomerSvc.Service
// TODO: 新增 Service 在此添加字段
}
// initServices 初始化所有 Service 实例
func initServices(s *stores) *services {
func initServices(s *stores, deps *Dependencies) *services {
return &services{
Account: accountSvc.New(s.Account, s.Role, s.AccountRole),
Role: roleSvc.New(s.Role, s.Permission, s.RolePermission),
Permission: permissionSvc.New(s.Permission),
Account: accountSvc.New(s.Account, s.Role, s.AccountRole),
Role: roleSvc.New(s.Role, s.Permission, s.RolePermission),
Permission: permissionSvc.New(s.Permission),
PersonalCustomer: personalCustomerSvc.NewService(s.PersonalCustomer, s.PersonalCustomerPhone, deps.VerificationService, deps.JWTManager, deps.Logger),
// TODO: 新增 Service 在此初始化
}
}

View File

@@ -7,22 +7,26 @@ import (
// stores 封装所有 Store 实例
// 注意:此结构体不导出,仅在 bootstrap 包内部使用
type stores struct {
Account *postgres.AccountStore
Role *postgres.RoleStore
Permission *postgres.PermissionStore
AccountRole *postgres.AccountRoleStore
RolePermission *postgres.RolePermissionStore
Account *postgres.AccountStore
Role *postgres.RoleStore
Permission *postgres.PermissionStore
AccountRole *postgres.AccountRoleStore
RolePermission *postgres.RolePermissionStore
PersonalCustomer *postgres.PersonalCustomerStore
PersonalCustomerPhone *postgres.PersonalCustomerPhoneStore
// TODO: 新增 Store 在此添加字段
}
// initStores 初始化所有 Store 实例
func initStores(deps *Dependencies) *stores {
return &stores{
Account: postgres.NewAccountStore(deps.DB, deps.Redis),
Role: postgres.NewRoleStore(deps.DB),
Permission: postgres.NewPermissionStore(deps.DB),
AccountRole: postgres.NewAccountRoleStore(deps.DB),
RolePermission: postgres.NewRolePermissionStore(deps.DB),
Account: postgres.NewAccountStore(deps.DB, deps.Redis),
Role: postgres.NewRoleStore(deps.DB),
Permission: postgres.NewPermissionStore(deps.DB),
AccountRole: postgres.NewAccountRoleStore(deps.DB),
RolePermission: postgres.NewRolePermissionStore(deps.DB),
PersonalCustomer: postgres.NewPersonalCustomerStore(deps.DB, deps.Redis),
PersonalCustomerPhone: postgres.NewPersonalCustomerPhoneStore(deps.DB),
// TODO: 新增 Store 在此初始化
}
}

View File

@@ -2,13 +2,23 @@ package bootstrap
import (
"github.com/break/junhong_cmp_fiber/internal/handler/admin"
"github.com/break/junhong_cmp_fiber/internal/handler/app"
"github.com/break/junhong_cmp_fiber/internal/middleware"
)
// Handlers 封装所有 HTTP 处理器
// 用于路由注册
type Handlers struct {
Account *admin.AccountHandler
Role *admin.RoleHandler
Permission *admin.PermissionHandler
Account *admin.AccountHandler
Role *admin.RoleHandler
Permission *admin.PermissionHandler
PersonalCustomer *app.PersonalCustomerHandler
// TODO: 新增 Handler 在此添加字段
}
// Middlewares 封装所有中间件
// 用于路由注册
type Middlewares struct {
PersonalAuth *middleware.PersonalAuthMiddleware
// TODO: 新增 Middleware 在此添加字段
}