1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- 'use strict';
- const assert = require('assert');
- const _ = require('lodash');
- 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');
- 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;
- }
- }
- module.exports = UtilService;
|