setting_controller.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 officeName = officeList[salesmanData.office];
  44. const date = new Date(projectData.create_time * 1000);// 如果date为10位不需要乘1000
  45. const Y = date.getFullYear() + '-';
  46. const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
  47. const D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  48. const h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  49. const m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  50. const s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds());
  51. const dateStr = Y + M + D + h + m + s;
  52. const renderData = {
  53. projectData,
  54. salesmanData,
  55. officeName,
  56. officeList,
  57. jsValidator,
  58. dateStr,
  59. };
  60. await this.layout('setting/info.ejs', renderData);
  61. } catch (error) {
  62. console.log(error);
  63. ctx.redirect('/dashboard');
  64. }
  65. }
  66. /**
  67. * 项目设置 -- 账号设置(Get)
  68. * @param ctx
  69. * @return {Promise<void>}
  70. */
  71. async user(ctx) {
  72. try {
  73. // 获取项目数据
  74. const projectId = ctx.session.sessionProject.id;
  75. const projectData = await ctx.service.project.getDataById(projectId);
  76. if (projectData === null) {
  77. throw '没有对应的项目数据';
  78. }
  79. const renderData = {
  80. projectData,
  81. };
  82. await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');
  83. } catch (error) {
  84. console.log(error);
  85. ctx.redirect('/dashboard');
  86. }
  87. }
  88. /**
  89. * 项目设置 -- 自定义标段分类(Get)
  90. *
  91. * @param ctx
  92. * @return {Promise<void>}
  93. */
  94. async category(ctx) {
  95. try {
  96. // 获取项目数据
  97. const projectId = ctx.session.sessionProject.id;
  98. const projectData = await ctx.service.project.getDataById(projectId);
  99. if (projectData === null) {
  100. throw '没有对应的项目数据';
  101. }
  102. const categoryData = await ctx.service.category.getAllCategory(projectId);
  103. const tenderData = await ctx.service.tender.getList();
  104. const renderData = {
  105. projectData,
  106. categoryType: settingConst.cType,
  107. categoryData,
  108. tenderData,
  109. };
  110. await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
  111. } catch (error) {
  112. console.log(error);
  113. ctx.redirect('/dashboard');
  114. }
  115. }
  116. /**
  117. * 新增分类(Ajax)
  118. *
  119. * @param ctx
  120. * @return {Promise<void>}
  121. */
  122. async addCategory(ctx) {
  123. try {
  124. const projectId = ctx.session.sessionProject.id;
  125. const responseData = {
  126. err: 0, msg: '', data: null,
  127. };
  128. const data = JSON.parse(ctx.request.body.data);
  129. if (!data.name || !data.type) {
  130. throw '提交数据错误';
  131. }
  132. responseData.data = await ctx.service.category.addCategory(projectId, data.name, data.type);
  133. ctx.body = responseData;
  134. } catch (err) {
  135. this.log(err);
  136. ctx.body = { err: 1, msg: err.toString(), data: null };
  137. }
  138. }
  139. /**
  140. * 编辑分类(Ajax)
  141. *
  142. * @param ctx
  143. * @return {Promise<void>}
  144. */
  145. async updateCategory(ctx) {
  146. try {
  147. const projectId = ctx.session.sessionProject.id;
  148. const responseData = { err: 0, msg: '', data: null };
  149. const data = JSON.parse(ctx.request.body.data);
  150. if (!data.id) {
  151. throw '提交数据错误';
  152. }
  153. if (data.name) {
  154. const count = await ctx.service.category.count({ pid: projectId, name: data.name });
  155. if (count >= 1) {
  156. throw '存在同名类别';
  157. }
  158. }
  159. const result = await ctx.service.category.update(data, { id: data.id });
  160. if (!result) {
  161. throw '提交数据失败';
  162. }
  163. responseData.data = await ctx.service.category.getCategory(data.id);
  164. ctx.body = responseData;
  165. } catch (err) {
  166. this.log(err);
  167. ctx.body = { err: 1, msg: err.toString(), data: null };
  168. }
  169. }
  170. async setCategoryValue(ctx) {
  171. try {
  172. const responseData = { err: 0, msg: '', data: {} };
  173. const data = JSON.parse(ctx.request.body.data);
  174. if (!data.id) {
  175. throw '提交数据错误';
  176. }
  177. await ctx.service.categoryValue.setCategoryValue(data.id, data.updateValue);
  178. responseData.data.category = await ctx.service.category.getCategory(data.id);
  179. responseData.data.tenders = await ctx.service.tender.getList();
  180. ctx.body = responseData;
  181. } catch (err) {
  182. this.log(err);
  183. ctx.body = { err: 1, msg: err instanceof String ? err : '提交数据失败', data: null };
  184. }
  185. }
  186. /**
  187. * 删除分类(Ajax)
  188. *
  189. * @param ctx
  190. * @return {Promise<void>}
  191. */
  192. async deleteCategory(ctx) {
  193. try {
  194. const projectId = ctx.session.sessionProject.id;
  195. const responseData = {
  196. err: 0, msg: '', data: null,
  197. };
  198. const data = JSON.parse(ctx.request.body.data);
  199. if (!data.id) {
  200. throw '提交数据错误';
  201. }
  202. await ctx.service.category.deleteCategory(projectId, data.id);
  203. ctx.body = responseData;
  204. } catch (err) {
  205. this.log(err);
  206. ctx.body = { err: 1, msg: err.toString(), data: null };
  207. }
  208. }
  209. /**
  210. * 调整分类层次排序(Ajax)
  211. *
  212. * @param ctx
  213. * @return {Promise<void>}
  214. */
  215. async resetCategoryLevel(ctx) {
  216. try {
  217. const projectId = ctx.session.sessionProject.id;
  218. const responseData = {
  219. err: 0, msg: '', data: null,
  220. };
  221. const data = JSON.parse(ctx.request.body.data);
  222. await ctx.service.category.resetCategoryLevel(data);
  223. responseData.data = await ctx.service.category.getAllCategory(projectId);
  224. ctx.body = responseData;
  225. } catch (err) {
  226. this.log(err);
  227. ctx.body = { err: 1, msg: err.toString(), data: null };
  228. }
  229. }
  230. /** update porject info
  231. * @author wangfeng
  232. * @date 2018-10-12 15:48:05
  233. * @param ctx
  234. * @return {Promise<void>}
  235. */
  236. async updateinfo(ctx) {
  237. try {
  238. const projectId = ctx.params.id;
  239. const responseData = {
  240. err: 0, msg: '', data: null,
  241. };
  242. const conditionData = {
  243. id: projectId,
  244. };
  245. const data = ctx.request.body;
  246. const result = await ctx.service.project.update(data, conditionData);
  247. if (!result) {
  248. throw '提交数据失败';
  249. }
  250. ctx.redirect('/setting/info');
  251. } catch (err) {
  252. this.log(err);
  253. ctx.body = { err: 1, msg: err.toString(), data: null };
  254. }
  255. }
  256. }
  257. return SettingController;
  258. };