VideoUploadCom.jsx 3.6 KB
Newer Older
1 2
import React from 'react'
import {extend} from 'umi-request';
钟是志's avatar
钟是志 committed
3
import queryConfig from "./queryConfig";
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

import {Upload, message} from 'antd';
import {isJSON} from "@/webPublic/one_stop_public/copy";

const getToken = () => {
  const x = localStorage.getItem('uploadVideoServiceToken');
  if(x === "null" || x === "undefined"){
    return null;
  }
  return x;
};

const configService = queryConfig('uploadVideoService');

const token = getToken();

const umiRequest = extend({
  // errorHandler, // 默认错误处理
  credentials: 'omit', // 默认请求是否带上cookie
  mode: 'cors',
});

export default class VideoUploadCom extends React.Component {
  constructor(props) {
    super(props);
    const {value} = props;
    let url = value;
    if (isJSON(value)) {
      url = JSON.parse(value).url;
    }
    this.state = {
      url: url || '',
    }
  }

  triggerChange = (changedValue) => {
    // Should provide an event to pass value to Form.
    const onChange = this.props.onChange;
    if (onChange) {
      onChange(changedValue);
    }
  };

  componentWillReceiveProps(nextProps) {
    // Should be a controlled component.
    if ('value' in nextProps) {
      const {value} = nextProps;
      if (isJSON(value)) {
        this.setState({
          url: JSON.parse(value).url,
        });
      }
    }
  }

  changeUrl = (info, key) => {
    if (info.file.status === 'done') {
      let filePathThis = info?.file?.response?.data?.filePath;
62
      message.info('正在获取视频缩略图,请稍后');
63 64
      if (filePathThis) {
        setTimeout(() => {
65 66 67 68 69 70 71 72 73 74 75 76
          umiRequest(`${configService}/folderConvertApi/videoScreenshot?token=${token}`, {
              method: "POST", // 暂时支持post 请求
              headers: {
                Accept: "application/json;charset=UTF-8",
                // 'Content-Type': 'application/json',
              },
              requestType: 'form',
              data: {
                path: filePathThis,
                screenshotTime: '00:00:5',
              },
          }).then((res) => {
77 78
            if (res && res.data) {
              let info = res.data;
钟是志's avatar
钟是志 committed
79
              const {screenshots, url, createTime, downloadUrl, filePath, fileExt} = info;
80 81 82 83 84 85
              let needInfo = {
                screenshots,
                url,
                createTime,
                downloadUrl,
                filePath,
86
                fileExt,
87 88 89 90 91
              };
              message.success(`视频上传成功`);
              this.triggerChange(JSON.stringify(needInfo));
            }
          });
92
        }, 3000);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
      }
    } else if (info.file.status === 'error') {
      message.error(`视频上传失败`);
    }
  };

  render() {
    const {json, disabled} = this.props;
    const {url} = this.state;
    console.log(url);
    return (
      <Upload.Dragger disabled={disabled}
                      accept={'.mp4,.webm,.ogg'}
                      url={url}
                      showUploadList={false}
                      name="file"
                      action={configService + '/uploadFileApi/upload'}
                      onChange={this.changeUrl}
                      multiple={false}
                      data={{
                        isConvert: true,
                        token,
                      }}
                      style={{padding: 0}}>
        {url ? <video src={url}
                      controls
                      style={{
                        height: json.height,
                        width: json.width,
                      }}/> :
          <div style={{height: json.height, width: json.width}}>

          </div>}

      </Upload.Dragger>
    );

  }
}