page.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 === 'exercise') {
  179. return this.refreshExercise();
  180. }
  181. return this.refreshExamination();
  182. }
  183. refreshExercise() {
  184. Exercise.allStruct().then(result => {
  185. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  186. this.exerciseItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  187. this.setState({
  188. exerciseList: list,
  189. exerciseMap: getMap(list, 'id'),
  190. exerciseStruct: formatTreeData(list.map(row => {
  191. if (row.level < 4) return row;
  192. row = Object.assign({}, row);
  193. row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  194. e.preventDefault();
  195. row.questionStatus = row.questionStatus > 0 ? 0 : 1;
  196. Exercise.editStruct(row).then(() => {
  197. this.refresh();
  198. });
  199. }}>{row.questionStatus > 0 ? [<Icon type='pause' />, <span>提问中</span>] : [<Icon type="caret-right" />, <span>提问关闭</span>]}</Button></div>;
  200. return row;
  201. }), 'id', 'title', 'parentId'),
  202. });
  203. });
  204. }
  205. refreshExamination() {
  206. Examination.allStruct().then(result => {
  207. const list = result.map(row => { row.title = `${row.titleZh}/${row.titleEn}`; return row; });
  208. this.examinationItemList[1].tree = formatTreeData([{ title: '根节点', id: 0 }].concat(list), 'id', 'title', 'parentId');
  209. this.setState({
  210. examinationList: list,
  211. examinationMap: getMap(list, 'id'),
  212. examinationStruct: formatTreeData(list.map(row => {
  213. if (row.level !== 2) return row;
  214. // row = Object.assign({}, row);
  215. // row.title = <div className='node'>{row.title}<Button className='after-node' size='small' type={row.questionStatus > 0 ? 'primary' : 'ghost'} onClick={(e) => {
  216. // e.preventDefault();
  217. // row.payStatus = row.payStatus > 0 ? 0 : 1;
  218. // Examination.editStruct(row).then(() => {
  219. // this.refresh();
  220. // });
  221. // }}>{row.payStatus > 0 ? [<Icon type="pay-circle" />, <span>付费</span>] : [<Icon type="eye" />, <span>免费</span>]}</Button></div>;
  222. return row;
  223. }), 'id', 'title', 'parentId'),
  224. });
  225. });
  226. }
  227. addChildAction() {
  228. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  229. let itemList;
  230. let node;
  231. let dep;
  232. if (tab === 'exercise') {
  233. itemList = this.exerciseItemList;
  234. if (selectedKeys.length > 0) {
  235. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])));
  236. }
  237. // 长难句还在该逻辑内,不允许添加下属节点
  238. if (node.level <= 1 || node.level >= 4) {
  239. asyncSMessage('不允许添加该节点的子节点', 'warn');
  240. return;
  241. }
  242. dep = {};
  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. if (tab === 'exercise') {
  259. handler = Exercise.addStruct(data);
  260. } else {
  261. handler = Examination.addStruct(data);
  262. }
  263. return handler.then(() => {
  264. asyncSMessage('新增成功!');
  265. this.refresh();
  266. });
  267. });
  268. }
  269. addBrotherAction() {
  270. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  271. let itemList;
  272. let node;
  273. let dep;
  274. if (tab === 'exercise') {
  275. itemList = this.exerciseItemList;
  276. if (selectedKeys.length > 0) {
  277. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])));
  278. }
  279. if (node.level <= 2) {
  280. asyncSMessage('不允许添加该节点的兄弟节点', 'warn');
  281. return;
  282. }
  283. dep = {
  284. parentName: this.state.exerciseMap[node.parentId].title,
  285. };
  286. } else {
  287. itemList = this.examinationItemList;
  288. if (selectedKeys.length > 0) {
  289. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])));
  290. }
  291. if (node.level <= 1) {
  292. asyncSMessage('不允许添加该节点的兄弟节点', 'warn');
  293. return;
  294. }
  295. dep = {
  296. isAdapt: node.isAdapt,
  297. parentName: this.state.examinationMap[node.parentId].title,
  298. };
  299. }
  300. asyncForm('新增', itemList, Object.assign({ parentId: `${node.parentId}`, parentName: node.title }, dep), data => {
  301. let handler;
  302. if (tab === 'exercise') {
  303. handler = Exercise.addStruct(data);
  304. } else {
  305. handler = Examination.addStruct(data);
  306. }
  307. return handler.then(() => {
  308. asyncSMessage('新增成功!');
  309. this.refresh();
  310. });
  311. });
  312. }
  313. editAction() {
  314. const { tab, exerciseList, examinationList, selectedKeys } = this.state;
  315. let itemList;
  316. let node;
  317. if (tab === 'exercise') {
  318. itemList = this.exerciseItemList;
  319. ([node] = exerciseList.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  320. row.parentId = `${row.parentId}`;
  321. return row;
  322. }));
  323. if (node.level <= 2) {
  324. itemList = this.exerciseItemListLimit;
  325. }
  326. if (node.extend) {
  327. asyncSMessage('该节点不允许编辑', 'warn');
  328. return;
  329. }
  330. node.parentName = this.state.exerciseMap[node.parentId].title;
  331. } else {
  332. itemList = this.examinationItemList;
  333. ([node] = examinationList.filter(row => row.id === Number(selectedKeys[0])).map(row => {
  334. row.parentId = `${row.parentId}`;
  335. return row;
  336. }));
  337. if (node.level <= 1) {
  338. itemList = this.examinationItemListLimit;
  339. }
  340. // 千行可以被修改
  341. // if (node.extend) {
  342. // asyncSMessage('该节点不允许编辑', 'warn');
  343. // return;
  344. // }
  345. node.parentName = this.state.examinationMap[node.parentId].title;
  346. }
  347. asyncForm('编辑', itemList, node, data => {
  348. let handler;
  349. if (tab === 'exercise') {
  350. handler = Exercise.editStruct(data);
  351. } else {
  352. handler = Examination.editStruct(data);
  353. }
  354. return handler.then(() => {
  355. asyncSMessage('编辑成功!');
  356. this.refresh();
  357. });
  358. });
  359. }
  360. deleteAction() {
  361. const { tab, checkedKeys } = this.state;
  362. asyncDelConfirm('删除确认', '是否删除选中节点?', () => {
  363. if (tab === 'exercise') {
  364. return Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  365. }
  366. return Promise.all(checkedKeys.map(row => Examination.delStruct({ id: row })));
  367. }).then(() => {
  368. asyncSMessage('删除成功!');
  369. this.refresh();
  370. });
  371. }
  372. renderExamination() {
  373. const { loading } = this.state;
  374. return <Block loading={loading}>
  375. <ActionLayout
  376. itemList={this.exerciseActionList}
  377. selectedKeys={this.state.selectedKeys}
  378. checkedKeys={this.state.checkedKeys}
  379. onAction={key => this.onAction(key)} />
  380. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.examinationStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  381. this.setState({ selectedKeys });
  382. }} checkedKeys={this.state.checkedKeys} checkStrictly onCheck={(checkedKeys, e) => {
  383. if (e.node.props.level > 2) {
  384. this.setState({ checkedKeys: { checkedKeys, halfCheckedKeys: e.halfCheckedKeys } });
  385. }
  386. }} />
  387. </Block>;
  388. }
  389. renderExercise() {
  390. const { loading } = this.state;
  391. return <Block loading={loading}>
  392. <ActionLayout
  393. itemList={this.exerciseActionList}
  394. selectedKeys={this.state.selectedKeys}
  395. checkedKeys={this.state.checkedKeys}
  396. onAction={key => this.onAction(key)} />
  397. <TreeLayout autoExpandParent defaultExpandAll checkable itemList={this.state.exerciseStruct} selectedKeys={this.state.selectedKeys} onSelect={(selectedKeys) => {
  398. this.setState({ selectedKeys });
  399. }} checkedKeys={this.state.checkedKeys} checkStrictly onCheck={(checkedKeys, e) => {
  400. if (e.node.props.level > 2) {
  401. this.setState({ checkedKeys: { checkedKeys, halfCheckedKeys: e.halfCheckedKeys } });
  402. }
  403. }} />
  404. </Block>;
  405. }
  406. renderView() {
  407. const { tab } = this.state;
  408. return <Block><Tabs activeKey={tab} onChange={(value) => {
  409. this.setState({ tab: value, selectedKeys: [], checkedKeys: [] });
  410. this.refresh(value);
  411. }}>
  412. <Tabs.TabPane tab="练习结构" key="exercise">
  413. {this.renderExercise()}
  414. </Tabs.TabPane>
  415. <Tabs.TabPane tab="模考结构" key="examination">
  416. {this.renderExamination()}
  417. </Tabs.TabPane>
  418. </Tabs></Block>;
  419. }
  420. }