cheny 4 years ago
parent
commit
a7064ed17f
7 changed files with 211 additions and 31 deletions
  1. 3 0
      app/controller/.dock.js
  2. 34 0
      app/controller/login.js
  3. 3 2
      app/model/dock.js
  4. 7 0
      app/router.js
  5. 24 28
      app/service/dock.js
  6. 132 0
      app/service/login.js
  7. 8 1
      config/config.default.js

+ 3 - 0
app/controller/.dock.js

@@ -3,6 +3,7 @@ module.exports = {
     requestBody: [
       "room_id",
       "password",
+      "room_phone",
       "title",
       "desc",
       "start_time",
@@ -26,6 +27,7 @@ module.exports = {
   update: {
     params: ["!id"],
     requestBody: [
+      "room_phone",
       "title",
       "desc",
       "status",
@@ -50,6 +52,7 @@ module.exports = {
   index: {
     parameters: {
       query: [
+        "room_phone",
         "room_id",
         "title",
         "desc",

+ 34 - 0
app/controller/login.js

@@ -0,0 +1,34 @@
+'use strict';
+
+const Controller = require('egg').Controller;
+
+// 登录管理
+class LoginController extends Controller {
+
+  constructor(ctx) {
+    super(ctx);
+    this.service = this.ctx.service.login;
+  }
+
+  async login() {
+    const res = await this.service.login(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
+
+  async token() {
+    const res = await this.service.token(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
+
+  async destroy() {
+    const res = await this.service.destroy(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
+
+  async wxlogin() {
+    const res = await this.service.wxlogin(this.ctx.request.body);
+    this.ctx.ok({ data: res });
+  }
+}
+
+module.exports = LoginController;

+ 3 - 2
app/model/dock.js

@@ -1,7 +1,7 @@
 'use strict';
 const Schema = require('mongoose').Schema;
 const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
-
+const { Secret } = require('naf-framework-mongoose/lib/model/schema');
 // 产品
 const goods = new Schema({
   totaltype: { type: String, required: false, maxLength: 5 }, // 产品类型
@@ -60,7 +60,7 @@ const vipuser = new Schema({
 vipuser.index({ id: 1 });
 const Dock = {
   room_id: { type: String, required: true, maxLength: 10 }, // 房间号
-  password: { type: String, required: false, maxLength: 200 }, // 密码
+  password: { type: Secret, select: false }, // 密码
   title: { type: String, required: false, maxLength: 200 }, // 对接会标题
   desc: { type: String, maxLength: 1000 }, // 简介
   status: { type: String, default: '0', maxLength: 1 }, // 状态:0准备中;1已开始;2已结束
@@ -75,6 +75,7 @@ const Dock = {
   adminuser: { type: String, required: false, maxLength: 200 }, // 用户姓名
   phone: { type: String, required: false, maxLength: 200 }, // 电话
   vipuser: { type: [ vipuser ], default: [] }, // vip用户
+  role: { type: String, default: '3', maxLength: 20 }, // 展会角色
   // is_allowed: { type: String, default: '0', maxLength: 1 }, // 0未审核;1已允许;2已拒绝
   // reason: { type: String, required: false, maxLength: 200 }, // 拒绝理由
   // roomname: { type: String, required: false }, // 房间名称

+ 7 - 0
app/router.js

@@ -52,6 +52,13 @@ module.exports = app => {
   router.post('/api/live/dock/updatevipuser/:id', controller.dock.updatevipuser);
   router.post('/api/live/dock/createvipuser/:id', controller.dock.createvipuser);
 
+  // 用户登录
+  router.post('/api/live/dock/login', controller.login.login);
+  // 根据token取得用户信息
+  router.post('/api/live/dock/token', controller.login.token);
+  // 用户退出登录
+  router.post('/api/live/dock/logout', controller.login.destroy);
+
   // 栏目表设置路由
   router.resources('column', '/api/live/column', controller.column); // index、create、show、destroy
   router.post(

+ 24 - 28
app/service/dock.js

@@ -6,41 +6,37 @@ const { ObjectId } = require('mongoose').Types;
 const { CrudService } = require('naf-framework-mongoose/lib/service');
 const { BusinessError } = require('naf-core').Error;
 const moment = require('moment');
+const jwt = require('jsonwebtoken');
+
 class ChatService extends CrudService {
   constructor(ctx) {
     super(ctx, 'dock');
     this.model = this.ctx.model.Dock;
   }
+  // 创建登录Token
+  async createJwtPwd(password) {
+    const { secret } = this.config.jwt;
+    const token = await jwt.sign(password, secret);
+    return token;
+  }
   async create(body) {
-    const res = await this.model.create(body);
-    if (res) {
-      const url = this.ctx.app.config.axios.auth.baseUrl;
-      const newdata = {
-        name: body.adminuser,
-        phone: body.phone,
-        passwd: body.passwd,
-        role: '3',
-        pid: body.pid,
-        code: body.code,
-        room_id: '123',
-        password: '123',
-      };
-      const user = await this.ctx.curl(url, {
-        method: 'post',
-        headers: {
-          'content-type': 'application/json',
-        },
-        dataType: 'json',
-        data: JSON.stringify(newdata),
-      });
-      if (user.data.errcode === 0) {
-        const result = await this.model.findById(res.id);
-        result.adminuser = body.adminuser;
-        result.phone = body.phone;
-        result.save();
-      }
+    // roomid与密码每次加一
+    const findroom = await this.model.find().sort({ room_id: -1 });
+    if (findroom.length > 0) {
+      const room = parseInt(findroom[0].room_id) + 1;
+      // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
+      const pas = await this.createJwtPwd(room);
+      body.room_id = room;
+      body.password = { secret: pas };
+    } else {
+      // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
+      const room = '1001';
+      const pas = await this.createJwtPwd(room);
+      body.room_id = room;
+      body.password = { secret: pas };
     }
-    return { ...JSON.parse(JSON.stringify(res)), adminuser: body.adminuser, phone: body.phone };
+    const res = await this.model.create(body);
+    return { ...JSON.parse(JSON.stringify(res)), password: res.password.secret, room_id: res.room_id };
   }
   // 产品
   async goods({ id }, body) {

+ 132 - 0
app/service/login.js

@@ -0,0 +1,132 @@
+'use strict';
+
+const assert = require('assert');
+const { ObjectId } = require('mongoose').Types;
+const { CrudService } = require('naf-framework-mongoose/lib/service');
+const { BusinessError, ErrorCode } = require('naf-core').Error;
+const jwt = require('jsonwebtoken');
+const uuid = require('uuid');
+
+class LoginService extends CrudService {
+  constructor(ctx) {
+    super(ctx, 'login');
+    this.model = this.ctx.model.Dock;
+  }
+
+  // 用户登录
+  async login(data) {
+    const { room_phone, passwd, role } = data;
+    const phone = room_phone;
+    const room_id = room_phone;
+    // 根据用户输入的手机号查询其他用户表中是否存在相应数据
+    let user = await this.model.findOne({ phone, role });
+    const flag = true;
+    if (!user) {
+      user = await this.model.findOne({ room_id, role });
+    }
+    // 如果用户不存在抛出异常
+    if (!user) {
+      throw new BusinessError(ErrorCode.USER_NOT_EXIST);
+    }
+    let _user = '';
+    if (flag) {
+      // 手机
+      _user = await this.model.findOne({ phone }, '+passwd');
+    } else {
+      // 房间号
+      _user = await this.model.findOne({ room_id }, '+passwd');
+    }
+
+    // 将用户输入的密码进行加密并与查询到的用户数据密码相比对
+    const pas = await this.createJwtPwd(passwd);
+    // 如果两个密码不一致抛出异常
+    if (pas !== _user.passwd.secret) {
+      throw new BusinessError(ErrorCode.BAD_PASSWORD);
+    }
+
+    // if (role === '3') {
+    //   console.log('vip用户');
+    // } else if (role === '8') {
+    //   console.log('展会vip用户');
+    // } else {
+    //   console.log('无此展会');
+    // }
+
+
+    // if (_user.role === '3' || _user.role === '8') {
+    //   const url = 'http://127.0.0.1:9004/api/market/user/' + _user.uid;
+    //   const marketuser = await this.ctx.curl(url, {
+    //     method: 'get',
+    //     headers: {
+    //       'content-type': 'application/json',
+    //     },
+    //     dataType: 'json',
+    //   });
+    //   if (marketuser.data.data.status !== '1') {
+    //     throw new BusinessError(ErrorCode.ACCESS_DENIED);
+    //   }
+    // } else if (_user.role === '6') {
+    //   const url = 'http://127.0.0.1:9004/api/market/expertsuser/' + _user.uid;
+    //   const expertsuser = await this.ctx.curl(url, {
+    //     method: 'get',
+    //     headers: {
+    //       'content-type': 'application/json',
+    //     },
+    //     dataType: 'json',
+    //   });
+    //   if (expertsuser.data.data.status !== '1') {
+    //     throw new BusinessError(ErrorCode.ACCESS_DENIED);
+    //   }
+    // }
+    // 取出用户的类型,根据用户类型返回相应信息
+    const state = uuid();
+    const key = `free:auth:state:${state}`;
+    // const _menus = [];
+    // for (const elm of user.menus) {
+    //   const _menu = await this.rmodel.findById({ _id: ObjectId(elm) });
+    //   if (_menu) {
+    //     _menus.push({ id: elm, role_name: _menu.role_name, url: _menu.url });
+    //   }
+    // }
+    // user.menus = JSON.stringify(_menus);
+    const token = await this.createJwt(user);
+    await this.app.redis.set(key, token, 'EX', 60 * 60 * 24);
+    return { key };
+  }
+
+  // 创建登录Token
+  async createJwtPwd(password) {
+    const { secret } = this.config.jwt;
+    const token = await jwt.sign(password, secret);
+    return token;
+  }
+
+  // 创建登录Token
+  async createJwt({ id, name, uid, phone, role, menus, remark, openid, deptid, deptname, pid, code }) {
+    const { secret, expiresIn = '1d', issuer = role } = this.config.jwt;
+    const subject = phone;
+    const res = { uid: id, userid: uid, name, phone, role, menus, openid, remark, deptid, deptname, pid, code };
+    const token = await jwt.sign(res, secret, { expiresIn, issuer, subject });
+    return token;
+  }
+
+  // 取得redis内token信息
+  async token({ key }) {
+    assert(key, 'key不能为空');
+    const token = await this.app.redis.get(key);
+    if (!token) {
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, 'token已经过期');
+    }
+    return { token };
+  }
+
+  // 删除操作
+  async destroy({ key }) {
+    const res = await this.app.redis.del(key);
+    console.log(res);
+    return res;
+  }
+
+
+}
+module.exports = LoginService;

+ 8 - 1
config/config.default.js

@@ -1,7 +1,7 @@
 /* eslint valid-jsdoc: "off" */
 
 'use strict';
-
+const { jwt } = require('./config.secret');
 /**
  * @param {Egg.EggAppInfo} appInfo app info
  */
@@ -59,6 +59,13 @@ module.exports = appInfo => {
     agent: true,
   };
 
+  // // JWT config
+  config.jwt = {
+    ...jwt,
+    expiresIn: '1d',
+    issuer: 'jobs',
+  };
+
   return {
     ...config,
     ...userConfig,