videoForm.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import React from 'react';
  2. import $ from 'jquery/src/ajax';
  3. import {
  4. Icon,
  5. Modal,
  6. message,
  7. Spin,
  8. Upload,
  9. Input,
  10. Button,
  11. Form,
  12. Select
  13. } from 'antd';
  14. import { videoType,videoPosition } from '@/dataDic.js';
  15. import { splitUrl,getVideoStatus} from '@/tools.js';
  16. import Crop from '@/crop/cropBlock';
  17. const NewDetailForm = Form.create()(React.createClass({
  18. getInitialState() {
  19. return {
  20. visible: false,
  21. loading: false,
  22. auditStatus: 0,
  23. fileList:[],
  24. textFileList: [],
  25. videoImg: [],
  26. vedioUrl:[],
  27. previewVisible:false,
  28. previewImage:''
  29. };
  30. },
  31. loadData(id) {
  32. this.setState({
  33. loading: true
  34. });
  35. $.ajax({
  36. method: "get",
  37. dataType: "json",
  38. crossDomain: false,
  39. url: globalConfig.context + '/api/admin/video/getVideoById' ,
  40. data: {
  41. id: id
  42. },
  43. success: function (data) {
  44. let thisData = data.data,provice=[];
  45. if (!thisData) {
  46. if (data.error && data.error.length) {
  47. message.warning(data.error[0].message);
  48. };
  49. thisData = {};
  50. };
  51. let fileList =thisData.url ? splitUrl(thisData.url, ',',globalConfig.videoServer) : [];
  52. fileList[0].url=thisData.transcoding==1?'javascript:;':globalConfig.videoServer+thisData.url;
  53. fileList[0].name=(thisData.url).split('/').pop();
  54. fileList[0].thumbUrl=globalConfig.avatarHost + '/upload'+thisData.coverUrl;
  55. this.setState({
  56. id:thisData.id,
  57. data: thisData,
  58. fileList:fileList,
  59. videoImg: thisData.coverUrl ? thisData.coverUrl.split(',') : []
  60. });
  61. }.bind(this),
  62. }).always(function () {
  63. this.setState({
  64. loading: false
  65. });
  66. }.bind(this));
  67. },
  68. handleSubmit(e,index) {
  69. e.preventDefault();
  70. this.props.form.validateFields((err, values) => {
  71. //url转化
  72. let theVideoUrl = [];
  73. if (this.state.fileList.length) {
  74. let picArr = [];
  75. this.state.fileList.map(function (item) {
  76. if ( item.response && item.response.data && item.response.data.length ){
  77. picArr.push(item.response.data);
  78. }
  79. });
  80. theVideoUrl = picArr.join(",");
  81. }else{
  82. message.warning('请上传视频文件!');
  83. return false;
  84. };
  85. if(!theVideoUrl.length){
  86. message.warning('请上传视频文件!');
  87. return false;
  88. };
  89. if (!this.state.videoImg.length) {
  90. message.warning('请上传视频封面图片!');
  91. return false;
  92. };
  93. if (!err) {
  94. this.setState({
  95. loading: true
  96. });
  97. $.ajax({
  98. method: "post",
  99. dataType: "json",
  100. async:true,
  101. url: this.props.data.id ? globalConfig.context + '/api/admin/video/updateVideo' : globalConfig.context + '/api/admin/video/insertVideo',
  102. data: {
  103. id: this.props.data.id,
  104. type:values.type,
  105. name: values.name,
  106. position:values.position||0,
  107. url: theVideoUrl.length?theVideoUrl:'',
  108. summary:values.summary,
  109. coverUrl:this.state.videoImg.join(','),
  110. status:index?index:0
  111. }
  112. }).done(function (data) {
  113. this.setState({
  114. loading: false
  115. });
  116. if (!data.error.length) {
  117. message.success('操作成功!');
  118. this.setState({
  119. visible: false
  120. });
  121. this.props.handOk();
  122. } else {
  123. message.warning(data.error[0].message);
  124. }
  125. }.bind(this));
  126. }
  127. });
  128. },
  129. cancelFun(){
  130. this.state.edit={};
  131. this.props.closeDesc();
  132. },
  133. componentWillMount() {
  134. if (this.props.data.id) {
  135. this.loadData(this.props.data.id);
  136. } else {
  137. this.state.data = {};
  138. this.state.videoImg = [];
  139. this.state.fileList=[];
  140. };
  141. },
  142. componentWillReceiveProps(nextProps) {
  143. if (!this.props.visible && nextProps.visible) {
  144. if (nextProps.data.id) {
  145. this.loadData(nextProps.data.id);
  146. } else {
  147. this.state.data = {};
  148. this.state.videoImg = [];
  149. this.state.theData={};
  150. this.state.fileList=[];
  151. };
  152. this.props.form.resetFields();
  153. };
  154. },
  155. //视频上传
  156. handleChange(info) {
  157. let fileList = info.fileList;
  158. this.setState({ fileList,loading:true });
  159. if (info.file.status !== 'uploading') {
  160. }
  161. if (info.file.status === 'done') {
  162. fileList[0].url='javascript:;';
  163. fileList[0].name=(fileList[0].response.data).split('/').pop();
  164. let theDate = fileList[0].response;
  165. if(theDate.error&&theDate.error.length){
  166. message.error(theDate.error[0].message);
  167. this.setState({
  168. fileList:[],
  169. loading:false,
  170. })
  171. return ;
  172. };
  173. message.success('上传成功');
  174. this.setState({
  175. fileList,
  176. loading:false
  177. })
  178. } else if (info.file.status === 'error') {
  179. message.error(`上传失败`);
  180. this.setState({
  181. loading:false,
  182. })
  183. }
  184. },
  185. beforeUpload(info){
  186. if(info.type.indexOf('video')<0){
  187. message.warning('请上传MP4格式视频文件.');
  188. return false;
  189. };
  190. let sizeMb = info.size/(1024*1024),size=100;
  191. if(sizeMb>size){ //100Mb之内
  192. message.warning(`视频大小不超过${size}`);
  193. return false;
  194. };
  195. },
  196. handleCancel() {
  197. this.setState({ previewVisible: false });
  198. let video = document.getElementsByTagName('video')[0];
  199. video.pause();
  200. },
  201. handlePreview(file) {
  202. this.setState({
  203. previewImage: file.url || file.thumbUrl,
  204. previewVisible: true
  205. });
  206. },
  207. videoRemove(file){
  208. let fileList = this.state.fileList;
  209. this.setState(({ fileList }) => {
  210. const index = (fileList).indexOf(file);
  211. const newFileList = fileList.slice();
  212. newFileList.splice(index, 1);
  213. return {
  214. fileList: newFileList,
  215. loading:false,
  216. };
  217. });
  218. },
  219. render() {
  220. const theData = this.state.data || {};
  221. const { getFieldDecorator } = this.props.form;
  222. const FormItem = Form.Item;
  223. const formItemLayout = {
  224. labelCol: { span: 6 },
  225. wrapperCol: { span: 12 },
  226. };
  227. const fileList= this.state.fileList||[];
  228. return (
  229. <div className='login'>
  230. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  231. <Spin spinning={this.state.loading}>
  232. <div className="clearfix">
  233. <FormItem className="half-item" {...formItemLayout} label="视频名称">
  234. {getFieldDecorator('name', {
  235. rules: [ { required: true, message: '此项为必填项!' } ],
  236. initialValue: theData.name
  237. })(<Input placeholder="请输入视频的名称" />)}
  238. </FormItem>
  239. <FormItem className="half-item" {...formItemLayout} label="视频类型">
  240. {getFieldDecorator('type', {
  241. rules: [ { required: true, message: '此项为必填项!' } ],
  242. initialValue: theData.type||theData.type=='0'?String(theData.type):theData.type
  243. })(<Select placeholder="请选择视频类型">
  244. {videoType.map(item=>{
  245. return <Select.Option value={item.value}>{item.key}</Select.Option>
  246. })}
  247. </Select>)}
  248. </FormItem>
  249. </div>
  250. <div className="clearfix">
  251. <FormItem
  252. className="half-item"
  253. {...formItemLayout}
  254. label={<span><strong style={{color:'#f00',fontSize:12}}>* </strong>视频文件</span>}
  255. >
  256. {theData.transcoding==1||!this.props.data.id?<Upload
  257. action={globalConfig.context +'/api/admin/video/upload' }
  258. data={{ sign:'video' }}
  259. fileList={this.state.fileList}
  260. listType="text"
  261. beforeUpload={this.beforeUpload.bind(this)}
  262. onChange={this.handleChange.bind(this)}
  263. onRemove ={this.videoRemove}
  264. >
  265. {fileList.length >= 1 ? null : <div>
  266. <Button type="primary">点击上传</Button>
  267. </div>}
  268. </Upload>:
  269. <div className="clearfix">
  270. <Upload
  271. action={globalConfig.context + '/api/admin/video/upload'}
  272. data={{ sign: 'video'}}
  273. listType="picture-card"
  274. fileList={this.state.fileList}
  275. onPreview={this.handlePreview}
  276. onChange={this.handleChange}
  277. onRemove ={this.videoRemove}
  278. beforeUpload={this.beforeUpload.bind(this)}
  279. >
  280. {this.state.fileList.length >= 1 ? null : <div>
  281. <Icon type="plus" />
  282. <div className="ant-upload-text">点击上传</div>
  283. </div>}
  284. </Upload>
  285. <Modal maskClosable={false} visible={this.state.previewVisible} footer={null} onCancel={this.handleCancel}>
  286. <video controls alt="example" style={{ width: '100%' }} src={this.state.previewImage}></video>
  287. </Modal>
  288. </div>}
  289. </FormItem>
  290. <FormItem
  291. className="half-item"
  292. {...formItemLayout}
  293. label={<span><strong style={{color:'#f00',fontSize:12}}>* </strong>封面图片</span>}
  294. >
  295. <Crop
  296. getAllImg={(e)=>{this.setState({videoImg:e})}}
  297. number = {1}
  298. aspectRatio = {1/1}
  299. url ={globalConfig.context + '/api/admin/video/upload'}
  300. uploadData = {{sign:'video_cover'}}
  301. urlArr = {this.state.videoImg}
  302. />
  303. </FormItem>
  304. </div>
  305. <div className="clearfix">
  306. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="视频简介">
  307. {getFieldDecorator('summary', {
  308. initialValue: theData.summary
  309. })(<Input type="textarea" rows={4} placeholder="输入视频简介" />)}
  310. </FormItem>
  311. </div>
  312. <div className="clearfix">
  313. <FormItem className="half-item" {...formItemLayout} label="视频发布位置">
  314. {getFieldDecorator('position', {
  315. initialValue: theData.position||theData.position=='0'?String(theData.position):undefined
  316. })(<Select placeholder="请选择发布位置">
  317. {videoPosition.map(item=>{
  318. return <Select.Option value={item.value}>{item.key}</Select.Option>
  319. })}
  320. </Select>)}
  321. </FormItem>
  322. {theData.releaseTime&&<FormItem className="half-item" {...formItemLayout} label="发布人">
  323. <span>{theData.owner=='1'?'管理员':theData.owner}</span>
  324. </FormItem>}
  325. {theData.createTime&&<FormItem className="half-item" {...formItemLayout} label="发布状态">
  326. <span>{getVideoStatus(theData.status)}</span>
  327. </FormItem>}
  328. {this.props.data.id&&<FormItem className="half-item" {...formItemLayout} label="视频状态">
  329. <span>{theData.transcoding=='2'?'已转码':'正在转码'}</span>
  330. </FormItem>}
  331. </div>
  332. <div className="clearfix">
  333. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  334. <Button className="set-submit" type="primary" htmlType="submit">
  335. 保存草稿
  336. </Button>
  337. {(this.props.data.id&&theData.transcoding=='2')&&<Button
  338. className="set-submit"
  339. type="ghost"
  340. onClick={(e) => {
  341. this.handleSubmit(e,1)
  342. }}
  343. >
  344. 发布视频
  345. </Button>}
  346. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  347. 取消
  348. </Button>
  349. </FormItem>
  350. </div>
  351. </Spin>
  352. </Form>
  353. </div>
  354. )
  355. }
  356. }));
  357. export default NewDetailForm;