util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. 'use strict';
  2. const assert = require('assert');
  3. const _ = require('lodash');
  4. const fs = require('fs');
  5. const Excel = require('exceljs');
  6. const { ObjectId } = require('mongoose').Types;
  7. const { CrudService } = require('naf-framework-mongoose/lib/service');
  8. const { BusinessError, ErrorCode } = require('naf-core').Error;
  9. const moment = require('moment');
  10. const nodemailer = require('nodemailer');
  11. class UtilService extends CrudService {
  12. async updatedate() {
  13. let date = new Date();
  14. date = moment(date).format('YYYY-MM-DD HH:mm:ss');
  15. return date;
  16. }
  17. async sendMail(email, subject, text, html) {
  18. const setting = await this.ctx.model.Setting.findOne();
  19. let user_email = this.ctx.app.config.user_email;
  20. let auth_code = this.ctx.app.config.auth_code;
  21. if (setting) {
  22. user_email = setting.user_email;
  23. auth_code = setting.auth_code;
  24. }
  25. const transporter = nodemailer.createTransport({
  26. host: 'smtp.exmail.qq.com',
  27. secureConnection: true,
  28. port: 465,
  29. auth: {
  30. user: user_email, // 账号
  31. pass: auth_code, // 授权码
  32. },
  33. });
  34. const mailOptions = {
  35. from: user_email, // 发送者,与上面的user一致
  36. to: email, // 接收者,可以同时发送多个,以逗号隔开
  37. subject, // 标题
  38. text, // 文本
  39. html,
  40. };
  41. try {
  42. await transporter.sendMail(mailOptions);
  43. return true;
  44. } catch (err) {
  45. return false;
  46. }
  47. }
  48. async findone({ modelname }, data) {
  49. // 查询单条
  50. const _model = _.capitalize(modelname);
  51. const res = await this.ctx.model[_model].findOne({ ...data });
  52. return res;
  53. }
  54. async findbyids({ modelname }, { data }) {
  55. // 共通批量查询方法
  56. const _model = _.capitalize(modelname);
  57. const res = [];
  58. for (const elm of data) {
  59. const result = await this.ctx.model[_model].findById(elm);
  60. res.push(result);
  61. }
  62. return res;
  63. }
  64. async findmodel({ modelname }) {
  65. const _model = _.capitalize(modelname);
  66. const data = this.ctx.model[_model].prototype.schema.obj;
  67. return data;
  68. }
  69. async utilMethod(data, body) {
  70. console.log(data, body);
  71. // const res = await this.ctx.model.Student.updateMany({ ...data, classid: { $exists: true } }, { classid: null });
  72. const { code, ids, bedroomid } = body;
  73. for (const id of ids) {
  74. const r = await this.ctx.model.Student.findById(id);
  75. r.bedroom = code;
  76. r.bedroomid = bedroomid;
  77. await r.save();
  78. // await this.ctx.model.Student.findByIdAndUpdate(id, { bedroom: code, bedroomid });
  79. }
  80. }
  81. async toExcel(dataList, meta, fn = '导出结果') {
  82. // 导出excel
  83. const { app } = this;
  84. const nowDate = new Date().getTime();
  85. const filename = `${fn}-${nowDate}.xlsx`;
  86. // 取出预设存储地址
  87. const rootPath = `${app.config.cdn.repos_root_path}`;
  88. const rooturl = `${app.config.cdn.repos_root_url_excel}`;
  89. const path = `${rootPath}${rooturl}`;
  90. if (!path) {
  91. throw new BusinessError(ErrorCode.BUSINESS, '服务端没有设置存储路径');
  92. }
  93. // 如果不存在文件夹,就创建
  94. if (!fs.existsSync(path)) {
  95. fs.mkdirSync(path);
  96. }
  97. // 生成文件
  98. const filepath = `${path}${filename}`;
  99. fs.createWriteStream(filepath);
  100. const workbook = new Excel.Workbook();
  101. const sheet = workbook.addWorksheet('sheet');
  102. sheet.columns = meta;
  103. sheet.addRows(dataList);
  104. await workbook.xlsx.writeFile(filepath);
  105. return `/files/excel/${filename}`;
  106. }
  107. }
  108. module.exports = UtilService;