123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- 'use strict';
- /**
- * 项目相关控制器
- *
- * @author CaiAoLin
- * @date 2017/11/29
- * @version
- */
- const officeList = require('../const/cld_office').list;
- module.exports = app => {
- class ProjectController extends app.BaseController {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- ctx.showProject = true;
- }
- /**
- * 项目信息页面
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async info(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- 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 renderData = {
- projectData,
- salesmanData,
- officeList,
- jsValidator,
- };
- await this.layout('project/info.ejs', renderData);
- } catch (error) {
- this.setMessage(error.toString(), this.messageType.ERROR);
- ctx.redirect('/dashboard');
- }
- }
- /**
- * 保存项目信息操作
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async saveInfo(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionUser.projectId;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- const rule = ctx.service.project.rule('saveInfo');
- ctx.validate(rule);
- const updateData = {
- name: ctx.request.body.name,
- };
- const result = await ctx.service.project.update(updateData, { id: projectData.id });
- if (!result) {
- throw '保存项目信息失败';
- }
- this.setMessage('保存成功', this.messageType.SUCCESS);
- } catch (error) {
- this.setMessage(error.toString(), this.messageType.ERROR);
- }
- ctx.redirect(ctx.request.header.referer);
- }
- /**
- * 切换项目
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async switchProject(ctx) {
- let projectId = ctx.params.projectId;
- try {
- projectId = parseInt(projectId);
- if (isNaN(projectId) || projectId <= 0) {
- throw '参数错误!';
- }
- const result = await ctx.service.project.switchProject(projectId);
- if (!result) {
- throw '切换项目失败!';
- }
- } catch (error) {
- this.setMessage(error.toString(), this.messageType.ERROR);
- }
- ctx.redirect(ctx.request.header.referer);
- }
- /**
- * 查询项目或企业(暂无)的参与人
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- async searchAccount(ctx) {
- const projectId = ctx.session.sessionProject.id;
- const responseData = {
- err: 0,
- msg: '',
- data: [],
- };
- try {
- if (!projectId) {
- throw '当前未打开项目';
- }
- const data = JSON.parse(ctx.request.body.data);
- const name = data.keyword;
- if (!name || name === '') {
- throw '请输入姓名进行检索';
- }
- // 查询方式,默认为只查询一个
- const type = data.type;
- if (type === undefined) {
- responseData.data = await ctx.service.projectAccount.getAccountInfoByName(projectId, name);
- } else if (type === 'more') {
- responseData.data = await ctx.service.projectAccount.getAccountInfoByName(projectId, name, 1);
- }
- } catch (err) {
- responseData.err = 1;
- responseData.msg = err.toString();
- }
- ctx.body = responseData;
- }
- }
- return ProjectController;
- };
|