lrf пре 2 година
родитељ
комит
771e1aa5e1

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

@@ -62,4 +62,10 @@ module.exports = {
   checkBindPhone: {
     requestBody: ['!code', '!id', '!phone'],
   },
+  toLoginByCode: {
+    requestBody: ['!phone'],
+  },
+  checkLoginCode: {
+    requestBody: ['!code', '!phone'],
+  },
 };

+ 1 - 0
app/service/user/admin.js

@@ -31,6 +31,7 @@ class AdminService extends CrudService {
     const token = this.ctx.service.util.jwt.encrypt(user);
     return token;
   }
+
 }
 
 module.exports = AdminService;

+ 32 - 1
app/service/user/user.js

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

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

@@ -7,6 +7,9 @@ const rkey = 'user';
 const ckey = 'user.user';
 const keyZh = '用户';
 const routes = [
+  { method: 'post', path: `${rkey}/toLoginByCode`, controller: `${ckey}.toLoginByCode`, name: `${ckey}toLoginByCode`, zh: `${keyZh}手机验证码登陆` },
+  { method: 'post', path: `${rkey}/checkLoginCode`, controller: `${ckey}.checkLoginCode`, name: `${ckey}checkLoginCode`, zh: `${keyZh}手机验证码登陆校验` },
+
   { method: 'post', path: `${rkey}/checkBindPhone`, controller: `${ckey}.checkBindPhone`, name: `${ckey}checkBindPhone`, 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}发送绑定邮箱验证码` },

+ 3 - 0
config/config.prod.js

@@ -17,6 +17,9 @@ module.exports = appInfo => {
   config.emailConfig = {
     config: 'tehq',
   };
+  config.smsConfig = {
+    config: 'tehq',
+  };
   return {
     ...config,
   };