新版本,代理功能上线
This commit is contained in:
		
							
								
								
									
										169
									
								
								app/user/cmd/api/internal/logic/agent/applyforagentlogic.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										169
									
								
								app/user/cmd/api/internal/logic/agent/applyforagentlogic.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,169 @@ | ||||
| package agent | ||||
|  | ||||
| import ( | ||||
| 	"context" | ||||
| 	"fmt" | ||||
| 	"github.com/pkg/errors" | ||||
| 	"github.com/zeromicro/go-zero/core/stores/redis" | ||||
| 	"github.com/zeromicro/go-zero/core/stores/sqlx" | ||||
| 	"time" | ||||
| 	"tydata-server/app/user/model" | ||||
| 	jwtx "tydata-server/common/jwt" | ||||
| 	"tydata-server/common/xerr" | ||||
| 	"tydata-server/pkg/lzkit/lzUtils" | ||||
|  | ||||
| 	"tydata-server/app/user/cmd/api/internal/svc" | ||||
| 	"tydata-server/app/user/cmd/api/internal/types" | ||||
|  | ||||
| 	"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) { | ||||
| 	// 校验验证码 | ||||
| 	redisKey := fmt.Sprintf("%s:%s", "agentApply", req.Mobile) | ||||
| 	cacheCode, err := l.svcCtx.Redis.Get(redisKey) | ||||
| 	if err != nil { | ||||
| 		if errors.Is(err, redis.Nil) { | ||||
| 			return nil, errors.Wrapf(xerr.NewErrMsg("验证码已过期"), "代理申请, 验证码过期: %s", req.Mobile) | ||||
| 		} | ||||
| 		return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 读取验证码redis缓存失败, mobile: %s, err: %+v", req.Mobile, err) | ||||
| 	} | ||||
| 	if cacheCode != req.Code { | ||||
| 		return nil, errors.Wrapf(xerr.NewErrMsg("验证码不正确"), "代理申请, 验证码不正确: %s", req.Mobile) | ||||
| 	} | ||||
| 	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. 未注册账号申请代理 | ||||
| 		user, findUserErr := l.svcCtx.UserModel.FindOneByMobile(l.ctx, req.Mobile) | ||||
| 		if findUserErr != nil && !errors.Is(findUserErr, model.ErrNotFound) { | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "手机登录, 读取数据库获取用户失败, mobile: %s, err: %+v", req.Mobile, err) | ||||
| 		} | ||||
| 		if user == nil { | ||||
| 			user = &model.User{Mobile: req.Mobile} | ||||
| 			if len(user.Nickname) == 0 { | ||||
| 				user.Nickname = req.Mobile | ||||
| 			} | ||||
| 			insertResult, userInsertErr := l.svcCtx.UserModel.Insert(transCtx, session, user) | ||||
| 			if userInsertErr != nil { | ||||
| 				return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 数据库插入新用户失败, mobile%s, err: %+v", req.Mobile, err) | ||||
| 			} | ||||
| 			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 | ||||
| 			userAuth.AuthKey = req.Mobile | ||||
| 			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 | ||||
| 		agentAuditModel, findAgentAuditErr := l.svcCtx.AgentAuditModel.FindOneByUserId(transCtx, user.Id) | ||||
| 		if findAgentAuditErr != nil && !errors.Is(findAgentAuditErr, model.ErrNotFound) { | ||||
| 			return errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "代理申请, 查找审核列表失败%+v", findAgentAuditErr) | ||||
| 		} | ||||
| 		if agentAuditModel != nil { | ||||
| 			if agentAuditModel.Status == 0 { | ||||
| 				return errors.Wrapf(xerr.NewErrMsg("您的代理申请中"), "代理申请, 代理申请中") | ||||
| 			} else { | ||||
| 				return errors.Wrapf(xerr.NewErrMsg("您已申请过代理"), "代理申请, 代理已申请过") | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		var agentAudit model.AgentAudit | ||||
| 		agentAudit.UserId = user.Id | ||||
| 		agentAudit.Mobile = req.Mobile | ||||
| 		agentAudit.Region = req.Region | ||||
| 		agentAudit.WechatId = lzUtils.StringToNullString(req.WechatID) | ||||
| 		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.WechatId = lzUtils.StringToNullString(req.WechatID) | ||||
| 		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 != "" { | ||||
| 			ancestorAgentModel, findAgentModelErr := l.svcCtx.AgentModel.FindOneByMobile(transCtx, req.Ancestor) | ||||
| 			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 | ||||
| } | ||||
		Reference in New Issue
	
	Block a user