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
<template>
  <div>
    <svg-icon v-if="!image" name="image_empty"
              width="100" height="100" style=" margin: 5px"
              color=""/>
    <el-image v-else
              style="width: 100px; height: 100px; margin: 5px"
              :src="image"
              :preview-src-list="[image]"
              :zIndex="1000"
              fit="cover"
              disabled
    />
    <el-upload class="upload-demo"
               action
               accept=".jpeg,.jpg,.png"
               :http-request="doUpload"
               :headers="uploadHeaders"
               :show-file-list="false"
               :on-success="handleUploadSuccess"
               :on-error="handleUploadError"
               :before-upload="beforeUpload">
      <el-button size="small" :icon="icon.Upload">
        {{ $t('button.Upload') }}
      </el-button>
    </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";
import * as elementplus from "element-plus";
 
export default {
  name: "AvatarUploadExt",
  components: {},
  props: {
    image: null,
    successCallBack: null
  },
  setup(props) {
    let {proxy} = getCurrentInstance();
    const store = useBasicInfoStore();
    const state = reactive({
      staticDomain: WGURL.staticDomain,
      uploadHeaders: [{'Authorization': localStorage.getItem('Authorization')}],
    });
 
    onMounted(() => {
      Init();
    });
    onBeforeUnmount(() => {
 
    });
 
    const Init = () => {
 
    };
 
    const doUpload = (fileObject) => {
      let fd = new FormData()
      fd.append('file', fileObject.file);
 
      UploadFileToFastDFS(fd).then(resp => {
        if (resp) {
          if (resp.data && resp.data.code !== 200) {
            elementplus.ElMessageBox.alert(
                `${proxy.$t('message.' + resp.data.message)}`,
                '',
                {
                  confirmButtonText: 'OK',
                  type: 'error',
                }
            );
          } else {
            props.successCallBack(resp.data.data.linkurl);
          }
        }
      });
    };
 
    const handleUploadSuccess = (res, file) => {
 
    };
    const beforeUpload = (file) => {
      const isJPG = file.type === 'image/jpeg';
      const isPNG = file.type === 'image/png';
      const isLt2M = file.size / 1024 / 1024 < 2;
 
      if (!isJPG && !isPNG) {
        proxy.$message.error(proxy.$t('message.MSG_ERROR_UPLOAD_FILE_FORMAT_INVALID'))
      }
      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);
    };
 
    return {
      ...toRefs(state),
      props,
      icon,
      doUpload,
      handleUploadSuccess,
      beforeUpload,
      handleUploadError,
    }
  },
}
</script>
 
<style scoped>
 
</style>