lrf 2 년 전
부모
커밋
b6a9223334
5개의 변경된 파일32개의 추가작업 그리고 4개의 파일을 삭제
  1. 3 0
      app/controller/user/config/.user.js
  2. 23 4
      app/service/user/user.js
  3. 1 0
      app/service/util/http-util.js
  4. 1 0
      app/z_router/user/user.js
  5. 4 0
      config/config.default.js

+ 3 - 0
app/controller/user/config/.user.js

@@ -56,4 +56,7 @@ module.exports = {
   checkBindEmail: {
     requestBody: ['!code', '!id', '!email'],
   },
+  toBindPhone: {
+    requestBody: ['!id', '!phone'],
+  },
 };

+ 23 - 4
app/service/user/user.js

@@ -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);
   }
 }
 

+ 1 - 0
app/service/util/http-util.js

@@ -76,6 +76,7 @@ class HttpUtilService extends AxiosService {
     }
     const { status } = res;
     console.warn(`[${uri}] fail: ${status}-${res.data.message} `);
+    throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求失败');
   }
 }
 

+ 1 - 0
app/z_router/user/user.js

@@ -8,6 +8,7 @@ const ckey = 'user.user';
 const keyZh = '用户';
 const routes = [
   // { method: 'get', path: `${rkey}/getUserInfo`, controller: `${ckey}.getUserInfo`, name: `${ckey}GetUserInfo`, zh: `${keyZh}换取用户信息` },
+  { method: 'post', path: `${rkey}/toBindPhone`, controller: `${ckey}.toBindPhone`, name: `${ckey}toBindPhone`, zh: `${keyZh}发送绑定手机验证码` },
   { method: 'post', path: `${rkey}/toBindEmail`, controller: `${ckey}.toBindEmail`, name: `${ckey}toBindEmail`, zh: `${keyZh}发送绑定邮箱验证码` },
   { method: 'post', path: `${rkey}/checkBindEmail`, controller: `${ckey}.checkBindEmail`, name: `${ckey}checkBindEmail`, zh: `${keyZh}校验绑定邮箱` },
   { method: 'post', path: `${rkey}/wxLogin`, controller: `${ckey}.wxLogin`, name: `${ckey}wxLogin`, zh: `${keyZh}微信登陆` },

+ 4 - 0
config/config.default.js

@@ -74,10 +74,14 @@ module.exports = appInfo => {
     wechat: 'https://broadcast.waityou24.cn/wechat/api',
     // email: 'http://broadcast.waityou24.cn/semail/api',
     email: 'http://127.0.0.1:14002/semail/api',
+    sms: 'http://127.0.0.1:14003/sms/api',
   };
   config.emailConfig = {
     config: 'free',
   };
+  config.smsConfig = {
+    config: 'free',
+  };
   // 中间件
   config.requestLog = {
     toMongoDB: true,