'use strict'; const _ = require('lodash'); const moment = require('moment'); const { CrudService } = require('naf-framework-mongoose-free/lib/service'); const { ObjectId } = require('mongoose').Types; const fs = require('fs'); class UtilService extends CrudService { constructor(ctx) { super(ctx); this.mq = this.ctx.mq; } async utilMethod(query, body) { // console.log(this.mq); this.ctx.service.patent.patentinfo.export({ query: { apply_personal: '长春' } }); // return res; } async expertExport() { 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}图片下载失败`); } } } 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) { return this.turnFilter(this.turnDateRangeQuery(query)); } /** * 将查询条件中模糊查询的标识转换成对应object * @param {Object} filter 查询条件 */ turnFilter(filter) { const str = /^%\S*%$/; const keys = Object.keys(filter); for (const key of keys) { const res = key.match(str); if (res) { const newKey = key.slice(1, key.length - 1); filter[newKey] = new RegExp(filter[key]); delete filter[key]; } } return filter; } /** * 将时间转换成对应查询Object * @param {Object} filter 查询条件 */ turnDateRangeQuery(filter) { const keys = Object.keys(filter); for (const k of keys) { if (k.includes('@')) { const karr = k.split('@'); if (karr.length === 2) { const type = karr[1]; if (type === 'start') { if (filter[k] && filter[k] !== '') { filter[karr[0]] = { ..._.get(filter, karr[0], {}), $gte: filter[k], }; } } else { if (filter[k] && filter[k] !== '') { filter[karr[0]] = { ..._.get(filter, karr[0], {}), $lte: filter[k], }; } } delete filter[k]; } } } return filter; } } module.exports = UtilService;