新增小程序手机号登录

This commit is contained in:
cool 2024-04-15 15:45:05 +08:00
parent 4166eaafe5
commit 9121567b3e
2 changed files with 50 additions and 27 deletions

View File

@ -43,7 +43,7 @@ export class AppUserLoginController extends BaseController {
@CoolTag(TagTypes.IGNORE_TOKEN)
@Post('/phone', { summary: '手机号登录' })
async phone(@Body('phone') phone: string, @Body('smsCode') smsCode: string) {
return this.ok(await this.userLoginService.phone(phone, smsCode));
return this.ok(await this.userLoginService.phoneVerifyCode(phone, smsCode));
}
@CoolTag(TagTypes.IGNORE_TOKEN)
@ -58,6 +58,15 @@ export class AppUserLoginController extends BaseController {
);
}
@CoolTag(TagTypes.IGNORE_TOKEN)
@Post('/miniPhone', { summary: '绑定小程序手机号' })
async miniPhone(@Body() body) {
const { code, encryptedData, iv } = body;
return this.ok(
await this.userLoginService.miniPhone(code, encryptedData, iv)
);
}
@CoolTag(TagTypes.IGNORE_TOKEN)
@Get('/captcha', { summary: '图片验证码' })
async captcha(

View File

@ -54,32 +54,35 @@ export class UserLoginService extends BaseService {
}
/**
*
*
* @param phone
* @param smsCode
*/
async phone(phone, smsCode) {
async phoneVerifyCode(phone, smsCode) {
// 1、检查短信验证码 2、登录
const check = await this.userSmsService.checkCode(phone, smsCode);
if (check) {
let user: any = await this.userInfoEntity.findOneBy({
phone: Equal(phone),
});
if (!user) {
user = {
phone,
unionid: phone,
loginType: 2,
nickName: phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2'),
};
await this.userInfoEntity.insert(user);
}
return this.token({ id: user.id });
return await this.phone(phone);
} else {
throw new CoolCommException('验证码错误');
}
}
/**
*
* @param code
* @param encryptedData
* @param iv
*/
async miniPhone(code, encryptedData, iv) {
const phone = await this.userWxService.miniPhone(code, encryptedData, iv);
if (phone) {
return await this.phone(phone);
} else {
throw new CoolCommException('获得手机号失败,请检查配置');
}
}
/**
*
* @param access_token
@ -89,22 +92,33 @@ export class UserLoginService extends BaseService {
const instance = await this.pluginService.getInstance('uniphone');
const phone = await instance.getPhone(access_token, openid, appId);
if (phone) {
let user: any = await this.userInfoEntity.findOneBy({ phone });
if (!user) {
user = {
phone,
unionid: phone,
loginType: 2,
nickName: phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2'),
};
await this.userInfoEntity.insert(user);
}
return this.token({ id: user.id });
return await this.phone(phone);
} else {
throw new CoolCommException('获得手机号失败,请检查配置');
}
}
/**
*
* @param phone
* @returns
*/
async phone(phone: string) {
let user: any = await this.userInfoEntity.findOneBy({
phone: Equal(phone),
});
if (!user) {
user = {
phone,
unionid: phone,
loginType: 2,
nickName: phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2'),
};
await this.userInfoEntity.insert(user);
}
return this.token({ id: user.id });
}
/**
*
* @param code