page.js 13 KB

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