videoForm.jsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. },
  191. handlePreview(file) {
  192. this.setState({
  193. previewImage: file.url || file.thumbUrl,
  194. previewVisible: true
  195. });
  196. },
  197. videoRemove(file){
  198. let fileList = this.state.fileList;
  199. this.setState(({ fileList }) => {
  200. const index = (fileList).indexOf(file);
  201. const newFileList = fileList.slice();
  202. newFileList.splice(index, 1);
  203. return {
  204. fileList: newFileList,
  205. loading:false,
  206. };
  207. });
  208. },
  209. render() {
  210. const theData = this.state.data || {};
  211. const { getFieldDecorator } = this.props.form;
  212. const FormItem = Form.Item;
  213. const formItemLayout = {
  214. labelCol: { span: 6 },
  215. wrapperCol: { span: 12 },
  216. };
  217. const fileList= this.state.fileList||[];
  218. return (
  219. <div className='login'>
  220. <Form layout="horizontal" onSubmit={(e)=>{this.handleSubmit(e,0)}} id="demand-form">
  221. <Spin spinning={this.state.loading}>
  222. <div className="clearfix">
  223. <FormItem className="half-item" {...formItemLayout} label="视频名称">
  224. {getFieldDecorator('name', {
  225. rules: [ { required: true, message: '此项为必填项!' } ],
  226. initialValue: theData.name
  227. })(<Input placeholder="请输入视频的名称" />)}
  228. </FormItem>
  229. <FormItem className="half-item" {...formItemLayout} label="视频类型">
  230. {getFieldDecorator('type', {
  231. rules: [ { required: true, message: '此项为必填项!' } ],
  232. initialValue: theData.type||theData.type=='0'?String(theData.type):theData.type
  233. })(<Select placeholder="请选择视频类型">
  234. {videoType.map(item=>{
  235. return <Select.Option value={item.value}>{item.key}</Select.Option>
  236. })}
  237. </Select>)}
  238. </FormItem>
  239. </div>
  240. <div className="clearfix">
  241. <FormItem
  242. className="half-item"
  243. {...formItemLayout}
  244. label={<span><strong style={{color:'#f00',fontSize:12}}>* </strong>视频文件</span>}
  245. >
  246. {theData.transcoding==1?<Upload
  247. action={globalConfig.context +'/api/admin/video/upload' }
  248. data={{ sign:'video' }}
  249. fileList={this.state.fileList}
  250. listType="text"
  251. beforeUpload={this.beforeUpload.bind(this)}
  252. onChange={this.handleChange.bind(this)}
  253. onRemove ={this.videoRemove}
  254. >
  255. {fileList.length >= 1 ? null : <div>
  256. <Button type="primary">点击上传</Button>
  257. </div>}
  258. </Upload>:
  259. <div className="clearfix">
  260. <Upload
  261. action={globalConfig.context + '/api/admin/video/upload'}
  262. data={{ sign: 'video'}}
  263. listType="picture-card"
  264. fileList={this.state.fileList}
  265. onPreview={this.handlePreview}
  266. onChange={this.handleChange}
  267. onRemove ={this.videoRemove}
  268. beforeUpload={this.beforeUpload.bind(this)}
  269. >
  270. {this.state.fileList.length >= 1 ? null : <div>
  271. <Icon type="plus" />
  272. <div className="ant-upload-text">点击上传</div>
  273. </div>}
  274. </Upload>
  275. <Modal maskClosable={false} visible={this.state.previewVisible} footer={null} onCancel={this.handleCancel}>
  276. <video controls alt="example" style={{ width: '100%' }} src={this.state.previewImage}></video>
  277. </Modal>
  278. </div>}
  279. </FormItem>
  280. <FormItem
  281. className="half-item"
  282. {...formItemLayout}
  283. label={<span><strong style={{color:'#f00',fontSize:12}}>* </strong>封面图片</span>}
  284. >
  285. <Crop
  286. getAllImg={(e)=>{this.setState({videoImg:e})}}
  287. number = {1}
  288. aspectRatio = {1/1}
  289. url ={globalConfig.context + '/api/admin/video/upload'}
  290. uploadData = {{sign:'video_cover'}}
  291. urlArr = {this.state.videoImg}
  292. />
  293. </FormItem>
  294. </div>
  295. <div className="clearfix">
  296. <FormItem labelCol={{ span: 3 }} wrapperCol={{ span: 18 }} label="视频简介">
  297. {getFieldDecorator('summary', {
  298. initialValue: theData.summary
  299. })(<Input type="textarea" rows={4} placeholder="输入视频简介" />)}
  300. </FormItem>
  301. </div>
  302. <div className="clearfix">
  303. <FormItem className="half-item" {...formItemLayout} label="发布人">
  304. <span>{theData.owner?theData.owner:'管理员'}</span>
  305. </FormItem>
  306. {theData.createTime&&<FormItem className="half-item" {...formItemLayout} label="发布状态">
  307. <span>{getReleaseStateList(theData.status)}</span>
  308. </FormItem>}
  309. </div>
  310. <div className="clearfix">
  311. <FormItem wrapperCol={{ span: 12, offset: 3 }}>
  312. <Button className="set-submit" type="primary" htmlType="submit">
  313. 保存草稿
  314. </Button>
  315. <Button
  316. className="set-submit"
  317. type="ghost"
  318. onClick={(e) => {
  319. this.handleSubmit(e,1)
  320. }}
  321. >
  322. 发布视频
  323. </Button>
  324. <Button className="set-submit" type="ghost" onClick={this.cancelFun}>
  325. 取消
  326. </Button>
  327. </FormItem>
  328. </div>
  329. </Spin>
  330. </Form>
  331. </div>
  332. )
  333. }
  334. }));
  335. export default NewDetailForm;