init: taotie-api 项目初始化

This commit is contained in:
2026-05-16 00:14:19 +08:00
commit eb15ef4b87
33 changed files with 1746 additions and 0 deletions

11
service/service.go Normal file
View File

@@ -0,0 +1,11 @@
package service
import (
"github.com/google/wire"
)
// 依赖注入节点
var ServiceProd = wire.NewSet(
NewUserService,
NewTenantService,
)

45
service/tenantservice.go Normal file
View File

@@ -0,0 +1,45 @@
package service
import (
"context"
"taotie-api/common"
"taotie-api/core"
"taotie-api/model/do/tenantdo"
"taotie-api/model/po"
"taotie-api/repo"
"github.com/gookit/goutil/errorx"
)
type TenantService struct {
cfg *core.Configuration
tenantRepo *repo.TenantRepo[*po.TTenant]
}
func NewTenantService(cfg *core.Configuration, tenantRepo *repo.TenantRepo[*po.TTenant]) *TenantService {
return &TenantService{cfg, tenantRepo}
}
func (rp *TenantService) Create(ctx context.Context, in *tenantdo.CreateIn) (*tenantdo.CreateOut, error) {
// 查重
tenant, err := rp.tenantRepo.FindOneByTenantName(ctx, in.TenantName)
if err != nil {
return nil, err
}
if tenant != nil {
return nil, errorx.With(common.ErrSysDuplicate, "租户名称重复")
}
// 创建租户
tenant = &po.TTenant{
TenantName: in.TenantName,
}
tenantId, err := rp.tenantRepo.Create(ctx, tenant)
if err != nil {
return nil, err
}
return &tenantdo.CreateOut{
Id: tenantId,
}, nil
}

125
service/userservice.go Normal file
View File

@@ -0,0 +1,125 @@
package service
import (
"context"
"fmt"
"taotie-api/common"
"taotie-api/core"
"taotie-api/model/do/userdo"
"taotie-api/model/po"
"taotie-api/repo"
"taotie-api/utils/sctx"
"taotie-api/utils/sjwt"
"time"
"github.com/gookit/goutil/errorx"
)
type UserService struct {
cfg *core.Configuration
userRepo *repo.UserRepo[*po.TUser]
tenantRepo *repo.TenantRepo[*po.TTenant]
}
func NewUserService(mdb *repo.MongoDb, cfg *core.Configuration, userRepo *repo.UserRepo[*po.TUser], tenantRepo *repo.TenantRepo[*po.TTenant]) *UserService {
return &UserService{
cfg,
userRepo,
tenantRepo,
}
}
// Login 登录
func (s *UserService) Login(ctx context.Context, in *userdo.LoginIn) (*userdo.LoginOut, error) {
// 验证租户
ttenant, err := s.tenantRepo.FindOneByTenantNo(ctx, in.TenantNo)
if err != nil {
return nil, err
}
if ttenant == nil {
return nil, common.ErrUserTenantNotFound
}
// 验证用户
tuser, err := s.userRepo.FindOneByTenantIdAndUserName(ctx, ttenant.Id(), in.UserName)
if err != nil {
return nil, err
}
if tuser == nil {
return nil, common.ErrUserNotFound
}
if tuser.Password != in.Password {
return nil, common.ErrUserPasswordError
}
return &userdo.LoginOut{
TenantId: ttenant.Id(),
UserId: tuser.Id(),
UserName: tuser.UserName,
}, nil
}
// GenToken 生成token
func (s *UserService) GenToken(ctx context.Context, in *userdo.GenTokenIn) (*userdo.GenTokenOut, error) {
token, err := sjwt.GenerateToken(in.TenantNo, in.UserId, in.UserName, s.cfg.JWT.TimeOut, s.cfg.JWT.SignString)
if err != nil {
return nil, err
}
return &userdo.GenTokenOut{
Token: token,
}, nil
}
func (s *UserService) Create(ctx context.Context, in *userdo.CreateIn) (*userdo.CreateOut, error) {
// 查重:租户名称
ttenant, err := s.tenantRepo.FindOneByTenantName(ctx, in.TenantName)
if err != nil {
return nil, err
}
if ttenant != nil {
return nil, errorx.With(common.ErrSysDuplicate, "租户名称重复")
}
// 先设置临时用户到 context避免 BaseRepo.Create 中 cuser 为 nil 导致 panic
sctx.SetCurrentUser(ctx, &sctx.CurrentUser{
UserId: "",
UserName: in.UserName,
TenantId: "",
})
// 生成 tenantNo基于时间戳
tenantNo := fmt.Sprintf("T%d", time.Now().UnixMilli())
// 创建租户
ttenant = &po.TTenant{
TenantNo: tenantNo,
TenantName: in.TenantName,
}
tenantId, err := s.tenantRepo.Create(ctx, ttenant)
if err != nil {
return nil, err
}
// 更新 context 中的 TenantId
sctx.SetCurrentUser(ctx, &sctx.CurrentUser{
UserId: "",
UserName: in.UserName,
TenantId: tenantId,
})
// 创建用户
tuser := &po.TUser{
UserName: in.UserName,
Password: in.Password,
}
userId, err := s.userRepo.Create(ctx, tuser)
if err != nil {
return nil, err
}
return &userdo.CreateOut{
TenantId: tenantId,
UserId: userId,
UserName: in.UserName,
}, nil
}