util.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. const _ = require('lodash');
  3. const moment = require('moment');
  4. const { CrudService } = require('naf-framework-mongoose/lib/service');
  5. const { ObjectId } = require('mongoose').Types;
  6. const fs = require('fs');
  7. class UtilService extends CrudService {
  8. constructor(ctx) {
  9. super(ctx);
  10. this.mq = this.ctx.mq;
  11. }
  12. async utilMethod(query, body) {
  13. const Path = require('path');
  14. const Excel = require('exceljs');
  15. const { data } = await this.ctx.service.users.expert.query();
  16. const root_path = 'E:\\exportFile\\';
  17. const file_type = '';
  18. if (!fs.existsSync(`${root_path}${file_type}`)) {
  19. // 如果不存在文件夹,就创建
  20. fs.mkdirSync(`${root_path}${file_type}`);
  21. }
  22. const workbook = new Excel.Workbook();
  23. let sheet;
  24. sheet = workbook.addWorksheet('sheet');
  25. const meta = this.getHeader();
  26. const head = meta.map(i => i.label);
  27. // sheet.addRows(head);
  28. const rows = [];
  29. rows.push(head);
  30. for (let i = 0; i < data.length; i++) {
  31. const e = data[i];
  32. const row = [];
  33. let imgid;
  34. for (const obj of meta) {
  35. const { key } = obj;
  36. if (key !== 'img_path') {
  37. row.push(e[key] || '');
  38. } else if (e.img_path) {
  39. try {
  40. const suffix = Path.extname(e.img_path).substring(1);
  41. // 先请求图片buffer,然后addImage存起来
  42. const res = await this.ctx.curl(`http://broadcast.waityou24.cn${e.img_path}`);
  43. if (res.status === 200) {
  44. const buffer = res.data;
  45. imgid = workbook.addImage({
  46. buffer,
  47. extension: suffix,
  48. });
  49. }
  50. } catch (error) {
  51. console.log(`${e.name}图片下载失败`);
  52. }
  53. }
  54. }
  55. rows.push(row);
  56. if (imgid || imgid === 0) {
  57. sheet.addImage(imgid, {
  58. tl: { col: 15.2, row: i + 1 + 0.2 },
  59. br: { col: 16, row: i + 1 + 1 },
  60. editAs: 'oneCell',
  61. });
  62. }
  63. }
  64. sheet.addRows(rows);
  65. const filepath = `${root_path}专家导出.xlsx`;
  66. await workbook.xlsx.writeFile(filepath);
  67. }
  68. getHeader() {
  69. return [
  70. { key: 'name', label: '用户姓名' },
  71. { key: 'phone', label: '联系电话' },
  72. { key: 'education', label: '最高学历' },
  73. { key: 'school', label: '毕业学校' },
  74. { key: 'birthDate', label: '出生日期' },
  75. { key: 'email', label: '电子邮箱' },
  76. { key: 'qqwx', label: 'QQ/微信' },
  77. { key: 'company', label: '工作单位' },
  78. { key: 'zwzc', label: '职务职称' },
  79. { key: 'expertise', label: '擅长领域' },
  80. { key: 'workexperience', label: '工作经历' },
  81. { key: 'scientific', label: '科研综述' },
  82. { key: 'undertakingproject', label: '承担项目' },
  83. { key: 'scienceaward', label: '科技奖励' },
  84. { key: 'social', label: '社会任职' },
  85. { key: 'img_path', label: '用户头像' },
  86. ];
  87. }
  88. dealQuery(query) {
  89. return this.turnFilter(this.turnDateRangeQuery(query));
  90. }
  91. /**
  92. * 将查询条件中模糊查询的标识转换成对应object
  93. * @param {Object} filter 查询条件
  94. */
  95. turnFilter(filter) {
  96. const str = /^%\S*%$/;
  97. const keys = Object.keys(filter);
  98. for (const key of keys) {
  99. const res = key.match(str);
  100. if (res) {
  101. const newKey = key.slice(1, key.length - 1);
  102. filter[newKey] = new RegExp(filter[key]);
  103. delete filter[key];
  104. }
  105. }
  106. return filter;
  107. }
  108. /**
  109. * 将时间转换成对应查询Object
  110. * @param {Object} filter 查询条件
  111. */
  112. turnDateRangeQuery(filter) {
  113. const keys = Object.keys(filter);
  114. for (const k of keys) {
  115. if (k.includes('@')) {
  116. const karr = k.split('@');
  117. // 因为是针对处理范围日期,所以必须只有,开始时间和结束时间
  118. if (karr.length === 2) {
  119. const type = karr[1];
  120. if (type === 'start') {
  121. filter[karr[0]] = {
  122. ..._.get(filter, karr[0], {}),
  123. $gte: filter[k],
  124. };
  125. } else {
  126. filter[karr[0]] = {
  127. ..._.get(filter, karr[0], {}),
  128. $lte: filter[k],
  129. };
  130. }
  131. delete filter[k];
  132. }
  133. }
  134. }
  135. return filter;
  136. }
  137. }
  138. module.exports = UtilService;