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 | 168 ++++++++++++++++++++-------
src/pages/production/wire/attachment/index.vue | 168 ++++++++++++++++++++-------
2 files changed, 250 insertions(+), 86 deletions(-)
diff --git a/src/pages/production/twist/attachment/index.vue b/src/pages/production/twist/attachment/index.vue
index 1af3ac6..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>
@@ -72,7 +72,8 @@
// 鐩存帴璋冪敤閫氱敤鏌ョ湅鎺ュ彛鏌ヨ闄勪欢鍒楄〃
// 浣跨敤绀轰緥涓殑闄勪欢ID鏁扮粍 [850,851]
- const attachmentIds: number[] = detailData.value.attachmentId !== null? detailData.value.attachmentId.split(",") : []; // 浣跨敤HTTP鏂囦欢涓殑绀轰緥鏁版嵁
+ const attachmentIds: number[] =
+ detailData.value.attachmentId !== null ? detailData.value.attachmentId.split(",") : []; // 浣跨敤HTTP鏂囦欢涓殑绀轰緥鏁版嵁
if (attachmentIds.length === 0) {
return;
}
@@ -90,53 +91,134 @@
// 鏂板闄勪欢
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: any = await AttachmentAPI.uploadAttachmentFiles(filePaths);
- const result = uploadResults.map((it) => {
- 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("涓婁紶鎴愬姛");
- // 閲嶆柊鑾峰彇闄勪欢鍒楄〃
- // 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 (aid: number) => {
try {
diff --git a/src/pages/production/wire/attachment/index.vue b/src/pages/production/wire/attachment/index.vue
index 1af3ac6..c654299 100644
--- a/src/pages/production/wire/attachment/index.vue
+++ b/src/pages/production/wire/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>
@@ -72,7 +72,8 @@
// 鐩存帴璋冪敤閫氱敤鏌ョ湅鎺ュ彛鏌ヨ闄勪欢鍒楄〃
// 浣跨敤绀轰緥涓殑闄勪欢ID鏁扮粍 [850,851]
- const attachmentIds: number[] = detailData.value.attachmentId !== null? detailData.value.attachmentId.split(",") : []; // 浣跨敤HTTP鏂囦欢涓殑绀轰緥鏁版嵁
+ const attachmentIds: number[] =
+ detailData.value.attachmentId !== null ? detailData.value.attachmentId.split(",") : []; // 浣跨敤HTTP鏂囦欢涓殑绀轰緥鏁版嵁
if (attachmentIds.length === 0) {
return;
}
@@ -90,53 +91,134 @@
// 鏂板闄勪欢
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: any = await AttachmentAPI.uploadAttachmentFiles(filePaths);
- const result = uploadResults.map((it) => {
- 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("涓婁紶鎴愬姛");
- // 閲嶆柊鑾峰彇闄勪欢鍒楄〃
- // 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 (aid: number) => {
try {
--
Gitblit v1.9.3