83 lines
3.0 KiB
JavaScript
83 lines
3.0 KiB
JavaScript
'use strict';
|
|
|
|
const MainService = require('../../main');
|
|
|
|
class printTemplateService extends MainService {
|
|
constructor(ctx) {
|
|
super(ctx);
|
|
const _noNeedLogin = '';
|
|
const _noNeedRight = 'list,save,create,delete,listTemplatesByType';
|
|
const _noNeedLog = '*';
|
|
const _noNeedAuth = 'list,save,create,delete,listTemplatesByType';
|
|
this.systemInit(_noNeedLogin, _noNeedRight, _noNeedLog, _noNeedAuth);
|
|
}
|
|
async list(data) {
|
|
const { service } = this;
|
|
const result = await service.systems.setting.printTemplate.getTree();
|
|
return this.success('ok', { presetTree: result, code: 200 });
|
|
}
|
|
async save(data) {
|
|
console.log('接收到保存请求:', data);
|
|
const { service } = this;
|
|
const result = await service.systems.setting.printTemplate.save(data);
|
|
return this.success('ok', result);
|
|
}
|
|
async create(data) {
|
|
const { service } = this;
|
|
const { template_name, business_type, page_width, page_height } = data || {};
|
|
|
|
if (!template_name || String(template_name).trim() === '') {
|
|
return this.fail('template_name 参数不能为空', { code: 400 });
|
|
}
|
|
if (!business_type || String(business_type).trim() === '') {
|
|
return this.fail('business_type 参数不能为空', { code: 400 });
|
|
}
|
|
if (page_width === undefined || page_width === null || page_width === '') {
|
|
return this.fail('page_width 参数不能为空', { code: 400 });
|
|
}
|
|
if (page_height === undefined || page_height === null || page_height === '') {
|
|
return this.fail('page_height 参数不能为空', { code: 400 });
|
|
}
|
|
|
|
try {
|
|
const result = await service.systems.setting.printTemplate.createTemplate(data);
|
|
return this.success('ok', result);
|
|
} catch (error) {
|
|
if (error.message === '同一业务类型和平台下已存在自定义模板') {
|
|
return this.fail(error.message, { code: 409 });
|
|
}
|
|
return this.fail(error.message || '创建打印模板失败', { code: 500 });
|
|
}
|
|
}
|
|
async delete(data) {
|
|
const { service } = this;
|
|
const { id } = data || {};
|
|
const templateId = Number(id);
|
|
|
|
if (!templateId || Number.isNaN(templateId)) {
|
|
return this.fail('id 参数不能为空', { code: 400 });
|
|
}
|
|
|
|
try {
|
|
const result = await service.systems.setting.printTemplate.deleteTemplate(templateId);
|
|
return this.success('ok', result);
|
|
} catch (error) {
|
|
if (error.message === '模板不存在或无权限') {
|
|
return this.fail(error.message, { code: 404 });
|
|
}
|
|
return this.fail(error.message || '删除打印模板失败', { code: 500 });
|
|
}
|
|
}
|
|
async listTemplatesByType(data) {
|
|
const { service } = this;
|
|
console.log('接收到根据业务类型查询模板请求:', data);
|
|
const { businessType } = data;
|
|
if(!businessType){
|
|
throw new Error('缺少业务类型参数');
|
|
}
|
|
const list = await service.systems.setting.printTemplate.listTemplatesByType(businessType);
|
|
return this.success('ok', { data: list, code: 200 });
|
|
}
|
|
}
|
|
module.exports = printTemplateService;
|