178 lines
7.9 KiB
JavaScript
178 lines
7.9 KiB
JavaScript
"use strict";
|
|
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
};
|
|
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
};
|
|
var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
return function (target, key) { decorator(target, key, paramIndex); }
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.ApiAuthService = void 0;
|
|
const common_1 = require("@nestjs/common");
|
|
const typeorm_1 = require("@nestjs/typeorm");
|
|
const typeorm_2 = require("typeorm");
|
|
const user_model_1 = require("../../../model/user.model");
|
|
const auth_service_1 = require("../../common/service/auth.service");
|
|
const app_env_1 = require("../../../app.env");
|
|
const api_1 = require("../../common/api");
|
|
const captcha_service_1 = require("../../common/service/captcha.service");
|
|
const invite_code_pool_model_1 = require("../../../model/invite_code_pool.model");
|
|
const wallet_model_1 = require("../../../model/wallet.model");
|
|
const team_relation_model_1 = require("../../../model/team_relation.model");
|
|
const user_sign_in_model_1 = require("../../../model/user_sign_in.model");
|
|
const data_source_context_1 = require("../../common/context/data_source.context");
|
|
const app_info_context_1 = require("../context/app_info.context");
|
|
const im_help_1 = require("../../common/util/im_help");
|
|
const user_log_service_1 = require("../../common/service/user_log.service");
|
|
let ApiAuthService = class ApiAuthService {
|
|
authService;
|
|
captchaService;
|
|
userModel;
|
|
userLogService;
|
|
constructor(authService, captchaService, userModel, userLogService) {
|
|
this.authService = authService;
|
|
this.captchaService = captchaService;
|
|
this.userModel = userModel;
|
|
this.userLogService = userLogService;
|
|
}
|
|
async login(phone, password, captchaKey, captchaCode) {
|
|
if (captchaKey && captchaCode) {
|
|
const isValid = await this.captchaService.validate(captchaKey, captchaCode);
|
|
if (!isValid) {
|
|
return api_1.Api.error('验证码错误', 400);
|
|
}
|
|
}
|
|
const user = await this.userModel.findOne({
|
|
where: { phone, password },
|
|
});
|
|
if (!user) {
|
|
return api_1.Api.error('账号或密码错误');
|
|
}
|
|
await this.userModel.update({
|
|
id: user.id,
|
|
}, {
|
|
device: app_info_context_1.AppInfoContext.getAppInfo().device,
|
|
});
|
|
await this.userLogService.logLogin(user.id);
|
|
await this.userLogService.logStarted(user.id);
|
|
const token = await this.createToken({ sub: user.id });
|
|
return api_1.Api.success({ token });
|
|
}
|
|
async createToken(payload) {
|
|
return await this.authService.create(payload, {
|
|
secret: app_env_1.AppEnv.JWT_API_SECRET,
|
|
});
|
|
}
|
|
async register(phone, password, inviteCode, captchaKey, captchaCode) {
|
|
const isValid = await this.captchaService.validate(captchaKey, captchaCode);
|
|
if (!isValid) {
|
|
return api_1.Api.error('验证码错误', 400);
|
|
}
|
|
const ip = app_info_context_1.AppInfoContext.getAppInfo().ip;
|
|
if (ip) {
|
|
const ipCount = await this.userModel.count({
|
|
where: { registerIp: ip },
|
|
});
|
|
if (ipCount >= 5) {
|
|
return api_1.Api.error('该 IP 已注册满 5 个账号,无法继续注册');
|
|
}
|
|
}
|
|
return data_source_context_1.DataSourceContext.startTransaction(async (transaction) => {
|
|
const userRepo = transaction.getRepository(user_model_1.UserModel);
|
|
const inviteCodeRepo = transaction.getRepository(invite_code_pool_model_1.InviteCodePoolModel);
|
|
const walletRepo = transaction.getRepository(wallet_model_1.WalletModel);
|
|
const teamRelationRepo = transaction.getRepository(team_relation_model_1.TeamRelationModel);
|
|
const userSignInRepo = transaction.getRepository(user_sign_in_model_1.UserSignInModel);
|
|
const existingUser = await userRepo.findOne({
|
|
where: { phone },
|
|
});
|
|
if (existingUser) {
|
|
return api_1.Api.error('手机号已被注册');
|
|
}
|
|
const sponsorUser = await userRepo.findOne({
|
|
where: { inviteCode },
|
|
});
|
|
if (!sponsorUser) {
|
|
return api_1.Api.error('邀请码无效');
|
|
}
|
|
const user = userRepo.create({
|
|
nick: `${phone.slice(0, 3)}****${phone.slice(7)}`,
|
|
phone,
|
|
password,
|
|
inviteCode,
|
|
device: app_info_context_1.AppInfoContext.getAppInfo().device,
|
|
registerIp: ip,
|
|
});
|
|
const newUser = await userRepo.save(user);
|
|
const inviteCodeRecord = await inviteCodeRepo.findOneByOrFail({
|
|
id: newUser.id,
|
|
});
|
|
await userRepo.update({ id: newUser.id }, { inviteCode: inviteCodeRecord.code });
|
|
await walletRepo.insert({
|
|
userId: newUser.id,
|
|
});
|
|
let sponsorTeamRelation = null;
|
|
if (sponsorUser.id) {
|
|
sponsorTeamRelation = await teamRelationRepo.findOne({
|
|
where: { userId: sponsorUser.id },
|
|
});
|
|
}
|
|
const teamRelation = teamRelationRepo.create({
|
|
userId: newUser.id,
|
|
parentId: sponsorUser.id,
|
|
grandparentId: sponsorTeamRelation?.parentId || 0,
|
|
greatGrandparentId: sponsorTeamRelation?.grandparentId ?? 0,
|
|
path: sponsorTeamRelation
|
|
? `${sponsorTeamRelation.path}/${newUser.id}`
|
|
: `${newUser.id}`,
|
|
level: (sponsorTeamRelation?.level ?? 0) + 1,
|
|
});
|
|
await teamRelationRepo.save(teamRelation);
|
|
const userSignIn = userSignInRepo.create({
|
|
userId: newUser.id,
|
|
consecutiveDays: 0,
|
|
makeUpCount: 2,
|
|
});
|
|
await userSignInRepo.save(userSignIn);
|
|
await this.userLogService.logRegister(newUser.id);
|
|
await this.userLogService.logStarted(newUser.id);
|
|
await this.importUserAndJoinGroup(newUser);
|
|
const token = await this.createToken({ sub: newUser.id });
|
|
return api_1.Api.success({ token });
|
|
});
|
|
}
|
|
async importUserAndJoinGroup(user) {
|
|
const imUid = app_env_1.AppEnv.IM_USER_ID_PREFIX + user.id;
|
|
const arr = [
|
|
{
|
|
UserID: imUid,
|
|
Nick: user.nick,
|
|
FaceUrl: user.avatar,
|
|
},
|
|
];
|
|
await im_help_1.IMHelp.multiaccountImportUser(arr);
|
|
await im_help_1.IMHelp.importGroupMember({
|
|
GroupId: app_env_1.AppEnv.IM_DEFAULT_GROUP_ID,
|
|
MemberList: [
|
|
{
|
|
Member_Account: imUid,
|
|
},
|
|
],
|
|
});
|
|
}
|
|
};
|
|
exports.ApiAuthService = ApiAuthService;
|
|
exports.ApiAuthService = ApiAuthService = __decorate([
|
|
(0, common_1.Injectable)(),
|
|
__param(2, (0, typeorm_1.InjectRepository)(user_model_1.UserModel)),
|
|
__metadata("design:paramtypes", [auth_service_1.AuthService,
|
|
captcha_service_1.CaptchaService,
|
|
typeorm_2.Repository,
|
|
user_log_service_1.UserLogService])
|
|
], ApiAuthService);
|
|
//# sourceMappingURL=api-auth.service.js.map
|