添加图片直接上传到OSS功能

perf/2025b
Ycc 3 months ago
parent f87e4a0b36
commit 527e923b5b

@ -0,0 +1,179 @@
import { useEffect, useState } from "react";
import { Upload, List } from "antd";
import { UploadOutlined } from "@ant-design/icons";
import { Image } from "antd";
import { fetchJSON } from "@/utils/request";
import { HT3_HOST } from "@/config";
//
const simple_encrypt = text => {
const key = "TPDa*UU8h5%!zS";
let encrypted = [];
let keyIndex = 0;
for (let i = 0; i < text.length; i++) {
const charCode = text.charCodeAt(i);
const keyCharCode = key.charCodeAt(keyIndex);
const encryptedChar = charCode ^ keyCharCode;
encrypted.push(encryptedChar);
keyIndex = (keyIndex + 1) % key.length;
}
return encrypted.map(byte => byte.toString(16).padStart(2, "0")).join("");
};
//
const getImageList = async key => {
try {
const { errcode, result } = await fetchJSON(`${HT3_HOST}/oss/list_unique_key?key=${key}`);
if (errcode === 0) {
return result
.map(file => ({
key: file.key,
encrypt_key: file.encrypt_key,
size: file.size,
status: "done",
url: file.url,
last_modified: file.last_modified,
}))
.sort((a, b) => {
const dateA = new Date(a.last_modified);
const dateB = new Date(b.last_modified);
return dateA - dateB;
});
}
} catch (error) {
console.error("获取图片列表失败", error);
}
return [];
};
//
const deleteImage = async key => {
try {
const { errcode } = await fetchJSON(`${HT3_HOST}/oss/delete_unique_key?key=${key}`, {
method: "GET",
});
return errcode === 0;
} catch (error) {
console.error("删除图片失败", error);
return false;
}
};
//
const getSignature = async (file, key, onSuccess, onError) => {
try {
const { errcode, result } = await fetchJSON(`${HT3_HOST}/oss/signature_unique_key?key=${key}&filename=${file.name}`);
if (errcode === 0) {
const { method, host, signed_headers } = result;
const response = await fetch(host, {
method,
headers: signed_headers,
body: file,
});
if (response.ok) {
onSuccess(response, file);
} else {
onError(new Error("图片上传失败"));
}
}
} catch (error) {
console.error("获取签名失败:", error);
onError(error);
}
};
const ImageUploader = props => {
const [fileList, setFileList] = useState([]);
const [isLoading, setIsLoading] = useState(false);
const [previewOpen, setPreviewOpen] = useState(false);
const [previewImage, setPreviewImage] = useState("");
const key = simple_encrypt(props.osskey);
//
useEffect(() => {
const loadImages = async () => {
setIsLoading(true);
const images = await getImageList(key);
setFileList(images);
setIsLoading(false);
};
if (key) {
loadImages();
}
}, [key]);
//
const handleDelete = async file => {
const success = await deleteImage(file.encrypt_key);
if (success) {
setFileList(prevList => prevList.filter(item => item.key !== file.key));
console.log("删除成功");
} else {
console.error("删除失败");
}
};
//
const handleChange = ({ fileList: newFileList }) => {
setFileList(newFileList);
};
const handleUploadFile = ({ file, onProgress, onSuccess, onError }) => {
getSignature(
file,
key,
(response, file) => {
getImageList(key).then(newImages => {
setFileList(prevList => {
//
const index = prevList.findIndex(item => item.status === "uploading");
if (index !== -1) {
const newPrevList = [...prevList];
newPrevList.splice(index, 1);
//
const newItems = newImages.filter(newItem => !newPrevList.some(prevItem => prevItem.key === newItem.key));
//
return [...newPrevList, ...newItems];
}
return prevList;
});
});
},
onError
);
};
const handlePreview = file => {
if (!file.url) {
return;
}
setPreviewImage(file.url);
setPreviewOpen(true);
};
return (
<>
<Upload customRequest={handleUploadFile} multiple={true} onRemove={handleDelete} listType="picture-card" fileList={fileList} onPreview={handlePreview} onChange={handleChange}>
<div>
<UploadOutlined />
<div style={{ marginTop: 8 }}>上传图片</div>
</div>
</Upload>
<List loading={isLoading} dataSource={fileList} />
{previewImage && (
<Image
wrapperStyle={{ display: "none" }}
preview={{
visible: previewOpen,
onVisibleChange: visible => setPreviewOpen(visible),
afterOpenChange: visible => !visible && setPreviewImage(""),
}}
src={previewImage}
/>
)}
</>
);
};
export default ImageUploader;

@ -2,6 +2,7 @@ export const PROJECT_NAME = "GHHub";
// mode: test内部测试使用
export const HT_HOST = import.meta.env.MODE === 'test' ? 'http://120.79.9.217:10024' : import.meta.env.PROD ? 'https://p9axztuwd7x8a7.mycht.cn' : 'http://202.103.68.144:890'
export const HT3_HOST = 'https://hub.globalhighlights.com/ht3.0'
export const OVERSEA_HOST = 'https://ht20-p9axztuwd7x8a7.mycht.cn'

@ -133,6 +133,7 @@ const trainTicketStore = create((set, get) => ({
const searchParams = {
VEI_SN: VEI_SN,
GRI_SN: GRI_SN,
ServiceType: 2,
};
const { errcode, result } = await fetchJSON(`${HT_HOST}/Service_BaseInfoWeb/GetVeiFlightPlanChange`, searchParams);
const _result = errcode !== 0 ? [] : result;

@ -9,6 +9,8 @@ import trainTicketStore from "@/stores/Trainticket";
import { usingStorage } from "@/hooks/usingStorage";
import BackBtn from "@/components/BackBtn";
import { station_names } from "@/views/trainticket/station_name";
import { Upload } from "antd";
import ImageUploader from "@/components/ImageUploader";
const TrainticketPlan = props => {
const { coli_sn, gri_sn } = useParams();
@ -243,7 +245,7 @@ const TrainticketPlan = props => {
</Form.Item>
</Space>
</Form.Item>
<Form.Item name="ServiceType" hidden initialValue="2"></Form.Item>
<Form.Item name="ServiceType" hidden initialValue="2"><input type="hidden" /></Form.Item>
</Col>
<Col md={24} lg={4} xxl={4}>
<Space direction="vertical">
@ -270,6 +272,15 @@ const TrainticketPlan = props => {
</Space>
</Col>
</Row>
<Divider orientation="left">车票图片</Divider>
<Row gutter={16}>
<Col md={24} lg={4} xxl={4}></Col>
<Col md={24} lg={16} xxl={16}>
<ImageUploader osskey={`ghh/${trainInfo.GRI_SN}-${trainInfo.GRI_No}/trainticket/ticketimage/${trainInfo.CLF_SN}`} />
</Col>
<Col md={24} lg={4} xxl={4}></Col>
</Row>
</Form>
<Form
@ -414,13 +425,6 @@ const TrainticketPlan = props => {
const handleDelete = CLC_SN => {
deleteFlightCost(CLC_SN)
.then(() => {
notification.success({
message: `成功`,
description: "删除成功!",
placement: "top",
duration: 4,
icon: <LikeTwoTone />,
});
getPlanDetail(travelAgencyId, gri_sn);
})
.catch(() => {

Loading…
Cancel
Save