mirror of
https://github.com/cool-team-official/cool-admin-midway.git
synced 2024-11-01 14:10:29 +08:00
新增小程序手机号登录
This commit is contained in:
parent
4166eaafe5
commit
9121567b3e
@ -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(
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user