Bang Hu
2 天以前 b3fedd4221b807a07058be9d5d5d8ba8998adbcb
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
<!--
 * @Description: 本系统用的上传组件 只能上传图片和pdf 单个文件
 * @Version: 2.0
 * @Autor: wuyun
 * @Date: 2022-08-19 09:43:21
 * @LastEditors: wuyun
 * @LastEditTime: 2022-08-30 11:38:14
-->
<template>
  <div>
    <el-upload ref="upload"
               class="avatar-uploader"
               action=""
               :accept="accept"
               :limit="limit"
               :fileList="state.fileList"
               :on-exceed="handleExceed"
               list-type="picture-card"
               :before-upload="handleBeforeUpload"
               :http-request="uploadRequest"
               :on-preview="handlePreview"
               :on-remove="handleRemoveFile"
    >
      <el-icon>
        <Plus/>
      </el-icon>
    </el-upload>
  </div>
</template>
<script lang="ts" setup>
import {ElNotification, ElMessage, UploadRawFile, UploadRequestOptions, genFileId} from 'element-plus'
import type {FormInstance, FormRules, UploadInstance, UploadProps, UploadUserFile} from 'element-plus'
import {Refresh} from '@element-plus/icons-vue'
import commonApi from "@/api/commonApi";
 
interface Props {
  accept?: string
  limit?: number | undefined
  files?: UploadUserFile[]
}
 
const props = withDefaults(defineProps<Props>(), {
  accept: 'jpg,jpeg,png,bmp,gif',
  limit: 1,
  fileList: () => []
})
 
interface State {
  btnLoading: boolean
  fileList: UploadUserFile[]
}
 
const state = reactive<any>({
  btnLoading: false,
  fileList: [],
  formData: {
    fileList: [], // 上传
    fileIds: [] as string[],
  },
  fileObj: {
    id: '',
    orderId: '',
    orderLogId: '',
    fileId: '',
    bucket: '',
    originalName: '',
    encryptedName: '',
    suffix: '',
    size: undefined,
    url: '',
    isImg: false
  },
})
const emit = defineEmits(['update:fileList', 'removeFile'])
const upload = ref<UploadInstance>()
 
const handleExceed = () => {
 
}
// // 新上传的组件覆盖之前的组件
// const handleExceed: UploadProps['onExceed'] = (files) => {
//   // upload.value!.clearFiles()
//   // const file = files[0] as UploadRawFile
//   // file.uid = genFileId()
//   // upload.value!.handleStart(file)
//   // upload.value!.submit()
//   emit('emitExceed')
// }
//
// async function uploadRequest(options: UploadRequestOptions): Promise<any> {
//   console.log('options', options)
//   const fileData = new FormData()
//   fileData.append('file', options.file)
//   state.btnLoading = true
//   emit('emitUploadRequest', fileData, (res: boolean) => {
//     state.btnLoading = false
//   })
// }
//
// /**
//  * 在 before-upload 钩子中限制用户上传文件的格式和大小
//  */
// const handleBeforeUpload = (file: UploadRawFile) => {
//   var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
//   const extension = testmsg === 'jpg'
//   const extension2 = testmsg === 'jpeg'
//   const extension3 = testmsg === 'png'
//   const extension4 = testmsg === 'gif'
//   const extension5 = testmsg === 'bmp'
//   const extension6 = testmsg === 'pdf'
//   if (!extension && !extension2 && !extension3 && !extension4 && !extension5 && !extension6) {
//     ElNotification({
//       type: 'warning',
//       message: '上传文件只能是.jpg/.jpeg/.png/.gif/.pdf格式!'
//     })
//   }
//   return extension || extension2 || extension3 || extension4 || extension5 || extension6
// }
 
 
// 上传校验
const handleBeforeUpload = (file: UploadRawFile) => {
  // // console.log('文件上传---------->', file)
  // if (file.name.length > 41) {
  //   ElNotification({
  //     type: 'warning',
  //     message: '上传文件名字过长!'
  //   })
  //   return
  // }
  //
  // if (file.size >= 10485760) {
  //   ElNotification({
  //     type: 'warning',
  //     message: '上传文件过大0000!'
  //   })
  //   return
  // }
  //
  // var testmsg = file.name.substring(file.name.lastIndexOf('.') + 1)
  // state.fileNameData = file.name.substring(0, file.name.lastIndexOf("."))
  // state.nameSuffix = testmsg
  // const extension = testmsg === 'jpg'
  // const extension2 = testmsg === 'jpeg'
  // const extension3 = testmsg === 'png'
  // if (!extension && !extension2 && !extension3) {
  //   ElNotification({
  //     type: 'warning',
  //     message: '上传文件只能是.jpg/.jpeg/.png!'
  //   })
  //
  //   return
  // }
  // // return extension || extension2 || extension3
 
}
 
// 图片上传
async function uploadRequest(options: UploadRequestOptions): Promise<any> {
 
  if (options.file.size >= 10485760) {
    ElNotification({
      type: 'warning',
      message: '上传文件过大!'
    })
    state.fileList = []
    return
  }
 
 
  var testmsg = options.file.name.substring(options.file.name.lastIndexOf('.') + 1)
  state.fileNameData = options.file.name.substring(0, options.file.name.lastIndexOf("."))
  const extension = testmsg === 'jpg'
  const extension2 = testmsg === 'jpeg'
  const extension3 = testmsg === 'png'
  if (!extension && !extension2 && !extension3) {
    ElNotification({
      type: 'warning',
      message: '上传文件只能是.jpg/.jpeg/.png!'
    })
    state.fileList = []
    return
  }
 
 
  const formData = new FormData()
  formData.append('file', options.file)
  state.formData.fileIds = []
  try {
    const res = await commonApi.uploadFile(formData)
    state.formData.fileIds.push(res.data.fileId)
    state.fileObj.fileId = res.data.fileId
    ElNotification({
      type: 'success',
      message: '上传成功!'
    })
 
 
    emit('update:fileList', res.data.fileId)
 
 
  } catch (err) {
    ElNotification({
      type: 'error',
      message: '上传失败,请重试!'
    })
  }
}
 
// 预览/下载
const handlePreview = (file?: any) => {
  // // if (state.fileObj.isImg) {
  // getImgFlowUrl(state.fileObj.fileId || '').then((res: any) => {
  //   console.log("查看图片-----", res.data)
  //   if (window.URL.createObjectURL != undefined) {
  //     const myBlob = new window.Blob([res.data], {type: 'image/jpeg'})
  //     const url = window.URL.createObjectURL(myBlob)
  //     state.fileObj.url = url
  //     imageUrl.value = url
  //
  //     // 预览图片弹框
  //     imgViewDialogVisible.value = true
  //   }
  // })
  // // }
}
 
// 删除图片
const handleRemoveFile = (UploadFile: UploadFile) => {
  // 重置关于图片的数据
  state.formData.fileList = []
  state.formData.fileIds = []
  state.formData.fileId = ''
  state.fileObj = {
    id: '',
    orderId: '',
    orderLogId: '',
    fileId: '',
    bucket: '',
    originalName: '',
    encryptedName: '',
    suffix: '',
    size: undefined,
    url: '',
    isImg: false
  }
  emit('removeFile')
 
  upload && upload.value?.clearFiles()
 
}
 
const clearUploadFile = () => {
  // upload.value && upload.value.clearFiles()
  state.fileList = []
  // 重置关于图片的数据
  state.formData.fileList = []
  state.formData.fileIds = []
  state.formData.fileId = ''
  state.fileObj = {
    id: '',
    orderId: '',
    orderLogId: '',
    fileId: '',
    bucket: '',
    originalName: '',
    encryptedName: '',
    suffix: '',
    size: undefined,
    url: '',
    isImg: false
  }
}
 
// 获取到图片
const getPicUrlFun = (data?: any) => {
  console.log(data)
  state.fileList = []
  state.fileList = data
}
 
 
watchEffect(() => {
  // console.log("999999999999999",props.fileList)
})
 
// 将方法暴露给父组件
defineExpose({clearUploadFile, getPicUrlFun})
</script>