page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. import React from 'react';
  2. import { Tabs, Button, Icon } from 'antd';
  3. import './index.less';
  4. import Page from '@src/containers/Page';
  5. import Block from '@src/components/Block';
  6. import TreeLayout from '@src/layouts/TreeLayout';
  7. import ActionLayout from '@src/layouts/ActionLayout';
  8. import { formatTreeData, getMap } from '@src/services/Tools';
  9. import { asyncDelConfirm, asyncForm, asyncSMessage } from '@src/services/AsyncTools';
  10. // import { System } from '../../../stores/system';
  11. import { Examination } from '../../../stores/examination';
  12. import { Exercise } from '../../../stores/exercise';
  13. export default class extends Page {
  14. constructor(props) {
  15. super(props);
  16. this.exerciseActionList = [{
  17. key: 'addChild',
  18. name: '增加子节点',
  19. }, {
  20. key: 'addBrother',
  21. name: '增加兄弟节点',
  22. selectNum: 1,
  23. }, {
  24. key: 'edit',
  25. name: '编辑',
  26. selectNum: 1,
  27. }, {
  28. key: 'delete',
  29. name: '批量删除',
  30. type: 'danger',
  31. needCheck: 1,
  32. }];
  33. this.exerciseItemList = [{
  34. key: 'id',
  35. type: 'hidden',
  36. }, {
  37. key: 'parentId',
  38. type: 'hidden',
  39. }, {
  40. key: 'parentName',
  41. type: 'input',
  42. name: '父节点',
  43. disabled: true,
  44. }, {
  45. key: 'titleZh',
  46. name: '中文名称',
  47. type: 'input',
  48. placeholder: '请输入中文名称',
  49. required: true,
  50. }, {
  51. key: 'titleEn',
  52. name: '英文名称',
  53. type: 'input',
  54. placeholder: '请输入英文名称',
  55. }, {
  56. key: 'description',
  57. name: '描述',
  58. type: 'textarea',
  59. placeholder: '请输入描述',
  60. }, {
  61. key: 'isSentence',
  62. type: 'hidden',
  63. }, {
  64. key: 'isExamination',
  65. type: 'hidden',
  66. }];
  67. this.exerciseItemListLimit = [{
  68. key: 'id',
  69. type: 'hidden',
  70. }, {
  71. key: 'titleZh',
  72. name: '中文名称',
  73. type: 'input',
  74. placeholder: '请输入中文名称',
  75. required: true,
  76. }, {
  77. key: 'titleEn',
  78. name: '英文名称',
  79. type: 'input',
  80. placeholder: '请输入英文名称',
  81. }, {
  82. key: 'description',
  83. name: '描述',
  84. type: 'textarea',
  85. placeholder: '请输入描述',
  86. }, {
  87. key: 'isSentence',
  88. type: 'hidden',
  89. }, {
  90. key: 'isExamination',
  91. type: 'hidden',
  92. }];
  93. this.examinationActionList = [{
  94. key: 'addChild',
  95. name: '增加子节点',
  96. }, {
  97. key: 'addBrother',
  98. name: '增加兄弟节点',
  99. selectNum: 1,
  100. }, {
  101. key: 'edit',
  102. name: '编辑',
  103. selectNum: 1,
  104. }, {
  105. key: 'delete',
  106. name: '批量删除',
  107. type: 'danger',
  108. needCheck: 1,
  109. }];
  110. this.examinationItemList = [{
  111. key: 'id',
  112. type: 'hidden',
  113. }, {
  114. key: 'parentId',
  115. type: 'hidden',
  116. }, {
  117. key: 'parentName',
  118. type: 'input',
  119. name: '父节点',
  120. disabled: true,
  121. }, {
  122. key: 'titleZh',
  123. name: '中文名称',
  124. type: 'input',
  125. placeholder: '请输入中文名称',
  126. required: true,
  127. }, {
  128. key: 'titleEn',
  129. name: '英文名称',
  130. type: 'input',
  131. placeholder: '请输入英文名称',
  132. }, {
  133. key: 'description',
  134. name: '描述',
  135. type: 'textarea',
  136. placeholder: '请输入描述',
  137. }, {
  138. key: 'isAdapt',
  139. type: 'hidden',
  140. }];
  141. this.examinationItemListLimit = [{
  142. key: 'id',
  143. type: 'hidden',
  144. }, {
  145. key: 'titleZh',
  146. name: '中文名称',
  147. type: 'input',
  148. placeholder: '请输入中文名称',
  149. required: true,
  150. }, {
  151. key: 'titleEn',
  152. name: '英文名称',
  153. type: 'input',
  154. placeholder: '请输入英文名称',
  155. }, {
  156. key: 'description',
  157. name: '描述',
  158. type: 'textarea',
  159. placeholder: '请输入描述',
  160. }, {
  161. key: 'isAdapt',
  162. type: 'hidden',
  163. }];
  164. this.state.tab = 'exercise';
  165. }
  166. initData() {
  167. this.refresh(this.state.tab);
  168. }
  169. refresh(tab) {
  170. if (!tab) {
  171. ({ tab } = this.state);
  172. }
  173. if (tab === 'exercise') {
  174. return this.refreshExercise();
  175. }
  176. return this.refreshExamination();
  177. }
  178. refreshExercise() {
  179. Exercise.allStruct().then(result => {
  180. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  181. this.exerciseItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  182. this.setState({
  183. exerciseList: list,
  184. exerciseMap: getMap(list, 'id'),
  185. exerciseStruct: formatTreeData(list.map(row => {
  186. if (row.level < 4) return row;
  187. row = Object.assign({}, row);
  188. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  189. e.preventDefault();
  190. row.questionStatus = row.questionStatus > 0 ? 0 : 1;
  191. Exercise.editStruct(row).then(() => {
  192. this.refresh();
  193. });
  194. }}>{row.questionStatus > 0 ? [<Icon type='pause' />, <span>提问中</span>] : [<Icon type="caret-right" />, <span>提问关闭</span>]}</Button></div>;
  195. return row;
  196. }), 'id', 'title', 'parentId'),
  197. });
  198. });
  199. }
  200. refreshExamination() {
  201. Examination.allStruct().then(result => {
  202. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  203. this.examinationItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  204. this.setState({
  205. examinationList: list,
  206. examinationMap: getMap(list, 'id'),
  207. examinationStruct: formatTreeData(list.map(row => {
  208. if (row.level !== 2) return row;
  209. row = Object.assign({}, row);
  210. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  211. e.preventDefault();
  212. row.questionStatus = row.questionStatus > 0 ? 0 : 1;
  213. Examination.editStruct(row).then(() => {
  214. this.refresh();
  215. });
  216. }}>{row.questionStatus > 0 ? [<Icon type='pause' />, <span>提问中</span>] : [<Icon type="caret-right" />, <span>提问关闭</span>]}</Button></div>;
  217. return row;
  218. }), 'id', 'title', 'parentId'),
  219. });
  220. });
  221. }
  222. addChildAction() {
  223. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  224. let itemList;
  225. let node;
  226. let dep;
  227. if (tab === 'exercise') {
  228. itemList = this.exerciseItemList;
  229. if (selectedKeys.length > 0) {
  230. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])));
  231. }
  232. // 长难句还在该逻辑内,不允许添加下属节点
  233. if (node.level <= 1 || node.level >= 4) {
  234. asyncSMessage('不允许添加该节点的子节点', 'warn');
  235. return;
  236. }
  237. dep = {
  238. isSentence: node.isSentence,
  239. isCourse: node.isCourse,
  240. isData: node.isData,
  241. isExamination: node.isExamination,
  242. };
  243. } else {
  244. itemList = this.examinationItemList;
  245. if (selectedKeys.length > 0) {
  246. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])));
  247. }
  248. if (node.level <= 0 || node.level >= 3) {
  249. asyncSMessage('不允许添加该节点的子节点', 'warn');
  250. return;
  251. }
  252. dep = {
  253. isAdapt: node.isAdapt,
  254. };
  255. }
  256. asyncForm('新增', itemList, Object.assign({ parentId: node.id, parentName: node.title }, dep), data => {
  257. let handler;
  258. data.parentId = Number(data.parentId);
  259. if (tab === 'exercise') {
  260. handler = Exercise.addStruct(data);
  261. } else {
  262. handler = Examination.addStruct(data);
  263. }
  264. return handler.then(() => {
  265. asyncSMessage('新增成功!');
  266. this.refresh();
  267. });
  268. });
  269. }
  270. addBrotherAction() {
  271. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  272. let itemList;
  273. let node;
  274. let dep;
  275. if (tab === 'exercise') {
  276. itemList = this.exerciseItemList;
  277. if (selectedKeys.length > 0) {
  278. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])));
  279. }
  280. if (node.level <= 2) {
  281. asyncSMessage('不允许添加该节点的兄弟节点', 'warn');
  282. return;
  283. }
  284. dep = {
  285. isSentence: node.isSentence,
  286. isCourse: node.isCourse,
  287. isData: node.isData,
  288. isExamination: node.isExamination,
  289. parentName: this.state.exerciseMap[node.parentId].title,
  290. };
  291. } else {
  292. itemList = this.examinationItemList;
  293. if (selectedKeys.length > 0) {
  294. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])));
  295. }
  296. if (node.level <= 1) {
  297. asyncSMessage('不允许添加该节点的兄弟节点', 'warn');
  298. return;
  299. }
  300. dep = {
  301. isAdapt: node.isAdapt,
  302. parentName: this.state.examinationMap[node.parentId].title,
  303. };
  304. }
  305. asyncForm('新增', itemList, Object.assign({ parentId: node.parentId, parentName: node.title }, dep), data => {
  306. let handler;
  307. data.parentId = Number(data.parentId);
  308. if (tab === 'exercise') {
  309. handler = Exercise.addStruct(data);
  310. } else {
  311. handler = Examination.addStruct(data);
  312. }
  313. return handler.then(() => {
  314. asyncSMessage('新增成功!');
  315. this.refresh();
  316. });
  317. });
  318. }
  319. editAction() {
  320. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  321. let itemList;
  322. let node;
  323. if (tab === 'exercise') {
  324. itemList = this.exerciseItemList;
  325. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  326. // row.parentId = `${row.parentId}`;
  327. return row;
  328. }));
  329. if (node.level <= 2) {
  330. itemList = this.exerciseItemListLimit;
  331. }
  332. if (node.level <= 2) {
  333. asyncSMessage('该节点不允许编辑', 'warn');
  334. return;
  335. }
  336. node.parentName = this.state.exerciseMap[node.parentId].title;
  337. } else {
  338. itemList = this.examinationItemList;
  339. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  340. // row.parentId = `${row.parentId}`;
  341. return row;
  342. }));
  343. if (node.level <= 1) {
  344. itemList = this.examinationItemListLimit;
  345. }
  346. // 千行可以被修改
  347. // if (node.extend) {
  348. // asyncSMessage('该节点不允许编辑', 'warn');
  349. // return;
  350. // }
  351. node.parentName = this.state.examinationMap[node.parentId].title;
  352. }
  353. asyncForm('编辑', itemList, node, data => {
  354. let handler;
  355. data.parentId = Number(data.parentId);
  356. if (tab === 'exercise') {
  357. handler = Exercise.editStruct(data);
  358. } else {
  359. handler = Examination.editStruct(data);
  360. }
  361. return handler.then(() => {
  362. asyncSMessage('编辑成功!');
  363. this.refresh();
  364. });
  365. });
  366. }
  367. deleteAction() {
  368. const { tab, checkedKeys } = this.state;
  369. asyncDelConfirm('删除确认', '是否删除选中节点?', () => {
  370. let handler;
  371. if (tab === 'exercise') {
  372. handler = Promise.all(checkedKeys.checked.map(row => Exercise.delStruct({ id: row })));
  373. } else {
  374. handler = Promise.all(checkedKeys.checked.map(row => Examination.delStruct({ id: row })));
  375. }
  376. return handler.then(() => {
  377. asyncSMessage('删除成功!');
  378. this.refresh();
  379. });
  380. });
  381. }
  382. renderExamination() {
  383. const { loading } = this.state;
  384. return <Block loading={loading}>
  385. <ActionLayout
  386. itemList={this.exerciseActionList}
  387. selectedKeys={this.state.selectedKeys}
  388. checkedKeys={this.state.checkedKeys ? this.state.checkedKeys.checked || [] : []}
  389. onAction={key => this.onAction(key)} />
  390. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.examinationStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  391. this.setState({ selectedKeys });
  392. }} checkedKeys={this.state.checkedKeys} checkStrictly onCheck={(checkedKeys, e) => {
  393. if (e.node.props.level > 2) {
  394. this.setState({ checkedKeys });
  395. }
  396. }} />
  397. </Block>;
  398. }
  399. renderExercise() {
  400. const { loading } = this.state;
  401. return <Block loading={loading}>
  402. <ActionLayout
  403. itemList={this.exerciseActionList}
  404. selectedKeys={this.state.selectedKeys}
  405. checkedKeys={this.state.checkedKeys ? this.state.checkedKeys.checked || [] : []}
  406. onAction={key => this.onAction(key)} />
  407. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.exerciseStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  408. this.setState({ selectedKeys });
  409. }} checkedKeys={this.state.checkedKeys} checkStrictly onCheck={(checkedKeys, e) => {
  410. if (e.node.props.level > 2) {
  411. this.setState({ checkedKeys });
  412. }
  413. }} />
  414. </Block>;
  415. }
  416. renderView() {
  417. const { tab } = this.state;
  418. return <Block><Tabs activeKey={tab} onChange={(value) => {
  419. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  420. this.refresh(value);
  421. }}>
  422. <Tabs.TabPane tab="练习结构" key="exercise">
  423. {this.renderExercise()}
  424. </Tabs.TabPane>
  425. <Tabs.TabPane tab="模考结构" key="examination">
  426. {this.renderExamination()}
  427. </Tabs.TabPane>
  428. </Tabs></Block>;
  429. }
  430. }