2019-01-22 08:55:35 +00:00
|
|
|
const Vue = require('vue');
|
|
|
|
const VueI18n = require('vue-i18n');
|
|
|
|
const renderer = require('vue-server-renderer').createRenderer();
|
2019-10-24 05:41:54 +00:00
|
|
|
|
|
|
|
const fs = require('fs');
|
2019-01-24 14:03:01 +00:00
|
|
|
const pdf = require('html-pdf');
|
2019-01-22 08:55:35 +00:00
|
|
|
const juice = require('juice');
|
2019-10-24 05:41:54 +00:00
|
|
|
const path = require('path');
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-10-24 05:41:54 +00:00
|
|
|
const config = require('./config');
|
|
|
|
const reportsPath = '../templates/reports';
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
if (!process.env.OPENSSL_CONF)
|
|
|
|
process.env.OPENSSL_CONF = '/etc/ssl/';
|
|
|
|
|
2019-10-24 05:41:54 +00:00
|
|
|
Vue.use(VueI18n);
|
|
|
|
|
|
|
|
class Report {
|
|
|
|
constructor(name, args) {
|
|
|
|
this.name = name;
|
|
|
|
this.args = args;
|
|
|
|
}
|
|
|
|
|
|
|
|
get path() {
|
|
|
|
return `${reportsPath}/${this.name}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
get template() {
|
|
|
|
const templatePath = `${this.path}/${this.name}.html`;
|
|
|
|
const fullPath = path.resolve(__dirname, templatePath);
|
|
|
|
|
|
|
|
return fs.readFileSync(fullPath, 'utf8');
|
|
|
|
}
|
|
|
|
|
|
|
|
get locale() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
get style() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async render() {
|
|
|
|
const localePath = `${this.path}/locale`;
|
|
|
|
const stylePath = `${this.path}/assets/css/index`;
|
|
|
|
|
|
|
|
const stylesheet = require(stylePath);
|
|
|
|
const component = require(this.path);
|
|
|
|
|
|
|
|
component.i18n = require(localePath);
|
|
|
|
component.template = juice.inlineContent(this.template, stylesheet, {
|
|
|
|
inlinePseudoElements: true
|
|
|
|
});
|
|
|
|
|
|
|
|
const i18n = new VueI18n(config.i18n);
|
|
|
|
const app = new Vue({
|
|
|
|
i18n: i18n,
|
|
|
|
render: h => h(component, {
|
|
|
|
props: this.args
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
return renderer.renderToString(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
async toPdfStream() {
|
|
|
|
const template = await this.render();
|
|
|
|
let options = config.pdf;
|
|
|
|
|
|
|
|
const optionsPath = `${this.path}/options.json`;
|
|
|
|
if (fs.existsSync(optionsPath))
|
|
|
|
options = Object.assign(options, require(optionsPath));
|
|
|
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
pdf.create(template, options).toStream((err, stream) => {
|
|
|
|
resolve(stream);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Report;
|
|
|
|
|
|
|
|
/* module.exports = {
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-25 14:08:11 +00:00
|
|
|
path: `${appPath}/report`,
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
async render(name, ctx) {
|
|
|
|
const component = require(`${this.path}/${name}`);
|
2019-10-24 05:41:54 +00:00
|
|
|
const i18n = new VueI18n(config.i18n);
|
|
|
|
const app = new Vue({
|
|
|
|
i18n: i18n,
|
|
|
|
render: h => h(component, {
|
|
|
|
props: {
|
|
|
|
myProp: 'asd1'
|
|
|
|
}
|
|
|
|
})
|
2019-01-22 08:55:35 +00:00
|
|
|
});
|
2019-10-16 06:39:45 +00:00
|
|
|
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
return renderer.renderToString(app);
|
|
|
|
},
|
|
|
|
|
2019-10-24 05:41:54 +00:00
|
|
|
async preFetch(orgComponent, ctx) {
|
2019-01-25 14:08:11 +00:00
|
|
|
let component = Object.create(orgComponent);
|
2019-01-23 08:33:58 +00:00
|
|
|
let mergedData = {};
|
|
|
|
let asyncData = {};
|
2019-01-22 08:55:35 +00:00
|
|
|
let data = {};
|
2019-01-24 14:03:01 +00:00
|
|
|
let params = {};
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
if (Object.keys(ctx.body).length > 0)
|
|
|
|
params = ctx.body;
|
|
|
|
|
|
|
|
if (Object.keys(ctx.query).length > 0)
|
|
|
|
params = ctx.query;
|
|
|
|
|
2019-01-22 08:55:35 +00:00
|
|
|
await this.attachAssets(component);
|
|
|
|
|
2019-01-25 14:08:11 +00:00
|
|
|
if (orgComponent.hasOwnProperty('data'))
|
|
|
|
data = orgComponent.data();
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-25 14:08:11 +00:00
|
|
|
if (orgComponent.hasOwnProperty('asyncData'))
|
|
|
|
asyncData = await orgComponent.asyncData(ctx, params);
|
2019-01-23 08:33:58 +00:00
|
|
|
|
|
|
|
mergedData = Object.assign(mergedData, data, asyncData);
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-23 08:33:58 +00:00
|
|
|
component.data = function data() {
|
|
|
|
return mergedData;
|
|
|
|
};
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-25 14:08:11 +00:00
|
|
|
const components = orgComponent.components;
|
|
|
|
if (components) {
|
2019-01-22 08:55:35 +00:00
|
|
|
const promises = [];
|
2019-01-25 14:08:11 +00:00
|
|
|
const childNames = [];
|
|
|
|
component.components = {};
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-25 14:08:11 +00:00
|
|
|
Object.keys(components).forEach(childName => {
|
|
|
|
childNames.push(childName);
|
|
|
|
promises.push(this.preFetch(components[childName], ctx));
|
2019-01-22 08:55:35 +00:00
|
|
|
});
|
|
|
|
|
2019-01-25 14:08:11 +00:00
|
|
|
await Promise.all(promises).then(results => {
|
|
|
|
results.forEach((result, i) => {
|
|
|
|
component.components[childNames[i]] = result.component;
|
|
|
|
});
|
|
|
|
});
|
2019-01-22 08:55:35 +00:00
|
|
|
}
|
2019-01-25 14:08:11 +00:00
|
|
|
|
|
|
|
return {component};
|
2019-10-24 05:41:54 +00:00
|
|
|
},
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-10-24 05:41:54 +00:00
|
|
|
async attachAssets(component) {
|
2019-01-24 14:03:01 +00:00
|
|
|
const localePath = `${this.path}/${component.name}/locale`;
|
2019-01-22 08:55:35 +00:00
|
|
|
const templatePath = `${this.path}/${component.name}/index.html`;
|
2019-01-24 14:03:01 +00:00
|
|
|
const stylePath = `${this.path}/${component.name}/assets/css/index`;
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
const template = await fs.readFile(templatePath, 'utf8');
|
2019-01-24 14:03:01 +00:00
|
|
|
const css = require(stylePath);
|
2019-01-31 10:44:03 +00:00
|
|
|
const cssOptions = {inlinePseudoElements: true};
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
component.i18n = require(localePath);
|
2019-01-31 10:44:03 +00:00
|
|
|
component.template = juice.inlineContent(template, css, cssOptions);
|
2019-10-24 05:41:54 +00:00
|
|
|
},
|
2019-01-22 08:55:35 +00:00
|
|
|
|
|
|
|
async toPdf(name, ctx) {
|
2019-01-24 14:03:01 +00:00
|
|
|
const html = await this.render(name, ctx);
|
2019-10-21 09:09:18 +00:00
|
|
|
let options = config.pdf;
|
|
|
|
|
|
|
|
const optionsPath = `${this.path}/${name}/options.json`;
|
|
|
|
if (fs.existsSync(optionsPath))
|
|
|
|
options = Object.assign(options, require(optionsPath));
|
2019-01-22 08:55:35 +00:00
|
|
|
|
2019-01-24 14:03:01 +00:00
|
|
|
return new Promise(resolve => {
|
|
|
|
pdf.create(html, options).toStream((err, stream) => {
|
|
|
|
resolve(stream);
|
|
|
|
});
|
|
|
|
});
|
2019-01-22 08:55:35 +00:00
|
|
|
},
|
|
|
|
};
|
2019-10-24 05:41:54 +00:00
|
|
|
*/
|