util.js 5.5 KB

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