videoForm.jsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 } from '@/dataDic.js';
  15. import { getVideoType,getReleaseStateList,splitUrl} 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, ',','http://118.190.205.7:8000') : [];
  52. fileList[0].url=thisData.transcoding==1?'javascript:;':'http://118.190.205.7:8000'+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. url: theVideoUrl.length?theVideoUrl:'',
  107. summary:values.summary,
  108. coverUrl:this.state.videoImg.join(','),
  109. status:index?index:0
  110. }
  111. }).done(function (data) {
  112. this.setState({
  113. loading: false
  114. });
  115. if (!data.error.length) {
  116. message.success('操作成功!');
  117. this.setState({
  118. visible: false
  119. });
  120. this.props.handOk();
  121. } else {
  122. message.warning(data.error[0].message);
  123. }
  124. }.bind(this));
  125. }
  126. });
  127. },
  128. cancelFun(){
  129. this.state.edit={};
  130. this.props.closeDesc();
  131. },
  132. componentWillMount() {
  133. if (this.props.data.id) {
  134. this.loadData(this.props.data.id);
  135. } else {
  136. this.state.data = {};
  137. this.state.videoImg = [];
  138. this.state.fileList=[];
  139. };
  140. },
  141. componentWillReceiveProps(nextProps) {
  142. if (!this.props.visible && nextProps.visible) {
  143. if (nextProps.data.id) {
  144. this.loadData(nextProps.data.id);
  145. } else {
  146. this.state.data = {};
  147. this.state.videoImg = [];
  148. this.state.theData={};
  149. this.state.fileList=[];
  150. };
  151. this.props.form.resetFields();
  152. };
  153. },
  154. //视频上传
  155. handleChange(info) {
  156. let fileList = info.fileList;
  157. this.setState({ fileList,loading:true });
  158. if (info.file.status !== 'uploading') {
  159. }
  160. if (info.file.status === 'done') {
  161. message.success(`上传成功`);
  162. fileList[0].url='javascript:;';
  163. fileList[0].name=(fileList[0].response.data).split('/').pop();
  164. this.setState({
  165. fileList:fileList
  166. })
  167. this.setState({
  168. loading:false,
  169. })
  170. } else if (info.file.status === 'error') {
  171. message.error(`上传失败`);
  172. this.setState({
  173. loading:false,
  174. })
  175. }
  176. },
  177. beforeUpload(info){
  178. if(info.type.indexOf('video')<0){
  179. message.warning('请上传MP4格式视频文件.');
  180. return false;
  181. };
  182. let sizeMb = info.size/(1024*1024),size=100;
  183. if(sizeMb>size){ //100Mb之内
  184. message.warning(`视频大小不超过${size}`);
  185. return false;
  186. };
  187. },
  188. handleCancel() {
  189. this.setState({ previewVisible: false });
  190. let video = document.getElementsByTagName('video')[0];
  191. video.pause();
  192. },
  193. handlePreview(file) {
  194. this.setState({
  195. previewImage: file.url || file.thumbUrl,
  196. previewVisible: true
  197. });
  198. },
  199. videoRemove(file){
  200. let fileList = this.state.fileList;
  201. this.setState(({ fileList }) => {
  202. const index = (fileList).indexOf(file);
  203. const newFileList = fileList.slice();
  204. newFileList.splice(index, 1);
  205. return {
  206. fileList: newFileList,
  207. loading:false,
  208. };
  209. });
  210. },
  211. render() {
  212. const theData = this.state.data || {};
  213. const { getFieldDecorator } = this.props.form;
  214. const FormItem = Form.Item;
  215. const formItemLayout = {
  216. labelCol: { span: 6 },
  217. wrapperCol: { span: 12 },
  218. };
  219. const fileList= this.state.fileList||[];
  220. return (
  221. <div className='login'>
  222. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  223. <Spin spinning={this.state.loading}>
  224. <div className="clearfix">
  225. <FormItem className="half-item" {...formItemLayout} label="视频名称">
  226. {getFieldDecorator('name', {
  227. rules: [ { required: true, message: '此项为必填项!' } ],
  228. initialValue: theData.name
  229. })(<Input placeholder="请输入视频的名称" />)}
  230. </FormItem>
  231. <FormItem className="half-item" {...formItemLayout} label="视频类型">
  232. {getFieldDecorator('type', {
  233. rules: [ { required: true, message: '此项为必填项!' } ],
  234. initialValue: theData.type||theData.type=='0'?String(theData.type):theData.type
  235. })(<Select placeholder="请选择视频类型">
  236. {videoType.map(item=>{
  237. return <Select.Option value={item.value}>{item.key}</Select.Option>
  238. })}
  239. </Select>)}
  240. </FormItem>
  241. </div>
  242. <div className="clearfix">
  243. <FormItem
  244. className="half-item"
  245. {...formItemLayout}
  246. label={<span><strong style={{color:'#f00',fontSize:12}}>* </strong>视频文件</span>}
  247. >
  248. {theData.transcoding==1?<Upload
  249. action={globalConfig.context +'/api/admin/video/upload' }
  250. data={{ sign:'video' }}
  251. fileList={this.state.fileList}
  252. listType="text"
  253. beforeUpload={this.beforeUpload.bind(this)}
  254. onChange={this.handleChange.bind(this)}
  255. onRemove ={this.videoRemove}
  256. >
  257. {fileList.length >= 1 ? null : <div>
  258. <Button type="primary">点击上传</Button>
  259. </div>}
  260. </Upload>:
  261. <div className="clearfix">
  262. <Upload
  263. action={globalConfig.context + '/api/admin/video/upload'}
  264. data={{ sign: 'video'}}
  265. listType="picture-card"
  266. fileList={this.state.fileList}
  267. onPreview={this.handlePreview}
  268. onChange={this.handleChange}
  269. onRemove ={this.videoRemove}
  270. beforeUpload={this.beforeUpload.bind(this)}
  271. >
  272. {this.state.fileList.length >= 1 ? null : <div>
  273. <Icon type="plus" />
  274. <div className="ant-upload-text">点击上传</div>
  275. </div>}
  276. </Upload>
  277. <Modal maskClosable={false} visible={this.state.previewVisible} footer={null} onCancel={this.handleCancel}>
  278. <video controls alt="example" style={{ width: '100%' }} src={this.state.previewImage}></video>
  279. </Modal>
  280. </div>}
  281. </FormItem>
  282. <FormItem
  283. className="half-item"
  284. {...formItemLayout}
  285. label={<span><strong style={{color:'#f00',fontSize:12}}>* </strong>封面图片</span>}
  286. >
  287. <Crop
  288. getAllImg={(e)=>{this.setState({videoImg:e})}}
  289. number = {1}
  290. aspectRatio = {1/1}
  291. url ={globalConfig.context + '/api/admin/video/upload'}
  292. uploadData = {{sign:'video_cover'}}
  293. urlArr = {this.state.videoImg}
  294. />
  295. </FormItem>
  296. </div>
  297. <div className="clearfix">
  298. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="视频简介">
  299. {getFieldDecorator('summary', {
  300. initialValue: theData.summary
  301. })(<Input type="textarea" rows={4} placeholder="输入视频简介" />)}
  302. </FormItem>
  303. </div>
  304. <div className="clearfix">
  305. <FormItem className="half-item" {...formItemLayout} label="发布人">
  306. <span>{theData.owner?theData.owner:'管理员'}</span>
  307. </FormItem>
  308. {theData.createTime&&<FormItem className="half-item" {...formItemLayout} label="发布状态">
  309. <span>{getReleaseStateList(theData.status)}</span>
  310. </FormItem>}
  311. </div>
  312. <div className="clearfix">
  313. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  314. <Button className="set-submit" type="primary" htmlType="submit">
  315. 保存草稿
  316. </Button>
  317. <Button
  318. className="set-submit"
  319. type="ghost"
  320. onClick={(e) => {
  321. this.handleSubmit(e,1)
  322. }}
  323. >
  324. 发布视频
  325. </Button>
  326. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  327. 取消
  328. </Button>
  329. </FormItem>
  330. </div>
  331. </Spin>
  332. </Form>
  333. </div>
  334. )
  335. }
  336. }));
  337. export default NewDetailForm;