spring
2025-09-23 ab27e3ac8c7e90fd267bd6c3f0c0e06f25469697
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<template>
  <view class="attachment-container">
    <!-- 头部操作区 -->
    <view class="header-actions">
      <wd-button
        icon="file-add"
        :round="false"
        size="small"
        custom-class="add_btn"
        @click="addAttachment"
      >
        新增
      </wd-button>
    </view>
 
    <!-- 附件列表 -->
    <view class="attachment-list">
      <wd-status-tip v-if="attachmentList.length === 0" image="content" tip="暂无附件" />
 
      <wd-card
        v-for="item in attachmentList"
        :key="item.id"
        type="rectangle"
        custom-class="attachment-card"
        :border="false"
      >
        <view class="attachment-item" @click="previewAttachment(item)">
          <view class="attachment-info">
            <view class="attachment-name">{{ item.bucketFileName || item.name }}</view>
            <view class="attachment-meta">
              <text class="file-type">{{ getFileType(item.bucketFileName) }}</text>
              <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>
        </view>
      </wd-card>
    </view>
 
    <wd-toast />
  </view>
</template>
 
<script setup lang="ts">
import { ref, onMounted } from "vue";
import { useToast } from "wot-design-uni";
import AttachmentAPI from "@/api/product/attachment";
 
const toast = useToast();
 
// 页面参数
const reportId = ref("");
const reportType = ref("拉丝");
const attachmentList = ref<any[]>([]);
 
// 获取附件列表
const getAttachmentList = async () => {
  try {
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const options = (currentPage as any).options;
    const currentReportId = options?.reportId;
 
    if (currentReportId) {
      reportId.value = currentReportId;
 
      // 直接调用通用查看接口查询附件列表
      // 使用示例中的附件ID数组 [850,851]
      const attachmentIds: number[] = [850, 851]; // 使用HTTP文件中的示例数据
 
      const { data } = await AttachmentAPI.listAttachmentFiles(attachmentIds);
      attachmentList.value = data || [];
    } else {
      attachmentList.value = [];
    }
  } catch (error) {
    console.error("获取附件列表失败:", error);
    toast.show("获取附件列表失败");
    attachmentList.value = [];
  }
};
 
// 新增附件
const addAttachment = () => {
  uni.chooseFile({
    count: 9, // 最多选择9个文件
    type: "all", // 所有类型文件
    success: async (res) => {
      try {
        toast.show("正在上传...");
 
        // 上传文件
        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("上传失败");
      }
    },
    fail: (error) => {
      console.error("选择文件失败:", error);
      toast.show("选择文件失败");
    },
  });
};
 
// 删除附件
const deleteAttachment = async (attachmentId: number) => {
  try {
    uni.showModal({
      title: "确认删除",
      content: "确定要删除这个附件吗?",
      success: async (res) => {
        if (res.confirm) {
          // 前端手动删除:直接从列表中移除这条数据
          attachmentList.value = attachmentList.value.filter((item) => item.id !== attachmentId);
 
          // 获取剩余的附件ID组合
          const remainingIds = attachmentList.value.map((item) => item.id);
          const attachmentIds = remainingIds.join(",");
 
          // 调用报工添加附件接口,更新附件关联
          await AttachmentAPI.addOutputAttachments({
            id: parseInt(reportId.value),
            attachmentIds: attachmentIds,
          });
 
          toast.show("删除成功");
        }
      },
    });
  } catch (error) {
    console.error("删除失败:", error);
    toast.show("删除失败");
  }
};
 
// 预览附件
const previewAttachment = (item: any) => {
  // 根据文件类型进行预览
  const fileName = item.bucketFileName || item.name;
  const fileType = getFileType(fileName);
 
  if (fileType.startsWith("image")) {
    // 图片预览
    uni.previewImage({
      urls: [item.url],
      current: item.url,
    });
  } else {
    // 其他文件类型,可以下载或打开
    uni.downloadFile({
      url: item.url,
      success: (res) => {
        uni.openDocument({
          filePath: res.tempFilePath,
          success: () => {
            console.log("打开文档成功");
          },
          fail: (error) => {
            console.error("打开文档失败:", error);
            toast.show("无法预览此文件类型");
          },
        });
      },
      fail: (error) => {
        console.error("下载文件失败:", error);
        toast.show("下载文件失败");
      },
    });
  }
};
 
// 获取文件类型
const getFileType = (fileName: string) => {
  if (!fileName) return "unknown";
  const extension = fileName.split(".").pop()?.toLowerCase();
  switch (extension) {
    case "jpg":
    case "jpeg":
    case "png":
    case "gif":
    case "bmp":
    case "webp":
      return "image";
    case "pdf":
      return "pdf";
    case "doc":
    case "docx":
      return "word";
    case "xls":
    case "xlsx":
      return "excel";
    case "ppt":
    case "pptx":
      return "powerpoint";
    case "txt":
      return "text";
    case "zip":
    case "rar":
      return "archive";
    default:
      return "file";
  }
};
 
// 格式化文件大小
const formatFileSize = (size: number) => {
  if (size < 1024) return size + " B";
  if (size < 1024 * 1024) return (size / 1024).toFixed(1) + " KB";
  return (size / (1024 * 1024)).toFixed(1) + " MB";
};
 
// 格式化时间
const formatTime = (time: string) => {
  const date = new Date(time);
  return date.toLocaleString();
};
 
onMounted(() => {
  getAttachmentList();
});
</script>
 
<style lang="scss" scoped>
.attachment-container {
  padding: 12px;
  background: #f3f9f8;
  min-height: 100vh;
}
 
.header-actions {
  margin-bottom: 12px;
 
  :deep(.add_btn) {
    background: #0d867f;
    color: white;
    border: none;
  }
}
 
.attachment-list {
  .attachment-card {
    margin-bottom: 12px;
    border-radius: 4px;
  }
}
 
.attachment-item {
  display: flex;
  align-items: center;
  padding: 12px;
 
  .attachment-info {
    flex: 1;
 
    .attachment-name {
      font-size: 16px;
      font-weight: 500;
      color: #333;
      margin-bottom: 4px;
      word-break: break-all;
    }
 
    .attachment-meta {
      display: flex;
      gap: 12px;
      font-size: 12px;
      color: #999;
    }
  }
 
  .attachment-actions {
    margin-left: 12px;
 
    :deep(.wd-icon) {
      font-size: 20px;
      cursor: pointer;
    }
  }
}
</style>