Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
有来技术
vue3-element-admin
提交
bef4a1a9
V
vue3-element-admin
项目概览
有来技术
/
vue3-element-admin
通知
3
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
V
vue3-element-admin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
bef4a1a9
编写于
1月 08, 2022
作者:
郝
郝先瑞
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(SingleUpload.vue): 优化单图上传组件修改上传方式为自定义,刷新token可控制
上级
94856560
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
46 addition
and
50 deletion
+46
-50
src/components/Upload/SingleUpload.vue
src/components/Upload/SingleUpload.vue
+46
-50
未找到文件。
src/components/Upload/SingleUpload.vue
浏览文件 @
bef4a1a9
<
template
>
<div>
<el-upload
class=
"
image
-uploader"
:headers=
"headers
"
:action=
"uploadAction
"
class=
"
avatar
-uploader"
action=
"
"
list-type=
"picture-card
"
:show-file-list=
"false"
:on-success=
"handleUploadSuccess"
:before-upload=
"handleBeforeUpload"
:on-exceed=
"handleExceed"
:on-remove=
"handleRemove"
:limit=
"
props.maxCount
"
:
on-preview=
"handlePreview
"
:limit=
"
1
"
:
http-request=
"uploadImage
"
>
<img
v-if=
"imgUrl"
:src=
"imgUrl"
class=
"image"
/>
<el-icon
v-else
class=
"image-uploader-icon"
>
<plus/>
<img
v-if=
"imgUrl"
:src=
"imgUrl"
class=
"avatar"
/>
<el-icon
v-else
class=
"avatar-uploader-icon"
>
<Plus/>
</el-icon>
</el-upload>
<el-dialog
v-model=
"viewDialogVisible"
>
<img
width=
"100%"
:src=
"viewImgUrl"
>
</el-dialog>
</div>
</
template
>
<
script
setup
lang=
"ts"
>
import
{
computed
,
reactive
,
toRefs
}
from
"
vue
"
;
import
{
computed
,
reactive
,
toRefs
}
from
"
vue
"
;
import
{
Plus
}
from
'
@element-plus/icons-vue
'
import
{
ElMessage
}
from
"
element-plus
"
import
{
deleteFile
}
from
"
@/api/system/file
"
;
import
{
Local
}
from
"
@/utils/storage
"
;
import
{
deleteFile
,
uploadFile
}
from
"
@/api/system/file
"
;
const
emit
=
defineEmits
([
'
update:modelValue
'
]);
const
props
=
defineProps
({
...
...
@@ -45,10 +39,8 @@ const props = defineProps({
})
const
state
=
reactive
({
uploadAction
:
import
.
meta
.
env
.
VITE_APP_BASE_API
+
'
/youlai-admin/api/v1/files
'
,
viewImgUrl
:
''
,
viewDialogVisible
:
false
,
headers
:
{
authorization
:
Local
.
get
(
'
token
'
)}
previewImgUrl
:
''
,
previewDialogVisible
:
false
,
})
const
imgUrl
=
computed
({
...
...
@@ -60,33 +52,38 @@ const imgUrl = computed({
}
})
const
{
uploadAction
,
viewImgUrl
,
viewDialogVisible
,
headers
}
=
toRefs
(
state
)
function
handleUploadSuccess
(
response
:
any
)
{
const
fileUrl
=
response
.
data
emit
(
'
update:modelValue
'
,
fileUrl
)
}
function
handleBeforeUpload
(
file
:
any
)
{
const
isJPG
=
file
.
type
===
'
image/jpeg
'
const
isLt2M
=
file
.
size
/
1024
/
1024
<
2
if
(
!
isJPG
)
{
}
/* if (!isJPG) {
ElMessage.warning("此文件非图片文件")
return false
}*/
if
(
!
isLt2M
)
{
ElMessage
.
warning
(
"
上传图片不能大于2M
"
)
}
return
true
}
function
handleExceed
(
file
:
any
)
{
ElMessage
.
warning
(
'
最多只能上传
'
+
props
.
maxCount
+
''
)
/**
* 自定义图片上传
*
* @param params
*/
function
uploadImage
(
params
:
any
)
{
const
file
=
params
.
file
uploadFile
(
file
).
then
(
response
=>
{
const
imgUrl
=
response
.
data
emit
(
'
update:modelValue
'
,
imgUrl
)
})
}
function
handlePreview
(
file
:
any
)
{
state
.
viewImgUrl
=
imgUrl
.
value
as
string
state
.
viewDialogVisible
=
true
function
handleExceed
(
file
:
any
)
{
ElMessage
.
warning
(
'
最多只能上传
'
+
props
.
maxCount
)
}
function
handleRemove
(
file
:
any
,
fileList
:
Array
<
any
>
)
{
deleteFile
(
file
.
url
)
emit
(
'
update:modelValue
'
,
fileList
)
...
...
@@ -94,28 +91,27 @@ function handleRemove(file: any, fileList: Array<any>) {
</
script
>
<
style
lang=
"scss"
>
.image-uploader
.el-upload
{
border
:
1px
dashed
#d9d9d9
;
border-radius
:
6px
;
cursor
:
pointer
;
position
:
relative
;
overflow
:
hidden
;
}
.image-uploader
.el-upload
:hover
{
border-color
:
#409EFF
;
.avatar-uploader
{
.el-upload
{
border
:
1px
dashed
#d9d9d9
;
border-radius
:
6px
;
cursor
:
pointer
;
position
:
relative
;
overflow
:
hidden
;
&
:hover
{
border-color
:
#409EFF
;
}
}
}
.
image
-uploader-icon
{
.
avatar
-uploader-icon
{
font-size
:
28px
;
color
:
#8c939d
;
width
:
148px
;
height
:
148px
;
line-height
:
146px
;
text-align
:
center
;
}
.
image
{
.
avatar
{
width
:
148px
;
height
:
148px
;
display
:
block
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录