page.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. import React from 'react';
  2. import { Link } from 'react-router-dom';
  3. import moment from 'moment';
  4. import { Form, Input, Button, Row, Col, InputNumber, Upload, DatePicker, Checkbox, Icon } from 'antd';
  5. import './index.less';
  6. import Editor from '@src/components/Editor';
  7. import Page from '@src/containers/Page';
  8. import Block from '@src/components/Block';
  9. import TreeSelect from '@src/components/TreeSelect';
  10. import Select from '@src/components/Select';
  11. import EditTableCell from '@src/components/EditTableCell';
  12. import Radio from '@src/components/Radio';
  13. import ActionLayout from '@src/layouts/ActionLayout';
  14. import TableLayout from '@src/layouts/TableLayout';
  15. // import FileUpload from '@src/components/FileUpload';
  16. import { formatFormError, formatSeconds, formatTreeData, getMap } from '@src/services/Tools';
  17. import { asyncSMessage } from '@src/services/AsyncTools';
  18. import { CrowdList, CourseStatus, CourseVideoType } from '../../../../Constant';
  19. import { Course } from '../../../stores/course';
  20. import { System } from '../../../stores/system';
  21. import { Exercise } from '../../../stores/exercise';
  22. const CourseStatusMap = getMap(CourseStatus, 'value', 'label');
  23. export default class extends Page {
  24. init() {
  25. this.exerciseMap = {};
  26. this.actionList = [{
  27. key: 'add',
  28. type: 'primary',
  29. name: '上传视频',
  30. render: (item) => {
  31. return <Upload
  32. showUploadList={false}
  33. beforeUpload={(file) => System.uploadVideo(file).then((result) => {
  34. return Course.addNo({ courseId: this.params.id, resource: result.url, time: result.time });
  35. }).then(() => {
  36. this.refreshNo();
  37. })}
  38. >
  39. <Button>{item.name}</Button>
  40. </Upload>;
  41. },
  42. }];
  43. this.noColumns = [{
  44. title: '课程序号',
  45. dataIndex: 'no',
  46. }, {
  47. title: '名称',
  48. dataIndex: 'title',
  49. render: (text, record) => {
  50. return <EditTableCell value={text} onChange={(v) => {
  51. this.changeNo('title', record.id, v);
  52. }} />;
  53. },
  54. }, {
  55. title: '时长',
  56. dataIndex: 'time',
  57. render: (text) => {
  58. return formatSeconds(text);
  59. },
  60. }, {
  61. title: '视频地址',
  62. dataIndex: 'resource',
  63. render: (text) => {
  64. return <a href={text} target='_blank'>访问</a>;
  65. },
  66. }, {
  67. title: '试用视频',
  68. dataIndex: 'isTrail',
  69. render: (text, record) => {
  70. return <div>
  71. <Checkbox checked={!!text} onChange={(v) => {
  72. this.changeNo('isTrail', record.id, v.target.checked ? 1 : 0);
  73. }} />
  74. {text > 0 && <InputNumber value={record.startTrail} onChange={(v) => {
  75. this.changeNo('startTrail', record.id, v);
  76. }} />}
  77. {text > 0 && <InputNumber value={record.endTrail} onChange={(v) => {
  78. this.changeNo('endTrail', record.id, v);
  79. }} />}
  80. {text > 0 && <Upload
  81. showUploadList={false}
  82. beforeUpload={(file) => System.uploadVideo(file).then((result) => {
  83. return this.changeNo('trailResource', record.id, result.url);
  84. })}
  85. >
  86. <Button>上传视频{record.trailResource ? '(已上传)' : ''}</Button>
  87. </Upload>}
  88. </div>;
  89. },
  90. }, {
  91. title: '操作',
  92. dataIndex: 'handler',
  93. render: (text, record) => {
  94. const { total } = this.state;
  95. return <div className="table-button">
  96. {record.no > 1 && (
  97. <a onClick={() => {
  98. this.changeNo('no', record.id, record.no - 1);
  99. }}>上移</a>
  100. )}
  101. {record.no <= total && (
  102. <a onClick={() => {
  103. this.changeNo('no', record.id, record.no + 1);
  104. }}>下移</a>
  105. )}
  106. {(
  107. <a onClick={() => {
  108. this.delNo(record.id);
  109. }}>删除</a>
  110. )}
  111. </div>;
  112. },
  113. }];
  114. this.timeColumns = [{
  115. title: '时间段',
  116. dataIndex: 'time',
  117. render: (text, record) => {
  118. return <DatePicker.RangePicker value={[record.startTime, record.endTime]} onChange={(value) => {
  119. this.changeTime(record.id, { startTime: value[0], endTime: value[1] });
  120. }} />;
  121. },
  122. }, {
  123. title: '学员数量',
  124. dataIndex: 'studentNumber',
  125. render: (text, record) => {
  126. return <Link to={`/course/student/${this.params.id}?timeId=${record.id}`}>{text || 0}</Link>;
  127. },
  128. }, {
  129. title: '状态',
  130. dataIndex: 'status',
  131. render: (text, record) => {
  132. let status = 0;
  133. const start = new Date(record.startTime);
  134. const end = new Date(record.endTime);
  135. if (new Date().getTime() > start.getTime()) {
  136. status = 1;
  137. if (new Date().getTime() > end.getTime()) {
  138. status = 2;
  139. }
  140. }
  141. return CourseStatusMap[status] || status;
  142. },
  143. }];
  144. Exercise.courseStruct().then((result) => {
  145. const list = result.map(row => { row.title = `${row.titleZh}`; row.value = row.id; return row; });
  146. const tree = formatTreeData(list, 'id', 'title', 'parentId');
  147. this.exerciseMap = getMap(result.map(row => {
  148. row.title = `${row.titleZh}`;
  149. row.value = row.id;
  150. return row;
  151. }), 'id');
  152. this.setState({ exercise: tree });
  153. });
  154. }
  155. initData() {
  156. const { id } = this.params;
  157. const { module } = this.state.search;
  158. let handler;
  159. if (id) {
  160. handler = Course.get({ id });
  161. } else {
  162. handler = Promise.resolve({ structId: 0, courseModule: module });
  163. }
  164. handler
  165. .then(result => {
  166. this.setState({ module: result.courseModule });
  167. const { form } = this.props;
  168. result.structId = `${result.structId}`;
  169. form.setFieldsValue(result);
  170. if (id) {
  171. switch (result.courseModule) {
  172. case 'video':
  173. this.refreshNo();
  174. break;
  175. case 'online':
  176. this.refreshTime();
  177. break;
  178. default:
  179. }
  180. }
  181. this.setState({ data: result });
  182. });
  183. }
  184. refreshNo() {
  185. const { id } = this.params;
  186. Course.allNo({ courseId: id }).then(result => {
  187. this.setState({ list: result, total: result.length, no: true });
  188. });
  189. }
  190. changeNo(field, id, value) {
  191. Course.editNo({ id, [field]: value }).then(() => {
  192. this.refreshNo();
  193. });
  194. }
  195. delNo(id) {
  196. Course.delNo({ id }).then(() => {
  197. this.refreshNo();
  198. });
  199. }
  200. refreshTime() {
  201. const { id } = this.params;
  202. Course.listTime({ courseId: id }).then(result => {
  203. result.list = result.list.map(row => {
  204. row.startTime = moment(row.startTime);
  205. row.endTime = moment(row.endTime);
  206. return row;
  207. });
  208. this.setState({ list: result.list, total: result.total, time: true });
  209. });
  210. }
  211. changeTime(id, data) {
  212. data.id = id;
  213. Course.editTime(data).then(() => {
  214. this.refreshTime();
  215. });
  216. }
  217. submit() {
  218. const { form } = this.props;
  219. const { module } = this.state;
  220. form.validateFields((err) => {
  221. if (!err) {
  222. const data = form.getFieldsValue();
  223. data.parentStructId = this.exerciseMap[data.structId] ? this.exerciseMap[data.structId].parentId : 0;
  224. data.extend = this.exerciseMap[data.structId] ? this.exerciseMap[data.structId].extend : '';
  225. let handler;
  226. if (data.id) {
  227. handler = Course.edit(data);
  228. } else {
  229. data.courseModule = module;
  230. handler = Course.add(data);
  231. }
  232. handler.then((result) => {
  233. asyncSMessage('保存成功');
  234. if (data.id) {
  235. linkTo(`/course/detail/${data.id}`);
  236. } else {
  237. linkTo(`/course/detail/${result.id}`);
  238. }
  239. }).catch((e) => {
  240. if (e.result) form.setFields(formatFormError(data, e.result));
  241. });
  242. }
  243. });
  244. }
  245. renderVideo() {
  246. const { exercise, data } = this.state;
  247. const { getFieldDecorator, getFieldValue, setFieldsValue } = this.props.form;
  248. const cover = getFieldValue('cover');
  249. return <Block>
  250. <Form>
  251. {getFieldDecorator('id')(<input hidden />)}
  252. {exercise && data.structId && <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='学科'>
  253. {getFieldDecorator('structId', {
  254. rules: [{
  255. required: true, message: '请选择学科',
  256. }],
  257. initialValue: data.structId,
  258. })(
  259. <TreeSelect treeData={exercise} />,
  260. )}
  261. </Form.Item>}
  262. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='类型'>
  263. {getFieldDecorator('videoType', {
  264. rules: [
  265. { required: true, message: '请选择' },
  266. ],
  267. })(
  268. <Select select={CourseVideoType} />,
  269. )}
  270. </Form.Item>
  271. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='适合人群'>
  272. {getFieldDecorator('crowd', {
  273. rules: [
  274. { required: true, message: '请选择' },
  275. ],
  276. })(
  277. <Radio select={CrowdList} />,
  278. )}
  279. </Form.Item>
  280. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='定价'>
  281. {getFieldDecorator('price', {
  282. rules: [
  283. { required: true, message: '请输入价格' },
  284. ],
  285. })(
  286. <InputNumber placeholder='请输入' />,
  287. )}
  288. </Form.Item>
  289. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程名称'>
  290. {getFieldDecorator('title', {
  291. rules: [
  292. { required: true, message: '请输入课程名称' },
  293. ],
  294. })(
  295. <Input placeholder='请输入课程名称' />,
  296. )}
  297. </Form.Item>
  298. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='有效期'>
  299. {getFieldDecorator('expireDays', {
  300. rules: [
  301. { required: true, message: '请输入有效期' },
  302. ],
  303. })(
  304. <InputNumber placeholder='天' />,
  305. )}
  306. </Form.Item>
  307. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='教师'>
  308. {getFieldDecorator('teacher', {
  309. rules: [
  310. { required: true, message: '请输入教师' },
  311. ],
  312. })(
  313. <Input placeholder='请输入教师' />,
  314. )}
  315. </Form.Item>
  316. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程封面'>
  317. {getFieldDecorator('cover', {
  318. rules: [
  319. { required: true, message: '上传图片' },
  320. ],
  321. })(
  322. <Upload
  323. listType="picture-card"
  324. showUploadList={false}
  325. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  326. setFieldsValue({ cover: result.url });
  327. return Promise.reject();
  328. })}
  329. >
  330. {cover ? <img src={cover} alt="avatar" /> : <div>
  331. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  332. <div className="ant-upload-text">Upload</div>
  333. </div>}
  334. </Upload>,
  335. )}
  336. </Form.Item>
  337. </Form>
  338. </Block>;
  339. }
  340. renderNo() {
  341. const { no } = this.state;
  342. if (!no) return null;
  343. return <Block>
  344. <h1>上传正式视频</h1>
  345. <ActionLayout
  346. itemList={this.actionList}
  347. selectedKeys={this.state.selectedKeys}
  348. onAction={key => this.onAction(key)}
  349. />
  350. <TableLayout
  351. columns={this.noColumns}
  352. list={this.state.list}
  353. pagination={false}
  354. loading={this.props.core.loading}
  355. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  356. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  357. selectedKeys={this.state.selectedKeys}
  358. />
  359. </Block>;
  360. }
  361. renderInfoVideo() {
  362. const { getFieldDecorator } = this.props.form;
  363. return <Block flex>
  364. <h1>课程介绍</h1>
  365. <Form>
  366. <Form.Item label='老师资历'>
  367. {getFieldDecorator('teacherContent', {
  368. })(
  369. <Editor placeholder='输入内容' />,
  370. )}
  371. </Form.Item>
  372. <Form.Item label='基本参数'>
  373. {getFieldDecorator('baseContent', {
  374. })(
  375. <Editor placeholder='输入内容' />,
  376. )}
  377. </Form.Item>
  378. <Form.Item label='授课内容'>
  379. {getFieldDecorator('courseContent', {
  380. })(
  381. <Editor placeholder='输入内容' />,
  382. )}
  383. </Form.Item>
  384. <Form.Item label='授课重点'>
  385. {getFieldDecorator('pointContent', {
  386. })(
  387. <Editor placeholder='输入内容' />,
  388. )}
  389. </Form.Item>
  390. <Form.Item label='适合人群'>
  391. {getFieldDecorator('crowdContent', {
  392. })(
  393. <Editor placeholder='输入内容' />,
  394. )}
  395. </Form.Item>
  396. </Form>
  397. </Block>;
  398. }
  399. renderSyllabus() {
  400. const { getFieldDecorator } = this.props.form;
  401. return <Block flex>
  402. <Form>
  403. <Form.Item label='授课大纲'>
  404. {getFieldDecorator('syllabusContent', {
  405. })(
  406. <Editor placeholder='输入内容' />,
  407. )}
  408. </Form.Item>
  409. </Form>
  410. </Block>;
  411. }
  412. renderPromote() {
  413. const { getFieldDecorator } = this.props.form;
  414. return <Block flex>
  415. <Form>
  416. <Form.Item label='优惠信息'>
  417. {getFieldDecorator('promoteContent', {
  418. })(
  419. <Editor placeholder='输入内容' />,
  420. )}
  421. </Form.Item>
  422. </Form>
  423. </Block>;
  424. }
  425. renderOnline() {
  426. const { exercise, data } = this.state;
  427. const { getFieldDecorator } = this.props.form;
  428. return <Block>
  429. <Form>
  430. {getFieldDecorator('id')(<input hidden />)}
  431. {exercise && data.structId && <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='学科'>
  432. {getFieldDecorator('structId', {
  433. rules: [{
  434. required: true, message: '请选择学科',
  435. }],
  436. initialValue: data.structId,
  437. })(
  438. <TreeSelect treeData={exercise} />,
  439. )}
  440. </Form.Item>}
  441. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程名称'>
  442. {getFieldDecorator('title', {
  443. rules: [
  444. { required: true, message: '请输入课程名称' },
  445. ],
  446. })(
  447. <Input placeholder='请输入课程名称' />,
  448. )}
  449. </Form.Item>
  450. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问权限延迟'>
  451. {getFieldDecorator('askExtendDays', {
  452. rules: [
  453. { required: true, message: '请输入扩展天数' },
  454. ],
  455. })(
  456. <InputNumber placeholder='天' />,
  457. )}
  458. </Form.Item>
  459. </Form>
  460. </Block>;
  461. }
  462. renderTime() {
  463. const { time, timerange } = this.state;
  464. if (!time) return null;
  465. return <Block>
  466. <h1>课时管理</h1>
  467. <TableLayout
  468. columns={this.timeColumns}
  469. list={this.state.list}
  470. pagination={false}
  471. loading={this.props.core.loading}
  472. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  473. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  474. selectedKeys={this.state.selectedKeys}
  475. />
  476. <Row>
  477. <Col span={8}>
  478. <Form.Item>
  479. <DatePicker.RangePicker value={timerange} onChange={(value) => {
  480. this.setState({ timerange: value });
  481. }} />
  482. </Form.Item>
  483. </Col>
  484. <Col span={1}>
  485. <Form.Item>
  486. <Button onClick={() => {
  487. Course.addTime({ courseId: this.params.id, startTime: timerange[0], endTime: timerange[1] }).then(() => {
  488. this.refreshTime();
  489. this.setState({ timerange: null });
  490. });
  491. }}>添加</Button>
  492. </Form.Item>
  493. </Col>
  494. </Row>
  495. </Block>;
  496. }
  497. renderContent() {
  498. switch (this.state.module) {
  499. case 'online':
  500. return [this.renderOnline(), this.renderTime()];
  501. case 'video':
  502. return [this.renderVideo(), this.renderNo(), this.renderInfoVideo(), this.renderSyllabus(), this.renderPromote()];
  503. default:
  504. return <div />;
  505. }
  506. }
  507. renderView() {
  508. return <div flex>
  509. {this.renderContent()}
  510. <Row type="flex" justify="center">
  511. <Col>
  512. <Button type="primary" onClick={() => {
  513. this.submit();
  514. }}>保存</Button>
  515. </Col>
  516. </Row>
  517. </div>;
  518. }
  519. }