page.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import { Button, Modal, Checkbox, Form, InputNumber } 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: 'introduction',
  45. name: '编辑前言',
  46. render: (item) => {
  47. return <Link to='/subject/sentence/article?i=1'><Button>{item.name}</Button></Link>;
  48. },
  49. }, {
  50. key: 'article',
  51. name: '新建文章',
  52. render: (item) => {
  53. return <Link to='/subject/sentence/article'><Button>{item.name}</Button></Link>;
  54. },
  55. }, {
  56. key: 'question',
  57. name: '新建题目',
  58. render: (item) => {
  59. return <Link to='/subject/sentence/question'><Button>{item.name}</Button></Link>;
  60. },
  61. }, {
  62. key: 'delete',
  63. name: '批量删除',
  64. needSelect: 1,
  65. }];
  66. this.columns = [{
  67. title: 'chapter',
  68. dataIndex: 'chapter',
  69. }, {
  70. title: 'part',
  71. dataIndex: 'part',
  72. }, {
  73. title: 'title',
  74. dataIndex: 'title',
  75. }, {
  76. title: '操作',
  77. dataIndex: 'handler',
  78. render: (text, record) => {
  79. const item = this.structMap[this.state.search.chapter];
  80. return <div className="table-button">
  81. {item.exercise > 0 ? (<Link to={`/subject/sentence/question/${record.id}`}>编辑</Link>) : (<Link to={`/subject/sentence/article/${record.id}`}>编辑</Link>)}
  82. </div>;
  83. },
  84. }];
  85. this.structColumns = [{
  86. title: 'chapter',
  87. dataIndex: 'chapter',
  88. render: (text, record, index) => {
  89. return index + 1;
  90. },
  91. }, {
  92. title: '短名称',
  93. dataIndex: 'short',
  94. render: (text, record, index) => {
  95. return <EditTableCell value={text} onChange={(v) => {
  96. this.changeStruct(index, 'short', v);
  97. }} />;
  98. },
  99. }, {
  100. title: '长名称',
  101. dataIndex: 'title',
  102. render: (text, record, index) => {
  103. return <EditTableCell value={text} onChange={(v) => {
  104. this.changeStruct(index, 'title', v);
  105. }} />;
  106. },
  107. }, {
  108. title: '练习章',
  109. dataIndex: 'exercise',
  110. render: (text, record, index) => {
  111. return <Checkbox onChange={(e) => {
  112. if (e.target.checked) this.changeStruct(index, 'exercise', 1, 0);
  113. }} checked={!!text} />;
  114. },
  115. }];
  116. }
  117. initAuto() {
  118. this.outPage();
  119. this.interval = setInterval(() => {
  120. Slient.sentenceAuto().then((result) => {
  121. if (result.progress == null || result.progress === 100) {
  122. this.actionList[1].disabled = false;
  123. result.progress = 100;
  124. } else {
  125. this.actionList[1].disabled = true;
  126. }
  127. this.setState({ progress: result.progress });
  128. });
  129. }, 30000);
  130. }
  131. outPage() {
  132. if (this.interval) {
  133. clearInterval(this.interval);
  134. }
  135. }
  136. init() {
  137. Sentence.getStruct().then(result => {
  138. return this.refreshStruct(result);
  139. }).then(() => {
  140. this.initData();
  141. });
  142. }
  143. initData() {
  144. const item = this.structMap[this.state.search.chapter];
  145. if (!item) return;
  146. if (item.exercise) {
  147. Sentence.listQuestion(this.state.search).then(result => {
  148. result.list = result.list.map(row => {
  149. row.chapter = this.state.search.chapter || 0;
  150. return row;
  151. });
  152. this.setTableData(result.list, result.total);
  153. });
  154. } else {
  155. Sentence.listArticle(this.state.search).then(result => {
  156. this.setTableData(result.list, result.total);
  157. });
  158. }
  159. }
  160. refreshStruct(result) {
  161. result = result || {};
  162. result.chapters = result.chapters || [];
  163. filterForm[0].select = result.chapters.map((row, index) => { row.value = index + 1; return row; });
  164. this.structMap = getMap(filterForm[0].select, 'value');
  165. this.setState({ struct: result });
  166. }
  167. autoAction() {
  168. asyncDelConfirm('组卷确认', '是否重新组卷?', () => {
  169. return Sentence.auto().then(() => {
  170. asyncSMessage('开始组卷!');
  171. this.refresh();
  172. });
  173. });
  174. }
  175. structAction() {
  176. const { struct = {} } = this.state;
  177. this.open(struct);
  178. }
  179. changeStruct(index, field, value, other) {
  180. const { detail } = this.state;
  181. if (other !== undefined) {
  182. detail.chapters.forEach((row) => {
  183. row[field] = other;
  184. });
  185. }
  186. detail.chapters[index][field] = value;
  187. this.setState({ detail });
  188. }
  189. changeTrail(number) {
  190. const { detail } = this.state;
  191. detail.trailPages = number;
  192. this.setState({ detail });
  193. }
  194. submitStruct() {
  195. const { detail } = this.state;
  196. Sentence.setStruct(detail).then(() => {
  197. asyncSMessage('保存成功');
  198. this.close(false, 'detail');
  199. return this.refreshStruct(detail);
  200. }).then(() => {
  201. return this.initData();
  202. });
  203. }
  204. deleteAction() {
  205. const { selectedKeys } = this.state;
  206. const item = this.structMap[this.state.search.chapter];
  207. if (!item) return;
  208. asyncDelConfirm('删除确认', '是否删除选中?', () => {
  209. let handler;
  210. if (item.exercise) {
  211. handler = Promise.all(selectedKeys.map(row => Sentence.delQuestion({ id: row })));
  212. } else {
  213. handler = Promise.all(selectedKeys.map(row => Sentence.delArticle({ id: row })));
  214. }
  215. return handler.then(() => {
  216. asyncSMessage('删除成功!');
  217. this.refresh();
  218. });
  219. });
  220. }
  221. renderView() {
  222. return <Block flex>
  223. <FilterLayout
  224. show
  225. itemList={filterForm}
  226. data={this.state.search}
  227. onChange={data => {
  228. this.search(data);
  229. }} />
  230. <ActionLayout
  231. itemList={this.actionList}
  232. selectedKeys={this.state.selectedKeys}
  233. onAction={key => this.onAction(key)}
  234. />
  235. <TableLayout
  236. select
  237. columns={this.columns}
  238. list={this.state.list}
  239. pagination={this.state.page}
  240. loading={this.props.core.loading}
  241. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  242. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  243. selectedKeys={this.state.selectedKeys}
  244. />
  245. {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
  246. this.close(false, 'detail');
  247. }} onOk={() => {
  248. this.submitStruct();
  249. }}>
  250. <Form>
  251. <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='试用页数'>
  252. <InputNumber value={this.state.detail.trailPages || 0} onChange={(value) => {
  253. this.changeTrail(value);
  254. }} />
  255. </Form.Item>
  256. </Form>
  257. <TableLayout
  258. rowKey={'title'}
  259. columns={this.structColumns}
  260. list={this.state.detail.chapters}
  261. pagination={false}
  262. />
  263. <Button onClick={() => {
  264. const { detail } = this.state;
  265. detail.chapters.push({});
  266. this.setState({ detail });
  267. }}>增加chapter</Button></Modal>}
  268. </Block>;
  269. }
  270. }