From 4166eaafe595ce1547e24bc26e15d5b3a194de0a Mon Sep 17 00:00:00 2001 From: cool Date: Mon, 15 Apr 2024 15:18:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Euni=E4=B8=80=E9=94=AE?= =?UTF-8?q?=E6=89=8B=E6=9C=BA=E5=8F=B7=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/user/controller/app/login.ts | 12 ++++++++++++ src/modules/user/service/login.ts | 25 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) 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