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