|
@@ -10,98 +10,83 @@ class UtilService extends CrudService {
|
|
|
this.mq = this.ctx.mq;
|
|
|
}
|
|
|
async utilMethod(query, body) {
|
|
|
- // let products = await this.ctx.model.Product.find();
|
|
|
- // let personals = await this.ctx.model.Personal.find();
|
|
|
- // let orgs = await this.ctx.model.Organization.find();
|
|
|
- // if (_.isArray(products)) products = JSON.parse(JSON.stringify(products));
|
|
|
- // if (_.isArray(personals)) personals = JSON.parse(JSON.stringify(personals));
|
|
|
- // if (_.isArray(orgs)) orgs = JSON.parse(JSON.stringify(orgs));
|
|
|
- // let count = 1;
|
|
|
- // for (const product of products) {
|
|
|
- // const { phone, contacts, _id } = product;
|
|
|
- // console.log(`${count}:${product.name}:${phone}-${contacts}`);
|
|
|
- // let r = personals.find(f => _.isEqual(f.phone, phone) || _.isEqual(f.name, contacts));
|
|
|
- // if (r) {
|
|
|
- // product.user_id = r._id;
|
|
|
- // } else {
|
|
|
- // r = orgs.find(f => _.isEqual(f.phone, phone) || _.isEqual(f.name, contacts));
|
|
|
- // if (r) product.user_id = r._id;
|
|
|
- // }
|
|
|
- // if (r) {
|
|
|
- // console.log(`u:${r.phone}-${r.name}:${product.user_id}-${r._id}`);
|
|
|
- // } else {
|
|
|
- // console.log(`${product.name} 没找到用户`);
|
|
|
- // }
|
|
|
- // count++;
|
|
|
- // await this.ctx.model.Product.updateOne({ _id: ObjectId(_id) }, product);
|
|
|
- // }
|
|
|
- // const path = 'C:\\Users\\liuruifeng\\Desktop\\temp\\organization.json';
|
|
|
- // let text = await fs.readFileSync(path, { encoding: 'utf-8' });
|
|
|
- // text = JSON.parse(text);
|
|
|
- // for (const i of text) {
|
|
|
- // i.passwd = _.cloneDeep(i.password);
|
|
|
- // delete i.password;
|
|
|
- // }
|
|
|
- // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\no.json', JSON.stringify(text));
|
|
|
-
|
|
|
-
|
|
|
- // const path = 'C:\\Users\\liuruifeng\\Desktop\\temp\\expert.json';
|
|
|
- // let text = await fs.readFileSync(path, { encoding: 'utf-8' });
|
|
|
- // text = JSON.parse(text);
|
|
|
- // const ids = text.map(i => i.user_id.$oid);
|
|
|
- // const users = await this.ctx.model.Personal.find({ _id: ids }, '+password');
|
|
|
- // for (const i of text) {
|
|
|
- // const r = users.find(f => ObjectId(f._id).equals(i.user_id.$oid));
|
|
|
- // if (r) {
|
|
|
- // const { addr, office_phone, profession, name, phone } = r;
|
|
|
- // if (name)i.name = name;
|
|
|
- // if (phone)i.phone = phone;
|
|
|
- // if (addr)i.addr = addr;
|
|
|
- // if (office_phone)i.office_phone = office_phone;
|
|
|
- // if (profession)i.profession = profession;
|
|
|
- // }
|
|
|
- // const { img_path } = i;
|
|
|
- // const arr = [];
|
|
|
- // if (_.isString(img_path)) {
|
|
|
- // arr.push({ name: 'icon', url: img_path });
|
|
|
- // }
|
|
|
- // i.img_path = arr;
|
|
|
- // delete i.user_id;
|
|
|
- // }
|
|
|
- // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\new-expert.json', JSON.stringify(text));
|
|
|
+ const Path = require('path');
|
|
|
+ const Excel = require('exceljs');
|
|
|
|
|
|
+ const { data } = await this.ctx.service.users.expert.query();
|
|
|
+ const root_path = 'E:\\exportFile\\';
|
|
|
+ const file_type = '';
|
|
|
+ if (!fs.existsSync(`${root_path}${file_type}`)) {
|
|
|
+ // 如果不存在文件夹,就创建
|
|
|
+ fs.mkdirSync(`${root_path}${file_type}`);
|
|
|
+ }
|
|
|
+ const workbook = new Excel.Workbook();
|
|
|
+ let sheet;
|
|
|
+ sheet = workbook.addWorksheet('sheet');
|
|
|
+ const meta = this.getHeader();
|
|
|
+ const head = meta.map(i => i.label);
|
|
|
+ // sheet.addRows(head);
|
|
|
+ const rows = [];
|
|
|
+ rows.push(head);
|
|
|
+ for (let i = 0; i < data.length; i++) {
|
|
|
+ const e = data[i];
|
|
|
+ const row = [];
|
|
|
+ let imgid;
|
|
|
+ for (const obj of meta) {
|
|
|
+ const { key } = obj;
|
|
|
+ if (key !== 'img_path') {
|
|
|
+ row.push(e[key] || '');
|
|
|
+ } else if (e.img_path) {
|
|
|
+ try {
|
|
|
+ const suffix = Path.extname(e.img_path).substring(1);
|
|
|
+ // 先请求图片buffer,然后addImage存起来
|
|
|
+ const res = await this.ctx.curl(`http://broadcast.waityou24.cn${e.img_path}`);
|
|
|
+ if (res.status === 200) {
|
|
|
+ const buffer = res.data;
|
|
|
+ imgid = workbook.addImage({
|
|
|
+ buffer,
|
|
|
+ extension: suffix,
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ console.log(`${e.name}图片下载失败`);
|
|
|
+ }
|
|
|
|
|
|
- // for (const obj of text) {
|
|
|
- // obj.channel_id = { $oid: obj.channel_id };
|
|
|
- // }
|
|
|
- // const path = 'C:\\Users\\liuruifeng\\Desktop\\temp\\export\\dock.json';
|
|
|
- // let text = await fs.readFileSync(path, { encoding: 'utf-8' });
|
|
|
- // text = JSON.parse(text);
|
|
|
- // const dockArr = [];
|
|
|
- // const dockUser = [];
|
|
|
- // for (const obj of text) {
|
|
|
- // const dock = _.pick(obj, [ '_id', 'room_id', 'password', 'title', 'start_time', 'end_time', 'province', 'place', 'adminuser', 'phone', 'sponsor', 'organizer', 'videodata', 'vipuser', 'desc', 'status' ]);
|
|
|
- // dockArr.push(dock);
|
|
|
- // const apply = _.get(obj, 'apply', []);
|
|
|
- // const { _id } = obj;
|
|
|
- // for (const a of apply) {
|
|
|
- // a.dock_id = _id;
|
|
|
- // a.user_id = { $oid: a.user_id };
|
|
|
- // dockUser.push(a);
|
|
|
- // }
|
|
|
- // }
|
|
|
- // // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\export\\dock_data.json', JSON.stringify(dockArr));
|
|
|
- // const eupath = 'C:\\Users\\liuruifeng\\Desktop\\temp\\export\\expert,s_user.json';
|
|
|
- // let eutext = await fs.readFileSync(eupath, { encoding: 'utf-8' });
|
|
|
- // eutext = JSON.parse(eutext);
|
|
|
- // console.log(eutext.length);
|
|
|
- // for (const obj of text) {
|
|
|
- // if (_.isObject(obj.password)) continue;
|
|
|
- // const { password } = obj;
|
|
|
- // obj.password = { secret: password, mech: 'plain', createdAt: { $date: '2020-09-27T07:48:05.643Z' }, updatedAt: { $date: '2020-09-27T07:48:05.643Z' } };
|
|
|
- // }
|
|
|
- // await fs.writeFileSync('C:\\Users\\liuruifeng\\Desktop\\temp\\export\\role-5-rewrite.json', JSON.stringify(text));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ rows.push(row);
|
|
|
+ if (imgid || imgid === 0) {
|
|
|
+ sheet.addImage(imgid, {
|
|
|
+ tl: { col: 15.2, row: i + 1 + 0.2 },
|
|
|
+ br: { col: 16, row: i + 1 + 1 },
|
|
|
+ editAs: 'oneCell',
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ sheet.addRows(rows);
|
|
|
+ const filepath = `${root_path}专家导出.xlsx`;
|
|
|
+ await workbook.xlsx.writeFile(filepath);
|
|
|
+ }
|
|
|
|
|
|
+ getHeader() {
|
|
|
+ return [
|
|
|
+ { key: 'name', label: '用户姓名' },
|
|
|
+ { key: 'phone', label: '联系电话' },
|
|
|
+ { key: 'education', label: '最高学历' },
|
|
|
+ { key: 'school', label: '毕业学校' },
|
|
|
+ { key: 'birthDate', label: '出生日期' },
|
|
|
+ { key: 'email', label: '电子邮箱' },
|
|
|
+ { key: 'qqwx', label: 'QQ/微信' },
|
|
|
+ { key: 'company', label: '工作单位' },
|
|
|
+ { key: 'zwzc', label: '职务职称' },
|
|
|
+ { key: 'expertise', label: '擅长领域' },
|
|
|
+ { key: 'workexperience', label: '工作经历' },
|
|
|
+ { key: 'scientific', label: '科研综述' },
|
|
|
+ { key: 'undertakingproject', label: '承担项目' },
|
|
|
+ { key: 'scienceaward', label: '科技奖励' },
|
|
|
+ { key: 'social', label: '社会任职' },
|
|
|
+ { key: 'img_path', label: '用户头像' },
|
|
|
+ ];
|
|
|
}
|
|
|
|
|
|
dealQuery(query) {
|