新增uni一键手机号登录

This commit is contained in:
cool 2024-04-15 15:18:10 +08:00
parent b4152eb183
commit 4166eaafe5
2 changed files with 37 additions and 0 deletions

View File

@ -46,6 +46,18 @@ export class AppUserLoginController extends BaseController {
return this.ok(await this.userLoginService.phone(phone, smsCode)); return this.ok(await this.userLoginService.phone(phone, smsCode));
} }
@CoolTag(TagTypes.IGNORE_TOKEN)
@Post('/uniPhone', { summary: '一键手机号登录' })
async uniPhone(
@Body('access_token') access_token: string,
@Body('openid') openid: string,
@Body('appId') appId: string
) {
return this.ok(
await this.userLoginService.uniPhone(access_token, openid, appId)
);
}
@CoolTag(TagTypes.IGNORE_TOKEN) @CoolTag(TagTypes.IGNORE_TOKEN)
@Get('/captcha', { summary: '图片验证码' }) @Get('/captcha', { summary: '图片验证码' })
async captcha( async captcha(

View File

@ -80,6 +80,31 @@ export class UserLoginService extends BaseService {
} }
} }
/**
*
* @param access_token
* @param openid
*/
async uniPhone(access_token, openid, appId) {
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 });
} else {
throw new CoolCommException('获得手机号失败,请检查配置');
}
}
/** /**
* *
* @param code * @param code