videoForm.jsx 9.2 KB

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