import React, { useEffect, useState } from 'react'; import { Button, Col, Image, Row, Space } from 'antd'; import dayjs from 'dayjs'; import DrawerFileView from './drawer-file-view'; import { FieldConfig, CommonFilePath } from "../../tmpl/interface"; import { DownloadOutlined, PlayCircleOutlined } from '@ant-design/icons'; import HCService from "../../tmpl/hcservice-v3"; import Player from "griffith"; import Units from "../../units"; // 不得已而为之 const Player1: any = Player; export interface FileViewProps { fieldConfig: FieldConfig, serverKey?: string, downloadable?: boolean, value: any }; interface OssConfig { accessKeySecret: string; bucket: string; endpoint: string; protocol: string; beginDate?: string; endDate?: string; } const Index = (props: FileViewProps) => { const { downloadable = true, fieldConfig: { serverKey }, value } = props; const [filePaths, setFilePaths] = useState([]); const [playing, setPlaying] = useState(false); const [ossConfigs, setOssConfigs] = useState([]); useEffect(() => { if (serverKey) { const { accessKeySecret, bucket, endpoint, protocol, beginDate, endDate, otherConfig = [] } = Units.getExtendServers(serverKey) || {}; const temConfig = bucket && endpoint ? [{ accessKeySecret, bucket, endpoint, protocol, beginDate, endDate }] : []; otherConfig.forEach(r => { if (r.bucket && r.endpoint) temConfig.push(r); }); setOssConfigs(temConfig); } }, [serverKey]); // 根据时间使用对应的oss配置 const getOssConfig = (url: string) => { // 截取地址中的日期 const match = url.match(/\/(\d{4})\/(\d{1,2})\/(\d{1,2})\//); if (!match || ossConfigs.length === 1) { return ossConfigs[0]; } const date = dayjs(`${match[1]}-${match[2]}-${match[3]}`); const temConfig = ossConfigs.find(r => { const { beginDate, endDate } = r; if (!beginDate && !endDate) { return true; } if (!beginDate) { // 没有开始日期,则只判断结束日期,包含结束日期 return date.isBefore(endDate) || date.isSame(endDate); } if (!endDate) { // 不包含开始日期 return dayjs(beginDate).isBefore(date); } return dayjs(beginDate).isBefore(date) && (date.isBefore(endDate) || date.isSame(endDate)); }); return temConfig || ossConfigs[0]; } // 处理文件路径,多文件使用,分割 const handleFilePaths = (val: string) => { if (!val) { return []; } const filePaths = val.split(",")?.map(url => { // 地址存在关系字段则去掉 let lastIndex = url.lastIndexOf("@R@"); let temUrl = lastIndex !== -1 ? url.substring(lastIndex + 3) : url; if (url.startsWith("http")) { temUrl = url; } else if (ossConfigs?.length) { const ossConfig = getOssConfig(url); temUrl = encodeURI(`${ossConfig.protocol || "http"}://${ossConfig.bucket}.${ossConfig.endpoint}/${temUrl}`); } else { // 无oss配置时,使用本地地址 temUrl = Units.joinPath(Units.api(serverKey), `/v3/local-files?path=${temUrl}`); const path = HCService.getFileUrl(serverKey, temUrl); let fileName = HCService.getFileNameFrom(url); const contentType = HCService.getFileSuffixFrom(fileName); try { fileName = decodeURIComponent(fileName); } catch (error) { console.log("decodeURIComponent error", error, fileName); } return { path, contentType, fileName }; } const item = HCService.toFilePath(temUrl, serverKey); try { return { ...item, fileName: decodeURIComponent(item.fileName) }; } catch (error) { console.log("decodeURIComponent error", error, item.fileName); return item; } }) || []; return filePaths; } const handleValue = (val: string | string[]) => { if (!val) { return; } let filePaths = []; if (Array.isArray(val)) { // 兼容数据为数组的情况 filePaths = val.map(r => handleFilePaths(r)).flat(); } else { filePaths = handleFilePaths(val); } setFilePaths(filePaths); }; useEffect(() => { handleValue(value); }, [value, ossConfigs]); const download = (filePath: CommonFilePath) => { const time = new Date().getTime(); // 音视频等文件下载时需要加上时间戳,(音视频播放会占用oss网络通道,导致下载失败) const path = filePath?.path; if (path) { Units.downloadFile(path.includes("?") ? `${path}&t=${time}` : `${path}?t=${time}`, filePath.fileName); } }; const packFile2Show = (filePath: CommonFilePath) => { if (filePath) { if (["new", "delete"].includes(filePath.valid)) { return filePath.fileName === "new" ? filePath.fileName : undefined; } if (['png', 'jpg', 'jpeg', 'gif', 'PNG', 'JPG', 'JPEG', 'GIF', 'svg', 'SVG'].includes(filePath.contentType)) { return } else if (['mp4', 'MP4', 'mov', 'MOV', 'avi', 'AVI', 'WMV', 'wmv', "wav", "WAV"].includes(filePath.contentType)) { let sources = { ld: { play_url: filePath.path, } } return ( {playing ? : } {downloadable ? {downloadable ? : null} } else { return {downloadable ? <> : } } } else { return undefined; } } return filePaths.length > 1 ? {filePaths.map(filePath => packFile2Show(filePath))} : packFile2Show(filePaths[0]); }; export default Index;