zhangxiao
2024-08-09 7d0b7f478d812933b3dc3721095f7d5d8df44238
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
<template>
  <a-upload
    v-model:fileList="fileList"
    list-type="picture-card"
    :limit="limit"
    :action="action"
    @change="handleChange"
    image-preview
  />
</template>
 
<script setup>
  import { computed, ref, onMounted, watch, watchEffect } from 'vue';
  import { useUserStore } from '@/store';
 
  const userStore = useUserStore();
  const props = defineProps({
    limit: {
      type: Number,
      default: 1,
    },
    action: String, // 上传的服务器地址
    url: String, //回显的文件地址
  });
 
  const emit = defineEmits(['update:fileList', 'success']);
  const urls = computed(() => props.url);
  const fileList = ref([]);
 
  watch(
    () => props.url,
    (newVal) => {
      if (newVal) {
        fileList.value = newVal.split(',').map((item) => ({
          uid: item,
          name: item,
          status: 'done',
          url: item,
        }));
      }
    },
    {
      deep: true, // 开启深度监听
    }
  );
 
  onMounted(() => {
    if (urls.value) {
      fileList.value = urls.value.split(',').map((item) => ({
        uid: item,
        name: item,
        status: 'done',
        url: item,
      }));
    }
 
    // console.log(
    //   window.location.origin,
    //   import.meta.env.VITE_API_BASE_URL,
    //   8988
    // );
  });
 
  // console.log(urls.value, 8988);
 
  const handleChange = (fileList) => {
    emit('update:fileList', fileList);
    const successFiles = fileList.filter((item) => item.status === 'done');
    if (successFiles.length > 0) {
      emit(
        'success',
        successFiles.map((item) => item.response.data)
      );
    }
  };
</script>