page.js 8.6 KB

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