Bang Hu
1 天以前 8a709ba6db50831048f9c3e2452ea6dc6c3de36f
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
<!--
 * @Description: 
 * @Version: 2.0
 * @Autor: wuyun
 * @Date: 2023-09-12 09:36:23
 * @LastEditors: wuyun
 * @LastEditTime: 2024-09-05 11:45:39
-->
<template>
  <div>
    <div class="upload">
      <el-upload
        class="avatar-uploader"
        action="#"
        :show-file-list="false"
        :http-request="uplodFile"
        :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
      <span v-if="imageUrl" class="el-upload-list__item-preview">
        <el-image
          class="icon"
          :src="rightIcon"
          fit="cover"
          :preview-src-list="[imageUrl]"
        >
        </el-image>
      </span>
      <el-icon
        v-if="imageUrl"
        @click="clearUploadFile"
        class="avatar-uploader-delete"
        ><Delete
      /></el-icon>
    </div>
  </div>
</template>
 
<script lang="ts" setup>
import { ref } from 'vue'
import { Plus } from '@element-plus/icons-vue'
import type { UploadFile, UploadProps } from 'element-plus'
// import httpService from '@/api/axios'
// import rightIcon from '@/assets/images/fd.png'
import commonApi from '@/api/commonApi'
import { getAssetsImages } from '@/utils/common'
const rightIcon = getAssetsImages('fd.png')
 
const emit = defineEmits(['update:fileList', 'beginUpload'])
const dialogImageUrl = ref('')
const dialogVisible = ref(false)
const imageUrl = ref('')
const iconVisible = ref<any>('none')
const handleAvatarSuccess: UploadProps['onSuccess'] = (
  response,
  uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}
// 自定义上传文件
const uplodFile = (option: any) => {
  // compressImg(option.file, 1024).then((comRes: any) => {
  let formData: any = new FormData()
  formData.append('file', option.file)
  commonApi.uploadFile(formData).then((res: any) => {
    if (res.code == 200) {
      imageUrl.value = URL.createObjectURL(option.file)
      emit('update:fileList', res.data.fileId)
      ElNotification({
        type: 'success',
        message: '上传成功!',
      })
    } else {
      ElNotification({
        type: 'error',
        message: '上传失败,请重试!',
      })
    }
  })
  // return new Promise((resolve, reject) => {
  //   // 调用上传文件方法
  //   httpService({
  //     url: '/file/upload',
  //     headers: {
  //       'Content-Type': 'multipart/form-data',
  //     },
  //     data: param,
  //   })
  //     .then((res) => {
  //       if (res.code == 200) {
  //         imageUrl.value = URL.createObjectURL(option.file)
  //         emit('update:imageId', res.data)
  //         resolve(true)
  //       } else {
  //       }
  //     })
  //     .finally(() => {})
  // })
  // })
}
// 图片压缩
// const compressImg = (file: any, limit: any) => {
//   return new Promise((resolve) => {
//     if (file.size > limit) {
//       imageConversion.compressAccurately(file, limit).then((res) => {
//         resolve(res)
//       })
//     } else {
//       resolve(file)
//     }
//   })
// }
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (
    rawFile.type !== 'image/jpeg' &&
    rawFile.type !== 'image/jpg' &&
    rawFile.type !== 'image/png' &&
    rawFile.type !== 'image/bmp' &&
    rawFile.type !== 'image/gif'
  ) {
    ElMessage.error('只能上传图片格式!')
    return false
  } else if (rawFile.size >= 10485760) {
    ElMessage.error('上传文件过大!')
    return false
  }
  return true
}
const handlePictureCardPreview = (file?: UploadFile) => {
  dialogVisible.value = true
  dialogImageUrl.value = imageUrl.value
}
const getPicUrlFun = (url: string) => {
  imageUrl.value = url
}
const clearUploadFile = () => {
  imageUrl.value = ''
  emit('update:fileList', '')
}
const clearImgUrl = () => {
  imageUrl.value = ''
}
defineExpose({ getPicUrlFun, clearUploadFile })
</script>
<style lang="scss" scoped>
.upload {
  position: relative;
}
.avatar-uploader .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar-uploader-delete {
  position: absolute;
  left: 110px;
  top: 5px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  color: #666;
  &:hover {
    color: #007cee;
  }
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
 
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
 
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
}
.el-upload-list__item-preview {
  position: absolute;
  right: 3px;
  top: 3px;
  width: 20px;
  height: 20px;
  display: block;
  .icon {
    cursor: pointer;
  }
}
/* .upload {
  &:hover{
     .el-upload-list__item-preview{
      position: absolute;
       right: 5px;
        top:5px;
      width: 24px;
      height: 24px;
       display: block;
       .icon{
       cursor: pointer;
       }
    }
  }
} */
</style>