setting_controller.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date
  7. * @version
  8. */
  9. const officeList = require('../const/cld_office').list;
  10. const settingConst = require('../const/setting.js');
  11. const settingMenu = require('../../config/menu').settingMenu;
  12. module.exports = app => {
  13. class SettingController extends app.BaseController {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局context
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. ctx.subMenu = settingMenu;
  23. }
  24. /**
  25. * 项目信息页面(Get)
  26. *
  27. * @param {Object} ctx - egg全局变量
  28. * @return {void}
  29. */
  30. async info(ctx) {
  31. try {
  32. // 获取项目数据
  33. const projectId = ctx.session.sessionProject.id;
  34. const projectData = await ctx.service.project.getDataById(projectId);
  35. if (projectData === null) {
  36. throw '没有对应的项目数据';
  37. }
  38. // 获取销售人员数据
  39. const salesmanData = await ctx.service.manager.getDataById(projectData.manager_id);
  40. // 数据规则
  41. const rule = ctx.service.project.rule('saveInfo');
  42. const jsValidator = await this.jsValidator.convert(rule).build();
  43. const renderData = {
  44. projectData,
  45. salesmanData,
  46. officeList,
  47. jsValidator,
  48. };
  49. await this.layout('setting/info.ejs', renderData);
  50. } catch (error) {
  51. console.log(error);
  52. ctx.redirect('/dashboard');
  53. }
  54. }
  55. /**
  56. * 项目设置 -- 账号设置(Get)
  57. * @param ctx
  58. * @returns {Promise<void>}
  59. */
  60. async user(ctx) {
  61. try {
  62. // 获取项目数据
  63. const projectId = ctx.session.sessionProject.id;
  64. const projectData = await ctx.service.project.getDataById(projectId);
  65. if (projectData === null) {
  66. throw '没有对应的项目数据';
  67. }
  68. const renderData = {
  69. projectData,
  70. };
  71. await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');
  72. } catch (error) {
  73. console.log(error);
  74. ctx.redirect('/dashboard');
  75. }
  76. }
  77. /**
  78. * 项目设置 -- 自定义标段分类(Get)
  79. *
  80. * @param ctx
  81. * @returns {Promise<void>}
  82. */
  83. async category(ctx) {
  84. try {
  85. // 获取项目数据
  86. const projectId = ctx.session.sessionProject.id;
  87. const projectData = await ctx.service.project.getDataById(projectId);
  88. if (projectData === null) {
  89. throw '没有对应的项目数据';
  90. }
  91. const categoryData = await ctx.service.category.getAllCategory(projectId);
  92. const tenderData = await ctx.service.tender.getList();
  93. const renderData = {
  94. projectData,
  95. categoryType: settingConst.cType,
  96. categoryData,
  97. tenderData,
  98. };
  99. await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
  100. } catch (error) {
  101. console.log(error);
  102. ctx.redirect('/dashboard');
  103. }
  104. }
  105. /**
  106. * 新增分类(Ajax)
  107. *
  108. * @param ctx
  109. * @returns {Promise<void>}
  110. */
  111. async addCategory(ctx) {
  112. try {
  113. const projectId = ctx.session.sessionProject.id;
  114. const responseData = {
  115. err: 0, msg: '', data: null,
  116. };
  117. const data = JSON.parse(ctx.request.body.data);
  118. if (!data.name || !data.type) {
  119. throw '提交数据错误';
  120. }
  121. responseData.data = await ctx.service.category.addCategory(projectId, data.name, data.type);
  122. ctx.body = responseData;
  123. } catch (err) {
  124. console.log(err);
  125. ctx.body = {err: 1, msg: err.toString(), data: null};
  126. }
  127. }
  128. /**
  129. * 编辑分类(Ajax)
  130. *
  131. * @param ctx
  132. * @returns {Promise<void>}
  133. */
  134. async updateCategory(ctx) {
  135. try {
  136. const projectId = ctx.session.sessionProject.id;
  137. const responseData = { err: 0, msg: '', data: null, };
  138. const data = JSON.parse(ctx.request.body.data);
  139. if (!data.id) {
  140. throw '提交数据错误';
  141. }
  142. if (data.name) {
  143. const count = await ctx.service.category.count({pid: projectId, name: data.name});
  144. if (count >= 1) {
  145. throw '存在同名类别';
  146. }
  147. }
  148. const result = await ctx.service.category.update(data, {id: data.id});
  149. if (!result) {
  150. throw '提交数据失败';
  151. }
  152. responseData.data = await ctx.service.category.getCategory(data.id);
  153. ctx.body = responseData;
  154. } catch (err) {
  155. console.log(err);
  156. ctx.body = {err: 1, msg: err.toString(), data: null};
  157. }
  158. }
  159. async setCategoryValue(ctx) {
  160. try {
  161. const responseData = { err: 0, msg: '', data: {}, };
  162. const data = JSON.parse(ctx.request.body.data);
  163. if (!data.id) {
  164. throw '提交数据错误';
  165. }
  166. await ctx.service.categoryValue.setCategoryValue(data.id, data.updateValue);
  167. responseData.data.category = await ctx.service.category.getCategory(data.id);
  168. responseData.data.tenders = await ctx.service.tender.getList();
  169. ctx.body = responseData;
  170. } catch (err) {
  171. console.log(err);
  172. ctx.body = {err: 1, msg: err instanceof string ? err : '提交数据失败', data: null};
  173. }
  174. }
  175. /**
  176. * 删除分类(Ajax)
  177. *
  178. * @param ctx
  179. * @returns {Promise<void>}
  180. */
  181. async deleteCategory(ctx) {
  182. try {
  183. const projectId = ctx.session.sessionProject.id;
  184. const responseData = {
  185. err: 0, msg: '', data: null,
  186. };
  187. const data = JSON.parse(ctx.request.body.data);
  188. if (!data.id) {
  189. throw '提交数据错误';
  190. }
  191. await ctx.service.category.deleteCategory(projectId, data.id);
  192. ctx.body = responseData;
  193. } catch(err) {
  194. console.log(err);
  195. ctx.body = {err: 1, msg: err.toString(), data: null};
  196. }
  197. }
  198. /**
  199. * 调整分类层次排序(Ajax)
  200. *
  201. * @param ctx
  202. * @returns {Promise<void>}
  203. */
  204. async resetCategoryLevel(ctx) {
  205. try {
  206. const projectId = ctx.session.sessionProject.id;
  207. const responseData = {
  208. err: 0, msg: '', data: null,
  209. }
  210. const data = JSON.parse(ctx.request.body.data);
  211. await ctx.service.category.resetCategoryLevel(data);
  212. responseData.data = await ctx.service.category.getAllCategory(projectId);
  213. ctx.body = responseData;
  214. } catch (err) {
  215. console.log(err);
  216. ctx.body = {err: 1, msg: err.toString(), data: null};
  217. }
  218. }
  219. }
  220. return SettingController;
  221. };