123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- import React from 'react';
- import { Link } from 'react-router-dom';
- import { Button, Modal, Checkbox, Form, InputNumber } from 'antd';
- import './index.less';
- import Page from '@src/containers/Page';
- import Block from '@src/components/Block';
- import EditTableCell from '@src/components/EditTableCell';
- import FilterLayout from '@src/layouts/FilterLayout';
- import ActionLayout from '@src/layouts/ActionLayout';
- import TableLayout from '@src/layouts/TableLayout';
- import { getMap } from '@src/services/Tools';
- import { asyncSMessage, asyncDelConfirm } from '@src/services/AsyncTools';
- import { Sentence } from '../../../stores/sentence';
- import { Slient } from '../../../stores/slient';
- import { SwitchSelect } from '../../../../Constant';
- const SwitchSelectMap = getMap(SwitchSelect, 'value', 'label');
- const filterForm = [
- {
- key: 'chapter',
- type: 'select',
- allowClear: true,
- name: 'chapter',
- placeholder: '请选择',
- number: true,
- },
- {
- key: 'isTrail',
- type: 'select',
- name: '试用',
- allowClear: true,
- placeholder: '请选择',
- number: true,
- select: SwitchSelect,
- },
- // {
- // key: 'part',
- // type: 'select',
- // allowClear: true,
- // name: '状态',
- // number: true,
- // placeholder: '请选择',
- // },
- ];
- export default class extends Page {
- constructor(props) {
- super(props);
- this.structMap = {};
- this.actionList = [{
- key: 'struct',
- name: '编辑章节',
- }, {
- key: 'auto',
- name: '重新组卷',
- }, {
- key: 'introduction',
- name: '编辑前言',
- render: (item) => {
- return <Link to='/subject/sentence/article?i=1'><Button>{item.name}</Button></Link>;
- },
- }, {
- key: 'article',
- name: '新建文章',
- render: (item) => {
- return <Link to='/subject/sentence/article'><Button>{item.name}</Button></Link>;
- },
- }, {
- key: 'question',
- name: '新建题目',
- render: (item) => {
- return <Link to='/subject/sentence/question'><Button>{item.name}</Button></Link>;
- },
- }, {
- key: 'delete',
- name: '批量删除',
- needSelect: 1,
- }];
- this.columns = [{
- title: 'chapter',
- dataIndex: 'chapter',
- }, {
- title: 'part',
- dataIndex: 'part',
- }, {
- title: '名称',
- dataIndex: 'title',
- }, {
- title: '试用',
- dataIndex: 'isTrail',
- render: (text) => {
- return SwitchSelectMap[text ? 1 : 0] || '';
- },
- }, {
- title: '练习',
- dataIndex: 'exercise',
- render: () => {
- const item = this.structMap[this.state.search.chapter];
- return SwitchSelectMap[item.exercise ? 1 : 0] || '';
- },
- }, {
- title: '操作',
- dataIndex: 'handler',
- render: (text, record) => {
- const item = this.structMap[this.state.search.chapter];
- return <div className="table-button">
- {item.exercise > 0 ? (<Link to={`/subject/sentence/question/${record.id}`}>编辑</Link>) : (<Link to={`/subject/sentence/article/${record.id}`}>编辑</Link>)}
- </div>;
- },
- }];
- this.structColumns = [{
- title: 'chapter',
- dataIndex: 'chapter',
- render: (text, record, index) => {
- return index + 1;
- },
- }, {
- title: '短名称',
- dataIndex: 'short',
- render: (text, record, index) => {
- return <EditTableCell value={text} onChange={(v) => {
- this.changeStruct(index, 'short', v);
- }} />;
- },
- }, {
- title: '长名称',
- dataIndex: 'title',
- render: (text, record, index) => {
- return <EditTableCell value={text} onChange={(v) => {
- this.changeStruct(index, 'title', v);
- }} />;
- },
- }, {
- title: '练习章',
- dataIndex: 'exercise',
- render: (text, record, index) => {
- return <Checkbox onChange={(e) => {
- if (e.target.checked) this.changeStruct(index, 'exercise', 1, 0);
- }} checked={!!text} />;
- },
- }];
- }
- initAuto() {
- this.outPage();
- this.interval = setInterval(() => {
- Slient.sentenceAuto().then((result) => {
- if (result.progress == null || result.progress === 100) {
- this.actionList[1].disabled = false;
- result.progress = 100;
- } else {
- this.actionList[1].disabled = true;
- }
- this.setState({ progress: result.progress });
- });
- }, 30000);
- }
- outPage() {
- if (this.interval) {
- clearInterval(this.interval);
- }
- }
- init() {
- Sentence.getStruct().then(result => {
- return this.refreshStruct(result);
- }).then(() => {
- this.initData();
- });
- }
- initData() {
- const item = this.structMap[this.state.search.chapter];
- if (!item) return;
- if (item.exercise) {
- Sentence.listQuestion(this.state.search).then(result => {
- result.list = result.list.map(row => {
- row.chapter = this.state.search.chapter || 0;
- return row;
- });
- this.setTableData(result.list, result.total);
- });
- } else {
- Sentence.listArticle(this.state.search).then(result => {
- this.setTableData(result.list, result.total);
- });
- }
- }
- refreshStruct(result) {
- result = result || {};
- result.chapters = result.chapters || [];
- filterForm[0].select = result.chapters.map((row, index) => { row.value = index + 1; return row; });
- this.structMap = getMap(filterForm[0].select, 'value');
- this.setState({ struct: result });
- }
- autoAction() {
- asyncDelConfirm('组卷确认', '是否重新组卷?', () => {
- return Sentence.auto().then(() => {
- asyncSMessage('开始组卷!');
- this.refresh();
- });
- });
- }
- structAction() {
- const { struct = {} } = this.state;
- this.open(struct);
- }
- changeStruct(index, field, value, other) {
- const { detail } = this.state;
- if (other !== undefined) {
- detail.chapters.forEach((row) => {
- row[field] = other;
- });
- }
- detail.chapters[index][field] = value;
- this.setState({ detail });
- }
- changeTrail(number) {
- const { detail } = this.state;
- detail.trailPages = number;
- this.setState({ detail });
- }
- submitStruct() {
- const { detail } = this.state;
- Sentence.setStruct(detail).then(() => {
- asyncSMessage('保存成功');
- this.close(false, 'detail');
- return this.refreshStruct(detail);
- }).then(() => {
- return this.initData();
- });
- }
- deleteAction() {
- const { selectedKeys } = this.state;
- const item = this.structMap[this.state.search.chapter];
- if (!item) return;
- asyncDelConfirm('删除确认', '是否删除选中?', () => {
- let handler;
- if (item.exercise) {
- handler = Promise.all(selectedKeys.map(row => Sentence.delQuestion({ id: row })));
- } else {
- handler = Promise.all(selectedKeys.map(row => Sentence.delArticle({ id: row })));
- }
- return handler.then(() => {
- asyncSMessage('删除成功!');
- this.refresh();
- });
- });
- }
- renderView() {
- return <Block flex>
- <FilterLayout
- show
- itemList={filterForm}
- data={this.state.search}
- onChange={data => {
- data.page = 1;
- this.search(data);
- }} />
- <ActionLayout
- itemList={this.actionList}
- selectedKeys={this.state.selectedKeys}
- onAction={key => this.onAction(key)}
- />
- <TableLayout
- select
- columns={this.tableSort(this.columns)}
- list={this.state.list}
- pagination={this.state.page}
- loading={this.props.core.loading}
- onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
- onSelect={(keys, rows) => this.tableSelect(keys, rows)}
- selectedKeys={this.state.selectedKeys}
- />
- {this.state.detail && <Modal visible closable title='编辑章节' onCancel={() => {
- this.close(false, 'detail');
- }} onOk={() => {
- this.submitStruct();
- }}>
- <Form>
- <Form.Item labelCol={{ span: 4 }} wrapperCol={{ span: 16 }} label='试用页数'>
- <InputNumber value={this.state.detail.trailPages || 0} onChange={(value) => {
- this.changeTrail(value);
- }} />
- </Form.Item>
- </Form>
- <TableLayout
- rowKey={'title'}
- columns={this.structColumns}
- list={this.state.detail.chapters}
- pagination={false}
- />
- <Button onClick={() => {
- const { detail } = this.state;
- detail.chapters.push({});
- this.setState({ detail });
- }}>增加chapter</Button></Modal>}
- </Block>;
- }
- }
|