mirror of
https://github.com/vastxie/99AI.git
synced 2025-11-12 03:33:42 +08:00
v3.3.3
This commit is contained in:
89
dist/modules/auth/auth.controller.js
vendored
89
dist/modules/auth/auth.controller.js
vendored
@@ -13,18 +13,13 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AuthController = void 0;
|
||||
const verifyCode_dto_1 = require("./../verification/dto/verifyCode.dto");
|
||||
const authLogin_dto_1 = require("./dto/authLogin.dto");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const jwtAuth_guard_1 = require("../../common/auth/jwtAuth.guard");
|
||||
const auth_service_1 = require("./auth.service");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const swagger_1 = require("@nestjs/swagger");
|
||||
const authRegister_dto_1 = require("./dto/authRegister.dto");
|
||||
const updatePassword_dto_1 = require("./dto/updatePassword.dto");
|
||||
const auth_service_1 = require("./auth.service");
|
||||
const authLogin_dto_1 = require("./dto/authLogin.dto");
|
||||
const updatePassByOther_dto_1 = require("./dto/updatePassByOther.dto");
|
||||
const sendPhoneCode_dto_1 = require("./dto/sendPhoneCode.dto");
|
||||
const userRegisterByPhone_dto_1 = require("./dto/userRegisterByPhone.dto");
|
||||
const loginByPhone_dt_1 = require("./dto/loginByPhone.dt");
|
||||
const updatePassword_dto_1 = require("./dto/updatePassword.dto");
|
||||
let AuthController = class AuthController {
|
||||
constructor(authService) {
|
||||
this.authService = authService;
|
||||
@@ -32,15 +27,9 @@ let AuthController = class AuthController {
|
||||
async register(body, req) {
|
||||
return await this.authService.register(body, req);
|
||||
}
|
||||
async registerByPhone(body, req) {
|
||||
return await this.authService.registerByPhone(body, req);
|
||||
}
|
||||
async login(body, req) {
|
||||
return this.authService.login(body, req);
|
||||
}
|
||||
async loginByPhone(body, req) {
|
||||
return this.authService.loginByPhone(body, req);
|
||||
}
|
||||
async updatePassword(req, body) {
|
||||
return this.authService.updatePassword(req, body);
|
||||
}
|
||||
@@ -50,22 +39,11 @@ let AuthController = class AuthController {
|
||||
async getInfo(req) {
|
||||
return this.authService.getInfo(req);
|
||||
}
|
||||
async activateAccount(parmas, res) {
|
||||
return this.authService.activateAccount(parmas, res);
|
||||
}
|
||||
async registerSuccess(parmas) {
|
||||
const { username, id, email, teamName, registerSuccessEmailTitle, registerSuccessEmailTeamName, registerSuccessEmaileAppend } = parmas;
|
||||
return { username, id, email, teamName, registerSuccessEmailTitle, registerSuccessEmailTeamName, registerSuccessEmaileAppend };
|
||||
}
|
||||
async registerError(parmas) {
|
||||
const { message, teamName, registerFailEmailTitle, registerFailEmailTeamName } = parmas;
|
||||
return { message, teamName, registerFailEmailTitle, registerFailEmailTeamName };
|
||||
}
|
||||
async captcha(parmas) {
|
||||
return this.authService.captcha(parmas);
|
||||
}
|
||||
async sendPhoneCode(parmas) {
|
||||
return this.authService.sendPhoneCode(parmas);
|
||||
async sendCode(parmas) {
|
||||
return this.authService.sendCode(parmas);
|
||||
}
|
||||
};
|
||||
__decorate([
|
||||
@@ -74,18 +52,9 @@ __decorate([
|
||||
__param(0, (0, common_1.Body)()),
|
||||
__param(1, (0, common_1.Req)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [authRegister_dto_1.UserRegisterDto, Object]),
|
||||
__metadata("design:paramtypes", [Object, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "register", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)('registerByPhone'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '用户通过手机号注册' }),
|
||||
__param(0, (0, common_1.Body)()),
|
||||
__param(1, (0, common_1.Req)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [userRegisterByPhone_dto_1.UserRegisterByPhoneDto, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "registerByPhone", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)('login'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '用户登录' }),
|
||||
@@ -95,15 +64,6 @@ __decorate([
|
||||
__metadata("design:paramtypes", [authLogin_dto_1.UserLoginDto, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "login", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)('loginByPhone'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '用户手机号登录' }),
|
||||
__param(0, (0, common_1.Body)()),
|
||||
__param(1, (0, common_1.Req)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [loginByPhone_dt_1.LoginByPhoneDto, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "loginByPhone", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)('updatePassword'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '用户更改密码' }),
|
||||
@@ -136,33 +96,6 @@ __decorate([
|
||||
__metadata("design:paramtypes", [Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "getInfo", null);
|
||||
__decorate([
|
||||
(0, common_1.Get)('activateAccount'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '账户激活' }),
|
||||
__param(0, (0, common_1.Query)()),
|
||||
__param(1, (0, common_1.Res)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [verifyCode_dto_1.VerifyCodeDto, Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "activateAccount", null);
|
||||
__decorate([
|
||||
(0, common_1.Get)('registerSuccess'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '注册成功页面' }),
|
||||
(0, common_1.Render)('registerSuccess'),
|
||||
__param(0, (0, common_1.Query)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "registerSuccess", null);
|
||||
__decorate([
|
||||
(0, common_1.Get)('registerError'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '注册失败页面' }),
|
||||
(0, common_1.Render)('registerError'),
|
||||
__param(0, (0, common_1.Query)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "registerError", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)('captcha'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '获取一个图形验证码' }),
|
||||
@@ -172,13 +105,13 @@ __decorate([
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "captcha", null);
|
||||
__decorate([
|
||||
(0, common_1.Post)('sendPhoneCode'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '发送手机验证码' }),
|
||||
(0, common_1.Post)('sendCode'),
|
||||
(0, swagger_1.ApiOperation)({ summary: '发送验证码' }),
|
||||
__param(0, (0, common_1.Body)()),
|
||||
__metadata("design:type", Function),
|
||||
__metadata("design:paramtypes", [sendPhoneCode_dto_1.SendPhoneCodeDto]),
|
||||
__metadata("design:paramtypes", [Object]),
|
||||
__metadata("design:returntype", Promise)
|
||||
], AuthController.prototype, "sendPhoneCode", null);
|
||||
], AuthController.prototype, "sendCode", null);
|
||||
AuthController = __decorate([
|
||||
(0, swagger_1.ApiTags)('auth'),
|
||||
(0, common_1.Controller)('auth'),
|
||||
|
||||
42
dist/modules/auth/auth.module.js
vendored
42
dist/modules/auth/auth.module.js
vendored
@@ -7,34 +7,34 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AuthModule = void 0;
|
||||
const verifycation_entity_1 = require("./../verification/verifycation.entity");
|
||||
const typeorm_1 = require("@nestjs/typeorm");
|
||||
const verification_service_1 = require("./../verification/verification.service");
|
||||
const mailer_service_1 = require("../mailer/mailer.service");
|
||||
const nestjs_config_1 = require("nestjs-config");
|
||||
const auth_controller_1 = require("./auth.controller");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const auth_service_1 = require("./auth.service");
|
||||
const user_module_1 = require("../user/user.module");
|
||||
const passport_1 = require("@nestjs/passport");
|
||||
const jwt_1 = require("@nestjs/jwt");
|
||||
const jwt_strategy_1 = require("../../common/auth/jwt.strategy");
|
||||
const jwtAuth_guard_1 = require("../../common/auth/jwtAuth.guard");
|
||||
const userBalance_service_1 = require("../userBalance/userBalance.service");
|
||||
const balance_entity_1 = require("../userBalance/balance.entity");
|
||||
const accountLog_entity_1 = require("../userBalance/accountLog.entity");
|
||||
const config_entity_1 = require("../globalConfig/config.entity");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const jwt_1 = require("@nestjs/jwt");
|
||||
const passport_1 = require("@nestjs/passport");
|
||||
const typeorm_1 = require("@nestjs/typeorm");
|
||||
const nestjs_config_1 = require("nestjs-config");
|
||||
const whiteList_entity_1 = require("../chat/whiteList.entity");
|
||||
const chatGroup_entity_1 = require("../chatGroup/chatGroup.entity");
|
||||
const chatLog_entity_1 = require("../chatLog/chatLog.entity");
|
||||
const cramiPackage_entity_1 = require("../crami/cramiPackage.entity");
|
||||
const redisCache_service_1 = require("../redisCache/redisCache.service");
|
||||
const config_entity_1 = require("../globalConfig/config.entity");
|
||||
const mailer_service_1 = require("../mailer/mailer.service");
|
||||
const midjourney_entity_1 = require("../midjourney/midjourney.entity");
|
||||
const redisCache_module_1 = require("../redisCache/redisCache.module");
|
||||
const userBalance_entity_1 = require("../userBalance/userBalance.entity");
|
||||
const redisCache_service_1 = require("../redisCache/redisCache.service");
|
||||
const salesUsers_entity_1 = require("../sales/salesUsers.entity");
|
||||
const user_entity_1 = require("../user/user.entity");
|
||||
const whiteList_entity_1 = require("../chat/whiteList.entity");
|
||||
const user_module_1 = require("../user/user.module");
|
||||
const accountLog_entity_1 = require("../userBalance/accountLog.entity");
|
||||
const balance_entity_1 = require("../userBalance/balance.entity");
|
||||
const fingerprint_entity_1 = require("../userBalance/fingerprint.entity");
|
||||
const chatLog_entity_1 = require("../chatLog/chatLog.entity");
|
||||
const chatGroup_entity_1 = require("../chatGroup/chatGroup.entity");
|
||||
const midjourney_entity_1 = require("../midjourney/midjourney.entity");
|
||||
const userBalance_entity_1 = require("../userBalance/userBalance.entity");
|
||||
const userBalance_service_1 = require("../userBalance/userBalance.service");
|
||||
const verification_service_1 = require("./../verification/verification.service");
|
||||
const verifycation_entity_1 = require("./../verification/verifycation.entity");
|
||||
const auth_controller_1 = require("./auth.controller");
|
||||
const auth_service_1 = require("./auth.service");
|
||||
let AuthModule = class AuthModule {
|
||||
};
|
||||
AuthModule = __decorate([
|
||||
|
||||
220
dist/modules/auth/auth.service.js
vendored
220
dist/modules/auth/auth.service.js
vendored
@@ -13,23 +13,22 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.AuthService = void 0;
|
||||
const user_constant_1 = require("../../common/constants/user.constant");
|
||||
const utils_1 = require("../../common/utils");
|
||||
const globalConfig_service_1 = require("../globalConfig/globalConfig.service");
|
||||
const verification_constant_1 = require("../../common/constants/verification.constant");
|
||||
const verification_service_1 = require("./../verification/verification.service");
|
||||
const common_1 = require("@nestjs/common");
|
||||
const jwt_1 = require("@nestjs/jwt");
|
||||
const user_service_1 = require("../user/user.service");
|
||||
const mailer_service_1 = require("../mailer/mailer.service");
|
||||
const user_constant_1 = require("../../common/constants/user.constant");
|
||||
const userBalance_service_1 = require("../userBalance/userBalance.service");
|
||||
const config_entity_1 = require("../globalConfig/config.entity");
|
||||
const typeorm_1 = require("typeorm");
|
||||
const typeorm_2 = require("@nestjs/typeorm");
|
||||
const utils_1 = require("../../common/utils");
|
||||
const os = require("os");
|
||||
const redisCache_service_1 = require("../redisCache/redisCache.service");
|
||||
const svgCaptcha = require("svg-captcha");
|
||||
const typeorm_1 = require("@nestjs/typeorm");
|
||||
const bcrypt = require("bcryptjs");
|
||||
const os = require("os");
|
||||
const svgCaptcha = require("svg-captcha");
|
||||
const typeorm_2 = require("typeorm");
|
||||
const config_entity_1 = require("../globalConfig/config.entity");
|
||||
const mailer_service_1 = require("../mailer/mailer.service");
|
||||
const redisCache_service_1 = require("../redisCache/redisCache.service");
|
||||
const user_service_1 = require("../user/user.service");
|
||||
const userBalance_service_1 = require("../userBalance/userBalance.service");
|
||||
const verification_service_1 = require("./../verification/verification.service");
|
||||
let AuthService = class AuthService {
|
||||
constructor(configEntity, userService, jwtService, mailerService, verificationService, userBalanceService, redisCacheService, globalConfigService) {
|
||||
this.configEntity = configEntity;
|
||||
@@ -45,56 +44,75 @@ let AuthService = class AuthService {
|
||||
this.getIp();
|
||||
}
|
||||
async register(body, req) {
|
||||
await this.verificationService.verifyCaptcha(body);
|
||||
const user = await this.userService.createUserAndVerifycation(body, req);
|
||||
const { username, email, client, id } = user;
|
||||
const res = { username, email, id };
|
||||
client && (res.client = client);
|
||||
return res;
|
||||
}
|
||||
async registerByPhone(body, req) {
|
||||
const { username, password, phone, phoneCode, invitedBy } = body;
|
||||
await this.userService.verifyUserRegisterByPhone(body);
|
||||
const { username, password, contact, code, invitedBy } = body;
|
||||
let email = '', phone = '';
|
||||
const isEmail = /\S+@\S+\.\S+/.test(contact);
|
||||
const isPhone = /^\d{10,}$/.test(contact);
|
||||
if (isEmail) {
|
||||
email = contact;
|
||||
await this.userService.verifyUserRegister({ username, email });
|
||||
}
|
||||
else if (isPhone) {
|
||||
phone = contact;
|
||||
await this.userService.verifyUserRegister({ username, phone });
|
||||
}
|
||||
else {
|
||||
throw new common_1.HttpException('请提供有效的邮箱地址或手机号码。', common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
const nameSpace = await this.globalConfigService.getNamespace();
|
||||
const key = `${nameSpace}:PHONECODE:${phone}`;
|
||||
const redisPhoneCode = await this.redisCacheService.get({ key });
|
||||
if (!redisPhoneCode) {
|
||||
throw new common_1.HttpException('验证码已过期、请重新发送!', common_1.HttpStatus.BAD_REQUEST);
|
||||
const key = `${nameSpace}:CODE:${contact}`;
|
||||
const redisCode = await this.redisCacheService.get({ key });
|
||||
if (!redisCode) {
|
||||
common_1.Logger.log(`验证码过期: ${contact}`);
|
||||
throw new common_1.HttpException('验证码已过期,请重新发送!', common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
if (phoneCode !== redisPhoneCode) {
|
||||
throw new common_1.HttpException('验证码填写错误、请重新输入!', common_1.HttpStatus.BAD_REQUEST);
|
||||
if (code !== redisCode) {
|
||||
common_1.Logger.log(`验证码错误: ${contact} 输入的验证码: ${code}, 期望的验证码: ${redisCode}`);
|
||||
throw new common_1.HttpException('验证码填写错误,请重新输入!', common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
const email = `${(0, utils_1.createRandomUid)()}@nine.com`;
|
||||
const newUser = { username, password, phone, invitedBy, email, status: user_constant_1.UserStatusEnum.ACTIVE };
|
||||
console.log('开始创建用户...');
|
||||
let newUser;
|
||||
if (isEmail) {
|
||||
newUser = { username, password, email: contact, invitedBy, status: user_constant_1.UserStatusEnum.ACTIVE };
|
||||
}
|
||||
else {
|
||||
const email = `${(0, utils_1.createRandomUid)()}@aiweb.com`;
|
||||
newUser = { username, password, email, phone: contact, invitedBy, status: user_constant_1.UserStatusEnum.ACTIVE };
|
||||
}
|
||||
;
|
||||
console.log('获取默认用户头像...');
|
||||
const userDefautlAvatar = await this.globalConfigService.getConfigs(['userDefautlAvatar']);
|
||||
console.log(`使用默认用户头像: ${userDefautlAvatar}`);
|
||||
newUser.avatar = userDefautlAvatar;
|
||||
console.log('加密用户密码...');
|
||||
const hashedPassword = bcrypt.hashSync(password, 10);
|
||||
newUser.password = hashedPassword;
|
||||
console.log('保存新用户到数据库...');
|
||||
const u = await this.userService.createUser(newUser);
|
||||
console.log(`用户创建成功,用户ID: ${u.id}`);
|
||||
let inviteUser;
|
||||
if (invitedBy) {
|
||||
inviteUser = await this.userService.qureyUserInfoByInviteCode(invitedBy);
|
||||
}
|
||||
await this.userBalanceService.addBalanceToNewUser(u.id, inviteUser === null || inviteUser === void 0 ? void 0 : inviteUser.id);
|
||||
return;
|
||||
return { success: true, message: '注册成功' };
|
||||
}
|
||||
async login(user, req) {
|
||||
console.log(`开始用户登录流程,用户名: ${user.username}`);
|
||||
const u = await this.userService.verifyUserCredentials(user);
|
||||
const { username, id, email, role, openId, client } = u;
|
||||
if (!u) {
|
||||
console.error(`登录失败: 用户凭证无效 - 用户名: ${user.username}`);
|
||||
throw new common_1.HttpException('登录失败,用户凭证无效。', common_1.HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
const { username, id, email, role, openId, client, phone } = u;
|
||||
console.log(`用户凭证验证成功,用户ID: ${id}, 用户名: ${username}`);
|
||||
const ip = (0, utils_1.getClientIp)(req);
|
||||
await this.userService.savaLoginIp(id, ip);
|
||||
const token = await this.jwtService.sign({ username, id, email, role, openId, client });
|
||||
await this.redisCacheService.saveToken(id, token);
|
||||
return token;
|
||||
}
|
||||
async loginByPhone(body, req) {
|
||||
const u = await this.userService.verifyUserCredentials(body);
|
||||
const { username, id, email, role, openId, client } = u;
|
||||
const ip = (0, utils_1.getClientIp)(req);
|
||||
await this.userService.savaLoginIp(id, ip);
|
||||
const { phone } = body;
|
||||
console.log(`保存登录IP: ${ip} - 用户ID: ${id}`);
|
||||
const token = await this.jwtService.sign({ username, id, email, role, openId, client, phone });
|
||||
console.log(`JWT令牌生成成功 - 用户ID: ${id}`);
|
||||
await this.redisCacheService.saveToken(id, token);
|
||||
console.log(`令牌已保存到Redis - 用户ID: ${id}`);
|
||||
return token;
|
||||
}
|
||||
async loginByOpenId(user, req) {
|
||||
@@ -113,48 +131,6 @@ let AuthService = class AuthService {
|
||||
const { id } = req.user;
|
||||
return await this.userService.getUserInfo(id);
|
||||
}
|
||||
async activateAccount(params, res) {
|
||||
const emailConfigs = await this.configEntity.find({
|
||||
where: {
|
||||
configKey: (0, typeorm_1.In)([
|
||||
'registerSuccessEmailTitle',
|
||||
'registerSuccessEmailTeamName',
|
||||
'registerSuccessEmaileAppend',
|
||||
'registerFailEmailTitle',
|
||||
'registerFailEmailTeamName',
|
||||
]),
|
||||
},
|
||||
});
|
||||
const configMap = emailConfigs.reduce((pre, cur) => {
|
||||
pre[cur.configKey] = cur.configVal;
|
||||
return pre;
|
||||
}, {});
|
||||
try {
|
||||
const v = await this.verificationService.verifyCode(params, verification_constant_1.VerificationEnum.Registration);
|
||||
const { type, userId } = v;
|
||||
if (type !== verification_constant_1.VerificationEnum.Registration) {
|
||||
throw new common_1.HttpException('验证码类型错误', common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
const status = await this.userService.getUserStatus(userId);
|
||||
if (status === user_constant_1.UserStatusEnum.ACTIVE) {
|
||||
throw new common_1.HttpException('账户已被激活过', common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
await this.userService.updateUserStatus(v.userId, user_constant_1.UserStatusEnum.ACTIVE);
|
||||
const u = await this.userService.queryUserInfoById(v.userId);
|
||||
const { username, email, id, invitedBy } = u;
|
||||
let inviteUser;
|
||||
if (invitedBy) {
|
||||
inviteUser = await this.userService.qureyUserInfoByInviteCode(invitedBy);
|
||||
}
|
||||
await this.userBalanceService.addBalanceToNewUser(id, inviteUser === null || inviteUser === void 0 ? void 0 : inviteUser.id);
|
||||
res.redirect(`/api/auth/registerSuccess?id=${id.toString().padStart(4, '0')}&username=${username}&email=${email}®isterSuccessEmailTitle=${configMap.registerSuccessEmailTitle}®isterSuccessEmailTeamName=${configMap.registerSuccessEmailTeamName}®isterSuccessEmaileAppend=${configMap.registerSuccessEmaileAppend}`);
|
||||
}
|
||||
catch (error) {
|
||||
console.log('error: ', error);
|
||||
const message = error.response;
|
||||
res.redirect(`/api/auth/registerError?message=${message}®isterFailEmailTitle=${configMap.registerFailEmailTitle}®isterFailEmailTeamName=${configMap.registerFailEmailTeamName}`);
|
||||
}
|
||||
}
|
||||
async updatePassword(req, body) {
|
||||
const { id, client, role } = req.user;
|
||||
if (client && Number(client) > 0) {
|
||||
@@ -196,7 +172,7 @@ let AuthService = class AuthService {
|
||||
async captcha(parmas) {
|
||||
const nameSpace = await this.globalConfigService.getNamespace();
|
||||
const { color = '#fff' } = parmas;
|
||||
const captcha = svgCaptcha.createMathExpr({ background: color, height: 34, width: 120, noise: 3 });
|
||||
const captcha = svgCaptcha.createMathExpr({ background: color, height: 30, width: 120, noise: 5 });
|
||||
const text = captcha.text;
|
||||
const randomId = (0, utils_1.createRandomUid)();
|
||||
const key = `${nameSpace}:CAPTCHA:${randomId}`;
|
||||
@@ -206,20 +182,64 @@ let AuthService = class AuthService {
|
||||
code: randomId,
|
||||
};
|
||||
}
|
||||
async sendPhoneCode(body) {
|
||||
async sendCode(body) {
|
||||
await this.verificationService.verifyCaptcha(body);
|
||||
const { phone } = body;
|
||||
const nameSpace = await this.globalConfigService.getNamespace();
|
||||
const key = `${nameSpace}:PHONECODE:${phone}`;
|
||||
const ttl = await this.redisCacheService.ttl(key);
|
||||
if (ttl && ttl > 0) {
|
||||
throw new common_1.HttpException(`${ttl}秒内不得重复发送短信!`, common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
const { contact, username } = body;
|
||||
let email = '', phone = '';
|
||||
const code = (0, utils_1.createRandomCode)();
|
||||
const messageInfo = { phone, code };
|
||||
await this.verificationService.sendPhoneCode(messageInfo);
|
||||
await this.redisCacheService.set({ key, val: code }, 1 * 60);
|
||||
return '验证码发送成功、请填写验证码完成注册!';
|
||||
const isEmail = /\S+@\S+\.\S+/.test(contact);
|
||||
const isPhone = /^\d{10,}$/.test(contact);
|
||||
if (isEmail) {
|
||||
email = contact;
|
||||
await this.userService.verifyUserRegister({ username, email });
|
||||
}
|
||||
else if (isPhone) {
|
||||
phone = contact;
|
||||
await this.userService.verifyUserRegister({ username, phone });
|
||||
}
|
||||
else {
|
||||
throw new common_1.HttpException('请提供有效的邮箱地址或手机号码。', common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
const nameSpace = await this.globalConfigService.getNamespace();
|
||||
const key = `${nameSpace}:CODE:${contact}`;
|
||||
const ttl = await this.redisCacheService.ttl(key);
|
||||
if (ttl && ttl > 0 && isPhone) {
|
||||
throw new common_1.HttpException(`${ttl}秒内不得重复发送验证码!`, common_1.HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
if (isEmail) {
|
||||
const existingCode = await this.redisCacheService.get({ key });
|
||||
if (existingCode) {
|
||||
await this.mailerService.sendMail({
|
||||
to: email,
|
||||
context: {
|
||||
code: existingCode,
|
||||
},
|
||||
});
|
||||
return `验证码发送成功、请填写验证码完成注册!`;
|
||||
}
|
||||
else {
|
||||
try {
|
||||
await this.mailerService.sendMail({
|
||||
to: email,
|
||||
context: {
|
||||
code: code,
|
||||
},
|
||||
});
|
||||
console.log('邮件发送成功');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('邮件发送失败', error);
|
||||
}
|
||||
await this.redisCacheService.set({ key, val: code }, 10 * 60);
|
||||
return `验证码发送成功、请填写验证码完成注册!`;
|
||||
}
|
||||
}
|
||||
else if (isPhone) {
|
||||
const messageInfo = { phone, code };
|
||||
await this.verificationService.sendPhoneCode(messageInfo);
|
||||
await this.redisCacheService.set({ key, val: code }, 10 * 60);
|
||||
return `验证码发送成功、请填写验证码完成注册!`;
|
||||
}
|
||||
}
|
||||
createTokenFromFingerprint(fingerprint) {
|
||||
const token = this.jwtService.sign({
|
||||
@@ -235,8 +255,8 @@ let AuthService = class AuthService {
|
||||
};
|
||||
AuthService = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__param(0, (0, typeorm_2.InjectRepository)(config_entity_1.ConfigEntity)),
|
||||
__metadata("design:paramtypes", [typeorm_1.Repository,
|
||||
__param(0, (0, typeorm_1.InjectRepository)(config_entity_1.ConfigEntity)),
|
||||
__metadata("design:paramtypes", [typeorm_2.Repository,
|
||||
user_service_1.UserService,
|
||||
jwt_1.JwtService,
|
||||
mailer_service_1.MailerService,
|
||||
|
||||
Reference in New Issue
Block a user