index.tsx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import VideoNews from '@/components/VideoNews';
  2. import { getOssInfo } from '@/services/ant-design-pro/api';
  3. import { getMD5 } from '@/utils';
  4. import { InboxOutlined } from '@ant-design/icons';
  5. import { request } from '@umijs/max';
  6. import { message, Spin, Upload } from 'antd';
  7. import { RcFile } from 'antd/lib/upload';
  8. import React, { useState } from 'react';
  9. import './index.less';
  10. interface Props {
  11. value?: string;
  12. onChange?: (value?: string) => void;
  13. }
  14. /**
  15. * 视频上传
  16. * @returns
  17. */
  18. const UploadVideo: React.FC<Props> = ({ value, onChange }) => {
  19. /** 变量START */
  20. const [videoFile, setVideoFile] = useState<string | undefined>(value);
  21. const [loading, setLoading] = useState<boolean>(false);
  22. /** 变量END */
  23. const beforeUpload = async (file: RcFile) => {
  24. console.log('file--->', file);
  25. const isZipOrRar = file.type === 'video/mp4';
  26. if (!isZipOrRar) {
  27. message.error('您只能上传MP4文件!');
  28. }
  29. const fileSizeM = file.size / 1024 / 1024;
  30. const isLt20M = fileSizeM < 20;
  31. if (!isLt20M) {
  32. message.error(`视频大小必须小于20MB!`);
  33. }
  34. return isZipOrRar && isLt20M;
  35. };
  36. return (
  37. <div className="myUploadVideo">
  38. <Upload.Dragger
  39. name="file"
  40. accept="video/mp4"
  41. className="avatar-uploader"
  42. beforeUpload={beforeUpload}
  43. maxCount={1}
  44. showUploadList={false}
  45. disabled={loading}
  46. customRequest={(options: any) => {
  47. setLoading(true);
  48. getOssInfo({ type: 'video/mp4', fileType: 'video' }).then(async (res) => {
  49. try {
  50. let formData = new FormData();
  51. Object.keys(res.data).forEach((key: string) => {
  52. if (key !== 'url') {
  53. formData.append(key, res.data[key]);
  54. }
  55. });
  56. formData.append('file', options.file);
  57. const md5 = await getMD5(options.file);
  58. let urlData = await request(res?.data?.ossUrl, { method: 'POST', data: formData });
  59. setLoading(false);
  60. if (urlData?.data?.url) {
  61. setVideoFile(urlData?.data?.url);
  62. onChange?.(urlData?.data?.url);
  63. }
  64. } catch (error) {
  65. setLoading(false);
  66. }
  67. });
  68. }}
  69. >
  70. <Spin spinning={loading}>
  71. {videoFile ? (
  72. <div className="video-content">
  73. <VideoNews src={videoFile} />
  74. </div>
  75. ) : (
  76. <>
  77. <p className="ant-upload-drag-icon">
  78. <InboxOutlined />
  79. </p>
  80. <p className="ant-upload-text">单击或拖动视频到此区域进行上传</p>
  81. <p className="ant-upload-hint">素材大小:小于20M</p>
  82. </>
  83. )}
  84. </Spin>
  85. </Upload.Dragger>
  86. </div>
  87. );
  88. };
  89. export default React.memo(UploadVideo);