setting_controller.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. * 项目信息页面
  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. var date = new Date(projectData.create_time*1000);//如果date为10位不需要乘1000
  45. var Y = date.getFullYear() + '-';
  46. var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
  47. var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' ';
  48. var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':';
  49. var m = (date.getMinutes() <10 ? '0' + date.getMinutes() : date.getMinutes()) + ':';
  50. var 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. async user(ctx) {
  67. try {
  68. // 获取项目数据
  69. const projectId = ctx.session.sessionProject.id;
  70. const projectData = await ctx.service.project.getDataById(projectId);
  71. if (projectData === null) {
  72. throw '没有对应的项目数据';
  73. }
  74. const renderData = {
  75. projectData,
  76. };
  77. await this.layout('setting/user.ejs', renderData, 'setting/user_modal.ejs');
  78. } catch (error) {
  79. console.log(error);
  80. ctx.redirect('/dashboard');
  81. }
  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({ where: { pid: projectId } });
  92. const renderData = {
  93. projectData,
  94. categoryType: settingConst.cType,
  95. categoryData,
  96. };
  97. await this.layout('setting/category.ejs', renderData, 'setting/category_modal.ejs');
  98. } catch (error) {
  99. console.log(error);
  100. ctx.redirect('/dashboard');
  101. }
  102. }
  103. async addCategory(ctx) {
  104. try {
  105. const projectId = ctx.session.sessionProject.id;
  106. const responseData = {
  107. err: 0, msg: '', data: null,
  108. };
  109. const data = JSON.parse(ctx.request.body.data);
  110. if (!data.name || !data.type) {
  111. throw '提交数据错误';
  112. }
  113. responseData.data = await ctx.service.category.addCategory(projectId, data.name, data.type);
  114. ctx.body = responseData;
  115. } catch (err) {
  116. console.log(err);
  117. ctx.body = {err: 1, msg: err.toString(), data: null};
  118. }
  119. }
  120. async updateCategory(ctx) {
  121. try {
  122. const projectId = ctx.session.sessionProject.id;
  123. const responseData = {
  124. err: 0, msg: '', data: null,
  125. }
  126. const data = JSON.parse(ctx.request.body.data);
  127. if (!data.id) {
  128. throw '提交数据错误';
  129. }
  130. if (data.name) {
  131. const count = await ctx.service.category.count({pid: projectId, name: data.name});
  132. if (count >= 1) {
  133. throw '存在同名类别';
  134. }
  135. }
  136. if (data.value) {
  137. data.value = JSON.stringify(data.value)
  138. }
  139. const result = await ctx.service.category.update(data, {id: data.id});
  140. if (!result) {
  141. throw '提交数据失败'
  142. }
  143. responseData.data = await ctx.service.category.getCategory({id: data.id});
  144. ctx.body = responseData;
  145. } catch (err) {
  146. console.log(err);
  147. ctx.body = {err: 1, msg: err.toString(), data: null};
  148. }
  149. }
  150. async deleteCategory(ctx) {
  151. try {
  152. const projectId = ctx.session.sessionProject.id;
  153. const responseData = {
  154. err: 0, msg: '', data: null,
  155. };
  156. const data = JSON.parse(ctx.request.body.data);
  157. if (!data.id) {
  158. throw '提交数据错误';
  159. }
  160. const result = await ctx.service.category.deleteById(data.id);
  161. if (!result) {
  162. throw '提交数据失败'
  163. }
  164. ctx.body = responseData;
  165. } catch(err) {
  166. console.log(err);
  167. ctx.body = {err: 1, msg: err.toString(), data: null};
  168. }
  169. }
  170. /** update porject info
  171. * @author wangfeng
  172. * @date 2018-10-12 15:48:05
  173. * @param ctx
  174. * @returns {Promise<void>}
  175. */
  176. async updateinfo(ctx){
  177. try {
  178. const projectId = ctx.params.id;
  179. const responseData = {
  180. err: 0, msg: '', data: null,
  181. }
  182. const conditionData = {
  183. id:projectId
  184. }
  185. const data = ctx.request.body;
  186. const result = await ctx.service.project.update(data, conditionData);
  187. if (!result) {
  188. throw '提交数据失败'
  189. }
  190. ctx.redirect('/setting/info');
  191. } catch (err) {
  192. console.log(err);
  193. ctx.body = {err: 1, msg: err.toString(), data: null};
  194. }
  195. }
  196. }
  197. return SettingController;
  198. };