瀏覽代碼

个人用户全部导出

lrf 3 年之前
父節點
當前提交
74f164ba84
共有 5 個文件被更改,包括 90 次插入6 次删除
  1. 3 0
      app/controller/users/.personal.js
  2. 1 0
      app/router/users/personal.js
  3. 80 0
      app/service/users/personal.js
  4. 2 2
      config/config.default.js
  5. 4 4
      config/plugin.js

+ 3 - 0
app/controller/users/.personal.js

@@ -114,4 +114,7 @@ module.exports = {
   import: {
   import: {
     requestBody: ['uri'],
     requestBody: ['uri'],
   },
   },
+  export: {
+    requestBody: [],
+  },
 };
 };

+ 1 - 0
app/router/users/personal.js

@@ -7,6 +7,7 @@ module.exports = app => {
   const vision = 'v0';
   const vision = 'v0';
   const index = 'users';
   const index = 'users';
   const target = 'personal';
   const target = 'personal';
+  router.post(target, `${profix}${vision}/${index}/${target}/export`, controller[index][target].export);
   router.post(target, `${profix}${vision}/${index}/${target}/import`, controller[index][target].import);
   router.post(target, `${profix}${vision}/${index}/${target}/import`, controller[index][target].import);
   router.post(target, `${profix}${vision}/${index}/${target}/upgrade`, controller[index][target].upgrade);
   router.post(target, `${profix}${vision}/${index}/${target}/upgrade`, controller[index][target].upgrade);
   router.post(target, `${profix}${vision}/${index}/${target}/login`, controller[index][target].login);
   router.post(target, `${profix}${vision}/${index}/${target}/login`, controller[index][target].login);

+ 80 - 0
app/service/users/personal.js

@@ -6,6 +6,8 @@ const _ = require('lodash');
 const jwt = require('jsonwebtoken');
 const jwt = require('jsonwebtoken');
 const assert = require('assert');
 const assert = require('assert');
 const Excel = require('exceljs');
 const Excel = require('exceljs');
+const { sep } = require('path');
+const fs = require('fs');
 
 
 // 个人用户
 // 个人用户
 class PersonalService extends CrudService {
 class PersonalService extends CrudService {
@@ -154,6 +156,84 @@ class PersonalService extends CrudService {
       { column: '审核状态', key: 'status' },
       { column: '审核状态', key: 'status' },
     ];
     ];
   }
   }
+
+  async export() {
+    // 查询位置
+    let skip = 0;
+    // 一次处理多少条数据
+    const limit = 500;
+    const total = await this.model.count({ isdel: 0 });
+    // 循环次数
+    const times = _.ceil(_.divide(total, limit));
+    const nowDate = new Date().getTime();
+    // 文件 名称 及 路径
+    const filename = `个人用户导出-${nowDate}.xlsx`;
+    const root_path = _.get(this.ctx.app.config.export, 'root_path');
+    if (!fs.existsSync(`${root_path}`)) {
+      // 如果不存在文件夹,就创建
+      fs.mkdirSync(`${root_path}`);
+    }
+    const excel_path = `${sep}excel${sep}`;
+    const path = `${root_path}${excel_path}`;
+    if (!fs.existsSync(path)) {
+      // 如果不存在文件夹,就创建
+      fs.mkdirSync(path);
+    }
+    // 流式写入
+    const options = {
+      filename: `${path}${filename}`,
+    };
+    const workbook = new Excel.stream.xlsx.WorkbookWriter(options);
+    const sheet = workbook.addWorksheet('sheet');
+    const eMeta = this.exportMeta();
+    const eColumn = eMeta.map((i) => i.key);
+    let needDeal = eMeta.map((i, index) => {
+      if (_.isFunction(_.get(i, 'method'))) {
+        i.index = index;
+        return i;
+      }
+    });
+    needDeal = _.compact(needDeal);
+    for (let i = 1; i <= times; i++) {
+      if (i === 1) {
+        const head = eMeta.map((i) => i.zh);
+        sheet.addRow(head).commit();
+      }
+      const data = await this.model.find({ isdel: 0 }, '+password').skip(skip).limit(limit);
+      for (const d of data) {
+        const dup = Object.values(_.pick(d, eColumn));
+        // 处理特殊情况
+        for (const nd of needDeal) {
+          const { index, method } = nd;
+          dup[index] = method(dup[index]);
+        }
+        // 添加数据
+        sheet.addRow(dup).commit();
+      }
+      skip += limit;
+    }
+    sheet.commit();
+    await workbook.commit();
+    return `/files/excel/${filename}`;
+  }
+
+  exportMeta() {
+    return [
+      { key: 'name', zh: '用户名' },
+      { key: 'phone', zh: '手机号' },
+      { key: 'password', zh: '登录密码', method: (i) => _.get(i, 'secret') },
+      { key: 'code', zh: '邀请码' },
+      { key: 'card', zh: '身份证号' },
+      { key: 'email', zh: '邮箱' },
+      { key: 'addr', zh: '地址' },
+      { key: 'office_phone', zh: '办公电话' },
+      { key: 'juris', zh: '所属辖区' },
+      { key: 'school', zh: '院系' },
+      { key: 'major', zh: '专业' },
+      { key: 'zwzc', zh: '职务职称' },
+      { key: 'status', zh: '审核状态' },
+    ];
+  }
 }
 }
 
 
 module.exports = PersonalService;
 module.exports = PersonalService;

+ 2 - 2
config/config.default.js

@@ -73,11 +73,11 @@ module.exports = appInfo => {
   };
   };
 
 
   config.export = {
   config.export = {
-    root_path: 'E:\\exportFile',
+    root_path: 'D:\\temp',
     domain: 'http://broadcast.waityou24.cn',
     domain: 'http://broadcast.waityou24.cn',
   };
   };
   config.import = {
   config.import = {
-    root_path: 'E:\\exportFile',
+    root_path: 'D:\\temp',
     // root_path: 'D:\\free\\workspace\\server\\service-file\\upload',
     // root_path: 'D:\\free\\workspace\\server\\service-file\\upload',
   };
   };
   config.project = {
   config.project = {

+ 4 - 4
config/plugin.js

@@ -5,8 +5,8 @@
 //   enable: true,
 //   enable: true,
 //   package: 'egg-naf-amqp',
 //   package: 'egg-naf-amqp',
 // };
 // };
-// exports.redis = {
-//   enable: true,
-//   package: 'egg-redis',
-// };
+exports.redis = {
+  enable: true,
+  package: 'egg-redis',
+};