123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const tenderConst = require('../const/tender');
- const auditConst = require('../const/audit');
- const officeList = require('../const/cld_office').list;
- const settingConst = require('../const/setting.js');
- const scheduleConst = require('../const/schedule');
- const settingMenu = require('../../config/menu').settingMenu;
- const accountGroup = require('../const/account_group').group;
- const permission = require('../const/account_permission').permission;
- const noticeAgainConst = require('../const/account_permission').noticeAgain;
- const projectLog = require('../const/project_log');
- const imType = require('../const/tender').imType;
- const S2b = require('../lib/s2b');
- const measureType = require('../const/tender').measureType;
- const sendToWormhole = require('stream-wormhole');
- const path = require('path');
- const funSet = require('../const/fun_set');
- const projectSettingConst = require('../const/project_setting');
- const SpreadConst = require('../const/spread');
- const shenpiConst = require('../const/shenpi');
- module.exports = app => {
- class SettingController extends app.BaseController {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- ctx.subMenu = settingMenu;
- }
- async _checkMenu(pid) {
- const ctx = this.ctx;
- await this.ctx.service.s2bProj.refreshSessionS2b(pid);
- ctx.subMenu.s2b.display = !!ctx.session.sessionProject.gxby || !!ctx.session.sessionProject.dagl;
- }
- /**
- * 项目信息页面(Get)
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async info(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- await this._checkMenu(projectId);
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- // 获取销售人员数据
- const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
- // 数据规则
- const rule = ctx.service.project.rule('saveInfo');
- const jsValidator = await this.jsValidator.convert(rule).build();
- const officeName = officeList[salesmanData.office];
- const date = new Date(projectData.create_time * 1000);// 如果date为10位不需要乘1000
- const Y = date.getFullYear() + '-';
- const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
- const D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
- const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
- const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
- const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
- const dateStr = Y + M + D + h + m + s;
- const renderData = {
- projectData,
- salesmanData,
- officeName,
- officeList,
- jsValidator,
- dateStr,
- };
- await this.layout('setting/info.ejs', renderData);
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 项目设置 -- 账号设置(Get)
- * @param ctx
- * @return {Promise<void>}
- */
- async user(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- await this._checkMenu(projectId);
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '没有访问权限';
- }
- let keyword = '';
- if (ctx.query.keyword) {
- keyword = ctx.query.keyword;
- }
- let company = '';
- if (ctx.query.company) {
- company = ctx.query.company;
- }
- const page = ctx.page;
- const pageSize = ctx.pageSize;
- // 过滤数据
- ctx.service.projectAccount.searchFilter(ctx.request.query, projectId);
- ctx.sort = ['id', 'desc'];
- const total = await ctx.service.projectAccount.getCountWithBuilder();
- // 获取数据规则
- // const rule = ctx.service.projectAccount.rule('updateUser');
- // const frontRule = ctx.helper.validateConvert(rule);
- // const total = await ctx.service.projectAccount.count({ project_id: projectId });
- // 获取项目用户列表
- // const accountData = await ctx.service.projectAccount.getAllDataByCondition({
- // where: { project_id: projectId },
- // columns: ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group'],
- // limit: (app.config.pageSize * (page - 1)),
- // offset: app.config.pageSize,
- // });
- // const columns = ['id', 'account', 'name', 'company', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'account_group', 'bind'];
- // const accountData = await ctx.service.projectAccount.getListByProjectId(columns, projectId);
- const accountData = await ctx.service.projectAccount.getListWithBuilder();
- // 获取账号个数
- const user_total = await ctx.service.projectAccount.count({project_id: projectId});
- // 分页相关
- const pageInfo = {
- page,
- pageSizeSelect: 1,
- pageSize,
- total_num: total,
- total: Math.ceil(total / pageSize),
- queryData: JSON.stringify(ctx.urlInfo.query),
- };
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({where: {pid: projectId}});
- const renderData = {
- projectData,
- accountData,
- accountGroup,
- permission,
- pageInfo,
- keyword,
- user_total,
- unitList,
- company,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.user),
- // rule: JSON.stringify(frontRule),
- };
- await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 项目设置 -- 账号解绑设置(Post)
- * @param ctx
- * @return {Promise<void>}
- */
- async userUnbind(ctx) {
- const projectData = ctx.session.sessionProject;
- try {
- if (projectData.id === undefined) {
- throw '不存在对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '非管理员无权解绑账号';
- }
- const accountId = parseInt(ctx.request.body.id);
- const result = await ctx.service.projectAccount.update({bind: 0}, {id: accountId});
- if (!result) {
- throw '解绑失败';
- }
- this.setMessage('解绑成功', this.messageType.SUCCESS);
- } catch (error) {
- console.log(error);
- this.setMessage(error.toString(), this.messageType.ERROR);
- }
- ctx.redirect(ctx.request.header.referer);
- }
- /**
- * 项目权限 -- 账号设置(Get)
- * @param ctx
- * @return {Promise<void>}
- */
- async userPermissionSet(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '没有访问权限';
- }
- let keyword = '';
- if (ctx.query.keyword) {
- keyword = ctx.query.keyword;
- }
- let company = '';
- if (ctx.query.company) {
- company = ctx.query.company;
- }
- // 获取数据规则
- const page = ctx.page;
- const pageSize = ctx.pageSize;
- const columns = ['id', 'account', 'name', 'company', 'company_id', 'role', 'mobile', 'auth_mobile', 'telephone', 'enable', 'is_admin', 'bind', 'account_group', 'permission', 'cooperation', 'notice_again', 'unit_sign_path'];
- // 过滤数据
- ctx.service.projectAccount.searchFilter(ctx.request.query, projectId, columns);
- ctx.sort = ['id', 'desc'];
- const total = await ctx.service.projectAccount.getCountWithBuilder();
- const accountData = await ctx.service.projectAccount.getListWithBuilder();
- // 获取账号个数
- const user_total = await ctx.service.projectAccount.count({project_id: projectId});
- // 分页相关
- const pageInfo = {
- page,
- pageSizeSelect: 1,
- pageSize,
- total_num: total,
- total: Math.ceil(total / pageSize),
- queryData: JSON.stringify(ctx.urlInfo.query),
- };
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({where: {pid: projectId}});
- const noticeSet = projectData.notice_setting ? JSON.parse(projectData.notice_setting) : ctx.helper._.cloneDeep(projectSettingConst.noticeSetting);
- const renderData = {
- projectData,
- accountData,
- accountGroup,
- permission,
- keyword,
- permissionStr: JSON.stringify(permission),
- pageInfo,
- user_total,
- unitList,
- company,
- noticeAgainConst,
- noticeSet,
- fujianOssPath: ctx.app.config.fujianOssPath,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.user),
- // rule: JSON.stringify(frontRule),
- };
- await this.layout('setting/user_permission.ejs', renderData, 'setting/user_permission_modal.ejs');
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 项目权限 -- 参建单位(Get)
- * @param ctx
- * @return {Promise<void>}
- */
- async userUnit(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '没有访问权限';
- }
- const unitList = await ctx.service.constructionUnit.getAllDataByCondition({where: {pid: projectId}});
- for (const u of unitList) {
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: {
- project_id: projectId,
- company: u.name
- }
- });
- u.account_num = accountList.length;
- }
- // 获取账号个数
- const user_total = await ctx.service.projectAccount.count({project_id: projectId});
- const renderData = {
- projectData,
- accountGroup,
- unitList,
- user_total,
- company: null,
- fujianOssPath: ctx.app.config.fujianOssPath,
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.user),
- };
- await this.layout('setting/user_unit.ejs', renderData, 'setting/user_unit_modal.ejs');
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 项目设置 -- 单位新增设置(Post)
- * @param ctx
- * @return {Promise<void>}
- */
- async userUnitAdd(ctx) {
- const projectData = ctx.session.sessionProject;
- try {
- // 验证数据
- if (projectData.id === undefined) {
- throw '不存在对应的项目数据';
- }
- // 获取验证规则
- const rule = ctx.service.constructionUnit.rule('add');
- ctx.validate(rule);
- ctx.request.body.pid = projectData.id;
- const result = await ctx.service.constructionUnit.save(ctx.request.body);
- if (!result) {
- throw '保存单位失败';
- }
- this.setMessage('保存单位成功', this.messageType.SUCCESS);
- ctx.redirect('/' + ctx.controllerName + '/user/unit');
- } catch (error) {
- console.log(error);
- this.setMessage(error.toString(), this.messageType.ERROR);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 项目设置 -- 单位编辑(Post)
- * @param ctx
- * @return {Promise<void>}
- */
- async userUnitSave(ctx) {
- const projectData = ctx.session.sessionProject;
- const responseData = {
- err: 0, msg: '', data: {},
- };
- try {
- // 验证数据
- if (projectData.id === undefined) {
- throw '不存在对应的项目数据';
- }
- const data = JSON.parse(ctx.request.body.data);
- switch (data.type) {
- case 'update':
- const post_data = {
- pid: projectData.id,
- id: data.id,
- };
- post_data[data.val_name] = data.val;
- if (data.name) post_data.name = data.name;
- const result = await ctx.service.constructionUnit.save(post_data);
- if (!result) {
- throw '保存单位失败';
- }
- if (post_data.name) {
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: {
- project_id: projectData.id,
- company: post_data.name
- }
- });
- responseData.data = {account_num: accountList.length};
- }
- break;
- case 'delete':
- const accountList = await ctx.service.projectAccount.getAllDataByCondition({
- where: {
- project_id: projectData.id,
- company: data.name
- }
- });
- if (accountList.length > 0) {
- throw '该单位下还存在账号,无法删除';
- }
- await ctx.service.constructionUnit.del(data.id);
- break;
- case 'del-sign':
- const info = await ctx.service.constructionUnit.getDataById(data.id);
- let path = null;
- if (info.sign_path) {
- // 不删除地址,只删除数据库数据,防止已签章的报表丢失
- // await ctx.app.fujianOss.delete(ctx.app.config.fujianOssFolder + info.sign_path);
- const paths = info.sign_path.split('&%&');
- const newPath = paths.filter(item => item !== data.path);
- path = newPath.length !== 0 ? newPath.join('&%&') : null;
- }
- await ctx.service.constructionUnit.update({ sign_path: path }, { id: info.id });
- // 用户已选章也要判断,存在也要移除
- const userList = await ctx.service.projectAccount.getAllDataByCondition({ where: { company: info.name, project_id: projectData.id } });
- if (userList.length > 0) {
- for (const user of userList) {
- if (user.unit_sign_path && user.unit_sign_path.includes(data.path)) {
- const newSignPath = user.unit_sign_path.split('&%&').filter(item => item !== data.path).join('&%&');
- await ctx.service.projectAccount.update({ unit_sign_path: newSignPath ? newSignPath : null }, { id: user.id });
- }
- }
- }
- responseData.data = path;
- break;
- case 'import-users':
- responseData.data = await ctx.service.projectAccount.addUsers(projectData.id, data.users);
- break;
- default:
- break;
- }
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- async userUnitUpload(ctx) {
- const responseData = {
- err: 0, msg: '', data: null,
- };
- let stream;
- try {
- const parts = ctx.multipart({ autoFields: true });
- const paths = [];
- let index = 0;
- while ((stream = await parts()) !== undefined) {
- const create_time = Date.parse(new Date()) / 1000;
- const fileInfo = path.parse(stream.filename);
- const filepath = `app/public/upload/sign/unit/qianzhang_${create_time + index.toString() + fileInfo.ext}`;
- await ctx.app.fujianOss.put(ctx.app.config.fujianOssFolder + filepath, stream);
- await sendToWormhole(stream);
- paths.push(filepath);
- ++index;
- }
- const unit = await ctx.service.constructionUnit.getDataById(parts.field.id);
- const sign_path = unit.sign_path ? unit.sign_path.split('&%&') : [];
- sign_path.push(...paths);
- const result = await ctx.service.constructionUnit.update({ sign_path: sign_path.join('&%&') }, { id: unit.id });
- if (result) {
- responseData.data = { sign_path: sign_path.join('&%&') };
- } else {
- throw '添加数据库失败';
- }
- } catch (err) {
- this.log(err);
- responseData.err = 1;
- responseData.msg = err;
- }
- ctx.body = responseData;
- }
- /**
- * 项目设置 -- 账号启用和停用设置(Post)
- * @param ctx
- * @return {Promise<void>}
- */
- async userSwitch(ctx) {
- const responseData = {
- err: 0, msg: '', data: null,
- };
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '没有访问权限';
- }
- const data = JSON.parse(ctx.request.body.data);
- const result = await ctx.service.projectAccount.update(data, {id: data.id});
- if (!result) {
- throw '提交数据失败';
- }
- } catch (err) {
- this.log(err);
- responseData.err = 1;
- responseData.msg = err;
- }
- ctx.body = responseData;
- }
- /**
- * 项目设置 -- 账号添加(Post)
- * @param ctx
- * @return {Promise<void>}
- */
- async addUser(ctx) {
- const projectData = ctx.session.sessionProject;
- try {
- // 验证数据
- if (projectData.id === undefined) {
- throw '不存在对应的项目数据';
- }
- // 获取验证规则
- const rule = ctx.service.projectAccount.rule('add');
- ctx.validate(rule);
- // 判断新密码的强度
- const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
- if (!reg.test(ctx.request.body.password)) {
- throw '请设置至少包含数字和字母的密码';
- }
- ctx.request.body.project_id = projectData.id;
- const result = await ctx.service.projectAccount.save(ctx.request.body);
- if (!result) {
- throw '保存账号数据失败';
- }
- this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
- ctx.redirect('/' + ctx.controllerName + '/user');
- } catch (error) {
- console.log(error);
- ctx.session.postError = error.toString();
- this.setMessage(error.toString(), this.messageType.ERROR);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 项目设置 -- 账号编辑(Post)
- * @param ctx
- * @return {Promise<void>}
- */
- async updateUser(ctx) {
- const projectData = ctx.session.sessionProject;
- try {
- // 验证数据
- if (projectData.id === undefined) {
- throw '不存在对应的项目数据';
- }
- // 获取验证规则
- const rule = ctx.service.projectAccount.rule('modify');
- ctx.validate(rule);
- const result = await ctx.service.projectAccount.save(ctx.request.body);
- if (!result) {
- throw '保存账号数据失败';
- }
- this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
- ctx.redirect(ctx.request.header.referer);
- } catch (error) {
- console.log(error);
- this.setMessage(error.toString(), this.messageType.ERROR);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- async resetUserPassword(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '没有访问权限';
- }
- const accountId = parseInt(ctx.request.body.id);
- let password = ctx.request.body.reset_password;
- password = password.toString();
- const account = ctx.request.body.account !== undefined ? ctx.request.body.account : '';
- if (isNaN(accountId) || accountId <= 0 || password.length < 6) {
- throw '参数错误';
- }
- // 判断新密码的强度
- const reg = /^(?![0-9]+$)(?![a-zA-Z]+$).{6,16}$/;
- if (!reg.test(password)) {
- throw '请设置至少包含数字和字母的密码';
- }
- const result = await ctx.service.projectAccount.resetPassword(accountId, password, account);
- if (!result) {
- throw '重置密码失败!';
- }
- this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
- ctx.redirect(ctx.request.header.referer);
- } catch (error) {
- console.log(error);
- this.setMessage(error.toString(), this.messageType.ERROR);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 项目设置 -- 账号权限编辑(Post)
- * @param ctx
- * @return {Promise<void>}
- */
- async permission(ctx) {
- const projectData = ctx.session.sessionProject;
- try {
- // 验证数据
- if (projectData.id === undefined) {
- throw '不存在对应的项目数据';
- }
- const accountId = parseInt(ctx.request.body.id);
- const accountInfo = await ctx.service.projectAccount.getDataById(accountId);
- if (accountInfo === null) {
- throw '不存在该客户';
- }
- const result = await ctx.service.projectAccount.permissionSave(accountId, ctx.request.body);
- if (!result) {
- throw '修改权限失败!';
- }
- this.setMessage('保存账号数据成功', this.messageType.SUCCESS);
- ctx.redirect(ctx.request.header.referer);
- } catch (error) {
- console.log(error);
- this.setMessage(error.toString(), this.messageType.ERROR);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- // todo 暂时保留,如果支付审批不需要,则取消
- /**
- * 项目设置 -- 自定义标段分类(Get)
- *
- * @param ctx
- * @return {Promise<void>}
- */
- async category(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- await this._checkMenu(projectId);
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '没有访问权限';
- }
- const categoryData = await ctx.service.category.getAllCategory(projectId);
- const tenderData = await ctx.service.tender.getList('', null, 1);
- const renderData = {
- projectData,
- categoryType: settingConst.cType,
- categoryData,
- tenderData,
- };
- await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 新增分类(Ajax)
- *
- * @param ctx
- * @return {Promise<void>}
- */
- async addCategory(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const responseData = {
- err: 0, msg: '', data: null,
- };
- const data = JSON.parse(ctx.request.body.data);
- if (!data.name) {
- throw '提交数据错误';
- }
- responseData.data = await ctx.service.category.addCategory(projectId, data.name, settingConst.cType.key.dropDown);
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- /**
- * 编辑分类(Ajax)
- *
- * @param ctx
- * @return {Promise<void>}
- */
- async updateCategory(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const responseData = {err: 0, msg: '', data: null};
- const data = JSON.parse(ctx.request.body.data);
- if (!data.id) {
- throw '提交数据错误';
- }
- if (data.name) {
- const count = await ctx.service.category.count({pid: projectId, name: data.name});
- if (count >= 1) {
- throw '存在同名类别';
- }
- }
- const result = await ctx.service.category.update(data, {id: data.id});
- if (!result) {
- throw '提交数据失败';
- }
- responseData.data = await ctx.service.category.getCategory(data.id);
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- async setCategoryValue(ctx) {
- try {
- const responseData = {err: 0, msg: '', data: {}};
- const data = JSON.parse(ctx.request.body.data);
- if (!data.id) {
- throw '提交数据错误';
- }
- await ctx.service.categoryValue.setCategoryValue(data.id, data.updateValue);
- responseData.data.category = await ctx.service.category.getCategory(data.id);
- responseData.data.tenders = await ctx.service.tender.getList('', null, 1);
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = {err: 1, msg: err instanceof String ? err : '提交数据失败', data: null};
- }
- }
- /**
- * 删除分类(Ajax)
- *
- * @param ctx
- * @return {Promise<void>}
- */
- async deleteCategory(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const responseData = {
- err: 0, msg: '', data: null,
- };
- const data = JSON.parse(ctx.request.body.data);
- if (!data.id) {
- throw '提交数据错误';
- }
- await ctx.service.category.deleteCategory(projectId, data.id);
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- /**
- * 调整分类层次排序(Ajax)
- *
- * @param ctx
- * @return {Promise<void>}
- */
- async resetCategoryLevel(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const responseData = {
- err: 0, msg: '', data: null,
- };
- const data = JSON.parse(ctx.request.body.data);
- await ctx.service.category.resetCategoryLevel(data);
- responseData.data = await ctx.service.category.getAllCategory(projectId);
- ctx.body = responseData;
- } catch (err) {
- this.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- async selfCategoryLevel(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- await ctx.service.projectAccount.defaultUpdate({
- id: ctx.session.sessionUser.accountId,
- self_category_level: data.self_category_level || ''
- });
- ctx.body = {
- err: 0, msg: '', data: null,
- };
- } catch (err) {
- ctx.log(err);
- ctx.ajaxErrorBody(err, '保存自定义分类失败');
- }
- }
- /** update porject info
- * @author wangfeng
- * @date 2018-10-12 15:48:05
- * @param ctx
- * @return {Promise<void>}
- */
- async updateinfo(ctx) {
- try {
- const projectId = ctx.params.id;
- const responseData = {
- err: 0, msg: '', data: null,
- };
- const conditionData = {
- id: projectId,
- };
- const data = ctx.request.body;
- const result = await ctx.service.project.update(data, conditionData);
- if (!result) {
- throw '提交数据失败';
- }
- ctx.redirect('/setting/info');
- } catch (err) {
- this.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- async updateProjectSet(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const data = JSON.parse(ctx.request.body.data);
- const allowField = ['notice_setting']; // 暂时只一个
- if (!ctx.helper._.includes(allowField, data.field)) {
- throw '非配置的字段无法使用本方法';
- }
- const result = await ctx.service.project.updateProjectSet(projectId, data.field, data.pData);
- if (!result) {
- throw '提交数据失败';
- }
- ctx.body = { err: 0, msg: '', data: result };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- /**
- * 检测账户是否存在
- *
- * @param {Object} ctx -egg全局变量
- * @return {void}
- */
- async accountExist(ctx) {
- const projectData = ctx.session.sessionProject;
- const responseData = {
- err: 0, msg: '', data: null,
- };
- try {
- const data = JSON.parse(ctx.request.body.data);
- const account = data.account;
- if (projectData.id === undefined) {
- throw '不存在对应项目';
- }
- responseData.data = await ctx.service.projectAccount.isAccountExist(account, projectData.id);
- ctx.body = responseData;
- } catch (error) {
- ctx.body = {err: 1, msg: error.toString(), data: null};
- }
- }
- /**
- * 显示设置(Get)
- * @param {Object} ctx -egg全局变量
- * @return {void}
- */
- async show(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- await this._checkMenu(projectId);
- const projectData = await ctx.service.project.getDataById(projectId);
- if (!projectData) {
- throw '没有对应的项目数据';
- }
- if (ctx.session.sessionUser.is_admin === 0) {
- throw '没有访问权限';
- }
- const showList = await ctx.service.settingShow.getList(projectData.page_path);
- const sjsRela = await ctx.service.project.getSjsRela(projectId);
- const projectSpread = await ctx.service.projectSpread.getProjectSpreadType(projectId);
- const renderData = {projectData, showList, sjsRela, settingConst, projectSpread};
- await this.layout('setting/show.ejs', renderData);
- } catch (error) {
- this.log(error);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 更新显示设置列表
- * @param {Object} ctx -egg全局变量
- * @return {void}
- */
- async showListUpdate(ctx) {
- try {
- // 获取项目id
- const projectId = ctx.session.sessionProject.id;
- const {data} = ctx.request.body;
- const {id} = JSON.parse(data);
- const result = await ctx.service.settingShow.setDefaultLabel(id, projectId);
- const responseData = {err: 0, msg: '', data: result};
- ctx.body = responseData;
- } catch (error) {
- this.log(error);
- ctx.body = {err: 1, msg: error.toString(), data: null};
- }
- }
- async showSjsUpdate(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const data = JSON.parse(ctx.request.body.data);
- const result = await ctx.service.project.updateSjsRela(projectId, data.sub, data.field, data.key, data.value);
- ctx.body = {err: 0, msg: '', data: result};
- } catch (err) {
- this.log(err);
- this.ajaxErrorBody(err, '保存数据失败');
- }
- }
- async s2b(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- await this._checkMenu(projectId);
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) throw '没有对应的项目数据';
- if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
- const tenders = await ctx.service.tender.getAllDataByCondition({where: {project_id: projectId}});
- for (const t of tenders) {
- t.measure_type_str = t.measure_type === measureType.tz.value ? measureType.tz.title : measureType.gcl.title;
- const stages = await ctx.service.stage.getAllDataByCondition({where: {tid: t.id}});
- t.stage_count_str = `第${stages.length || 0}期`;
- const user = await ctx.service.projectAccount.getAccountInfoById(t.user_id);
- t.user_str = user.name + '-' + user.company;
- t.show_time = stages.length > 0 ? stages[stages.length - 1].cache_time_r || stages[stages.length - 1].in_time : t.create_time;
- }
- await this.layout('setting/s2b.ejs', {
- projectData,
- tenders,
- });
- } catch (error) {
- ctx.helper.log(error);
- ctx.redirect('/dashboard');
- }
- }
- async s2bUpdate(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) throw '没有对应的项目数据';
- if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
- const data = JSON.parse(ctx.request.body.data);
- if (!data.tid) throw '提交数据错误';
- const updateData = {};
- if (data.gxby_limit !== undefined) updateData.s2b_gxby_limit = data.gxby_limit;
- if (data.gxby_check !== undefined) updateData.s2b_gxby_check = data.gxby_check;
- if (data.dagl_limit !== undefined) updateData.s2b_dagl_limit = data.dagl_limit;
- if (data.dagl_check !== undefined) updateData.s2b_dagl_check = data.dagl_check;
- await ctx.service.tender.saveApiRela(data.tid, updateData);
- ctx.body = {err: 0, msg: '', data: null};
- } catch (error) {
- ctx.helper.log(error);
- this.ajaxErrorBody(error, '保存数据失败');
- }
- }
- async s2bUpdateStatus(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) throw '没有对应的项目数据';
- if (ctx.session.sessionUser.is_admin === 0) throw '没有访问权限';
- const data = JSON.parse(ctx.request.body.data);
- if (!data.type || data.status === undefined) throw '提交数据错误';
- const responseData = {err: 0, msg: '', data: null};
- switch (data.type) {
- case 'gxby':
- responseData.data = await this.ctx.service.s2bProj.updateGxbyStatus(projectId, data.status, data.limit, data.ratio);
- break;
- case 'dagl':
- responseData.data = await this.ctx.service.s2bProj.updateDaglStatus(projectId, data.status, data.limit, data.ratio);
- break;
- default:
- throw '提交数据错误';
- }
- ctx.body = responseData;
- } catch (error) {
- console.log(error);
- ctx.helper.log(error);
- this.ajaxErrorBody(error, '保存数据失败');
- }
- }
- async spread(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- await this._checkMenu(projectId);
- const projectData = await ctx.service.project.getDataById(projectId);
- if (!projectData) throw '没有对应的项目数据';
- const sType = ctx.query.stype;
- const code = ctx.query.code;
- const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectData.id, code);
- const renderData = {
- BaseSetCol: SpreadConst.BaseSetCol[sType],
- sType,
- code,
- colSet: projectSpread[sType],
- jsFiles: this.app.jsFiles.common.concat(this.app.jsFiles.setting.spread),
- };
- await this.layout('setting/spread.ejs', renderData, 'setting/spread_modal.ejs');
- } catch (error) {
- this.log(error);
- ctx.redirect('/dashboard');
- }
- }
- async saveSpread(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const data = JSON.parse(ctx.request.body.data);
- await this.ctx.service.projectSpread.updateProjectSet(projectId, data.code, data.sType, data.colSet);
- const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectId, data.code);
- ctx.body = { err: 0, msg: '', data: { colSet: projectSpread[data.sType] } };
- } catch (error) {
- ctx.log(error);
- ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
- }
- }
- async previewSpread(ctx) {
- try {
- const data = JSON.parse(ctx.request.body.data);
- const [billsSpread, posSpread] = this.ctx.service.projectSpread.generateRelaSpread(data.sType, data.colSet);
- ctx.body = { err: 0, msg: '', data: { billsSpread, posSpread } };
- } catch (error) {
- ctx.log(error);
- ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
- }
- }
- async resetSpread(ctx) {
- try {
- const projectId = ctx.session.sessionProject.id;
- const data = JSON.parse(ctx.request.body.data);
- const spreadTemplate = SpreadConst.ProjectSpreadTemplate.find(x => { return x.code === data.code; });
- const colSet = spreadTemplate.template[data.sType];
- await this.ctx.service.projectSpread.updateProjectSet(projectId, data.code, data.sType, colSet);
- const projectSpread = await this.ctx.service.projectSpread.getProjectSpread(projectId, data.code);
- ctx.body = { err: 0, msg: '', data: { colSet: projectSpread[data.sType] } };
- } catch (error) {
- ctx.log(error);
- ctx.ajaxErrorBody(error, '保存列设置失败,请刷新后重试');
- }
- }
- }
- return SettingController;
- };
|