util.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. 'use strict';
  2. const _ = require('lodash');
  3. const fs = require('fs');
  4. const Excel = require('exceljs');
  5. const { CrudService } = require('naf-framework-mongoose/lib/service');
  6. const { BusinessError, ErrorCode } = require('naf-core').Error;
  7. const moment = require('moment');
  8. const nodemailer = require('nodemailer');
  9. const docx = require('docx');
  10. const archiver = require('archiver');
  11. const { ObjectId } = require('mongoose').Types;
  12. class UtilService extends CrudService {
  13. constructor(ctx) {
  14. super(ctx);
  15. this.mq = this.ctx.mq;
  16. }
  17. async updatedate() {
  18. let date = new Date();
  19. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  20. return date;
  21. }
  22. async sendMail(email, subject, text, html) {
  23. const setting = await this.ctx.model.Setting.findOne();
  24. let user_email = this.ctx.app.config.user_email;
  25. let auth_code = this.ctx.app.config.auth_code;
  26. if (setting) {
  27. user_email = setting.user_email;
  28. auth_code = setting.auth_code;
  29. }
  30. const transporter = nodemailer.createTransport({
  31. host: 'smtp.exmail.qq.com',
  32. secureConnection: true,
  33. port: 465,
  34. auth: {
  35. user: user_email, // 账号
  36. pass: auth_code, // 授权码
  37. },
  38. });
  39. if (process.env.NODE_ENV === 'development') email = '402788946@qq.com';
  40. const mailOptions = {
  41. from: user_email, // 发送者,与上面的user一致
  42. to: email, // 接收者,可以同时发送多个,以逗号隔开
  43. subject, // 标题
  44. text, // 文本
  45. html,
  46. };
  47. try {
  48. await transporter.sendMail(mailOptions);
  49. return true;
  50. } catch (err) {
  51. return false;
  52. }
  53. }
  54. async findone({ modelname }, data) {
  55. // 查询单条
  56. const _model = _.capitalize(modelname);
  57. const res = await this.ctx.model[_model].findOne({ ...data });
  58. return res;
  59. }
  60. async findbyids({ modelname }, { data }) {
  61. // 共通批量查询方法
  62. const _model = _.capitalize(modelname);
  63. const res = [];
  64. for (const elm of data) {
  65. const result = await this.ctx.model[_model].findById(elm);
  66. res.push(result);
  67. }
  68. return res;
  69. }
  70. async findmodel({ modelname }) {
  71. const _model = _.capitalize(modelname);
  72. const data = this.ctx.model[_model].prototype.schema.obj;
  73. const keys = Object.keys(data);
  74. const res = {};
  75. for (const k of keys) {
  76. const obj = data[k];
  77. if (_.get(obj, 'zh')) res[k] = obj;
  78. }
  79. return res;
  80. }
  81. async utilMethod(query, body) {
  82. const crypto = require('crypto');
  83. const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
  84. modulusLength: 2048,
  85. publicKeyEncoding: {
  86. type: 'spki',
  87. format: 'pem',
  88. },
  89. privateKeyEncoding: {
  90. type: 'pkcs8',
  91. format: 'pem',
  92. cipher: 'aes-256-cbc',
  93. passphrase: 'sks_secret',
  94. },
  95. });
  96. console.log(publicKey);
  97. console.log('-------------------');
  98. console.log(privateKey);
  99. // const privateKey = `-----BEGIN ENCRYPTED PRIVATE KEY-----
  100. // MIIJrTBXBgkqhkiG9w0BBQ0wSjApBgkqhkiG9w0BBQwwHAQI+SbyJ6RzQY8CAggA
  101. // MAwGCCqGSIb3DQIJBQAwHQYJYIZIAWUDBAEqBBACELvWKbd/L0zagGXmrwrKBIIJ
  102. // UBn774mFtXJgWi9NVh+GO5CpcqDxU2JjP+lOILG0k9EIJiwVBH++LHtJX4S9Y7MP
  103. // qtJQZBLSdyjh7mzMige/xTSEkF2wGactP1QE2imfcT52NoHGQVyeFBfa+ZEZixPU
  104. // h/6uqS8TjABU561I3A/89vHYLrZ9va1T8AZTp4NbyUaD5zYCPUG+mn9Ln9PO56Bk
  105. // HEUXfZa2qGU819CxC2Ju4T/KMLDXHKVbbCiDnB1vLxuO4vSd8719nwR9czfTkGKv
  106. // MJtk7wbFUrstmEVFBM931D6wNT/BHtYGCf2xw10CwtSjLKbB/iINqH5CUYMjA6H8
  107. // 4ts7nBHkmkuRa5bGZSHeCR25fLE6i/WU0mAB4gnaU3nSmInA2JF5QFKV/2+RsZBN
  108. // DCAU8TwjnsY4dsqwC0CjUPY3EguiuXAVZ3eQO43ERIVuK6flfY1vaK1186KAfLf9
  109. // 9dYMcoag8ohs22etBiFVQZMy8YTC7knndyMyP+c/ohgENzUc1lPVHj1n0kGW2Mgu
  110. // Gc4mLXTmifr3SM/Z5cUI97i/OZ1aYm01G2UiR2ZLLdLSwuBoqs2J1r5Xk97BqaPm
  111. // f93PgENfafd7Sd+RH1f635FMKM61NbZBzNMDJmKW1B4RgezLgxanJy5AqJNGfiuS
  112. // 9m0daXKPEhAhh+Hx5LEYXxuSVgaSAJtvOZze5yHA/kHEy5+DZ5Gv31brEVHmAphA
  113. // EFSdtDbf44hqGMk2LAeiahkqc+MXz+RVFAvuh24qR8NeulIszu5ExHM13ZVL1GIP
  114. // IqnplKnX/Xjt8j1CJOxgiHJvqcX3LAg0NDh0CbmpSJVjjL/yqxwYMBSiLgS8ViT+
  115. // AaGqLvuT95Oyq7IiMDngymTMT4ilWjeY+ovFWC3JxwCL8kzFVFBL680JhOlvBVft
  116. // e9+JbJeV3xAq482FolBRXla6Eqe1eeFVjmY8+n/UqepT8CLk7hJxzN0VCVJsx7VT
  117. // wlBjRobcIdaLtwJllu7Ebpc2MwLVgPJvvNHigLJVNW5SyHhleT2pJZDROgm+0itc
  118. // 39L8MN5Jo/q/wqO1KO+Fx17xNiMhQr7rs+9zcy8a1/D8v0zmNbpayWx8z0V6TyYJ
  119. // 2QhQAPuO7mnxsbzZPEnj93+2ifoojU3YOgvxQCUR4LNbreScSotJmX8ADi9rpmgp
  120. // VxSUSpZ9smzX2kVMPQPWCOuNzuCJIGPmwI1+2wSGgYkPW1hjVReQTevrIt6YMKxh
  121. // 7NuiJ9Hzi96uGAlcfPPp85wR2NUGJ+++lu6874AHa4nfwrxBqps/e/IFgbxfOXoo
  122. // ZS1thHfViSlPeV6+gv2OdBb6mZbMlH2UWjKuXmvYPSKBy/PA3Kl9gyYbOnpBb7Cz
  123. // Bt0jpx0oJtWmndzXpRRDOj/ehppSe1tbhe3D1Eg1EOn3C0IS284dQ2JzMATbdxV3
  124. // okrKYcIxuYvCVNjgAo+GdfC+V3A4c15HlPjYsgy8hLIZGWbfFvNIFPNw7LmxAG6w
  125. // UD+EAFKtijRV/vKjMraiVfaB8LQ+QLgBkgE+8JFRkFUmwzCWB2KoewHPUbk+7/a1
  126. // 7PQx0+sk+ghIb6lkC0EOcAFjfDiQYJttZ/ax3txR76hHNHOcMKeFp5DgwBzlvllR
  127. // BHKRDHflyr6CGf9gTMw83xwHp9HCpz/BleBLvoMd23FZ/BXkHlBWUusfLlUj+iNp
  128. // uQuM/xXNZ9JTixibCcQzC5KFJsOcnGWkoID/nT22iR4G2nqivHcbq3GM/s+Ty9zQ
  129. // q7Pot2cCkPOSuacyuZxrBsWmSGVibMnYEDcWs4N3Vsgw+hrk886VLDNEt7bDwy83
  130. // KQflDKNSwEeav6NceWywmMWP6lQQoCPQtuUxdBpJ/vanrEOU8Tg5/PNiqnqtqtN2
  131. // Z6g3ZNyGFaYXkzxizznrIaH81HUmC5wVHmozzH7FiLqIt2yj+IbOBD9T4KiarnQX
  132. // zsgR+eYbHUO7wzOvlMj0O3n1jMHK01gBe5IlGcPW/n4/mUp8qACgVipKhO45NxL8
  133. // u9+BcPoBNCFD23sCnjUHChGHEhfNyQutjXyFUzmIkni4M5idudgri7KDHAdt/Sid
  134. // 6X2/R+b/p5zK9DqqtaEeRyQutzBJsCHaup9w46ySuen+NdfbCzITvaDpOpQJua9x
  135. // 4R+YEE6h31tlp13whR3wKNs7nrpS9oRQo3tXKIzDSXYZM81DuSR1fJ+6H9yAfkg5
  136. // 4ScerEFSU+nvwzx5Gpj8AfDQHKHOAb6uGjGMp2bDhIWqqYJZS5JkcqF3CNr7C6EE
  137. // A8iGy/QisX2cxquT2uKjjoIVSVVQKjrV4LI3f13bHEJ5I6RBrSWNic7WAKBkjxTH
  138. // OPtuz2cgL5KAs/hvhMqVaEmeGvC9mW0jy3S0rFFMZapb8OFfFM2fEVNNzMvVy2Gd
  139. // RAZMPY4hzyqHzlLDUOF8fGa+Tly6cG0Rjvt+EsGy7w3yhR4FEUD71T2Hdh4Qlj+f
  140. // J1TDw6uEHFoYZQB9mRM+b7zDZX6Q7f4Yqk8Zp4bmlvdvoPtFS8ai8qosHDKZfuJe
  141. // 5yRQhx7GUL6se2kzcb12ZvHWdq4i9DRZ9mamcr4lnqWc1lWF/yPxow42fcGEYiSn
  142. // fg458CL16dV2JMrlNI67KX/oxKQbtI8Qqow9GA0xHT9APeRHDwQH9hhPE+I9nzjk
  143. // vXiKSk1CyQcMFJv63Jelq9v9SlM4OhbLM6BnBMCc5UqZ5y7plN+j+1y07+TVB+qE
  144. // 1b8WXHKkWNm1PyUkX57amLjHzvk/z7yIAWQV35ZuZe0Hq65QfNuG8T6aMTNEUEVj
  145. // ciuYB1KM5XhtTJvn/EyDktpCMvPF5X++uT6Pq2dbHUSVng/JHWm8qOStBnzosvHR
  146. // Ebmw4uSELfqrIONBid3rUNJ0HuLhm9IcSspj+kmAkumOjX8tQ/GER/3DKs7phjir
  147. // sCAQBf9BxnJUICP8jbr+OYXfJs6FsPNrtEZOg+78BdFQpWGgLptUWRTjHUPR3bb2
  148. // J2i7ADAgW2t6eh+bhJ5JyDQ0xqGRVx57mHOvAlKkaa/GNMVexTqEH4eybilsQCpY
  149. // jEtwj9SJMe5jD3J4hzAQUaeOfyruuZ2R2nC1C/070TRL6qXf6DNnWeXiYIOuYW36
  150. // ere7RirrD3vwbgu1jzsixYgGSRXLYis42brchhzw7+9Ut3vvQCMc03aO3fe+6Aji
  151. // WZo3nW1v3vEf3fS4fFtQF7VP1a2A0Leawv7ZxXUS2A8S
  152. // -----END ENCRYPTED PRIVATE KEY-----`;
  153. // const publicKey = `-----BEGIN PUBLIC KEY-----
  154. // MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAzGSOl6+oYHZyHRzJYtC3
  155. // wlqczFs2QKHLi6bnQj1Sb6J4Kx0UkJqb2yJpyFenBTjW+W4rMM7bt9RzM4pHqjWI
  156. // ubTDFL8aU8R1LxOI/9HqpC9CTyZpmGjrCZoJz07sMz/RNo6e8nwpAdEN3Z8sSaGD
  157. // Wm+Jv8uk8NtXDksyXa9b1aAylxMCIu+yg1V+QvAqfxFW2HHQodjE1/8/QNMqPr90
  158. // DoCS35atn5vl6XQyHY40jk5r6it/zr7IBkuZnPJHSL3Mcvf2lZXYSg4Q2TQ9q4G5
  159. // zx4UsYK3oPI/bP4+KK2o+nmdHjP+YlWpgIjMOZ7bYBu90APHUaRskRAwbruBdN0v
  160. // UQuUvrZ+ePptXBwvqDycCwF0VVYSUKWC0rCkk+zGNiramsILRGEbkHZ0OCIW6vs1
  161. // aijKiUbDLVFXRjzwKFlrczt0DszEZDvBbVU6aaia1fvHoMTa9Qh48IL7czncgwc+
  162. // 7n8IxyiE69PAB/Y54AoNbANsAjbHJKL5POE3g4BfJfvzGStTGHT08tdMwfyWlKa4
  163. // V1f9/W1PiPvr018oTyEpjYuitA1AsslJWce2PysVQc5w4EVJHMSz9feqNYv8eFSD
  164. // rEXSHLXwtK0TK8asdX13zi2klGe6zpkejAuLVq+PUbMV6RzTLue5qlOp/pwGuVFM
  165. // Z9vYX8dtRkmfeDitSjRTpEkCAwEAAQ==
  166. // -----END PUBLIC KEY-----`;
  167. const data = { code: '1', org: '2', type: '中心管理员' };
  168. const enc = crypto.publicEncrypt(publicKey, Buffer.from(JSON.stringify(data)));
  169. const str = enc.toString('base64');
  170. console.log(str);
  171. const sb = Buffer.from(str, 'base64');
  172. const decpub = crypto.privateDecrypt(
  173. {
  174. key: privateKey,
  175. passphrase: 'sks_secret',
  176. },
  177. sb
  178. );
  179. console.log(`decpub:${decpub.toString()}`);
  180. // for (const trainPlan of allTranPlan) {
  181. // const { termnum = [] } = trainPlan;
  182. // const termids = termnum.map(f => ObjectId(f._id).toString());
  183. // // 查出该期所有的申请
  184. // const termApplys = await applyModel.find({ termid: termids }).lean();
  185. // const needDeletes = termApplys.filter(f => !(allSubject.find(s => s._id === f.subid) && allTeacher.find(t => t._id === f.teacherid)));
  186. // const deleteIds = needDeletes.map(i => i._id);
  187. // console.log(deleteIds.length);
  188. // // await applyModel.deleteMany({ _id: deleteIds });
  189. // }
  190. // // // 重置班级
  191. // // 找到要删除的学生
  192. // const res = await this.ctx.model.Student.find({ school_name: '吉林职业技术学院', planid: '60769b703cead37068645fb2', termid: { $ne: '60779c0b2ec7ac704ce301ab' } });
  193. // // const test = res.map(f => {
  194. // // return { name: f.name, openid: f.openid, id: f._id };
  195. // // });
  196. // const ids = res.map(i => i._id);
  197. // await this.ctx.model.User.deleteMany({ uid: ids });
  198. // await this.ctx.model.Student.deleteMany({ school_name: '吉林职业技术学院', planid: '60769b703cead37068645fb2', termid: { $ne: '60779c0b2ec7ac704ce301ab' } });
  199. // const ids = res.map(i => i.openid);
  200. // // console.log(ids);
  201. //
  202. // console.log(userRes.length);
  203. // const { planid, termid, batchid, classid } = query;
  204. // const filters = {};
  205. // if (classid) filters.classid = classid;
  206. // else if (batchid)filters.batchid = batchid;
  207. // else if (termid)filters.termid = termid;
  208. // else if (planid)filters.planid = planid;
  209. // else throw new BusinessError(ErrorCode.BADPARAM, '需要重置范围');
  210. // await this.ctx.model.Student.updateMany(filters, { classid: undefined });
  211. // // 重置班级结束
  212. }
  213. async getQueryOptions({ skip, limit, sort, desc } = {}) {
  214. if (sort && _.isString(sort)) {
  215. sort = { [sort]: desc ? -1 : 1 };
  216. } else if (sort && _.isArray(sort)) {
  217. sort = sort.map(f => ({ [f]: desc ? -1 : 1 })).reduce((p, c) => ({ ...p, ...c }), {});
  218. }
  219. return { skip, limit, sort };
  220. }
  221. /**
  222. * 更新进度,状态 不存在/完成以外(不是2的值)的数值, 不添加参数,只更新进度
  223. * @param {String} missionid 任务id
  224. * @param {String} progress 进度(人为划分)
  225. * @param {String} status 状态
  226. * @param {Object} params 参数
  227. */
  228. async updateProcess(missionid, progress, status, params) {
  229. try {
  230. if (!missionid) return;
  231. const { baseUrl } = _.get(this.ctx.app.config, 'mission');
  232. let url = `${baseUrl}`;
  233. let data;
  234. if (!baseUrl) return;
  235. if (!status || status !== '2') {
  236. url = `${url}/api/mission/progress`;
  237. data = { id: missionid, progress };
  238. } else if (status === '3') {
  239. url = `${url}/api/mission/update/${missionid}`;
  240. data = { status };
  241. } else {
  242. url = `${url}/api/mission/update/${missionid}`;
  243. data = { progress: '100', params, status };
  244. }
  245. await this.ctx.curl(url, {
  246. method: 'post',
  247. headers: {
  248. 'content-type': 'application/json',
  249. },
  250. data,
  251. dataType: 'json',
  252. });
  253. } catch (error) {
  254. console.error(`任务更新进度报错,missionid:${missionid}\n`);
  255. }
  256. }
  257. async teacherImport() {
  258. // const filepath = './teacherlist.xlsx';
  259. // const workbook = new Excel.Workbook();
  260. // await workbook.xlsx.readFile(filepath);
  261. // const worksheet = workbook.getWorksheet(1);
  262. // if (!worksheet) return;
  263. // let arr = [];
  264. // worksheet.eachRow((row, ri) => {
  265. // if (ri !== 1) {
  266. // const obj = {};
  267. // obj.name = row.getCell(3).value || undefined;
  268. // obj.department = row.getCell(4).value || undefined;
  269. // if (row.getCell(5).value) obj.job = row.getCell(5).value;
  270. // obj.phone = row.getCell(6).value || undefined;
  271. // obj.status = '4';
  272. // arr.push(obj);
  273. // }
  274. // });
  275. // // 检查谁生成过了, user表和teacher表
  276. // let ur = await this.ctx.model.User.find({ mobile: { $in: arr.map(i => i.phone) }, type: '3' });
  277. // let tr = await this.ctx.model.Teacher.find({ phone: { $in: arr.map(i => i.phone) } });
  278. // // 将有的老师过滤出去
  279. // if (ur) {
  280. // ur = JSON.parse(JSON.stringify(ur));
  281. // arr = arr.filter(f => !ur.find(uf => `${uf.mobile}` === `${f.phone}`));
  282. // }
  283. // if (tr) {
  284. // tr = JSON.parse(JSON.stringify(tr));
  285. // arr = arr.filter(f => !(tr.find(tf => `${tf.phone}` === `${f.phone}`)));
  286. // }
  287. // for (const tea of arr) {
  288. // const ctr = await this.ctx.model.Teacher.create(tea);
  289. // if (ctr) {
  290. // const obj = { name: tea.name, mobile: tea.phone, type: '3', uid: ctr._id };
  291. // const cur = await this.ctx.model.User.create(obj);
  292. // }
  293. // }
  294. // const user = await this.ctx.model.User.find({ passwd: { $exists: false } });
  295. // for (const u of user) {
  296. // u.passwd = { secret: '12345678' };
  297. // u.save();
  298. // }
  299. }
  300. /**
  301. * 导出excel
  302. * @param {Array} dataList 数据集合
  303. * @param {Array} meta 表头(可以没有)
  304. * @param {String} fn 文件名
  305. * @param {Array} opera 单元格操作
  306. */
  307. async toExcel(dataList, meta, fn = '导出结果', opera) {
  308. // 导出excel
  309. const { app } = this;
  310. const nowDate = new Date().getTime();
  311. const filename = `${fn}-${nowDate}.xlsx`;
  312. // 取出预设存储地址
  313. const rootPath = `${app.config.cdn.repos_root_path}`;
  314. const rooturl = `${app.config.cdn.repos_root_url_excel}`;
  315. const path = `${rootPath}${rooturl}`;
  316. if (!path) {
  317. throw new BusinessError(ErrorCode.BUSINESS, '服务端没有设置存储路径');
  318. }
  319. // if (process.env.NODE_ENV === 'development') path = 'E:\\exportFile\\';
  320. if (!fs.existsSync(path)) {
  321. // 如果不存在文件夹,就创建
  322. fs.mkdirSync(path);
  323. }
  324. // 生成文件
  325. const filepath = `${path}${filename}`;
  326. fs.createWriteStream(filepath);
  327. const workbook = new Excel.Workbook();
  328. const sheet = workbook.addWorksheet('sheet');
  329. if (meta) sheet.columns = meta;
  330. sheet.addRows(dataList);
  331. if (_.isArray(opera)) {
  332. for (const o of opera) {
  333. const { startRow, startCol, endRow, endCol } = o;
  334. sheet.mergeCells(startRow, startCol, endRow, endCol);
  335. }
  336. }
  337. // 垂直居中
  338. const length = dataList.length;
  339. const alignment = { vertical: 'middle', horizontal: 'center' };
  340. for (let i = 1; i <= length; i++) {
  341. sheet.getRow(i).alignment = alignment;
  342. }
  343. await workbook.xlsx.writeFile(filepath);
  344. return `/files/excel/${filename}`;
  345. }
  346. /**
  347. * 创建/重复写入excel
  348. * @param {Array} dataList 数据
  349. * @param {String} fn 文件名, 第一次进入时,只是单纯的名,第二次开始,有后缀与时间戳
  350. * @param {String} downloadPath 下载文件路径
  351. * @param {String} type 方向,没有默认横向
  352. */
  353. async toAsyncExcel(dataList, fn = '导出结果', downloadPath, type) {
  354. const { app } = this;
  355. const workbook = new Excel.Workbook();
  356. let sheet;
  357. // 取出预设存储地址
  358. const rootPath = `${app.config.cdn.repos_root_path}`;
  359. const rooturl = `${app.config.cdn.repos_root_url_excel}`;
  360. let path = `${rootPath}${rooturl}`;
  361. let filepath = '';
  362. if (!path) {
  363. throw new BusinessError(ErrorCode.BUSINESS, '服务端没有设置存储路径');
  364. }
  365. if (process.env.NODE_ENV === 'development') path = 'E:\\exportFile\\excel\\';
  366. if (!fs.existsSync(path)) {
  367. // 如果不存在文件夹,就创建
  368. fs.mkdirSync(path);
  369. }
  370. if (!downloadPath) {
  371. // 第一次进入,文件还未生成
  372. const nowDate = new Date().getTime();
  373. fn = `${fn}-${nowDate}.xlsx`;
  374. sheet = workbook.addWorksheet('sheet');
  375. } else {
  376. let domain = 'http://127.0.0.1';
  377. if (process.env.NODE_ENV === 'development') domain = `${domain}:8000`;
  378. const file = await this.ctx.curl(`${domain}${downloadPath}`);
  379. if (!(file && file.data)) {
  380. throw new BusinessError(ErrorCode.DATA_NOT_EXIST, '未找导出的excel');
  381. }
  382. // 读取文件
  383. await workbook.xlsx.load(file.data);
  384. sheet = workbook.getWorksheet('sheet');
  385. }
  386. if (!type || type === 'horizontal') sheet.addRows(dataList);
  387. else if (type === 'vertical') {
  388. for (let i = 1; i <= dataList.length; i++) {
  389. const element = dataList[i - 1];
  390. const rows = sheet.getRow(i);
  391. if (rows.values.length <= 0) rows.values = element;
  392. else rows.values = rows.values.concat(element);
  393. rows.commit();
  394. }
  395. }
  396. filepath = `${path}${fn}`;
  397. await workbook.xlsx.writeFile(filepath);
  398. return { downloadPath: `/files/excel/${fn}`, fn };
  399. }
  400. /**
  401. * 导出docx
  402. * @param {Array} data 数据[{title,content([]),author}]
  403. * @param {String} fn 文件名
  404. */
  405. async toDocx(data, fn = '培训心得') {
  406. const { Document, Packer, Paragraph, TextRun, HeadingLevel, AlignmentType } = docx;
  407. const doc = new Document();
  408. const children = [];
  409. // 整理数据
  410. for (let i = 0; i < data.length; i++) {
  411. const obj = data[i];
  412. const { title, content, author } = obj;
  413. const c = [];
  414. if (title) {
  415. const tit = new Paragraph({
  416. children: [ new TextRun({ text: title, bold: true }) ],
  417. heading: HeadingLevel.TITLE,
  418. alignment: AlignmentType.CENTER,
  419. });
  420. c.push(tit);
  421. }
  422. if (author) {
  423. const auth = new Paragraph({
  424. children: [ new TextRun({ color: '#000000', text: author }) ],
  425. heading: HeadingLevel.HEADING_2,
  426. alignment: AlignmentType.RIGHT,
  427. });
  428. c.push(auth);
  429. }
  430. if (content && _.isArray(content) && content.length > 0) {
  431. for (const cont of content) {
  432. const p = new Paragraph({
  433. children: [ new TextRun({ text: cont, bold: true }) ],
  434. });
  435. c.push(p);
  436. }
  437. }
  438. if (i !== data.length - 1) {
  439. // 换页
  440. const last = new Paragraph({
  441. pageBreakBefore: true,
  442. });
  443. c.push(last);
  444. }
  445. if (c.length > 0) children.push(...c);
  446. }
  447. doc.addSection({
  448. properties: {},
  449. children,
  450. });
  451. const { app } = this;
  452. const rootPath = `${app.config.cdn.repos_root_path}`;
  453. const rooturl = `${app.config.cdn.repos_root_url_experience}`;
  454. const path = `${rootPath}${rooturl}`;
  455. // 如果不存在文件夹,就创建
  456. // if (process.env.NODE_ENV === 'development') path = 'E:\\exportFile\\';
  457. if (!fs.existsSync(path)) {
  458. fs.mkdirSync(path);
  459. }
  460. const num = new Date().getTime();
  461. const buffer = await Packer.toBuffer(doc);
  462. fs.writeFileSync(`${path}${fn}-${num}.docx`, buffer);
  463. // Packer.toBuffer(doc).then(buffer => {
  464. // fs.writeFileSync(`${path}${fn}-${num}.docx`, buffer);
  465. // });
  466. return `/files${rooturl}${fn}-${num}.docx`;
  467. }
  468. /**
  469. * 将选择的文件导出到zip压缩包中,提供下载
  470. * @param {*} fileList 需要导入到zip中的列表,格式有2中: [{url:""}]或[String]
  471. * @param {*} fn 文件名,默认为 "导出结果"
  472. */
  473. async toZip(fileList, fn = '导出结果') {
  474. if (!_.isArray(fileList)) {
  475. throw new BusinessError(ErrorCode.DATA_INVALID, '需要压缩的文件数据格式错误');
  476. }
  477. fn = `${fn}.zip`;
  478. // zip文件夹创建
  479. const { app } = this;
  480. const rootPath = `${app.config.cdn.repos_root_path}`;
  481. const zipPath = `${app.config.cdn.repos_root_url_zip}`;
  482. let path = `${rootPath}${zipPath}`;
  483. if (process.env.NODE_ENV === 'development') path = 'E:\\exportFile\\';
  484. if (!fs.existsSync(path)) {
  485. fs.mkdirSync(path);
  486. }
  487. // 文件请求后将数据整理到这里
  488. const resetFileList = [];
  489. for (const file of fileList) {
  490. let uri = '';
  491. let filename = '';
  492. let prefixs;
  493. if (_.isString(file)) {
  494. uri = file;
  495. const arr = file.split('/');
  496. const last = _.last(arr);
  497. if (last) filename = last;
  498. } else if (_.isObject(file)) {
  499. const { uri: furi, url: furl, name, prefix } = file;
  500. if (furi) uri = furi;
  501. else if (furl) uri = furl;
  502. if (name) filename = name;
  503. else {
  504. const arr = uri.split('/');
  505. const last = _.last(arr);
  506. if (last) filename = last;
  507. }
  508. if (prefix) prefixs = prefix;
  509. }
  510. const obj = {};
  511. if (uri) obj.uri = uri;
  512. if (filename) obj.filename = filename;
  513. if (prefixs) obj.prefix = prefixs;
  514. resetFileList.push(obj);
  515. }
  516. // 导出
  517. const output = fs.createWriteStream(`${path}${fn}`);
  518. const archive = archiver('zip', {
  519. zlib: { level: 9 },
  520. });
  521. archive.pipe(output);
  522. // 请求文件,追加进压缩包
  523. for (const file of resetFileList) {
  524. const { uri, filename, prefix } = file;
  525. const res = await this.ctx.curl(`http://127.0.0.1${uri}`);
  526. if (res && res.data) {
  527. const options = {};
  528. if (filename) options.name = filename;
  529. if (prefix) options.prefix = prefix;
  530. if (filename) {
  531. archive.append(res.data, options);
  532. }
  533. }
  534. }
  535. await archive.finalize();
  536. return `/files${zipPath}${fn}`;
  537. }
  538. }
  539. module.exports = UtilService;