From 2767d5e2f41ccf766bf84c58f527b1538426fec7 Mon Sep 17 00:00:00 2001
From: spring <2396852758@qq.com>
Date: 星期三, 24 九月 2025 17:34:01 +0800
Subject: [PATCH] fix: 附件上传优化
---
src/pages/production/twist/attachment/index.vue | 186 +++++++++++++++++++++++++++++++++++----------
1 files changed, 143 insertions(+), 43 deletions(-)
diff --git a/src/pages/production/twist/attachment/index.vue b/src/pages/production/twist/attachment/index.vue
index e6c26f5..c654299 100644
--- a/src/pages/production/twist/attachment/index.vue
+++ b/src/pages/production/twist/attachment/index.vue
@@ -32,8 +32,8 @@
<text class="upload-time">{{ formatTime(item.createTime) }}</text>
</view>
</view>
- <view class="attachment-actions">
- <wd-icon name="delete" color="#ff4757" @click.stop="deleteAttachment(item.id)" />
+ <view class="attachment-actions" @click.stop>
+ <wd-icon name="delete" color="#ff4757" @click="deleteAttachment(item.id)" />
</view>
</view>
</wd-card>
@@ -55,21 +55,28 @@
const reportType = ref("缁炵嚎");
const attachmentList = ref<any[]>([]);
+const detailData = ref<any>({});
// 鑾峰彇闄勪欢鍒楄〃
-const getAttachmentList = async () => {
+const getAttachmentList = async (data: any) => {
try {
+ detailData.value = data;
+ console.log(" detailData.value", detailData.value);
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const options = (currentPage as any).options;
- const currentReportId = options?.reportId;
+ const currentReportId = detailData.value.id;
if (currentReportId) {
reportId.value = currentReportId;
// 鐩存帴璋冪敤閫氱敤鏌ョ湅鎺ュ彛鏌ヨ闄勪欢鍒楄〃
// 浣跨敤绀轰緥涓殑闄勪欢ID鏁扮粍 [850,851]
- const attachmentIds: number[] = [850, 851]; // 浣跨敤HTTP鏂囦欢涓殑绀轰緥鏁版嵁
+ const attachmentIds: number[] =
+ detailData.value.attachmentId !== null ? detailData.value.attachmentId.split(",") : []; // 浣跨敤HTTP鏂囦欢涓殑绀轰緥鏁版嵁
+ if (attachmentIds.length === 0) {
+ return;
+ }
const { data } = await AttachmentAPI.listAttachmentFiles(attachmentIds);
attachmentList.value = data || [];
} else {
@@ -84,45 +91,136 @@
// 鏂板闄勪欢
const addAttachment = () => {
- uni.chooseFile({
- count: 9, // 鏈�澶氶�夋嫨9涓枃浠�
- type: "all", // 鎵�鏈夌被鍨嬫枃浠�
- success: async (res) => {
- try {
- toast.show("姝e湪涓婁紶...");
-
- // 涓婁紶鏂囦欢
- const filePaths = Array.isArray(res.tempFilePaths)
- ? res.tempFilePaths
- : [res.tempFilePaths];
- const uploadResults = await AttachmentAPI.uploadAttachmentFiles(filePaths);
-
- // 鎻愬彇闄勪欢ID
- const attachmentIds = uploadResults.map((result) => result.data.id).join(",");
-
- // 鍏宠仈鍒版姤宸�
- await AttachmentAPI.addOutputAttachments({
- id: parseInt(reportId.value),
- attachmentIds: attachmentIds,
- });
-
- toast.show("涓婁紶鎴愬姛");
- // 閲嶆柊鑾峰彇闄勪欢鍒楄〃
- await getAttachmentList();
- } catch (error) {
- console.error("涓婁紶澶辫触:", error);
- toast.show("涓婁紶澶辫触");
+ // 鏄剧ず閫夋嫨鏂囦欢绫诲瀷鐨勫脊绐�
+ uni.showActionSheet({
+ itemList: ["閫夋嫨鍥剧墖", "閫夋嫨瑙嗛", "鎷嶇収", "褰曞儚"],
+ success: (res) => {
+ switch (res.tapIndex) {
+ case 0: // 閫夋嫨鍥剧墖
+ chooseImages();
+ break;
+ case 1: // 閫夋嫨瑙嗛
+ chooseVideos();
+ break;
+ case 2: // 鎷嶇収
+ takePhoto();
+ break;
+ case 3: // 褰曞儚
+ recordVideo();
+ break;
}
},
fail: (error) => {
- console.error("閫夋嫨鏂囦欢澶辫触:", error);
- toast.show("閫夋嫨鏂囦欢澶辫触");
+ console.error("閫夋嫨鏂囦欢绫诲瀷澶辫触:", error);
+ toast.show("閫夋嫨鏂囦欢绫诲瀷澶辫触");
},
});
};
+// 閫夋嫨鍥剧墖
+const chooseImages = () => {
+ uni.chooseImage({
+ count: 9,
+ sizeType: ["original", "compressed"],
+ sourceType: ["album"],
+ success: async (res) => {
+ const filePaths = Array.isArray(res.tempFilePaths) ? res.tempFilePaths : [res.tempFilePaths];
+ await handleFileUpload(filePaths);
+ },
+ fail: (error) => {
+ console.error("閫夋嫨鍥剧墖澶辫触:", error);
+ toast.show("閫夋嫨鍥剧墖澶辫触");
+ },
+ });
+};
+
+// 閫夋嫨瑙嗛
+const chooseVideos = () => {
+ uni.chooseVideo({
+ sourceType: ["album"],
+ maxDuration: 60,
+ camera: "back",
+ success: async (res) => {
+ await handleFileUpload([res.tempFilePath]);
+ },
+ fail: (error) => {
+ console.error("閫夋嫨瑙嗛澶辫触:", error);
+ toast.show("閫夋嫨瑙嗛澶辫触");
+ },
+ });
+};
+
+// 鎷嶇収
+const takePhoto = () => {
+ uni.chooseImage({
+ count: 1,
+ sizeType: ["original", "compressed"],
+ sourceType: ["camera"],
+ success: async (res) => {
+ const filePaths = Array.isArray(res.tempFilePaths) ? res.tempFilePaths : [res.tempFilePaths];
+ await handleFileUpload(filePaths);
+ },
+ fail: (error) => {
+ console.error("鎷嶇収澶辫触:", error);
+ toast.show("鎷嶇収澶辫触");
+ },
+ });
+};
+
+// 褰曞儚
+const recordVideo = () => {
+ uni.chooseVideo({
+ sourceType: ["camera"],
+ maxDuration: 60,
+ camera: "back",
+ success: async (res) => {
+ await handleFileUpload([res.tempFilePath]);
+ },
+ fail: (error) => {
+ console.error("褰曞儚澶辫触:", error);
+ toast.show("褰曞儚澶辫触");
+ },
+ });
+};
+
+// 澶勭悊鏂囦欢涓婁紶
+const handleFileUpload = async (filePaths: string[]) => {
+ try {
+ toast.show("姝e湪涓婁紶...");
+
+ // 涓婁紶鏂囦欢
+ const uploadResults: any = await AttachmentAPI.uploadAttachmentFiles(filePaths);
+ const result = uploadResults.map((it: any) => {
+ return it.data;
+ });
+ console.log("result", result);
+
+ // 鏇存柊闄勪欢鍒楄〃
+ const flattenedResult = result.flat();
+ attachmentList.value.push(...flattenedResult);
+ console.log(attachmentList.value);
+
+ // 鎻愬彇闄勪欢ID
+ const attachmentId = attachmentList.value.map((item: any) => item.id).join(",");
+
+ // 鍏宠仈鍒版姤宸�
+ if (attachmentId) {
+ await AttachmentAPI.addOutputAttachments({
+ id: parseInt(detailData.value.id),
+ attachmentId: attachmentId,
+ });
+ detailData.value.attachmentId = attachmentId;
+ }
+
+ toast.show("涓婁紶鎴愬姛");
+ } catch (error) {
+ console.error("涓婁紶澶辫触:", error);
+ toast.show("涓婁紶澶辫触");
+ }
+};
+
// 鍒犻櫎闄勪欢
-const deleteAttachment = async (attachmentId: number) => {
+const deleteAttachment = async (aid: number) => {
try {
uni.showModal({
title: "纭鍒犻櫎",
@@ -130,18 +228,17 @@
success: async (res) => {
if (res.confirm) {
// 鍓嶇鎵嬪姩鍒犻櫎锛氱洿鎺ヤ粠鍒楄〃涓Щ闄よ繖鏉℃暟鎹�
- attachmentList.value = attachmentList.value.filter((item) => item.id !== attachmentId);
+ attachmentList.value = attachmentList.value.filter((item) => item.id !== aid);
// 鑾峰彇鍓╀綑鐨勯檮浠禝D缁勫悎
- const remainingIds = attachmentList.value.map((item) => item.id);
- const attachmentIds = remainingIds.join(",");
+ const attachmentId = attachmentList.value.map((item) => item.id).join(",");
// 璋冪敤鎶ュ伐娣诲姞闄勪欢鎺ュ彛锛屾洿鏂伴檮浠跺叧鑱�
await AttachmentAPI.addOutputAttachments({
- id: parseInt(reportId.value),
- attachmentIds: attachmentIds,
+ id: parseInt(detailData.value.id),
+ attachmentId: attachmentId,
});
-
+ detailData.value.attachmentId = attachmentId;
toast.show("鍒犻櫎鎴愬姛");
}
},
@@ -235,7 +332,10 @@
};
onMounted(() => {
- getAttachmentList();
+ uni.$on("detailData", (data) => {
+ // 澶勭悊鎺ユ敹鍒扮殑鏁版嵁
+ getAttachmentList(data);
+ });
});
</script>
--
Gitblit v1.9.3