ProjectManagement.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <template>
  2. <div class="container-fluid">
  3. <Navbar
  4. :title="title"
  5. :is-go-back="true"
  6. />
  7. <GanttScale
  8. ref="ganttScale"
  9. gantt-div-id="ganttContainer"
  10. />
  11. <div style="line-height: 10px">
  12. <br />
  13. </div>
  14. <div
  15. id="ganttContainer"
  16. style="width:100%; height:100%px; min-height: 500px; background-color:white;"
  17. />
  18. </div>
  19. </template>
  20. <script>
  21. var Common = require('../common/Common.js');
  22. var Navbar = require('pc-component-v3').default.Navbar;
  23. var Loading = require('pc-component-v3').default.Loading;
  24. var GanttScale = require('../widget/GanttScale2.vue').default;
  25. module.exports = {
  26. data: function () {
  27. return {
  28. title: '',
  29. projectId: undefined,
  30. userList: [],
  31. canEdit: false,
  32. };
  33. },
  34. components: {
  35. Common,
  36. Navbar,
  37. Loading,
  38. GanttScale,
  39. },
  40. methods: {
  41. /**
  42. * 上一步
  43. */
  44. undo: function () {
  45. gantt.undo();
  46. },
  47. /**
  48. * 下一步
  49. */
  50. redo: function () {
  51. gantt.redo();
  52. },
  53. //查询所有任务及链接
  54. showTaskDtos: function () {
  55. var _self = this;
  56. var obj = _self.$route.params.projectId;
  57. $.ajax({
  58. url: Common.getApiURL('ProjectTaskResource/queryProjectAndTasks'),
  59. type: 'get',
  60. dataType: 'json',
  61. contentType: 'application/json',
  62. data: {
  63. 'projectId': obj,
  64. },
  65. beforeSend: function (request) {
  66. Common.addTokenToRequest(request);
  67. },
  68. success: function (data) {
  69. if (data == null) {
  70. return;
  71. }
  72. _self.title = data.projectname + '-时间节点';
  73. var projectTaskDtos = data.data;
  74. var events = [];
  75. var projectTaskRelationDtos = data.links;
  76. var events2 = [];
  77. for (var i = 0; i < projectTaskDtos.length; i++) {
  78. var event = {
  79. id: projectTaskDtos[i].id,
  80. text: projectTaskDtos[i].name,
  81. remarks: projectTaskDtos[i].remarks,
  82. owner_id: projectTaskDtos[i].owenUserId,
  83. start_date: projectTaskDtos[i].startDate,
  84. duration: projectTaskDtos[i].duration,
  85. parent: projectTaskDtos[i].parentId,
  86. type: projectTaskDtos[i].type,
  87. open: true,
  88. progress: projectTaskDtos[i].progress,
  89. index: projectTaskDtos[i].sequenceNo,
  90. };
  91. events.push(event);
  92. }
  93. for (let i = 0; i < projectTaskRelationDtos.length; i++) {
  94. let event = {
  95. id: projectTaskRelationDtos[i].id,
  96. source: projectTaskRelationDtos[i].predecessorTaskId,
  97. target: projectTaskRelationDtos[i].taskId,
  98. type: projectTaskRelationDtos[i].type,
  99. };
  100. events2.push(event);
  101. }
  102. var tasks = {
  103. data: events,
  104. links: events2,
  105. };
  106. gantt.clearAll();
  107. gantt.parse(tasks);
  108. },
  109. error: function (XMLHttpRequest, textStatus, errorThrown) {
  110. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  111. },
  112. });
  113. },
  114. /**
  115. * 初始化用户
  116. * @author ZhangTeng 20190221
  117. */
  118. initData: function () {
  119. var _self = this;
  120. //var obj = _self.$route.params.projectId;
  121. $.ajax({
  122. url: Common.getApiURL('TraceResource/queryUserByProjectId'),
  123. type: 'get',
  124. dataType: 'json',
  125. contentType: 'application/json',
  126. data: {
  127. 'projectId': _self.projectId,
  128. },
  129. beforeSend: function (request) {
  130. Common.addTokenToRequest(request);
  131. },
  132. success: function (data) {
  133. for (var x = 0; x < data.length; x++) {
  134. var event = {
  135. key: data[x].id,
  136. label: data[x].name,
  137. };
  138. _self.userList.push(event);
  139. }
  140. _self.initGantt();
  141. },
  142. error: function (XMLHttpRequest, textStatus, errorThrown) {
  143. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  144. },
  145. });
  146. },
  147. //保存数据
  148. runSaveGantt: function () {
  149. var _self = this;
  150. var canEdit = _self.canEdit;
  151. if (canEdit == false) {
  152. return;
  153. }
  154. var ganttData = [];
  155. var tasks = gantt.getTaskByTime();
  156. for (var x = 0; x < tasks.length; x++) {
  157. var globalTaskIndex = gantt.getGlobalTaskIndex(tasks[x].id);
  158. var event = {
  159. id: tasks[x].id,
  160. parentId: tasks[x].parent == 0 ? undefined : tasks[x].parent,
  161. name: tasks[x].text,
  162. type: tasks[x].type,
  163. projectItemId: _self.$route.params.projectId,
  164. startDate: tasks[x].start_date,
  165. endDate: tasks[x].end_date,
  166. progress: tasks[x].progress,
  167. sequenceNo: globalTaskIndex,
  168. remarks: tasks[x].remarks,
  169. owenUserId: tasks[x].owner_id,
  170. };
  171. ganttData.push(event);
  172. }
  173. var links = gantt.getLinks();
  174. var links2 = [];
  175. for (let x = 0; x < links.length; x++) {
  176. let event = {
  177. id: links[x].id,
  178. predecessorTaskId: links[x].source,
  179. taskId: links[x].target,
  180. type: links[x].type,
  181. };
  182. links2.push(event);
  183. }
  184. var projectItemDto2 = {
  185. projectId: _self.$route.params.projectId,
  186. data: ganttData,
  187. links: links2,
  188. };
  189. $.ajax({
  190. url: Common.getApiURL('ProjectTaskResource/saveTasksAndLinks'),
  191. type: 'post',
  192. dataType: 'json',
  193. contentType: 'application/json',
  194. data: JSON.stringify(projectItemDto2),
  195. beforeSend: function (request) {
  196. Common.addTokenToRequest(request);
  197. },
  198. success: function (data) {
  199. return;
  200. },
  201. error: function (XMLHttpRequest, textStatus, errorThrown) {
  202. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  203. },
  204. });
  205. },
  206. //获得登录用户权限
  207. personnelJurisdictionSet: function () {
  208. var _self = this;
  209. $.ajax({
  210. url: Common.getApiURL('TraceResource/queryPersonnelJurisdiction'),
  211. type: 'get',
  212. dataType: 'json',
  213. contentType: 'application/json',
  214. data: {
  215. 'projectId': _self.projectId,
  216. },
  217. beforeSend: function (request) {
  218. Common.addTokenToRequest(request);
  219. },
  220. success: function (data) {
  221. if (data == null) {
  222. return;
  223. }
  224. if (data.levelOfPerson == 1) {
  225. _self.canEdit = false;
  226. }
  227. if (data.levelOfPerson == 2 || data.levelOfPerson == 3) {
  228. _self.canEdit = true;
  229. }
  230. },
  231. error: function (XMLHttpRequest, textStatus, errorThrown) {
  232. Common.processException(XMLHttpRequest, textStatus, errorThrown);
  233. },
  234. });
  235. },
  236. //拿到前台数据
  237. getProjectItemDto: function () {
  238. var _self = this;
  239. var ganttData = [];
  240. var tasks = gantt.getTaskByTime();
  241. for (var x = 0; x < tasks.length; x++) {
  242. var globalTaskIndex = gantt.getGlobalTaskIndex(tasks[x].id);
  243. var event = {
  244. id: tasks[x].id,
  245. parentId: tasks[x].parent == 0 ? undefined : tasks[x].parent,
  246. name: tasks[x].text,
  247. type: tasks[x].type,
  248. projectItemId: _self.$route.params.projectId,
  249. startDate: tasks[x].start_date,
  250. endDate: tasks[x].end_date,
  251. progress: tasks[x].progress,
  252. sequenceNo: globalTaskIndex,
  253. remarks: tasks[x].remarks,
  254. owenUserId: tasks[x].owner_id,
  255. };
  256. ganttData.push(event);
  257. }
  258. var links = gantt.getLinks();
  259. var links2 = [];
  260. for (let x = 0; x < links.length; x++) {
  261. let event = {
  262. id: links[x].id,
  263. predecessorTaskId: links[x].source,
  264. taskId: links[x].target,
  265. type: links[x].type,
  266. };
  267. links2.push(event);
  268. }
  269. var projectItemDto2 = {
  270. projectId: _self.$route.params.projectId,
  271. data: ganttData,
  272. links: links2,
  273. };
  274. return projectItemDto2;
  275. },
  276. //设置返回前台的数据
  277. setDate: function (data) {
  278. var _self = this;
  279. _self.title = data.projectname + '-时间节点';
  280. var projectTaskDtos = data.data;
  281. var events = [];
  282. var projectTaskRelationDtos = data.links;
  283. var events2 = [];
  284. for (var i = 0; i < projectTaskDtos.length; i++) {
  285. var event = {
  286. id: projectTaskDtos[i].id,
  287. text: projectTaskDtos[i].name,
  288. remarks: projectTaskDtos[i].remarks,
  289. owner_id: projectTaskDtos[i].owenUserId,
  290. start_date: projectTaskDtos[i].startDate,
  291. duration: projectTaskDtos[i].duration,
  292. parent: projectTaskDtos[i].parentId,
  293. type: projectTaskDtos[i].type,
  294. open: true,
  295. progress: projectTaskDtos[i].progress,
  296. index: projectTaskDtos[i].sequenceNo,
  297. };
  298. events.push(event);
  299. }
  300. for (let i = 0; i < projectTaskRelationDtos.length; i++) {
  301. let event = {
  302. id: projectTaskRelationDtos[i].id,
  303. source: projectTaskRelationDtos[i].predecessorTaskId,
  304. target: projectTaskRelationDtos[i].taskId,
  305. type: projectTaskRelationDtos[i].type,
  306. };
  307. events2.push(event);
  308. }
  309. var tasks = {
  310. data: events,
  311. links: events2,
  312. };
  313. return tasks;
  314. },
  315. datedifference: function (sDate1, sDate2) {
  316. var dateSpan,
  317. tempDate,
  318. iDays;
  319. if (sDate1 == undefined || sDate2 == undefined) {
  320. return 0;
  321. }
  322. dateSpan = sDate2 - sDate1;
  323. dateSpan = Math.abs(dateSpan);
  324. iDays = (dateSpan / (24 * 3600 * 1000));
  325. return iDays;
  326. },
  327. /**
  328. * 初始化甘特图
  329. */
  330. initGantt: function () {
  331. let _self = this;
  332. gantt.serverList('staff', _self.userList);
  333. // end test data
  334. gantt.config.grid_width = 480;
  335. gantt.config.grid_resize = true;
  336. gantt.config.open_tree_initially = true;
  337. var labels = gantt.locale.labels;
  338. labels.section_description = '任务名';
  339. labels.column_remarks = labels.section_remarks = '备注';
  340. labels.column_owner = labels.section_owner = '责任人';
  341. function byId(list, id) {
  342. for (var i = 0; i < list.length; i++) {
  343. if (list[i].key == id)
  344. return list[i].label || '';
  345. }
  346. return '';
  347. }
  348. gantt.config.columns = [{
  349. name: 'text',
  350. resize: true,
  351. label: '任务名',
  352. tree: true,
  353. width: '*',
  354. align: 'left',
  355. },
  356. {
  357. name: 'start_date',
  358. resize: true,
  359. label: '开始日期',
  360. width: 80,
  361. align: 'center',
  362. },
  363. {
  364. name: 'owner',
  365. resize: true,
  366. width: 50,
  367. align: 'center',
  368. template: function (item) {
  369. return byId(gantt.serverList('staff'), item.owner_id);
  370. },
  371. },
  372. {
  373. name: 'duration',
  374. resize: true,
  375. label: '耗时',
  376. width: 40,
  377. align: 'center',
  378. },
  379. {
  380. name: 'add',
  381. width: 40,
  382. },
  383. ];
  384. // gantt.locale.labels.section_period = "时间范围";
  385. // gantt.config.lightbox_additional_height = 120;
  386. var sections = [{
  387. name: 'description',
  388. height: 40,
  389. map_to: 'text',
  390. type: 'textarea',
  391. },
  392. {
  393. name: 'owner',
  394. height: 26,
  395. map_to: 'owner_id',
  396. type: 'select',
  397. options: gantt.serverList('staff'),
  398. },
  399. {
  400. name: 'type',
  401. height: 26,
  402. type: 'typeselect',
  403. map_to: 'type',
  404. },
  405. {
  406. name: 'time',
  407. height: 26,
  408. type: 'duration',
  409. map_to: 'auto',
  410. },
  411. {
  412. name: 'remarks',
  413. map_to: 'remarks',
  414. type: 'textarea',
  415. },
  416. ];
  417. gantt.config.lightbox.sections = sections;
  418. gantt.config.lightbox.project_sections = sections;
  419. gantt.config.lightbox.milestone_sections = sections;
  420. gantt.templates.task_text = function (start, end, task) {
  421. var progress = task.progress || 0;
  422. return task.text + '(' + Math.floor(progress * 100) + '%' + ')';
  423. };
  424. //gantt.templates.grid_row_class =
  425. // gantt.templates.task_row_class =
  426. // gantt.templates.task_class = function (start, end, task) {
  427. // var css = [];
  428. // if (task.$virtual || task.type == gantt.config.types.project)
  429. // css.push("summary-bar");
  430. // if (task.owner_id) {
  431. // css.push("gantt_resource_task gantt_resource_" + task.owner_id);
  432. // }
  433. // return css.join(" ");
  434. // };
  435. gantt.attachEvent('onLoadEnd', function () {
  436. var styleId = 'dynamicGanttStyles';
  437. var element = document.getElementById(styleId);
  438. if (!element) {
  439. element = document.createElement('style');
  440. element.id = styleId;
  441. document.querySelector('head').appendChild(element);
  442. }
  443. var html = [];
  444. var resources = gantt.serverList('staff');
  445. element.innerHTML = html.join('');
  446. });
  447. //保存后执行
  448. // gantt.attachEvent("onAfterTaskAdd", function (id, item) {
  449. // var projectItemDto = _self.getProjectItemDto();
  450. // _self.setTaskType(projectItemDto);
  451. // });
  452. //删除后执行
  453. // gantt.attachEvent("onAfterTaskDelete", function (id, item) {
  454. // var projectItemDto = _self.getProjectItemDto();
  455. // _self.setTaskType(projectItemDto);
  456. // });
  457. //拖动任务后执行
  458. // gantt.attachEvent("onAfterTaskMove", function (id, item) {
  459. // var projectItemDto = _self.getProjectItemDto();
  460. // _self.setTaskType(projectItemDto);
  461. // });
  462. //初始化自动排版
  463. gantt.config.undo = true;
  464. gantt.config.redo = true;
  465. gantt.config.auto_scheduling = true;
  466. gantt.autoSchedule();
  467. //设置左边项目栏可拖动
  468. gantt.config.order_branch = true;
  469. //设置右边显示进度条
  470. gantt.config.layout = {
  471. //css: "gantt_container",
  472. rows: [{
  473. cols: [{
  474. view: 'grid',
  475. width: 320,
  476. scrollY: 'scrollVer',
  477. },
  478. {
  479. resizer: true,
  480. width: 1,
  481. },
  482. {
  483. view: 'timeline',
  484. scrollX: 'scrollHor',
  485. scrollY: 'scrollVer',
  486. },
  487. {
  488. resizer: true,
  489. width: 1,
  490. },
  491. {
  492. view: 'scrollbar',
  493. id: 'scrollVer',
  494. },
  495. ],
  496. },
  497. {
  498. view: 'scrollbar',
  499. id: 'scrollHor',
  500. height: 20,
  501. },
  502. ],
  503. };
  504. /* //显示进度百分比
  505. gantt.templates.progress_text = function(start, end, task) {
  506. return "<span style='text-align:left;'></span>";
  507. }; */
  508. //设置传入后台日期格式
  509. gantt.config.xml_date = '%Y-%m-%d %H:%i:%s'; // XML中的日期格式
  510. //允许出现异常时错误警报
  511. gantt.config.show_errors = true;
  512. //分上下级显示
  513. // gantt.templates.task_class = function (st, end, item) {
  514. // return item.$level == 0 ? "gantt_project" : ""
  515. // };
  516. gantt.config.autosize = 'y';
  517. gantt.init('ganttContainer');
  518. _self.$refs.ganttScale.setDefaultScale();
  519. _self.showTaskDtos();
  520. },
  521. },
  522. mounted: function () {
  523. var _self = this;
  524. _self.projectId = this.$route.params.projectId;
  525. console.log(_self.projectId);
  526. _self.initData();
  527. _self.personnelJurisdictionSet();
  528. // window.ganttTimer = setInterval(() => {
  529. // // 某些定时器操作
  530. // _self.runSaveGantt();
  531. // }, 60000);
  532. },
  533. beforeUnmount: function () {
  534. // if (window.ganttTimer != null) {
  535. // window.clearInterval(ganttTimer);
  536. // window.ganttTimer = null;
  537. // }
  538. },
  539. };
  540. </script>