page.js 7.2 KB

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