util.js 4.5 KB

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