123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- 'use strict';
- /**
- * 控制面板
- *
- * @author CaiAoLin
- * @date 2017/11/23
- * @version
- */
- const auditConst = require('../const/audit');
- const officeList = require('../const/cld_office').list;
- const maintainConst = require('../const/maintain');
- module.exports = app => {
- class DashboardController extends app.BaseController {
- /**
- * 控制面板页面
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async index(ctx) {
- const auditTenders = await ctx.service.ledgerAudit.getAuditTender(ctx.session.sessionUser.accountId);
- const auditStages = await ctx.service.stageAudit.getAuditStage(ctx.session.sessionUser.accountId);
- const auditChanges = await ctx.service.changeAudit.getAuditChange(ctx.session.sessionUser.accountId);
- const auditRevise = await ctx.service.reviseAudit.getAuditRevise(ctx.session.sessionUser.accountId);
- const auditMaterial = await ctx.service.materialAudit.getAuditMaterial(ctx.session.sessionUser.accountId);
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const lastNotice = pa.last_notice ? pa.last_notice : (pa.last_notice === 0 ? new Date() : new Date(pa.last_login * 1000));
- const noticeLedger = await ctx.service.ledgerAudit.getNoticeTender(ctx.session.sessionProject.id, pa.id, lastNotice);
- const noticeStage = await ctx.service.stageAudit.getNoticeStage(ctx.session.sessionProject.id, pa.id, lastNotice);
- const noticeChange = await ctx.service.changeAudit.getNoticeChange(ctx.session.sessionProject.id, pa.id, lastNotice);
- const noticeRevise = await ctx.service.reviseAudit.getNoticeRevise(ctx.session.sessionProject.id, pa.id, lastNotice);
- const noticeMaterial = await ctx.service.materialAudit.getNoticeMaterial(ctx.session.sessionProject.id, pa.id, lastNotice);
- // const noticeLedger = [];
- // const noticeStage = [];
- // const noticeChange = [];
- // const noticeRevise = [];
- // const noticeMaterial = [];
- const projectData = await ctx.service.project.getDataById(ctx.session.sessionProject.id);
- // 获取销售人员数据
- const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
- const officeName = officeList[salesmanData.office];
- // 获取版本信息
- const versionList = await ctx.service.version.getAllDataByCondition({ orders: [['id', 'desc']], limit: 5, offset: 0 });
- // 获取项目通知
- const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id);
- const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
- const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
- // 获取系统通知
- const sysMsgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, 2, 0, 2);
- // 获取系统维护信息
- const maintainData = await ctx.service.maintain.getDataById(1);
- const renderData = {
- auditTenders,
- auditStages,
- auditChanges,
- auditRevise,
- auditMaterial,
- role: pa.role,
- authMobile: pa.auth_mobile,
- acLedger: auditConst.ledger,
- acStage: auditConst.stage,
- acChange: auditConst.flow,
- acRevise: auditConst.revise,
- acMaterial: auditConst.material,
- noticeLedger,
- noticeStage,
- noticeChange,
- noticeRevise,
- noticeMaterial,
- projectData,
- salesmanData,
- officeName,
- versionList: JSON.parse(JSON.stringify(versionList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, '	')),
- msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, '	')),
- sysMsgList: JSON.parse(JSON.stringify(sysMsgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, '	')),
- userMsgPermission,
- uid: ctx.session.sessionUser.accountId,
- maintainData,
- maintainConst,
- };
- await this.layout('dashboard/index.ejs', renderData, 'dashboard/modal.ejs');
- await ctx.service.projectAccount.defaultUpdate({
- id: this.ctx.session.sessionUser.accountId,
- last_notice: new Date(),
- });
- }
- /**
- * 控制面板-通知页面
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async msgList(ctx) {
- const page = ctx.page;
- const type = ctx.request.query.type ? parseInt(ctx.request.query.type) : 1;
- const total = type === 1 ?
- await ctx.service.message.count({ project_id: ctx.session.sessionProject.id, type }) :
- await ctx.service.message.count({ status: 1, type });
- const limit = 5;
- const offset = limit * (this.ctx.page - 1);
- const msgList = await ctx.service.message.getMsgList(ctx.session.sessionProject.id, limit, offset, type);
- const pa = await ctx.service.projectAccount.getDataById(ctx.session.sessionUser.accountId);
- const userPermission = pa !== undefined && pa.permission !== '' ? JSON.parse(pa.permission) : null;
- const userMsgPermission = userPermission !== null && userPermission.project_msg !== undefined && parseInt(userPermission.project_msg) === 1;
- // 分页相关
- const pageInfo = {
- page,
- total: Math.ceil(total / limit),
- queryData: JSON.stringify(ctx.urlInfo.query),
- };
- const renderData = {
- uid: ctx.session.sessionUser.accountId,
- type,
- pageInfo,
- userMsgPermission,
- msgList: JSON.parse(JSON.stringify(msgList).replace(/\\r\\n/g, '<br>').replace(/\\"/g, '"').replace(/'/g, ''').replace(/\\t/g, '	')),
- };
- await this.layout('dashboard/msg.ejs', renderData);
- }
- /**
- * 控制面板-通知添加和编辑页面
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async msgAdd(ctx) {
- let id = ctx.params.id;
- id = parseInt(id);
- try {
- if (isNaN(id) || id < 0) {
- throw '参数错误';
- }
- const rule = ctx.service.message.rule();
- const jsValidator = await this.jsValidator.convert(rule).build();
- const msgInfo = id === 0 ? {} : await ctx.service.message.getDataById(id);
- const renderData = {
- jsValidator,
- msgInfo,
- };
- await this.layout('dashboard/msg_add.ejs', renderData, 'dashboard/msg_modal.ejs');
- } catch (error) {
- // this.setMessage(error.toString(), this.messageType.ERROR);
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 控制面板-通知保存
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async msgSet(ctx) {
- try {
- let id = ctx.params.id;
- id = parseInt(id);
- if (isNaN(id) || id < 0) {
- throw '参数错误';
- }
- const rule = ctx.service.message.rule();
- ctx.helper.validate(rule);
- const result = await ctx.service.message.save(id, ctx.request.body, ctx.session.sessionUser, ctx.session.sessionProject.id);
- if (result) {
- ctx.redirect('/dashboard/msg/list');
- }
- } catch (error) {
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 控制面板-通知删除
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async msgDelete(ctx) {
- try {
- let id = ctx.params.id;
- id = parseInt(id);
- if (isNaN(id) || id <= 0) {
- throw '参数错误';
- }
- const msgInfo = await ctx.service.message.getDataById(id);
- if (!msgInfo || msgInfo.create_uid !== ctx.session.sessionUser.accountId) {
- throw '通知不存在或无权限操作';
- }
- const result = await ctx.service.message.deleteById(msgInfo.id);
- if (result) {
- ctx.redirect('/dashboard/msg/list');
- }
- } catch (error) {
- ctx.redirect(ctx.request.header.referer);
- }
- }
- /**
- * 将推送记录设置为已读
- * @param {Object} ctx 上下文
- */
- async pushSet(ctx) {
- try {
- const { id } = JSON.parse(ctx.request.body.data);
- const data = await ctx.service.noticePush.set(id);
- ctx.body = { err: 0, msg: '' };
- } catch (err) {
- this.log(err);
- ctx.body = { err: 1, msg: err.toString(), data: null };
- }
- }
- }
- return DashboardController;
- };
|