Skip to content

Commit 5a3f175

Browse files
奇淼(piexlmaxbypanghutesun
authored
Add upload (#1016)
* [style] 修改右上角图标,选择统一风格的iconfont * [fix] fix webfont tree in readme file * (fix) 菜单编辑的时候,icon不居中 * 更新为使用组件 * 媒体库增加 普通上传、压缩上传按钮,方便媒体库直接上传图片 * 普通上传组件 * 增加数据类型切换后的的校验,避免使用错误的查询条件和字典条件。 Co-authored-by: bypanghu <bypanghu@163.com> Co-authored-by: tesun <36953434+tesun@users.noreply.github.com>
1 parent 3986e8e commit 5a3f175

File tree

5 files changed

+411
-344
lines changed

5 files changed

+411
-344
lines changed

web/src/components/chooseImg/index.vue

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
<template>
22
<el-drawer v-model="drawer" title="媒体库">
3+
<div class="gva-btn-list">
4+
<upload-common
5+
v-model:imageCommon="imageCommon"
6+
class="upload-btn-media-library"
7+
@on-success="open"
8+
/>
9+
<upload-image
10+
v-model:imageUrl="imageUrl"
11+
:file-size="512"
12+
:max-w-h="1080"
13+
class="upload-btn-media-library"
14+
@on-success="open"
15+
/>
16+
</div>
317
<div class="media">
418
<el-image
519
v-for="(item,key) in picList"
@@ -21,6 +35,11 @@
2135
<script setup>
2236
import { ref } from 'vue'
2337
import { getFileList } from '@/api/fileUploadAndDownload'
38+
import UploadImage from '@/components/upload/image.vue'
39+
import UploadCommon from '@/components/upload/common.vue'
40+
41+
const imageUrl = ref('')
42+
const imageCommon = ref('')
2443
2544
const emit = defineEmits(['enterImg'])
2645
defineProps({
@@ -56,6 +75,7 @@ defineExpose({ open })
5675
</script>
5776
5877
<style lang="scss">
78+
.upload-btn-media-library{margin-left: 20px;}
5979
.media{
6080
display:flex;
6181
flex-wrap:wrap;

web/src/components/upload/common.vue

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<template>
2+
<div>
3+
<el-upload
4+
:action="`${path}/fileUploadAndDownload/upload`"
5+
:before-upload="checkFile"
6+
:headers="{ 'x-token': userStore.token }"
7+
:on-error="uploadError"
8+
:on-success="uploadSuccess"
9+
:show-file-list="false"
10+
class="upload-btn"
11+
>
12+
<el-button size="small" type="primary">普通上传</el-button>
13+
</el-upload>
14+
</div>
15+
</template>
16+
17+
<script setup>
18+
19+
import { ref } from 'vue'
20+
import { ElMessage } from 'element-plus'
21+
import { useUserStore } from '@/pinia/modules/user'
22+
23+
const emit = defineEmits(['on-success'])
24+
const path = ref(import.meta.env.VITE_BASE_API)
25+
26+
const userStore = useUserStore()
27+
const fullscreenLoading = ref(false)
28+
29+
const checkFile = (file) => {
30+
fullscreenLoading.value = true
31+
const isJPG = file.type === 'image/jpeg'
32+
const isPng = file.type === 'image/png'
33+
const isLt2M = file.size / 1024 / 1024 < 0.5
34+
if (!isJPG && !isPng) {
35+
ElMessage.error('上传图片只能是 jpg或png 格式!')
36+
fullscreenLoading.value = false
37+
}
38+
if (!isLt2M) {
39+
ElMessage.error('未压缩未见上传图片大小不能超过 500KB,请使用压缩上传')
40+
fullscreenLoading.value = false
41+
}
42+
return (isPng || isJPG) && isLt2M
43+
}
44+
45+
const uploadSuccess = (res) => {
46+
const { data } = res
47+
if (data.file) {
48+
emit('on-success', data.file.url)
49+
}
50+
}
51+
52+
const uploadError = () => {
53+
ElMessage({
54+
type: 'error',
55+
message: '上传失败'
56+
})
57+
fullscreenLoading.value = false
58+
}
59+
60+
</script>
61+
62+
<script>
63+
64+
export default {
65+
name: 'UploadCommon',
66+
methods: {
67+
68+
}
69+
}
70+
</script>

web/src/view/example/upload/upload.vue

Lines changed: 7 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,11 @@
22
<div v-loading.fullscreen.lock="fullscreenLoading">
33
<div class="gva-table-box">
44
<div class="gva-btn-list">
5-
<el-upload
6-
:action="`${path}/fileUploadAndDownload/upload`"
7-
:before-upload="checkFile"
8-
:headers="{ 'x-token': userStore.token }"
9-
:on-error="uploadError"
10-
:on-success="uploadSuccess"
11-
:show-file-list="false"
12-
class="upload-btn"
13-
>
14-
<el-button size="small" type="primary">普通上传</el-button>
15-
</el-upload>
5+
<upload-common
6+
v-model:imageCommon="imageCommon"
7+
class="upload-btn"
8+
@on-success="getTableData"
9+
/>
1610
<upload-image
1711
v-model:imageUrl="imageUrl"
1812
:file-size="512"
@@ -72,6 +66,7 @@ import { downloadImage } from '@/utils/downloadImg'
7266
import { useUserStore } from '@/pinia/modules/user'
7367
import CustomPic from '@/components/customPic/index.vue'
7468
import UploadImage from '@/components/upload/image.vue'
69+
import UploadCommon from '@/components/upload/common.vue'
7570
import { formatDate } from '@/utils/format'
7671
7772
import { ref } from 'vue'
@@ -81,6 +76,7 @@ const path = ref(import.meta.env.VITE_BASE_API)
8176
const userStore = useUserStore()
8277
8378
const imageUrl = ref('')
79+
const imageCommon = ref('')
8480
8581
const page = ref(1)
8682
const total = ref(0)
@@ -137,46 +133,6 @@ const deleteFileFunc = async(row) => {
137133
})
138134
}
139135
140-
const fullscreenLoading = ref(false)
141-
const checkFile = (file) => {
142-
fullscreenLoading.value = true
143-
const isJPG = file.type === 'image/jpeg'
144-
const isPng = file.type === 'image/png'
145-
const isLt2M = file.size / 1024 / 1024 < 0.5
146-
if (!isJPG && !isPng) {
147-
ElMessage.error('上传图片只能是 jpg或png 格式!')
148-
fullscreenLoading.value = false
149-
}
150-
if (!isLt2M) {
151-
ElMessage.error('未压缩未见上传图片大小不能超过 500KB,请使用压缩上传')
152-
fullscreenLoading.value = false
153-
}
154-
return (isPng || isJPG) && isLt2M
155-
}
156-
const uploadSuccess = (res) => {
157-
fullscreenLoading.value = false
158-
if (res.code === 0) {
159-
ElMessage({
160-
type: 'success',
161-
message: '上传成功'
162-
})
163-
if (res.code === 0) {
164-
getTableData()
165-
}
166-
} else {
167-
ElMessage({
168-
type: 'warning',
169-
message: res.msg
170-
})
171-
}
172-
}
173-
const uploadError = () => {
174-
ElMessage({
175-
type: 'error',
176-
message: '上传失败'
177-
})
178-
fullscreenLoading.value = false
179-
}
180136
const downloadFile = (row) => {
181137
if (row.url.indexOf('http://') > -1 || row.url.indexOf('https://') > -1) {
182138
downloadImage(row.url, row.name)

0 commit comments

Comments
 (0)