|
@@ -10,10 +10,13 @@ class UserService extends CrudService {
|
|
|
super(ctx, 'user');
|
|
|
this.redis = this.app.redis;
|
|
|
this.model = this.ctx.model.User.User;
|
|
|
- this.emailKey = 'bindEmail:';
|
|
|
+ this.bindEmailKey = 'bindEmail:';
|
|
|
this.httpUtil = this.ctx.service.util.httpUtil;
|
|
|
this.emailServiceUrl = _.get(this.app, 'config.httpPrefix.email');
|
|
|
this.emailServiceConfig = _.get(this.app, 'config.emailConfig.config');
|
|
|
+ this.bindPhoneKey = 'bindPhone:';
|
|
|
+ this.smsServiceUrl = _.get(this.app, 'config.httpPrefix.sms');
|
|
|
+ this.smsServiceConfig = _.get(this.app, 'config.smsConfig.config');
|
|
|
this.conenctCode = '&&';
|
|
|
}
|
|
|
async beforeCreate(data) {
|
|
@@ -85,14 +88,14 @@ class UserService extends CrudService {
|
|
|
async toBindEmail({ id, email }) {
|
|
|
const code = _.random(100000, 999999);
|
|
|
const value = `${email}${this.conenctCode}${code}`;
|
|
|
- await this.redis.set(`${this.emailKey}${id}`, value, 'EX', 300);
|
|
|
+ await this.redis.set(`${this.bindEmailKey}${id}`, value, 'EX', 300);
|
|
|
// 发邮件
|
|
|
const data = { config: this.emailServiceConfig, template: 'bindEmail', receiver: email, params: { code } };
|
|
|
const url = `${this.emailServiceUrl}/sendEmail`;
|
|
|
await this.httpUtil.cpost(url, data);
|
|
|
}
|
|
|
async checkBindEmail({ code, id, email }) {
|
|
|
- const redisData = await this.redis.get(`${this.emailKey}${id}`);
|
|
|
+ const redisData = await this.redis.get(`${this.bindEmailKey}${id}`);
|
|
|
if (!redisData) throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '验证码已超时');
|
|
|
const arr = redisData.split(this.conenctCode);
|
|
|
const rEmail = _.head(arr);
|
|
@@ -100,7 +103,23 @@ class UserService extends CrudService {
|
|
|
if (code !== rCode) throw new BusinessError(ErrorCode.DATA_INVALID, '验证码错误');
|
|
|
if (email !== rEmail) throw new BusinessError(ErrorCode.DATA_INVALID, '要绑定的邮箱与接收验证码的邮箱不是同一个邮箱');
|
|
|
await this.model.updateOne({ _id: id }, { email });
|
|
|
- await this.redis.del(`${this.emailKey}${id}`);
|
|
|
+ await this.redis.del(`${this.bindEmailKey}${id}`);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 绑定手机验证码
|
|
|
+ * @param {Object} body 请求体
|
|
|
+ * @param body.id 用户id
|
|
|
+ * @param body.phone 用户要绑定的手机
|
|
|
+ */
|
|
|
+ async toBindPhone({ id, phone }) {
|
|
|
+ const code = _.random(100000, 999999);
|
|
|
+ const value = `${phone}${this.conenctCode}${code}`;
|
|
|
+ await this.redis.set(`${this.bindPhoneKey}${id}`, value, 'EX', 300);
|
|
|
+ // 发短信
|
|
|
+ const data = { config: this.smsServiceConfig, template: 'bind', phone, params: { code } };
|
|
|
+ const url = `${this.smsServiceUrl}/sendEmail`;
|
|
|
+ await this.httpUtil.cpost(url, data);
|
|
|
}
|
|
|
}
|
|
|
|