project_controller.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. /**
  3. * 项目相关控制器
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/29
  7. * @version
  8. */
  9. const officeList = require('../const/cld_office').list;
  10. module.exports = app => {
  11. class ProjectController extends app.BaseController {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局context
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. ctx.showProject = true;
  21. }
  22. /**
  23. * 项目信息页面
  24. *
  25. * @param {Object} ctx - egg全局变量
  26. * @return {void}
  27. */
  28. async info(ctx) {
  29. try {
  30. // 获取项目数据
  31. const projectId = ctx.session.sessionProject.id;
  32. const projectData = await ctx.service.project.getDataById(projectId);
  33. if (projectData === null) {
  34. throw '没有对应的项目数据';
  35. }
  36. // 获取销售人员数据
  37. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  38. // 数据规则
  39. const rule = ctx.service.project.rule('saveInfo');
  40. const jsValidator = await this.jsValidator.convert(rule).build();
  41. const renderData = {
  42. projectData,
  43. salesmanData,
  44. officeList,
  45. jsValidator,
  46. };
  47. await this.layout('project/info.ejs', renderData);
  48. } catch (error) {
  49. this.setMessage(error.toString(), this.messageType.ERROR);
  50. ctx.redirect('/dashboard');
  51. }
  52. }
  53. /**
  54. * 保存项目信息操作
  55. *
  56. * @param {Object} ctx - egg全局变量
  57. * @return {void}
  58. */
  59. async saveInfo(ctx) {
  60. try {
  61. // 获取项目数据
  62. const projectId = ctx.session.sessionUser.projectId;
  63. const projectData = await ctx.service.project.getDataById(projectId);
  64. if (projectData === null) {
  65. throw '没有对应的项目数据';
  66. }
  67. const rule = ctx.service.project.rule('saveInfo');
  68. ctx.validate(rule);
  69. const updateData = {
  70. name: ctx.request.body.name,
  71. };
  72. const result = await ctx.service.project.update(updateData, { id: projectData.id });
  73. if (!result) {
  74. throw '保存项目信息失败';
  75. }
  76. this.setMessage('保存成功', this.messageType.SUCCESS);
  77. } catch (error) {
  78. this.setMessage(error.toString(), this.messageType.ERROR);
  79. }
  80. ctx.redirect(ctx.request.header.referer);
  81. }
  82. /**
  83. * 切换项目
  84. *
  85. * @param {Object} ctx - egg全局变量
  86. * @return {void}
  87. */
  88. async switchProject(ctx) {
  89. let projectId = ctx.params.projectId;
  90. try {
  91. projectId = parseInt(projectId);
  92. if (isNaN(projectId) || projectId <= 0) {
  93. throw '参数错误!';
  94. }
  95. const result = await ctx.service.project.switchProject(projectId);
  96. if (!result) {
  97. throw '切换项目失败!';
  98. }
  99. } catch (error) {
  100. this.setMessage(error.toString(), this.messageType.ERROR);
  101. }
  102. ctx.redirect(ctx.request.header.referer);
  103. }
  104. /**
  105. * 查询项目或企业(暂无)的参与人
  106. *
  107. * @param {Object} ctx - egg全局变量
  108. * @return {void}
  109. */
  110. async searchAccount(ctx) {
  111. const projectId = ctx.session.sessionProject.id;
  112. const responseData = {
  113. err: 0,
  114. msg: '',
  115. data: [],
  116. };
  117. try {
  118. if (!projectId) {
  119. throw '当前未打开项目';
  120. }
  121. const data = JSON.parse(ctx.request.body.data);
  122. const name = data.keyword;
  123. if (!name || name === '') {
  124. throw '请输入姓名进行检索';
  125. }
  126. // 查询方式,默认为只查询一个
  127. const type = data.type;
  128. if (type === undefined) {
  129. responseData.data = await ctx.service.projectAccount.getAccountInfoByName(projectId, name);
  130. } else if (type === 'more') {
  131. responseData.data = await ctx.service.projectAccount.getAccountInfoByName(projectId, name, 1);
  132. }
  133. } catch (err) {
  134. responseData.err = 1;
  135. responseData.msg = err.toString();
  136. }
  137. ctx.body = responseData;
  138. }
  139. }
  140. return ProjectController;
  141. };