Explorar o código

调整实现方式,将接收消息放到各个业务中处理

liuyu %!s(int64=5) %!d(string=hai) anos
pai
achega
fbc46507b2
Modificáronse 7 ficheiros con 0 adicións e 334 borrados
  1. 0 18
      app.js
  2. 0 36
      app/model/enrollment.js
  3. 0 20
      app/service/axiox/student.js
  4. 0 133
      app/service/excelimport.js
  5. 0 71
      app/service/rabbitmq.js
  6. 0 55
      config/config.default.js
  7. 0 1
      package.json

+ 0 - 18
app.js

@@ -1,18 +0,0 @@
-'use strict';
-class AppBootHook {
-  constructor(app) {
-    this.app = app;
-  }
-
-  async didReady() {
-    // 应用已经启动完毕
-    const ctx = await this.app.createAnonymousContext();
-    // 企业入驻申请消息接收事件
-    await ctx.service.rabbitmq.receiveQueueMsg('stu_import');
-  }
-
-  async serverDidReady() {
-    // 应用已经启动完毕
-  }
-}
-module.exports = AppBootHook;

+ 0 - 36
app/model/enrollment.js

@@ -1,36 +0,0 @@
-'use strict';
-
-const Schema = require('mongoose').Schema;
-const metaPlugin = require('naf-framework-mongoose/lib/model/meta-plugin');
-
-// 学籍信息信息库
-const SchemaDefine = {
-  schid: { type: String, required: true, maxLength: 64 }, // 学校ID,等同于yxdm
-  year: { type: String, required: true, maxLength: 64 }, // 毕业年份
-  xh: { type: String, required: true, maxLength: 64 }, // 学号
-  xm: { type: String, required: true, maxLength: 64 }, // 姓名
-  sfzh: { type: String, required: true, maxLength: 64 }, // 身份证号
-  xb: { type: String, required: true, maxLength: 64 }, // 性别
-  mz: String, // 民族
-  zzmm: String, // 政治面貌
-  yxdm: { type: String, required: true, maxLength: 64 }, // 院校代码
-  yxmc: { type: String, required: true, maxLength: 64 }, // 院校名称
-  zydm: String, // 专业代码
-  zymc: { type: String, required: true, maxLength: 64 }, // 专业名称
-  xldm: String, // 学历代码
-  xl: { type: String, required: true, maxLength: 64 }, // 学历
-  syszddm: String, // 生源所在地代码
-  syszd: String, // 生源所在地
-  szyx: String, // 所在院系
-  szbj: String, // 所在班级
-};
-const schema = new Schema(SchemaDefine, { toJSON: { virtuals: true } });
-schema.index({ year: 1 });
-schema.index({ year: 1, schid: 1 });
-schema.index({ schid: 1, xh: 1 }, { unique: true, sparse: true }); // 同一学校学号必须唯一
-schema.plugin(metaPlugin);
-
-module.exports = app => {
-  const { mongoose } = app;
-  return mongoose.model('Enrollment', schema, 'stud_enrollment');
-};

+ 0 - 20
app/service/axiox/student.js

@@ -1,20 +0,0 @@
-'use strict';
-
-const _ = require('lodash');
-const { AxiosService } = require('naf-framework-mongoose/lib/service');
-
-const meta = {
-  create: {
-    uri: '/enrollments',
-    method: 'post',
-  },
-};
-
-class StudentService extends AxiosService {
-  constructor(ctx) {
-    super(ctx, meta, _.get(ctx.app.config, 'axios.stud'));
-    this.model = this.ctx.model.Enrollment;
-  }
-}
-
-module.exports = StudentService;

+ 0 - 133
app/service/excelimport.js

@@ -1,133 +0,0 @@
-'use strict';
-
-const XLSX = require('xlsx');
-const Service = require('egg').Service;
-
-class ExcelimportService extends Service {
-
-  constructor(ctx) {
-    super(ctx);
-    this.appid = '';
-  }
-
-  // 获取导入的XLSX文件中的数据
-  async getImportXLSXData(elem) {
-    // 导入学籍的情况
-    if (elem.type === '0') {
-      const filepath = this.ctx.app.config.baseDirImp + elem.content;
-      const file = await this.ctx.curl(filepath);
-      console.log(11111);
-      console.log(file.data);
-      const workbook = XLSX.read(file.data);
-      // 读取内容
-      const sheetNames = workbook.SheetNames; // 获取所有sheet页
-      const sheet = workbook.Sheets[sheetNames[0]]; // 通过取得当前sheet页
-      // const theadRule = [ sheet.A1.v, sheet.B1.v, sheet.C1.v, sheet.D1.v, sheet.E1.v, sheet.F1.v, sheet.G1.v, sheet.H1.v, sheet.I1.v, sheet.J1.v, sheet.K1.v, sheet.L1.v, sheet.M1.v, sheet.N1.v, sheet.O1.v, sheet.P1.v ];
-      const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
-      // const theadRule = [ '序号', '院系', '班级', '专业代码', '专业名称', '学号', '姓名', '身份证号', '性别', '学历代码', '学历名称', '毕业年份', '民族', '电话号码', '生源所在地', '政治面貌' ];
-      if (!params) return [];
-      // 取得配置文件中学生描述信息
-      const maps = this.ctx.app.config.studmaps;
-      const _datas = params.map(p => {
-        const data = {};
-        Object.keys(maps).forEach(k => { data[k] = p[maps[k]]; });
-        // Object.entries(maps).forEach(a => { data[a[0]] = p[a[1]]; });
-        return data;
-      });
-      return _datas;
-    }
-  }
-
-  // 将取得的数据校验格式是否正确
-  async validatedata(elem, _datas) {
-    let errCount = 0;
-    const errmsgRes = [];
-    for (const i in _datas) {
-      const data = _datas[i];
-      if (data.xh === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '学号为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-      if (data.xm === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '姓名为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-      if (data.sfzh === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '身份证号为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-      if (data.xb === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '性别为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-      if (data.yxdm === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '院校代码为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-      if (data.yxmc === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '院校名称为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-      if (data.zymc === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '专业名称为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-      if (data.xl === '') {
-        errCount++;
-        const errmsg = { ...data, errmsg: '学历为空' };
-        errmsgRes.push(errmsg);
-        break;
-      }
-    }
-    if (errCount !== 0) {
-      const updatedata = { status: '2', errmsg: errmsgRes };
-      console.log(updatedata);
-      await this.ctx.service.dataimp.update({ id: elem.id }, updatedata);
-    }
-    return errCount;
-  }
-
-  // 将取得的数据插入到数据表中
-  async dataimport(elem, _datas) {
-    // 将数据逐条导入学生库中
-    const queryData = { year: elem.createtime, schid: elem.userid, schname: elem.name };
-    let errCount = 0;
-    const errmsgRes = [];
-    for (const i in _datas) {
-      try {
-        await this.ctx.service.axiox.student.create(queryData, _datas[i]);
-      } catch (err) {
-        console.log(err);
-        errCount++;
-        errmsgRes.push(_datas[i]);
-      }
-    }
-    console.log(errCount);
-    // 导入成功时更新状态
-    if (errCount === 0) {
-      const updatedata = { status: '1' };
-      await this.ctx.service.dataimp.update({ id: elem.id }, updatedata);
-    } else {
-      console.log(errmsgRes);
-      const updatedata = { status: '2', errmsg: errmsgRes };
-      console.log(updatedata);
-      await this.ctx.service.dataimp.update({ id: elem.id }, updatedata);
-    }
-  }
-}
-
-module.exports = ExcelimportService;

+ 0 - 71
app/service/rabbitmq.js

@@ -1,71 +0,0 @@
-'use strict';
-
-const Service = require('egg').Service;
-
-class RabbitmqService extends Service {
-
-  constructor(ctx) {
-    super(ctx);
-    this.exType = 'topic';
-    this.durable = true;
-  }
-
-  // 发送消息
-  async sendQueueMsg(ex, routeKey, msg, parm) {
-    const { mq } = this.ctx;
-    if (mq) {
-      await mq.topic(ex, routeKey, msg, parm);
-    } else {
-      this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
-    }
-  }
-
-  // 接收消息
-  async receiveQueueMsg(ex) {
-    console.log(ex);
-    const self = this;
-    const { mq } = self.ctx;
-    if (mq) {
-      const ch = await mq.conn.createChannel();
-      try {
-        await ch.assertExchange(ex, self.exType, { durable: self.durable });
-        const q = await ch.assertQueue('', { exclusive: true });
-        console.log('==q=', q);
-        // 队列绑定 exchange
-        await ch.bindQueue(q.queue, ex, '*');
-        ch.consume(q.queue, msg => {
-          console.log('收到消息: ', msg);
-          const result = msg.content.toString();
-          const headers = msg.properties.headers;
-          // 插入待办事项到数据库中。
-          if (result != null) {
-            console.log('headers: ', headers);
-            const data = { userid: headers.userid, type: '0', status: '0' };
-            const that = this;
-            // 根据条件查询所有符合条件的待导入信息
-            this.ctx.service.dataimp.query(data).then(function(dataimpResult) {
-              for (const elem of dataimpResult) {
-                // 取得当前信息下excel文件的数据
-                that.ctx.service.excelimport.getImportXLSXData(elem).then(function(exceldata) {
-                  // 插入前数据格式校验
-                  that.ctx.service.excelimport.validatedata(elem, exceldata).then(function(errcount) {
-                    // 判断是否有数据错误,错误数为0时插入学生学籍库
-                    if (errcount === 0) {
-                      that.ctx.service.excelimport.dataimport(elem, exceldata);
-                    }
-                  });
-                });
-              }
-            });
-          }
-        }, { noAck: true });
-      } catch (e) {
-        console.log('==e==', e);
-      }
-    } else {
-      this.ctx.logger.error('!!!!!!没有配置MQ插件!!!!!!');
-    }
-  }
-}
-
-module.exports = RabbitmqService;

+ 0 - 55
config/config.default.js

@@ -45,61 +45,6 @@ module.exports = appInfo => {
     whitelist: [ '.xlsx' ], // 文件类型白名单
   };
 
-  // mongoose config
-  // config.mongoose = {
-  //   url: 'mongodb://127.0.0.1:27017/smart',
-  //   options: {
-  //     user: 'root',
-  //     pass: 'Ziyouyanfa#@!',
-  //     authSource: 'admin',
-  //     useNewUrlParser: true,
-  //     useCreateIndex: true,
-  //   },
-  // };
-  // base路径
-  config.baseDirImp = 'http://smart.cc-lotus.info';
-  // 学生信息url
-  config.stusDirImp = '/stud/enrollments';
-
-  config.studmaps = {
-    szyx: '院系',
-    szbj: '班级',
-    yxdm: '院校代码',
-    yxmc: '院校名称',
-    zydm: '专业代码',
-    zymc: '专业名称',
-    xh: '学号',
-    xm: '姓名',
-    sfzh: '身份证号',
-    xb: '性别',
-    xldm: '学历代码',
-    xl: '学历名称',
-    year: '毕业年份',
-    mz: '民族',
-    dhhm: '电话号码',
-    syszddm: '生源所在地代码',
-    syszd: '生源所在地',
-    zzmm: '政治面貌',
-  };
-
-  // axios service config
-  config.axios = {
-    stud: { // 学生服务
-      baseUrl: 'http://localhost:8101/api',
-    },
-  };
-
-  config.amqp = {
-    client: {
-      hostname: '127.0.0.1',
-      username: 'smart',
-      password: 'smart123',
-      vhost: 'smart',
-    },
-    app: true,
-    agent: true,
-  };
-
   // mongoose config
   config.mongoose = {
     url: 'mongodb://127.0.0.1:27017/smart',

+ 0 - 1
package.json

@@ -13,7 +13,6 @@
     "jsonwebtoken": "^8.5.1",
     "naf-framework-mongoose": "^0.6.11",
     "silly-datetime": "^0.1.2",
-    "xlsx": "^0.15.1"
   },
   "devDependencies": {
     "autod": "^3.1.0",