page.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Button, Modal, Checkbox } from 'antd';
  4. import './index.less';
  5. import Page from '@src/containers/Page';
  6. import Block from '@src/components/Block';
  7. import EditTableCell from '@src/components/EditTableCell';
  8. import FilterLayout from '@src/layouts/FilterLayout';
  9. import ActionLayout from '@src/layouts/ActionLayout';
  10. import TableLayout from '@src/layouts/TableLayout';
  11. // import { PreviewStatus } from '@src/services/Constant';
  12. import { getMap } from '@src/services/Tools';
  13. import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
  14. import { Sentence } from '../../../stores/sentence';
  15. import { Slient } from '../../../stores/slient';
  16. const filterForm = [
  17. {
  18. key: 'chapter',
  19. type: 'select',
  20. allowClear: true,
  21. name: 'chapter',
  22. placeholder: '请选择',
  23. number: true,
  24. },
  25. // {
  26. // key: 'part',
  27. // type: 'select',
  28. // allowClear: true,
  29. // name: '状态',
  30. // number: true,
  31. // placeholder: '请选择',
  32. // },
  33. ];
  34. export default class extends Page {
  35. constructor(props) {
  36. super(props);
  37. this.structMap = {};
  38. this.actionList = [{
  39. key: 'struct',
  40. name: '编辑章节',
  41. }, {
  42. key: 'auto',
  43. name: '重新组卷',
  44. }, {
  45. key: 'article',
  46. name: '新建文章',
  47. render: (item) => {
  48. return <Link to='/subject/sentence/article'><Button>{item.name}</Button></Link>;
  49. },
  50. }, {
  51. key: 'question',
  52. name: '新建题目',
  53. render: (item) => {
  54. return <Link to='/subject/sentence/question'><Button>{item.name}</Button></Link>;
  55. },
  56. }, {
  57. key: 'delete',
  58. name: '批量删除',
  59. needSelect: 1,
  60. }];
  61. this.columns = [{
  62. title: 'chapter',
  63. dataIndex: 'chapter',
  64. }, {
  65. title: 'part',
  66. dataIndex: 'part',
  67. }, {
  68. title: 'title',
  69. dataIndex: 'title',
  70. }, {
  71. title: '操作',
  72. dataIndex: 'handler',
  73. render: (text, record) => {
  74. const item = this.structMap[this.state.search.chapter];
  75. return <div className="table-button">
  76. {item.exercise > 0 ? (<Link to={`/subject/sentence/question/${record.id}`}>编辑</Link>) : (<Link to={`/subject/sentence/article/${record.id}`}>编辑</Link>)}
  77. </div>;
  78. },
  79. }];
  80. this.structColumns = [{
  81. title: 'chapter',
  82. dataIndex: 'chapter',
  83. render: (text, record, index) => {
  84. return index + 1;
  85. },
  86. }, {
  87. title: '短名称',
  88. dataIndex: 'short',
  89. render: (text, record, index) => {
  90. return <EditTableCell value={text} onChange={(v) => {
  91. this.changeStruct(index, 'short', v);
  92. }} />;
  93. },
  94. }, {
  95. title: '长名称',
  96. dataIndex: 'title',
  97. render: (text, record, index) => {
  98. return <EditTableCell value={text} onChange={(v) => {
  99. this.changeStruct(index, 'title', v);
  100. }} />;
  101. },
  102. }, {
  103. title: '练习章',
  104. dataIndex: 'exercise',
  105. render: (text, record, index) => {
  106. return <Checkbox onChange={(e) => {
  107. if (e.target.checked) this.changeStruct(index, 'exercise', 1, 0);
  108. }} checked={!!text} />;
  109. },
  110. }];
  111. }
  112. initAuto() {
  113. this.outPage();
  114. this.interval = setInterval(() => {
  115. Slient.sentenceAuto().then((result) => {
  116. if (result.process == null || result.process === 100) {
  117. this.actionList[1].disabled = false;
  118. result.process = 100;
  119. } else {
  120. this.actionList[1].disabled = true;
  121. }
  122. this.setState({ process: result.process });
  123. });
  124. }, 30000);
  125. }
  126. outPage() {
  127. if (this.interval) {
  128. clearInterval(this.interval);
  129. }
  130. }
  131. init() {
  132. Sentence.getStruct().then(result => {
  133. return this.refreshStruct(result);
  134. }).then(() => {
  135. this.initData();
  136. });
  137. }
  138. initData() {
  139. const item = this.structMap[this.state.search.chapter];
  140. if (!item) return;
  141. if (item.exercise) {
  142. Sentence.listQuestion(this.state.search).then(result => {
  143. result.list = result.list.map(row => {
  144. row.chapter = this.state.search.chapter || 0;
  145. return row;
  146. });
  147. this.setTableData(result.list, result.total);
  148. });
  149. } else {
  150. Sentence.listArticle(this.state.search).then(result => {
  151. this.setTableData(result.list, result.total);
  152. });
  153. }
  154. }
  155. refreshStruct(result) {
  156. result = result || {};
  157. result.chapters = result.chapters || [];
  158. filterForm[0].select = result.chapters.map((row, index) => { row.value = index + 1; return row; });
  159. this.structMap = getMap(filterForm[0].select, 'value');
  160. this.setState({ struct: result });
  161. }
  162. autoAction() {
  163. asyncDelConfirm('组卷确认', '是否重新组卷?', () => {
  164. return Sentence.auto();
  165. }).then(() => {
  166. asyncSMessage('开始组卷!');
  167. this.refresh();
  168. });
  169. }
  170. structAction() {
  171. const { struct = {} } = this.state;
  172. this.open(struct);
  173. }
  174. changeStruct(index, field, value, other) {
  175. const { detail } = this.state;
  176. if (other !== undefined) {
  177. detail.chapters.forEach((row) => {
  178. row[field] = other;
  179. });
  180. }
  181. detail.chapters[index][field] = value;
  182. this.setState({ detail });
  183. }
  184. submitStruct() {
  185. const { detail } = this.state;
  186. Sentence.setStruct(detail).then(() => {
  187. asyncSMessage('保存成功');
  188. this.close(false, 'detail');
  189. return this.refreshStruct(detail);
  190. }).then(() => {
  191. return this.initData();
  192. });
  193. }
  194. deleteAction() {
  195. const { selectedKeys } = this.state;
  196. const item = this.structMap[this.state.search.chapter];
  197. if (!item) return;
  198. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  199. if (item.exercise) {
  200. return Promise.all(selectedKeys.map(row => Sentence.delQuestion({ id: row })));
  201. }
  202. return Promise.all(selectedKeys.map(row => Sentence.delArticle({ id: row })));
  203. }).then(() => {
  204. asyncSMessage('删除成功!');
  205. this.refresh();
  206. });
  207. }
  208. renderView() {
  209. return <Block flex>
  210. <FilterLayout
  211. show
  212. itemList={filterForm}
  213. data={this.state.search}
  214. onChange={data => {
  215. this.search(data);
  216. }} />
  217. <ActionLayout
  218. itemList={this.actionList}
  219. selectedKeys={this.state.selectedKeys}
  220. onAction={key => this.onAction(key)}
  221. />
  222. <TableLayout
  223. select
  224. columns={this.columns}
  225. list={this.state.list}
  226. pagination={this.state.page}
  227. loading={this.props.core.loading}
  228. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  229. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  230. selectedKeys={this.state.selectedKeys}
  231. />
  232. {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
  233. this.close(false, 'detail');
  234. }} onOk={() => {
  235. this.submitStruct();
  236. }}>
  237. <TableLayout
  238. rowKey={'title'}
  239. columns={this.structColumns}
  240. list={this.state.detail.chapters}
  241. pagination={false}
  242. />
  243. <Button type='link' onClick={() => {
  244. const { detail } = this.state;
  245. detail.chapters.push({});
  246. this.setState({ detail });
  247. }}>增加chapter</Button></Modal>}
  248. </Block>;
  249. }
  250. }