diff --git a/src/modules/user/controller/app/login.ts b/src/modules/user/controller/app/login.ts index 01a9cf7..a64ce97 100644 --- a/src/modules/user/controller/app/login.ts +++ b/src/modules/user/controller/app/login.ts @@ -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( diff --git a/src/modules/user/service/login.ts b/src/modules/user/service/login.ts index 97a4da7..8e1e006 100644 --- a/src/modules/user/service/login.ts +++ b/src/modules/user/service/login.ts @@ -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