VideoCreate.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import React, { PureComponent } from 'react';
  2. import pathToRegexp from 'path-to-regexp';
  3. import { Card, Form, Radio, Input, Button } from 'antd';
  4. import { connect } from 'dva';
  5. import { routerRedux } from 'dva/router';
  6. import PageHeaderLayout from '../../../layouts/PageHeaderLayout';
  7. import { resourceTypes, resourceQuality } from '../../../utils/utils';
  8. import { Hotax } from '../../../utils/config';
  9. const formItemLayout = {
  10. labelCol: {
  11. xs: { span: 24 },
  12. sm: { span: 6 },
  13. },
  14. wrapperCol: {
  15. xs: { span: 24 },
  16. sm: { span: 14 },
  17. md: { span: 12 },
  18. },
  19. };
  20. const submitFormLayout = {
  21. wrapperCol: {
  22. xs: { span: 24, offset: 0 },
  23. sm: { span: 12, offset: 6 },
  24. },
  25. };
  26. @Form.create()
  27. @connect(({ loading }) => ({
  28. submitting: loading.models.resource,
  29. }))
  30. export default class VideoCreatePage extends PureComponent {
  31. isEdit = () => {
  32. const { location } = this.props;
  33. const match = pathToRegexp('/resource/video-edit/:id').exec(location.pathname);
  34. if (match) {
  35. return match[1];
  36. }
  37. return false;
  38. };
  39. handlePageBack = () => {
  40. const { UIParams, Queryers } = this.props.location.state || {};
  41. this.props.dispatch(
  42. routerRedux.push({
  43. pathname: '/resource/video',
  44. state: { UIParams, Queryers },
  45. })
  46. );
  47. };
  48. handlePageSubmit = (e) => {
  49. e.preventDefault();
  50. this.props.form.validateFieldsAndScroll((err, values) => {
  51. if (!err) {
  52. const { UIParams, Queryers } = this.props.location.state || {};
  53. const matchId = this.isEdit();
  54. if (matchId) {
  55. this.props.dispatch({
  56. type: 'resource/updateVideo',
  57. payload: { id: matchId, status: Hotax.STATUS_NORMAL, ...values },
  58. states: { UIParams, Queryers },
  59. });
  60. return;
  61. }
  62. this.props.dispatch({
  63. type: 'resource/createVideo',
  64. payload: { status: Hotax.STATUS_NORMAL, ...values },
  65. states: { UIParams, Queryers },
  66. });
  67. }
  68. });
  69. };
  70. render() {
  71. const { form, submitting, location } = this.props;
  72. const { getFieldDecorator } = form;
  73. const { state = {} } = location;
  74. const { code, name, size, type, path, rate, quality, format } = state;
  75. return (
  76. <PageHeaderLayout>
  77. <Card>
  78. <Form onSubmit={this.handlePageSubmit}>
  79. <Form.Item label="视频编号" {...formItemLayout}>
  80. {getFieldDecorator('code', {
  81. rules: [
  82. {
  83. required: true, message: '视频编号不能为空',
  84. }, {
  85. pattern: /^[a-zA-Z0-9|-]+$/g, message: '编号包含非法字符',
  86. },
  87. ],
  88. initialValue: code,
  89. })(
  90. <Input placeholder="请填写" />
  91. )}
  92. </Form.Item>
  93. <Form.Item label="视频名称" {...formItemLayout}>
  94. {getFieldDecorator('name', {
  95. rules: [{ required: true, message: '视频名称不能为空' }],
  96. initialValue: name,
  97. })(
  98. <Input placeholder="请填写" />
  99. )}
  100. </Form.Item>
  101. <Form.Item label="视频路径" {...formItemLayout}>
  102. {getFieldDecorator('path', {
  103. rules: [{ required: true, message: '资源路径为必填项' }],
  104. initialValue: path,
  105. })(
  106. <Input placeholder="请填写" />
  107. )}
  108. </Form.Item>
  109. <Form.Item label="资源类型" {...formItemLayout}>
  110. {getFieldDecorator('type', {
  111. rules: [{ required: true, message: '资源类型不能为空' }],
  112. initialValue: type || Hotax.RESOURCE_VIDEO,
  113. })(
  114. <Radio.Group disabled>
  115. {
  116. Object.keys(resourceTypes).map(key =>
  117. (
  118. <Radio.Button
  119. key={key}
  120. value={parseInt(key, 10)}
  121. >{resourceTypes[key]}
  122. </Radio.Button>
  123. )
  124. )
  125. }
  126. </Radio.Group>
  127. )}
  128. </Form.Item>
  129. <Form.Item label="清晰度" {...formItemLayout}>
  130. {getFieldDecorator('quality', {
  131. rules: [{ required: true, message: '清晰度为必选项' }],
  132. initialValue: quality || Hotax.QUALITY_HIGH,
  133. })(
  134. <Radio.Group disabled>
  135. {
  136. Object.keys(resourceQuality).map(key =>
  137. (
  138. <Radio.Button
  139. key={key}
  140. value={key}
  141. >{resourceQuality[key]}
  142. </Radio.Button>
  143. )
  144. )
  145. }
  146. </Radio.Group>
  147. )}
  148. </Form.Item>
  149. <Form.Item label="视频格式" {...formItemLayout}>
  150. {getFieldDecorator('format', {
  151. rules: [{ required: true, message: '视频格式不能为空' }],
  152. initialValue: format || 'm3u8',
  153. })(
  154. <Input disabled placeholder="请填写" />
  155. )}
  156. </Form.Item>
  157. <Form.Item label="视频码流" {...formItemLayout}>
  158. {getFieldDecorator('rate', {
  159. initialValue: rate,
  160. })(
  161. <Input placeholder="请填写" />
  162. )}
  163. </Form.Item>
  164. <Form.Item label="视频大小" {...formItemLayout}>
  165. {getFieldDecorator('size', {
  166. initialValue: size,
  167. })(
  168. <Input placeholder="请填写" addonAfter="字节" />
  169. )}
  170. </Form.Item>
  171. <Form.Item {...submitFormLayout} style={{ marginTop: 32 }}>
  172. <Button onClick={this.handlePageBack}>取消</Button>
  173. <Button
  174. type="primary"
  175. htmlType="submit"
  176. loading={submitting}
  177. style={{ marginLeft: 8 }}
  178. >提交
  179. </Button>
  180. </Form.Item>
  181. </Form>
  182. </Card>
  183. </PageHeaderLayout>
  184. );
  185. }
  186. }