| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 | 'use strict';/** * Created by Tony on 2019/9/26. */const fs = require('fs');const sourceTypeConst = require('../const/source_type');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);            const rst = await ctx.service.roleRptRel.createRoleRelationship(params.tender_id, params.rpt_id, params.sid, params.business_id, '', params.rel_content);            ctx.body = { data: rst };            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 bzId = params.business_id;            const srcKey = sourceTypeConst.sourceTypeKey[params.source_type];            // console.log(params.rel_content);            const rst = await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, bzId, srcKey, params.rel_content);            const roleRel = await ctx.service.roleRptRel.getRoleRptRelByDetailIds(params.tender_id, params.rpt_id, params.stage_id, bzId);            // 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;            const roleRel = params.rel_content;            const bzId = params.business_id;            const srcKey = sourceTypeConst.sourceTypeKey[params.source_type];            // console.log(params.rel_content);            await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, bzId, srcKey, params.rel_content); // 传过来的数据是额外项目的,还需要单独处理当前的            const rst = await ctx.service.roleRptRel.updateMultiRoleRelationship(conParams, bzId, srcKey, roleRel);            ctx.body = { data: rst };            ctx.status = 201;        }        // 多个变更令更新签名角色关联        async updateMultiChangesRoleRelationship(ctx) {            const params = JSON.parse(ctx.request.body.params);            const conParams = params.selectedChanges;            const roleRel = params.rel_content;            const bzId = params.business_id;            const srcKey = sourceTypeConst.sourceTypeKey[params.source_type];            console.log('before:');            console.log(params.rel_content);            await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, bzId, srcKey, params.rel_content); // 传过来的数据是额外变更令的,还需要单独处理当前的            // update完默认当前的变更令后要清理审核日期(其他变更令的审核日期肯定会有出入的)            params.rel_content.forEach(role_rel => {                role_rel.sign_date = null;            });            console.log('after:');            console.log(params.rel_content);            const rst = await ctx.service.roleRptRel.updateMultiChangeRoleRelationship(conParams, srcKey, roleRel);            ctx.body = { data: rst };            ctx.status = 201;        }        // 多个XX(变更方案,变更申请...)更新签名角色关联        async updateMultiOtherRoleRelationship(ctx) {            const params = JSON.parse(ctx.request.body.params);            const conParams = params.selected;            const roleRel = params.rel_content;            const bzId = params.business_id;            const srcKey = sourceTypeConst.sourceTypeKey[params.source_type];            await ctx.service.roleRptRel.updateRoleRelationship(params.id, params.tender_id, params.rpt_id, params.stage_id, bzId, srcKey, params.rel_content); // 传过来的数据是额外变更令的,还需要单独处理当前的            // update完默认当前的变更令后要清理审核日期(其他变更令的审核日期肯定会有出入的)            params.rel_content.forEach(role_rel => {                role_rel.sign_date = null;            });            const rst = await ctx.service.roleRptRel.updateMultiOtherRoleRelationship(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 = [];            }        }    }}
 |