123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- 'use strict';
- /**
- *
- *
- * @author Mai
- * @date
- * @version
- */
- const officeList = require('../const/cld_office').list;
- const settingConst = require('../const/setting.js');
- const settingMenu = require('../../config/menu').settingMenu;
- module.exports = app => {
- class SettingController extends app.BaseController {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局context
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- ctx.subMenu = settingMenu;
- }
- /**
- * 项目信息页面
- *
- * @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('setting/info.ejs', renderData);
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- async user(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- const renderData = {
- projectData,
- };
- await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- async category(ctx) {
- try {
- // 获取项目数据
- const projectId = ctx.session.sessionProject.id;
- const projectData = await ctx.service.project.getDataById(projectId);
- if (projectData === null) {
- throw '没有对应的项目数据';
- }
- const categoryData = await ctx.service.category.getAllCategory({ where: { pid: projectId } });
- const renderData = {
- projectData,
- categoryType: settingConst.cType,
- categoryData,
- };
- await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
- } catch (error) {
- console.log(error);
- ctx.redirect('/dashboard');
- }
- }
- 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 || !data.type) {
- throw '提交数据错误';
- }
- responseData.data = await ctx.service.category.addCategory(projectId, data.name, data.type);
- ctx.body = responseData;
- } catch (err) {
- console.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- 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 '存在同名类别';
- }
- }
- if (data.value) {
- data.value = JSON.stringify(data.value)
- }
- const result = await ctx.service.category.update(data, {id: data.id});
- if (!result) {
- throw '提交数据失败'
- }
- responseData.data = await ctx.service.category.getCategory({id: data.id});
- ctx.body = responseData;
- } catch (err) {
- console.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- 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 '提交数据错误';
- }
- const result = await ctx.service.category.deleteById(data.id);
- if (!result) {
- throw '提交数据失败'
- }
- ctx.body = responseData;
- } catch(err) {
- console.log(err);
- ctx.body = {err: 1, msg: err.toString(), data: null};
- }
- }
- }
- return SettingController;
- };
|