123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328 |
- import React from 'react';
- import { Form, Button, Row, Col, Icon, Input, InputNumber, Upload } from 'antd';
- import './index.less';
- import Editor from '@src/components/Editor';
- import Page from '@src/containers/Page';
- import Block from '@src/components/Block';
- import Radio from '@src/components/Radio';
- import TreeSelect from '@src/components/TreeSelect';
- // import EditTableCell from '@src/components/EditTableCell';
- // import ActionLayout from '@src/layouts/ActionLayout';
- // import TableLayout from '@src/layouts/TableLayout';
- import { formatFormError, formatTreeData, getMap } from '@src/services/Tools';
- import { asyncSMessage } from '@src/services/AsyncTools';
- import { SwitchSelect, DataType } from '../../../../Constant';
- // import { User } from '../../../stores/user';
- import { Exercise } from '../../../stores/exercise';
- import { Course } from '../../../stores/course';
- import { System } from '../../../stores/system';
- export default class extends Page {
- init() {
- this.exerciseMap = {};
- Exercise.dataStruct().then((result) => {
- const list = result.map(row => { row.title = `${row.titleZh}`; row.value = row.id; return row; });
- const tree = formatTreeData(list, 'id', 'title', 'parentId');
- this.exerciseMap = getMap(result.map(row => {
- row.title = `${row.titleZh}`;
- row.value = row.id;
- return row;
- }), 'id');
- this.setState({ exercise: tree });
- });
- }
- initData() {
- const { id } = this.params;
- let handler;
- if (id) {
- handler = Course.getData({ id });
- } else {
- handler = Promise.resolve({ structId: '' });
- }
- handler
- .then(result => {
- const { setFieldsValue } = this.props.form;
- result.structId = `${result.structId || ' '}`;
- setFieldsValue(result);
- this.setState({ data: result });
- });
- }
- submit() {
- const { form } = this.props;
- form.validateFields((err, values) => {
- console.log(values);
- if (!err) {
- const data = form.getFieldsValue();
- data.parentStructId = this.exerciseMap[data.structId] ? this.exerciseMap[data.structId].parentId : 0;
- data.isSentence = this.exerciseMap[data.structId] ? (this.exerciseMap[data.structId].extend === 'sentence' ? 1 : 0) : 0;
- let handler;
- if (data.id) {
- handler = Course.editData(data);
- } else {
- data.courseModule = module;
- handler = Course.addData(data);
- }
- handler.then(() => {
- asyncSMessage('保存成功');
- }).catch((e) => {
- if (e.result) form.setFields(formatFormError(data, e.result));
- });
- }
- });
- }
- renderBase() {
- const { exercise, data } = this.state;
- const { getFieldDecorator } = this.props.form;
- const structIdConfig = {
- rules: [{
- required: true, message: '请选择学科',
- }],
- };
- if (data.structId) {
- structIdConfig.initialValue = data.structId;
- }
- return <Block>
- <h1>基本信息</h1>
- <Form>
- {getFieldDecorator('id')(<input hidden />)}
- {exercise && data && <Form.Item key={data.structId} labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='学科'>
- {getFieldDecorator('structId', structIdConfig)(
- <TreeSelect treeData={exercise} />,
- )}
- </Form.Item>}
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='资料形式'>
- {getFieldDecorator('dataType', {
- rules: [{
- required: true, message: '请选择类型',
- }],
- })(
- <Radio select={DataType} />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='适合新手'>
- {getFieldDecorator('isNovice', {
- rules: [{
- required: true, message: '请选择',
- }],
- })(
- <Radio select={SwitchSelect} />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='原创资料'>
- {getFieldDecorator('isOriginal', {
- rules: [{
- required: true, message: '请选择',
- }],
- })(
- <Radio select={SwitchSelect} />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='资料名称'>
- {getFieldDecorator('title', {
- rules: [{
- required: true, message: '请输入名称',
- }],
- })(
- <Input placeholder='请输入' />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='价格'>
- {getFieldDecorator('price', {
- rules: [{
- required: true, message: '请输入价格',
- }],
- })(
- <InputNumber placeholder='请输入' min={0} />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='页数'>
- {getFieldDecorator('pages', {
- rules: [{
- required: true, message: '请输入页数',
- }],
- })(
- <InputNumber placeholder='请输入' min={0} />,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='链接地址'>
- {getFieldDecorator('link', {
- rules: [{
- required: true, message: '请输入地址',
- }],
- })(
- <Input placeholder='请输入' />,
- )}
- </Form.Item>
- </Form>
- </Block>;
- }
- renderInfo() {
- const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
- const cover = getFieldValue('cover');
- return <Block>
- <h1>宣传信息</h1>
- <Form>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='资料封面'>
- {getFieldDecorator('cover', {
- rules: [
- { required: true, message: '上传图片' },
- ],
- })(
- <Upload
- listType="picture-card"
- showUploadList={false}
- beforeUpload={(file) => System.uploadImage(file).then((result) => {
- setFieldsValue({ cover: result.url });
- return Promise.reject();
- })}
- >
- {cover ? <img src={cover} alt="avatar" /> : <div>
- <Icon type={this.state.loading ? 'loading' : 'plus'} />
- <div className="ant-upload-text">Upload</div>
- </div>}
- </Upload>,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='摘要介绍'>
- {getFieldDecorator('description', {
- rules: [{
- required: true, message: '请输入摘要',
- }],
- })(
- <Input placeholder='请输入' />,
- )}
- </Form.Item>
- {/* <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='试用页码'>
- <Row>
- <Col span={3}>
- <Form.Item>
- {getFieldDecorator('trailStart')(
- <InputNumber min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
- )}
- </Form.Item>
- </Col>
- <Col span={1}>至</Col>
- <Col span={3}>
- <Form.Item>
- {getFieldDecorator('trailEnd')(
- <InputNumber min={1} precision={0} formatter={(v) => parseInt(v, 10) || 1} />,
- )}
- </Form.Item>
- </Col>
- </Row>
- </Form.Item> */}
- </Form>
- </Block>;
- }
- renderFile() {
- const { getFieldDecorator, setFieldsValue, getFieldValue } = this.props.form;
- const resource = getFieldValue('resource');
- const trailResource = getFieldValue('trailResource');
- return <Block>
- <h1>资料文件</h1>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='正式版本'>
- {resource && <a href={resource} target="_blank">访问</a>}
- {getFieldDecorator('resource', {
- rules: [
- { required: true, message: '上传文件' },
- ],
- })(
- <Upload
- showUploadList={false}
- beforeUpload={(file) => System.uploadImage(file).then((result) => {
- setFieldsValue({ resource: result.url });
- return Promise.reject();
- })}
- >
- <Button>上传文件</Button>
- </Upload>,
- )}
- </Form.Item>
- <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='试用版本'>
- {trailResource && <a href={trailResource} target="_blank">访问</a>}
- {getFieldDecorator('trailResource', {
- rules: [
- { required: true, message: '上传文件' },
- ],
- })(
- <Upload
- showUploadList={false}
- beforeUpload={(file) => System.uploadImage(file).then((result) => {
- setFieldsValue({ trailResource: result.url });
- return Promise.reject();
- })}
- >
- <Button>上传文件</Button>
- </Upload>,
- )}
- </Form.Item>
- </Block>;
- }
- renderContent() {
- const { getFieldDecorator } = this.props.form;
- return <Block flex>
- <Form>
- <Form.Item label='资料介绍'>
- {getFieldDecorator('content', {
- })(
- <Editor placeholder='输入内容' />,
- )}
- </Form.Item>
- </Form>
- </Block>;
- }
- renderAuthor() {
- const { getFieldDecorator } = this.props.form;
- return <Block flex>
- <Form>
- <Form.Item label='作者介绍'>
- {getFieldDecorator('authorContent', {
- })(
- <Editor placeholder='输入内容' />,
- )}
- </Form.Item>
- </Form>
- </Block>;
- }
- renderMethod() {
- const { getFieldDecorator } = this.props.form;
- return <Block flex>
- <Form>
- <Form.Item label='获取方式'>
- {getFieldDecorator('methodContent', {
- })(
- <Editor placeholder='输入内容' />,
- )}
- </Form.Item>
- </Form>
- </Block>;
- }
- renderView() {
- return <div flex>
- {this.renderBase()}
- {this.renderInfo()}
- {this.renderFile()}
- {this.renderContent()}
- {this.renderAuthor()}
- {this.renderMethod()}
- <Row type="flex" justify="center">
- <Col>
- <Button type="primary" onClick={() => {
- this.submit();
- }}>保存</Button>
- </Col>
- </Row>
- </div>;
- }
- }
|