xuruiqian
2025-06-04 17ce21955b4e3402d3d5868b52e50bfdd55bc572
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
<template>
  <div>
    <el-scrollbar v-if="fileList&&fileList.length>0" max-height="200px">
      <el-space wrap>
        <el-card style="width: 250px; margin: 5px;"
                 v-for="(item, i) in fileList" :index="i + 1 + ''" :item="item">
          <svg-icon v-if="!item.response" name="image_empty" width="100%" height="80" color=""/>
          <el-image v-else style="width: 100%; height: 80px;" :src="item.response" :fit="'contain'"
                    :preview-src-list="[item.response]"
                    :zIndex="1000"/>
          <div style="display: flex;justify-content: space-between">
            <p class="text-overflow">
              <svg-icon name="masterdata" width="23" height="23" color="var(--el-color-primary)"
                        style="margin-right: 5px;"/>
              <b>{{ item.name }}</b>
            </p>
            <el-button link size="default" type="primary" :icon="icon.Remove"
                       @click="onRemoveFile(item.name)">
              <p class="text-overflow">{{ $t('button.Remove') }}</p>
            </el-button>
          </div>
        </el-card>
      </el-space>
      <el-divider :style="{ 'margin': '10px 0px' }"></el-divider>
    </el-scrollbar>
    <el-upload class="upload-demo" drag
               action
               :http-request="doUpload"
               :headers="uploadHeaders"
               :show-file-list="false"
               :on-success="handleUploadSuccess"
               :on-error="handleUploadError"
               :before-upload="beforeUpload">
      <div>
        <!--        <el-icon class="el-icon&#45;&#45;upload" style="height: 40px; width: 40px;">-->
        <!--          <upload-filled/>-->
        <!--        </el-icon>-->
        <div class="el-upload__text">
          {{ $t('message.DropFileHereOr') }} <em>{{ $t('message.ClickToUpload') }}</em>
        </div>
      </div>
      <template #tip>
        <div class="el-upload__tip">
          {{ $t('message.FilesWithSizeLessThan') }} 2MB
        </div>
      </template>
    </el-upload>
  </div>
</template>
 
<script>
import * as icon from '@element-plus/icons-vue'
import {getCurrentInstance, onBeforeUnmount, onMounted, reactive, toRefs} from "vue";
import {useBasicInfoStore} from "@/store/basicInfo";
import {UploadFileToFastDFS} from "@/api/common";
 
export default {
  name: "UploadDocument",
  components: {},
  props: {
    files: null,
    successCallBack: null
  },
  setup(props) {
    let {proxy} = getCurrentInstance();
    const store = useBasicInfoStore();
    const state = reactive({
      staticDomain: WGURL.staticDomain,
      uploadHeaders: [{'Authorization': localStorage.getItem('Authorization')}],
      fileList: [],
      resultFileList: [],
    });
 
    onMounted(() => {
      Init();
    });
    onBeforeUnmount(() => {
 
    });
 
    const Init = () => {
      if (props.files) {
        state.fileList = [];
        props.files.map(file => {
          state.fileList.push({
            name: file.name,
            response: file.url
          });
        });
      }
    };
 
    const onRemoveFile = (name) => {
      if (state.fileList && state.fileList.length > 0) {
        state.fileList = state.fileList.filter(e => e.name !== name);
      }
    };
 
    const doUpload = (fileObject) => {
      let fd = new FormData()
      fd.append('file', fileObject.file);
 
      UploadFileToFastDFS(fd).then(resp => {
        props.successCallBack(resp);
      });
    };
 
    const handleUploadSuccess = (res, file) => {
 
    };
    const beforeUpload = (file) => {
      const isLt2M = file.size / 1024 / 1024 < 2;
 
      if (!isLt2M) {
        proxy.$message.error(proxy.$t('message.MSG_ERROR_UPLOAD_FILE_SIZE_INVALID'));
 
        return false;
      }
 
      return true;
    };
    const handleUploadError = (res, file) => {
      proxy.$message.error(res);
    };
    const exportValue = () => {
      state.resultFileList = [];
      state.fileList.map(file => {
        state.resultFileList.push({
          name: file.name,
          url: file.response
        });
      });
 
      return state.resultFileList;
    }
 
    return {
      ...toRefs(state),
      props,
      icon,
      onRemoveFile,
      doUpload,
      handleUploadSuccess,
      beforeUpload,
      handleUploadError,
      exportValue,
    }
  },
}
</script>
 
<style scoped>
 
</style>