diff --git a/src/modules/user/controller/app/login.ts b/src/modules/user/controller/app/login.ts index efa1e4b..01a9cf7 100644 --- a/src/modules/user/controller/app/login.ts +++ b/src/modules/user/controller/app/login.ts @@ -46,6 +46,18 @@ export class AppUserLoginController extends BaseController { 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) @Get('/captcha', { summary: '图片验证码' }) async captcha( diff --git a/src/modules/user/service/login.ts b/src/modules/user/service/login.ts index 5170118..97a4da7 100644 --- a/src/modules/user/service/login.ts +++ b/src/modules/user/service/login.ts @@ -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