123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- 'use strict';
- /**
- * Created by Tony on 2019/9/26.
- */
- const fs = require('fs');
- module.exports = app => {
- class ReportController extends app.BaseController {
- /**
- * 创建电子签名角色
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- async createSignatureRole(ctx) {
- const params = JSON.parse(ctx.request.body.params);
- // console.log(params);
- const rst = await ctx.service.signatureRole.createRole(params.name, params.bind_acc_id, params.prj_id, params.tender_id);
- // console.log(rst);
- ctx.body = { data: rst };
- // ctx.body = { data: { msg: 'test the network' } };
- ctx.status = 201;
- }
- /**
- * 创建签名角色关联
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- async createRoleRel(ctx) {
- const params = JSON.parse(ctx.request.body.params);
- // console.log(params);
- const rst = await ctx.service.roleRptRel.createRoleRelationship(params.tender_id, params.rpt_id, params.rel_content);
- // console.log(rst);
- ctx.body = { data: rst };
- // ctx.body = { data: { msg: 'test the network' } };
- ctx.status = 201;
- }
- /**
- * 更新签名角色关联
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- async updateRoleRel(ctx) {
- const params = JSON.parse(ctx.request.body.params);
- // console.log(params);
- const rst = await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, params.rel_content);
- const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_id, params.stage_id);
- // const roleRel = params.rel_content;
- await encodeSignatureDataUri(roleRel, this.app.baseDir);
- // console.log(rst);
- ctx.body = { data: rst, signatureRelInfo: roleRel };
- // ctx.body = { data: { msg: 'test the network' } };
- ctx.status = 201;
- }
- /**
- * 获取多个标段签名角色关联
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- async getMultiRoleRptRels(ctx) {
- const params = JSON.parse(ctx.request.body.params);
- const conParams = params.selectedTenders;
- const rst = await ctx.service.roleRptRel.getCrossTenderRoleRptRels(conParams);
- ctx.body = { data: rst };
- ctx.status = 201;
- }
- /**
- * 跨标段更新签名角色关联
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- async updateCrossTendersRoleRelationship(ctx) {
- const params = JSON.parse(ctx.request.body.params);
- const conParams = params.selectedTenders;
- // conParams.push([params.tender_id, params.stage_id, params.rpt_id]); // 传过来的数据是额外项目的,还需要加上自己一个
- const roleRel = params.rel_content;
- // updateMultiRoleRelationship
- // console.log(params.rel_content);
- await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, params.rel_content); // 传过来的数据是额外项目的,还需要单独处理当前的
- const rst = await ctx.service.roleRptRel.updateMultiRoleRelationship(conParams, roleRel);
- ctx.body = { data: rst };
- ctx.status = 201;
- }
- /**
- * 更新最近使用签名
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- async updateSignatureUsed(ctx) {
- const params = JSON.parse(ctx.request.body.params);
- const used_time = ctx.request.body.create_time;
- const signused = await ctx.service.signatureUsed.updateUsed(params, used_time);
- const signUsedList = await ctx.service.signatureUsed.getSignatureUsedByTenderId(params.tender_id);
- // const rst = await ctx.service.signatureRole.createRole(params.name, params.bind_acc_id, params.prj_id, params.tender_id);
- // console.log(rst);
- ctx.body = { data: signUsedList };
- // ctx.body = { data: { msg: 'test the network' } };
- ctx.status = 201;
- }
- }
- return ReportController;
- };
- function isFileExisted(file) {
- return new Promise(function(resolve, reject) {
- fs.access(file, err => {
- if (err) {
- reject(false);
- } else {
- resolve(true);
- }
- });
- });
- }
- async function encodeSignatureDataUri(roleRel, baseDir) {
- if (roleRel) {
- for (const singleRoleRel of roleRel) {
- if (singleRoleRel.rel_content !== null && singleRoleRel.rel_content !== undefined && singleRoleRel.rel_content !== '') {
- const roleRelContent = JSON.parse(singleRoleRel.rel_content);
- for (const role of roleRelContent) {
- // console.log(role);
- if (role.sign_path !== '') {
- const filePath = baseDir + '/app' + role.sign_path;
- try {
- const res = await isFileExisted(filePath);
- if (res) {
- const bData = fs.readFileSync(filePath);
- const base64Str = bData.toString('base64');
- const datauri = 'data:image/png;base64,' + base64Str;
- role.sign_pic = datauri;
- } else {
- console.log('文件不存在:' + filePath);
- }
- } catch (err) {
- console.error(err);
- }
- }
- }
- singleRoleRel.rel_content = JSON.stringify(roleRelContent);
- } else {
- singleRoleRel.rel_content = [];
- }
- }
- }
- }
|