util.js 4.6 KB

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