123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- const fs = require('fs');
- const Excel = require('exceljs');
- const { ObjectId } = require('mongoose').Types;
- const { CrudService } = require('naf-framework-mongoose/lib/service');
- const { BusinessError, ErrorCode } = require('naf-core').Error;
- const moment = require('moment');
- const nodemailer = require('nodemailer');
- const { template } = require('lodash');
- const docx = require('docx');
- class UtilService extends CrudService {
- async updatedate() {
- let date = new Date();
- date = moment(date).format('YYYY-MM-DD HH:mm:ss');
- return date;
- }
- async sendMail(email, subject, text, html) {
- const setting = await this.ctx.model.Setting.findOne();
- let user_email = this.ctx.app.config.user_email;
- let auth_code = this.ctx.app.config.auth_code;
- if (setting) {
- user_email = setting.user_email;
- auth_code = setting.auth_code;
- }
- const transporter = nodemailer.createTransport({
- host: 'smtp.exmail.qq.com',
- secureConnection: true,
- port: 465,
- auth: {
- user: user_email, // 账号
- pass: auth_code, // 授权码
- },
- });
- const mailOptions = {
- from: user_email, // 发送者,与上面的user一致
- to: email, // 接收者,可以同时发送多个,以逗号隔开
- subject, // 标题
- text, // 文本
- html,
- };
- try {
- await transporter.sendMail(mailOptions);
- return true;
- } catch (err) {
- return false;
- }
- }
- async findone({ modelname }, data) {
- // 查询单条
- const _model = _.capitalize(modelname);
- const res = await this.ctx.model[_model].findOne({ ...data });
- return res;
- }
- async findbyids({ modelname }, { data }) {
- // 共通批量查询方法
- const _model = _.capitalize(modelname);
- const res = [];
- for (const elm of data) {
- const result = await this.ctx.model[_model].findById(elm);
- res.push(result);
- }
- return res;
- }
- async findmodel({ modelname }) {
- const _model = _.capitalize(modelname);
- const data = this.ctx.model[_model].prototype.schema.obj;
- return data;
- }
- async utilMethod(data, body) {
- console.log(data, body);
- }
- async toExcel(dataList, meta, fn = '导出结果') {
- // 导出excel
- const { app } = this;
- const nowDate = new Date().getTime();
- const filename = `${fn}-${nowDate}.xlsx`;
- // 取出预设存储地址
- const rootPath = `${app.config.cdn.repos_root_path}`;
- const rooturl = `${app.config.cdn.repos_root_url_excel}`;
- const path = `${rootPath}${rooturl}`;
- if (!path) {
- throw new BusinessError(ErrorCode.BUSINESS, '服务端没有设置存储路径');
- }
- // 如果不存在文件夹,就创建
- if (!fs.existsSync(path)) {
- fs.mkdirSync(path);
- }
- // 生成文件
- const filepath = `${path}${filename}`;
- fs.createWriteStream(filepath);
- const workbook = new Excel.Workbook();
- const sheet = workbook.addWorksheet('sheet');
- sheet.columns = meta;
- sheet.addRows(dataList);
- await workbook.xlsx.writeFile(filepath);
- return `/files/excel/${filename}`;
- }
- /**
- * 导出docx
- * @param {Array} data 数据[{title,content([]),author}]
- * @param {String} fn 文件名
- */
- async toDocx(data, fn = '培训心得') {
- const {
- Document,
- Packer,
- Paragraph,
- TextRun,
- HeadingLevel,
- AlignmentType,
- } = docx;
- const doc = new Document();
- const children = [];
- // 整理数据
- for (let i = 0; i < data.length; i++) {
- const obj = data[i];
- const { title, content, author } = obj;
- const c = [];
- if (title) {
- const tit = new Paragraph({
- children: [ new TextRun({ text: title, bold: true }) ],
- heading: HeadingLevel.TITLE,
- alignment: AlignmentType.CENTER,
- });
- c.push(tit);
- }
- if (author) {
- const auth = new Paragraph({
- children: [ new TextRun({ color: '#000000', text: author }) ],
- heading: HeadingLevel.HEADING_2,
- alignment: AlignmentType.RIGHT,
- });
- c.push(auth);
- }
- if (content && _.isArray(content) && content.length > 0) {
- for (const cont of content) {
- const p = new Paragraph({
- children: [ new TextRun({ text: cont, bold: true }) ],
- });
- c.push(p);
- }
- }
- if (i !== data.length - 1) {
- // 换页
- const last = new Paragraph({
- pageBreakBefore: true,
- });
- c.push(last);
- }
- if (c.length > 0) children.push(...c);
- }
- doc.addSection({
- properties: {},
- children,
- });
- const { app } = this;
- const rootPath = `${app.config.cdn.repos_root_path}`;
- const rooturl = `${app.config.cdn.repos_root_url_experience}`;
- const path = `${rootPath}${rooturl}`;
- // 如果不存在文件夹,就创建
- if (!fs.existsSync(path)) {
- fs.mkdirSync(path);
- }
- const num = new Date().getTime();
- Packer.toBuffer(doc).then(buffer => {
- fs.writeFileSync(`${path}${fn}-${num}.docx`, buffer);
- });
- return `/files${rooturl}${fn}-${num}.docx`;
- }
- }
- module.exports = UtilService;
|