page.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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, title: file.name.split('.')[0] });
  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, expireDays: 180 });
  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('useExpireDays', {
  309. rules: [
  310. { required: true, message: '请输入有效期' },
  311. ],
  312. })(
  313. <InputNumber placeholder='天' />,
  314. )}
  315. </Form.Item>
  316. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='教师'>
  317. {getFieldDecorator('teacher', {
  318. rules: [
  319. { required: true, message: '请输入教师' },
  320. ],
  321. })(
  322. <Input placeholder='请输入教师' />,
  323. )}
  324. </Form.Item>
  325. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程封面'>
  326. {getFieldDecorator('cover', {
  327. rules: [
  328. { required: true, message: '上传图片' },
  329. ],
  330. })(
  331. <Upload
  332. listType="picture-card"
  333. showUploadList={false}
  334. beforeUpload={(file) => System.uploadImage(file).then((result) => {
  335. setFieldsValue({ cover: result.url });
  336. return Promise.reject();
  337. })}
  338. >
  339. {cover ? <img src={cover} alt="avatar" /> : <div>
  340. <Icon type={this.state.loading ? 'loading' : 'plus'} />
  341. <div className="ant-upload-text">Upload</div>
  342. </div>}
  343. </Upload>,
  344. )}
  345. </Form.Item>
  346. </Form>
  347. </Block>;
  348. }
  349. renderNo() {
  350. const { no } = this.state;
  351. if (!no) return null;
  352. return <Block>
  353. <h1>上传正式视频</h1>
  354. <ActionLayout
  355. itemList={this.actionList}
  356. selectedKeys={this.state.selectedKeys}
  357. onAction={key => this.onAction(key)}
  358. />
  359. <TableLayout
  360. columns={this.noColumns}
  361. list={this.state.list}
  362. pagination={false}
  363. loading={this.props.core.loading}
  364. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  365. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  366. selectedKeys={this.state.selectedKeys}
  367. />
  368. </Block>;
  369. }
  370. renderInfoVideo() {
  371. const { getFieldDecorator } = this.props.form;
  372. return <Block flex>
  373. <h1>课程介绍</h1>
  374. <Form>
  375. <Form.Item label='老师资历'>
  376. {getFieldDecorator('teacherContent', {
  377. })(
  378. <Editor placeholder='输入内容' />,
  379. )}
  380. </Form.Item>
  381. <Form.Item label='基本参数'>
  382. {getFieldDecorator('baseContent', {
  383. })(
  384. <Editor placeholder='输入内容' />,
  385. )}
  386. </Form.Item>
  387. <Form.Item label='授课内容'>
  388. {getFieldDecorator('courseContent', {
  389. })(
  390. <Editor placeholder='输入内容' />,
  391. )}
  392. </Form.Item>
  393. <Form.Item label='授课重点'>
  394. {getFieldDecorator('pointContent', {
  395. })(
  396. <Editor placeholder='输入内容' />,
  397. )}
  398. </Form.Item>
  399. <Form.Item label='适合人群'>
  400. {getFieldDecorator('crowdContent', {
  401. })(
  402. <Editor placeholder='输入内容' />,
  403. )}
  404. </Form.Item>
  405. </Form>
  406. </Block>;
  407. }
  408. renderSyllabus() {
  409. const { getFieldDecorator } = this.props.form;
  410. return <Block flex>
  411. <Form>
  412. <Form.Item label='授课大纲'>
  413. {getFieldDecorator('syllabusContent', {
  414. })(
  415. <Editor placeholder='输入内容' />,
  416. )}
  417. </Form.Item>
  418. </Form>
  419. </Block>;
  420. }
  421. renderPromote() {
  422. const { getFieldDecorator } = this.props.form;
  423. return <Block flex>
  424. <Form>
  425. <Form.Item label='优惠信息'>
  426. {getFieldDecorator('promoteContent', {
  427. })(
  428. <Editor placeholder='输入内容' />,
  429. )}
  430. </Form.Item>
  431. </Form>
  432. </Block>;
  433. }
  434. renderOnline() {
  435. const { exercise, data } = this.state;
  436. const { getFieldDecorator } = this.props.form;
  437. return <Block>
  438. <Form>
  439. {getFieldDecorator('id')(<input hidden />)}
  440. {exercise && data.structId && <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='学科'>
  441. {getFieldDecorator('structId', {
  442. rules: [{
  443. required: true, message: '请选择学科',
  444. }],
  445. initialValue: data.structId,
  446. })(
  447. <TreeSelect treeData={exercise} />,
  448. )}
  449. </Form.Item>}
  450. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='课程名称'>
  451. {getFieldDecorator('title', {
  452. rules: [
  453. { required: true, message: '请输入课程名称' },
  454. ],
  455. })(
  456. <Input placeholder='请输入课程名称' />,
  457. )}
  458. </Form.Item>
  459. <Form.Item labelCol={{ span: 5 }} wrapperCol={{ span: 16 }} label='提问权限延迟'>
  460. {getFieldDecorator('askExtendDays', {
  461. rules: [
  462. { required: true, message: '请输入扩展天数' },
  463. ],
  464. })(
  465. <InputNumber placeholder='天' />,
  466. )}
  467. </Form.Item>
  468. </Form>
  469. </Block>;
  470. }
  471. renderTime() {
  472. const { time, timerange } = this.state;
  473. if (!time) return null;
  474. return <Block>
  475. <h1>课时管理</h1>
  476. <TableLayout
  477. columns={this.timeColumns}
  478. list={this.state.list}
  479. pagination={false}
  480. loading={this.props.core.loading}
  481. onChange={(pagination, filters, sorter) => this.tableChange(pagination, filters, sorter)}
  482. onSelect={(keys, rows) => this.tableSelect(keys, rows)}
  483. selectedKeys={this.state.selectedKeys}
  484. />
  485. <Row>
  486. <Col span={8}>
  487. <Form.Item>
  488. <DatePicker.RangePicker value={timerange} onChange={(value) => {
  489. this.setState({ timerange: value });
  490. }} />
  491. </Form.Item>
  492. </Col>
  493. <Col span={1}>
  494. <Form.Item>
  495. <Button onClick={() => {
  496. Course.addTime({ courseId: this.params.id, startTime: timerange[0], endTime: timerange[1] }).then(() => {
  497. this.refreshTime();
  498. this.setState({ timerange: null });
  499. });
  500. }}>添加</Button>
  501. </Form.Item>
  502. </Col>
  503. </Row>
  504. </Block>;
  505. }
  506. renderContent() {
  507. switch (this.state.module) {
  508. case 'online':
  509. return [this.renderOnline(), this.renderTime()];
  510. case 'video':
  511. return [this.renderVideo(), this.renderNo(), this.renderInfoVideo(), this.renderSyllabus(), this.renderPromote()];
  512. default:
  513. return <div />;
  514. }
  515. }
  516. renderView() {
  517. return <div flex>
  518. {this.renderContent()}
  519. <Row type="flex" justify="center">
  520. <Col>
  521. <Button type="primary" onClick={() => {
  522. this.submit();
  523. }}>保存</Button>
  524. </Col>
  525. </Row>
  526. </div>;
  527. }
  528. }