qnc-server-tob/app/user/cmd/api/internal/logic/agent/applyforagentlogic.go

185 lines
7.7 KiB
Go
Raw Normal View History

2025-03-07 03:48:59 +08:00
package agent
import (
"context"
2025-04-26 15:10:01 +08:00
"database/sql"
2025-03-07 03:48:59 +08:00
"fmt"
2025-04-11 13:10:17 +08:00
"qnc-server/app/user/model"
jwtx "qnc-server/common/jwt"
"qnc-server/common/xerr"
"qnc-server/pkg/lzkit/crypto"
2025-03-07 03:48:59 +08:00
"time"
2025-03-15 01:33:31 +08:00
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/stores/redis"
"github.com/zeromicro/go-zero/core/stores/sqlx"
2025-04-11 13:10:17 +08:00
"qnc-server/app/user/cmd/api/internal/svc"
"qnc-server/app/user/cmd/api/internal/types"
2025-03-07 03:48:59 +08:00
"github.com/zeromicro/go-zero/core/logx"
)
type ApplyForAgentLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewApplyForAgentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ApplyForAgentLogic {
return &ApplyForAgentLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *ApplyForAgentLogic) ApplyForAgent(req *types.AgentApplyReq) (resp *types.AgentApplyResp, err error) {
2025-04-08 12:49:19 +08:00
secretKey := l.svcCtx.Config.Encrypt.SecretKey
encryptedMobile, err := crypto.EncryptMobile(req.Mobile, secretKey)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败: %v", err)
}
2025-03-07 03:48:59 +08:00
// 校验验证码
2025-04-08 12:49:19 +08:00
redisKey := fmt.Sprintf("%s:%s", "agentApply", encryptedMobile)
2025-03-07 03:48:59 +08:00
cacheCode, err := l.svcCtx.Redis.Get(redisKey)
if err != nil {
if errors.Is(err, redis.Nil) {
2025-04-08 12:49:19 +08:00
return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "代理申请, 验证码过期: %s", encryptedMobile)
2025-03-07 03:48:59 +08:00
}
2025-04-08 12:49:19 +08:00
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 读取验证码redis缓存失败, mobile: %s, err: %+v", encryptedMobile, err)
2025-03-07 03:48:59 +08:00
}
if cacheCode != req.Code {
2025-04-08 12:49:19 +08:00
return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "代理申请, 验证码不正确: %s", encryptedMobile)
2025-03-07 03:48:59 +08:00
}
if req.Ancestor == req.Mobile {
return nil, errors.Wrapf(xerr.NewErrMsg("不能成为自己的代理"), "")
}
var userID int64
transErr := l.svcCtx.AgentAuditModel.Trans(l.ctx, func(transCtx context.Context, session sqlx.Session) error {
// 两种情况1. 已注册账号然后申请代理 2. 未注册账号申请代理
2025-04-26 15:10:01 +08:00
user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, sql.NullString{String: encryptedMobile, Valid: true})
2025-03-07 03:48:59 +08:00
if findUserErr != nil && !errors.Is(findUserErr, model.ErrNotFound) {
2025-04-08 12:49:19 +08:00
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 读取数据库获取用户失败, mobile: %s, err: %+v", encryptedMobile, err)
2025-03-07 03:48:59 +08:00
}
if user == nil {
2025-04-26 15:10:01 +08:00
user = &model.User{Mobile: sql.NullString{String: encryptedMobile, Valid: true}}
2025-04-11 13:10:17 +08:00
// if len(user.Nickname) == 0 {
// user.Nickname = encryptedMobile
// }
2025-03-07 03:48:59 +08:00
insertResult, userInsertErr := l.svcCtx.UserModel.Insert(transCtx, session, user)
if userInsertErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 数据库插入新用户失败, mobile%s, err: %+v", encryptedMobile, userInsertErr)
2025-03-07 03:48:59 +08:00
}
lastId, lastInsertIdErr := insertResult.LastInsertId()
if lastInsertIdErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 获取新用户ID失败, err:%+v, user:%+v", lastInsertIdErr, user)
}
user.Id = lastId
userID = lastId
userAuth := new(model.UserAuth)
userAuth.UserId = lastId
2025-04-08 12:49:19 +08:00
userAuth.AuthKey = encryptedMobile
2025-03-07 03:48:59 +08:00
userAuth.AuthType = model.UserAuthTypeAgentDirect
if _, userAuthInsertErr := l.svcCtx.UserAuthModel.Insert(transCtx, session, userAuth); userAuthInsertErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 数据库插入用户认证失败, err:%+v", userAuthInsertErr)
}
}
userID = user.Id
2025-03-15 01:33:31 +08:00
// 使用SelectBuilder构建查询查找符合user_id的记录并按创建时间降序排序获取最新一条
builder := l.svcCtx.AgentAuditModel.SelectBuilder().Where("user_id = ?", user.Id).OrderBy("create_time DESC").Limit(1)
agentAuditList, findAgentAuditErr := l.svcCtx.AgentAuditModel.FindAll(transCtx, builder, "")
if findAgentAuditErr != nil {
2025-03-07 03:48:59 +08:00
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 查找审核列表失败%+v", findAgentAuditErr)
}
2025-03-15 01:33:31 +08:00
if len(agentAuditList) > 0 {
agentAuditModel := agentAuditList[0]
2025-03-07 03:48:59 +08:00
if agentAuditModel.Status == 0 {
return errors.Wrapf(xerr.NewErrMsg("您的代理申请中"), "代理申请, 代理申请中")
} else {
return errors.Wrapf(xerr.NewErrMsg("您已申请过代理"), "代理申请, 代理已申请过")
}
}
var agentAudit model.AgentAudit
agentAudit.UserId = user.Id
2025-04-08 12:49:19 +08:00
agentAudit.Mobile = encryptedMobile
2025-03-07 03:48:59 +08:00
agentAudit.Region = req.Region
agentAudit.Status = 1
_, insetAgentAuditErr := l.svcCtx.AgentAuditModel.Insert(transCtx, session, &agentAudit)
if insetAgentAuditErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "代理申请, 保存代理审核信息失败: %v", insetAgentAuditErr)
}
//agentAuditID, _ := agentAuditInsert.LastInsertId()
//agentAuditRow, findAgentAuditModelErr := l.svcCtx.AgentAuditModel.FindOne(l.ctx, agentAuditID)
//if findAgentAuditModelErr != nil {
// return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "代理申请, 查找代理审核信息失败: %v", insetAgentAuditErr)
//}
//agentAuditRow.Status = 1
//updateAgentAuditErr := l.svcCtx.AgentAuditModel.UpdateWithVersion(transCtx, session, agentAuditRow)
//if updateAgentAuditErr != nil {
// return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 通过代理审核失败: %+v", updateAgentAuditErr)
//}
// 新增代理
var agentModel model.Agent
agentModel.Mobile = agentAudit.Mobile
agentModel.Region = agentAudit.Region
agentModel.UserId = agentAudit.UserId
agentModel.LevelName = model.AgentLeveNameNormal
2025-03-07 03:48:59 +08:00
agentModelInsert, insertAgentModelErr := l.svcCtx.AgentModel.Insert(transCtx, session, &agentModel)
if insertAgentModelErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 新增代理失败: %+v", insertAgentModelErr)
}
agentID, _ := agentModelInsert.LastInsertId()
// 关联上级
if req.Ancestor != "" {
2025-05-09 20:02:07 +08:00
ancestorEncryptedMobile, err := crypto.EncryptMobile(req.Ancestor, secretKey)
if err != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "加密手机号失败: %v", err)
}
ancestorAgentModel, findAgentModelErr := l.svcCtx.AgentModel.FindOneByMobile(transCtx, ancestorEncryptedMobile)
2025-03-07 03:48:59 +08:00
if findAgentModelErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 查找上级代理失败: %+v", findAgentModelErr)
}
agentClosureModel := model.AgentClosure{
AncestorId: ancestorAgentModel.Id,
DescendantId: agentID,
Depth: 1,
}
_, insertAgentClosureModelErr := l.svcCtx.AgentClosureModel.Insert(transCtx, session, &agentClosureModel)
if insertAgentClosureModelErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 添加代理上下级关联失败: %+v", insertAgentClosureModelErr)
}
}
// 新增代理钱包
var agentWallet model.AgentWallet
agentWallet.AgentId = agentID
_, insertAgentWalletModelErr := l.svcCtx.AgentWalletModel.Insert(transCtx, session, &agentWallet)
if insertAgentWalletModelErr != nil {
return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 新增代理钱包失败: %+v", insertAgentWalletModelErr)
}
return nil
})
if transErr != nil {
return nil, transErr
}
token, generaErr := jwtx.GenerateJwtToken(userID, l.svcCtx.Config.JwtAuth.AccessSecret, l.svcCtx.Config.JwtAuth.AccessExpire)
if generaErr != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "代理申请, 生成token失败 : %d", userID)
}
// 获取当前时间戳
now := time.Now().Unix()
return &types.AgentApplyResp{
AccessToken: token,
AccessExpire: now + l.svcCtx.Config.JwtAuth.AccessExpire,
RefreshAfter: now + l.svcCtx.Config.JwtAuth.RefreshAfter,
}, nil
}