|
@@ -17,6 +17,7 @@ class UserService extends CrudService {
|
|
|
this.bindPhoneKey = 'bindPhone:';
|
|
|
this.smsServiceUrl = _.get(this.app, 'config.httpPrefix.sms');
|
|
|
this.smsServiceConfig = _.get(this.app, 'config.smsConfig.config');
|
|
|
+ this.phoneLoginKey = 'phoneLogin:';
|
|
|
this.conenctCode = '&&';
|
|
|
}
|
|
|
async beforeCreate(data) {
|
|
@@ -131,10 +132,40 @@ class UserService extends CrudService {
|
|
|
const rphone = _.head(arr);
|
|
|
const rCode = _.last(arr);
|
|
|
if (code !== rCode) throw new BusinessError(ErrorCode.DATA_INVALID, '验证码错误');
|
|
|
- if (phone !== rphone) throw new BusinessError(ErrorCode.DATA_INVALID, '要绑定的邮箱与接收验证码的邮箱不是同一个邮箱');
|
|
|
+ if (phone !== rphone) throw new BusinessError(ErrorCode.DATA_INVALID, '要绑定的手机号与接收验证码的手机号不是同一个手机号');
|
|
|
await this.model.updateOne({ _id: id }, { phone });
|
|
|
await this.redis.del(`${this.bindPhoneKey}${id}`);
|
|
|
}
|
|
|
+
|
|
|
+ // 发送登陆验证码
|
|
|
+ async toLoginByCode({ phone }) {
|
|
|
+ const code = _.random(100000, 999999);
|
|
|
+ const value = `${phone}${this.conenctCode}${code}`;
|
|
|
+ await this.redis.set(`${this.phoneLoginKey}${phone}`, value, 'EX', 300);
|
|
|
+ // 发短信
|
|
|
+ const data = { config: this.smsServiceConfig, template: 'login', phone, params: { code } };
|
|
|
+ const url = `${this.smsServiceUrl}/sendMessage`;
|
|
|
+ await this.httpUtil.cpost(url, data);
|
|
|
+ }
|
|
|
+ // 检查登陆验证码,然后登陆
|
|
|
+ async checkLoginCode({ phone, code }) {
|
|
|
+ const redisData = await this.redis.get(`${this.phoneLoginKey}${phone}`);
|
|
|
+ if (!redisData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '验证码已超时');
|
|
|
+ const arr = redisData.split(this.conenctCode);
|
|
|
+ const rphone = _.head(arr);
|
|
|
+ const rCode = _.last(arr);
|
|
|
+ if (code !== rCode) throw new BusinessError(ErrorCode.DATA_INVALID, '验证码错误');
|
|
|
+ if (phone !== rphone) throw new BusinessError(ErrorCode.DATA_INVALID, '要登陆的手机号与接收验证码的手机号不是同一个手机号');
|
|
|
+ const { populate } = this.getRefMods();
|
|
|
+ const user = await this.model.findOne({ phone }).populate(populate);
|
|
|
+ if (!user) throw new BusinessError(ErrorCode.USER_NOT_EXIST);
|
|
|
+ const { status } = user;
|
|
|
+ if (status !== '0') throw new BusinessError(ErrorCode.USER_NOT_BIND, '该账号处于禁止使用状态');
|
|
|
+ delete user.meta;
|
|
|
+ delete user.__v;
|
|
|
+ const token = this.ctx.service.util.jwt.encrypt(user);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
module.exports = UserService;
|