feat: 手机号登录、邮箱验证码注册

This commit is contained in:
xiaojunnuo
2024-11-29 19:00:05 +08:00
parent 87bbf6f140
commit 7b55337c5e
55 changed files with 2150 additions and 337 deletions
@@ -88,7 +88,7 @@ export class CodeService {
/**
*/
async sendEmailCode(email: string, randomStr: string) {
console.assert(!email, '手机号不能为空');
console.assert(!email, 'Email不能为空');
console.assert(!randomStr, 'randomStr不能为空');
const code = randomNumber(4);
@@ -98,7 +98,7 @@ export class CodeService {
receivers: [email],
});
const key = this.buildEmailCodeKey(email, code);
const key = this.buildEmailCodeKey(email, randomStr);
cache.set(key, code, {
ttl: 5 * 60 * 1000, //5分钟
});
@@ -1,6 +1,6 @@
import { AliyunAccess, AliyunClient } from '@certd/plugin-plus';
import { logger } from '@certd/basic';
import { ISmsService, PluginInputs, SmsPluginCtx } from './api.js';
import { AliyunAccess, AliyunClient } from '@certd/plugin-lib';
export type AliyunSmsConfig = {
accessId: string;
regionId: string;
@@ -154,30 +154,30 @@ export class UserService extends BaseService<UserEntity> {
}
async register(type: string, user: UserEntity) {
if (type !== 'username') {
if (!user.password) {
user.password = simpleNanoId();
}
if (!user.username) {
user.username = 'user_' + simpleNanoId();
if (!user.password) {
user.password = simpleNanoId();
}
if (type === 'username') {
const username = user.username;
const old = await this.findOne([{ username: username }, { mobile: username }, { email: username }]);
if (old != null) {
throw new CommonException('用户名已被注册');
}
}
if (type === 'mobile') {
user.nickName = user.mobile.substring(0, 3) + '****' + user.mobile.substring(7);
}
} else if (type === 'mobile') {
const mobile = user.mobile;
const old = await this.findOne({ username: user.username });
if (old != null) {
throw new CommonException('用户名已被注册');
}
if (user.mobile) {
const old = await this.findOne({ mobile: user.mobile });
user.nickName = mobile.substring(0, 3) + '****' + mobile.substring(7);
const old = await this.findOne([{ username: mobile }, { mobile: mobile }, { email: mobile }]);
if (old != null) {
throw new CommonException('手机号已被注册');
}
}
if (user.email) {
const old = await this.findOne({ email: user.email });
} else if (type === 'email') {
const email = user.email;
const old = await this.findOne([{ username: email }, { mobile: email }, { email: email }]);
if (old != null) {
throw new CommonException('邮箱已被注册');
}
@@ -186,10 +186,10 @@ export class UserService extends BaseService<UserEntity> {
let newUser: UserEntity = UserEntity.of({
username: user.username,
password: user.password,
nickName: user.nickName || user.username,
avatar: user.avatar || '',
email: user.email || '',
mobile: user.mobile || '',
nickName: user.nickName || user.username,
avatar: user.avatar || '',
phoneCode: user.phoneCode || '86',
status: 1,
passwordVersion: 2,