videoForm.jsx 9.3 KB

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