Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
提交反馈
为 GitLab 提交贡献
登录
切换导航
I
ibzuaa
项目
项目
详情
动态
版本
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
ibiz4jteam
ibzuaa
提交
d239bc50
提交
d239bc50
编写于
4年前
作者:
laizhilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
表单自定义图片上传组件:支持缩略图,预览,ocr识别,下载,删除,是否持久化
上级
c09599f6
变更
2
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
505 行增加
和
0 行删除
+505
-0
app-register.ts
app_web/src/app-register.ts
+1
-0
ibiz-image-upload.vue
...eb/src/components/ibiz-image-upload/ibiz-image-upload.vue
+504
-0
未找到文件。
app_web/src/app-register.ts
浏览文件 @
d239bc50
...
@@ -178,5 +178,6 @@ export const AppComponents = {
...
@@ -178,5 +178,6 @@ export const AppComponents = {
v
.
component
(
'app-menu-item'
,
AppMenuItem
);
v
.
component
(
'app-menu-item'
,
AppMenuItem
);
v
.
component
(
'app-message-popover'
,
AppMessagePopover
);
v
.
component
(
'app-message-popover'
,
AppMessagePopover
);
v
.
component
(
'ibiz-file-upload'
,
()
=>
import
(
'./components/ibiz-file-upload/ibiz-file-upload.vue'
));
v
.
component
(
'ibiz-file-upload'
,
()
=>
import
(
'./components/ibiz-file-upload/ibiz-file-upload.vue'
));
v
.
component
(
'ibiz-image-upload'
,
()
=>
import
(
'./components/ibiz-image-upload/ibiz-image-upload.vue'
));
},
},
};
};
\ No newline at end of file
This diff is collapsed.
Click to expand it.
app_web/src/components/ibiz-image-upload/ibiz-image-upload.vue
0 → 100644
浏览文件 @
d239bc50
<
template
>
<div
id=
"image-upload"
>
<el-upload
ref=
"imageUpload"
multiple
:file-list=
"imageList"
list-type=
"picture-card"
:action=
"getAction"
:headers=
"myHeaders"
:limit=
"limit"
:before-upload=
"beforeUpload"
:http-request=
"customUploadImage"
>
<i
class=
"el-icon-plus"
></i>
<div
slot=
"file"
slot-scope=
"
{file,index}">
<img
class=
"el-upload-list__item-thumbnail"
:src=
"file.url"
>
<span
class=
"el-upload-list__item-actions"
>
<!--预览按钮-->
<span
class=
"el-upload-list__item-preview"
@
click=
"onPreview(file)"
title=
"预览"
v-show=
"showPreview"
>
<i
class=
"el-icon-view"
></i>
</span>
<!--OCR按钮-->
<span
class=
"el-upload-list__item-delete"
@
click=
"onOcr(file)"
title=
"OCR识别"
v-show=
"showOcrview && (file.name.match(/^.+\.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG|bmp|BMP)$/))"
>
<i
class=
"el-icon-camera"
></i>
</span>
<!--下载按钮-->
<span
class=
"el-upload-list__item-delete"
@
click=
"onDownload(file)"
title=
"下载"
>
<i
class=
"el-icon-download"
></i>
</span>
<!--删除按钮-->
<span
class=
"el-upload-list__item-delete"
@
click=
"onRemove(file)"
title=
"删除"
>
<i
class=
"el-icon-delete"
></i>
</span>
</span>
</div>
</el-upload>
<el-dialog
:visible
.
sync=
"dialogVisible"
:modal=
"false"
>
<img
width=
"100%"
:src=
"dialogImageUrl"
alt=
""
>
</el-dialog>
</div>
</
template
>
<
script
>
import
{
Upload
,
Message
,
MessageBox
}
from
'element-ui'
;
import
Axios
from
'axios'
;
import
{
Subject
,
Unsubscribable
}
from
'rxjs'
;
// 当前登录人token
var
token
=
"Bearer "
+
localStorage
.
getItem
(
'token'
);
export
default
{
name
:
"ibiz-image-upload"
,
components
:
{
'el-upload'
:
Upload
,
},
props
:
{
// 当前表单对象
data
:
{
type
:
Object
,
},
// 当前属性名
formItemName
:
{
type
:
String
,
},
// 当前属性值
value
:
{
type
:
String
,
},
// 订阅表单状态
formState
:
{
type
:
Subject
},
// 默认为当前实体名称,有指定则按表单参数
folder
:
{
type
:
String
,
},
// 默认为当前实体主键id,有指定则按表单参数
ownerid
:
{
type
:
String
,
},
// 默认为当前属性名,有指定则按表单参数
ownertype
:
{
type
:
String
,
},
// 持久化
persistence
:
{
type
:
Boolean
,
default
:
false
},
// 是否显示预览按钮
showPreview
:
{
type
:
Boolean
,
default
:
false
},
// 是否显示OCR按钮
showOcrview
:
{
type
:
Boolean
,
default
:
false
},
// 单图片大小
size
:
{
type
:
Number
,
default
:
2
},
// 限制上传个数
limit
:
{
type
:
Number
,
default
:
20
},
},
data
()
{
return
{
// 表单是否处于编辑状态(有真实主键,srfuf='1';srfuf='0'时处于新建未保存)
srfuf
:
'0'
,
// 上传提示语
uploadTip
:
`单个文件大小不超过
${
this
.
size
}
M,文件不超过
${
this
.
limit
}
个`
,
// 图片列表
imageList
:
[],
// headers
myHeaders
:
{
Authorization
:
token
},
// 表单状态事件
formStateEvent
:
Unsubscribable
|
undefined
,
// 批量更新标识,false为不更新,true才可以更新
isUpdateBatch
:
true
,
// 新建状态标识,true为新建,false为编辑
isCreate
:
true
,
// 预览弹出框显示标识,true显示,false隐藏
dialogVisible
:
false
,
// 预览弹出框中的图片地址
dialogImageUrl
:
''
,
// 存放图片的fileid,用于图片列表定位
imageFileids
:
[],
}
},
computed
:
{
/**
* return action
*/
getAction
()
{
return
'/net-disk/upload/'
+
this
.
getFolder
+
'?ownertype='
+
this
.
getOwnertype
+
'&ownerid='
+
this
.
getOwnerid
;
},
/**
* return folder
*/
getFolder
()
{
return
typeof
this
.
folder
==
"string"
?
this
.
folder
:
JSON
.
stringify
(
this
.
folder
);
},
/**
* return ownertype
*/
getOwnertype
()
{
return
typeof
this
.
ownertype
==
"string"
?
this
.
ownertype
:
JSON
.
stringify
(
this
.
ownertype
);
},
/**
* return ownerid
*/
getOwnerid
()
{
return
typeof
this
.
ownerid
==
"string"
?
this
.
ownerid
:
JSON
.
stringify
(
this
.
ownerid
);
}
},
watch
:
{},
created
()
{
this
.
formStateEvent
=
this
.
formState
.
subscribe
((
$event
)
=>
{
// 表单加载完成
if
(
Object
.
is
(
$event
.
type
,
'load'
))
{
const
data
=
JSON
.
parse
(
JSON
.
stringify
(
$event
.
data
));
// 编辑表单,保存时不进行批量更新
if
(
data
.
srfuf
==
'1'
)
{
this
.
isCreate
=
false
;
this
.
isUpdateBatch
=
false
;
}
// 当persistence = true时,表单持久化
if
(
this
.
persistence
==
true
)
{
// 直接从表单的data数据里获取当前属性的值
if
(
data
[
this
.
formItemName
]
&&
this
.
imageList
.
length
==
0
)
{
const
files
=
JSON
.
parse
(
data
[
this
.
formItemName
]);
files
.
forEach
((
item
,
i
)
=>
{
// 图片列表显示缩略图需要获取真实的图片信息
if
(
item
.
id
&&
item
.
name
){
this
.
getRealImageData
(
item
);
}
});
}
}
else
{
// 发送get请求获取图片列表
this
.
getFiles
();
}
}
// 表单保存完成
if
(
Object
.
is
(
$event
.
type
,
'save'
))
{
// 批量更新文件表中的ownerid
if
(
this
.
isUpdateBatch
==
true
&&
this
.
imageList
.
length
>
0
)
{
this
.
updateFileBatch
(
this
.
imageList
);
}
}
});
},
methods
:
{
/**
* 获取图片列表
*/
getFiles
()
{
// 拼接url
const
getUrl
=
'/net-disk/files/'
+
this
.
getFolder
;
// 发送get请求
Axios
.
get
(
getUrl
,
{
params
:
{
ownertype
:
this
.
getOwnertype
,
ownerid
:
this
.
getOwnerid
,
},
}).
then
(
response
=>
{
if
(
!
response
||
response
.
status
!=
200
)
{
Message
.
error
(
"获取图片列表失败!"
);
return
;
}
// 返回的是一个jsonArray
if
(
response
.
data
)
{
const
files
=
JSON
.
parse
(
JSON
.
stringify
(
response
.
data
));
if
(
this
.
imageList
.
length
==
0
)
{
files
.
forEach
((
item
,
i
)
=>
{
// 图片列表显示缩略图需要获取真实的图片信息
if
(
item
.
id
&&
item
.
name
){
this
.
getRealImageData
(
item
);
}
});
}
}
}).
catch
(
error
=>
{
Message
.
error
(
"获取图片列表失败:"
+
error
);
});
},
/**
* 获取真实的图片信息
* @param fileid
*/
getRealImageData
(
file
){
let
fileData
=
file
;
// 拼接url,与下载一致
const
downloadUrl
=
'/net-disk/download/'
+
this
.
getFolder
+
'/'
+
fileData
.
id
+
'/'
+
fileData
.
name
;
// 发送get请求
Axios
.
get
(
downloadUrl
,
{
headers
:
{
'authcode'
:
fileData
.
authcode
},
responseType
:
'blob'
,
}).
then
(
res
=>
{
if
(
!
res
||
res
.
status
!=
200
)
{
Message
.
error
(
"下载缩略图失败!"
);
}
// 请求成功,后台返回的是一个文件流
if
(
res
.
data
)
{
// 用blob对象获取文件流
var
blob
=
new
Blob
([
res
.
data
],
{
type
:
res
.
headers
[
'content-type'
]});
// 通过文件流创建下载链接
var
href
=
URL
.
createObjectURL
(
blob
);
// 将下载链接保存到图片中
fileData
.
url
=
href
;
// 保存图片fileid
if
(
fileData
.
fileid
)
{
this
.
imageFileids
.
push
(
fileData
.
fileid
);
}
else
if
(
fileData
.
id
)
{
this
.
imageFileids
.
push
(
fileData
.
id
);
}
else
{
Message
.
error
(
"图片id不存在!"
);
return
;
}
// 保存图片到图片列表进行显示
this
.
imageList
.
push
(
fileData
);
}
else
{
Message
.
error
(
'下载缩略图失败,未获取到文件!'
);
}
}).
catch
(
error
=>
{
Message
.
error
(
"下载缩略图失败:"
+
error
);
});
},
/**
* 上传之前
*/
beforeUpload
(
file
){
// 支持上传的图片格式
if
(
!
file
.
name
.
match
(
/^.+
\.(
gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG|bmp|BMP
)
$/
)){
Message
.
error
(
`上传失败,仅支持'gif,jpg,png,bmp'格式的图片!`
);
return
false
;
}
// 文件大小
const
isSize
=
file
.
size
/
1024
/
1024
<
this
.
size
;
if
(
!
isSize
)
{
Message
.
error
(
`上传失败,单个图片不得超过
${
this
.
size
}
M!`
);
return
false
;
}
},
/**
* 自定义上传图片
*/
customUploadImage
(
param
){
// 上传的文件
let
file
=
param
.
file
;
// formData传参
let
formData
=
new
FormData
();
formData
.
append
(
'file'
,
file
);
// 拼接url
const
uploadUrl
=
this
.
getAction
;
// 发送post请求
Axios
.
post
(
uploadUrl
,
formData
,
{
timeout
:
2000
}).
then
(
response
=>
{
if
(
!
response
||
response
.
status
!=
200
)
{
Message
.
error
(
'上传图片失败!'
);
}
// 返回的是一个jsonobject
if
(
response
.
data
)
{
let
returnData
=
response
.
data
;
// 拼接缩略图下载url
const
downloadUrl
=
'/net-disk/download/'
+
this
.
getFolder
+
'/'
+
returnData
.
id
+
'/'
+
returnData
.
name
;
// 发送get请求
Axios
.
get
(
downloadUrl
,
{
headers
:
{
'authcode'
:
returnData
.
authcode
},
responseType
:
'blob'
,
}).
then
(
res
=>
{
if
(
!
res
||
res
.
status
!=
200
)
{
Message
.
error
(
"下载缩略图失败!"
);
return
;
}
// 请求成功,后台返回的是一个文件流
if
(
res
.
data
)
{
// 用blob对象获取文件流
var
blob
=
new
Blob
([
res
.
data
],
{
type
:
res
.
headers
[
'content-type'
]});
// 通过文件流创建下载链接
var
href
=
URL
.
createObjectURL
(
blob
);
// 将下载链接保存到本次上传成功后返回的jsonobject中
returnData
.
url
=
href
;
// 保存jsonobject中的图片fileid
if
(
returnData
.
fileid
)
{
this
.
imageFileids
.
push
(
returnData
.
fileid
);
}
else
if
(
returnData
.
id
)
{
this
.
imageFileids
.
push
(
returnData
.
id
);
}
else
{
Message
.
error
(
"图片id不存在!"
);
return
;
}
// 保存jsonobject到图片列表进行显示
this
.
imageList
.
push
(
returnData
);
// 新建表单上传时,后续需要批量更新操作
if
(
this
.
isCreate
==
true
)
{
this
.
isUpdateBatch
=
true
;
}
// persistence=true时,需要持久化表单属性
if
(
this
.
persistence
==
true
&&
this
.
imageList
.
length
>
0
)
{
const
value
=
JSON
.
stringify
(
this
.
imageList
);
this
.
$emit
(
'formitemvaluechange'
,
{
name
:
this
.
formItemName
,
value
:
value
});
}
}
else
{
Message
.
error
(
'下载缩略图失败,未获取到文件!'
);
}
}).
catch
(
error
=>
{
Message
.
error
(
"下载缩略图失败:"
+
error
);
});
}
}).
catch
(
err
=>
{
Message
.
error
(
'上传图片失败:'
+
err
);
});
},
/**
* 预览
* @param file
*/
onPreview
(
file
){
this
.
dialogImageUrl
=
file
.
url
;
this
.
dialogVisible
=
true
;
},
/**
* Ocr识别
* @param file
*/
onOcr
(
file
)
{
// 拼接url
const
id
=
typeof
file
.
id
==
"string"
?
file
.
id
:
JSON
.
stringify
(
file
.
id
);
const
name
=
typeof
file
.
name
==
"string"
?
file
.
name
:
JSON
.
stringify
(
file
.
name
);
const
ocrUrl
=
'/net-disk/ocrview/'
+
this
.
getFolder
+
'/'
+
id
+
'/'
+
name
+
'?authcode='
+
file
.
authcode
;
// 新窗口打开url
window
.
open
(
ocrUrl
);
},
/**
* 下载
* @param file
*/
onDownload
(
file
)
{
// 拼接url
const
id
=
typeof
file
.
id
==
"string"
?
file
.
id
:
JSON
.
stringify
(
file
.
id
);
const
name
=
typeof
file
.
name
==
"string"
?
file
.
name
:
JSON
.
stringify
(
file
.
filename
);
const
downloadUrl
=
'/net-disk/download/'
+
this
.
getFolder
+
'/'
+
id
+
'/'
+
name
;
// 发送get请求
Axios
.
get
(
downloadUrl
,
{
headers
:
{
'authcode'
:
file
.
authcode
},
responseType
:
'blob'
,
}).
then
(
response
=>
{
if
(
!
response
||
response
.
status
!=
200
)
{
Message
.
error
(
"下载图片失败!"
);
return
;
}
// 请求成功,后台返回的是一个文件流
if
(
response
.
data
)
{
// 获取文件名
const
disposition
=
response
.
headers
[
'content-disposition'
];
const
filename
=
disposition
.
split
(
'filename='
)[
1
];
// 用blob对象获取文件流
var
blob
=
new
Blob
([
response
.
data
],
{
type
:
response
.
headers
[
'content-type'
]});
// 通过文件流创建下载链接
var
href
=
URL
.
createObjectURL
(
blob
);
// 创建一个a元素并设置相关属性
var
a
=
document
.
createElement
(
'a'
);
a
.
href
=
href
;
a
.
download
=
filename
;
// 添加a元素到当前网页
document
.
body
.
appendChild
(
a
);
// 触发a元素的点击事件,实现下载
a
.
click
();
// 下载完成,从当前网页移除a元素
document
.
body
.
removeChild
(
a
);
// 释放blob对象
URL
.
revokeObjectURL
(
href
);
}
else
{
Message
.
error
(
'下载图片失败,未找到图片!'
);
}
}).
catch
(
error
=>
{
Message
.
error
(
"下载图片失败:"
+
error
);
});
},
/**
* 删除
* @param file
*/
onRemove
(
file
)
{
if
(
file
)
{
MessageBox
.
confirm
(
'此操作将永久删除该文件, 是否继续?'
,
'提示'
,
{
confirmButtonText
:
'确定'
,
cancelButtonText
:
'取消'
,
type
:
'warning'
}).
then
(()
=>
{
if
(
this
.
imageFileids
.
indexOf
(
file
.
id
)
!=-
1
)
{
// 要删除的图片在图片列表中的下标
const
index
=
this
.
imageFileids
.
indexOf
(
file
.
id
);
// 拼接url
const
deleteUrl
=
'/net-disk/files/'
+
file
.
id
;
// 发送delete请求
Axios
.
delete
(
deleteUrl
).
then
(
response
=>
{
if
(
!
response
||
response
.
status
!=
200
)
{
Message
.
error
(
"删除图片失败!"
);
}
// 从fileid数组中删除
this
.
imageFileids
.
splice
(
index
,
1
);
// 从图片列表中删除
this
.
imageList
.
splice
(
index
,
1
);
// persistence=true,时需要持久化表单属性
if
(
this
.
persistence
==
true
)
{
const
value
=
JSON
.
stringify
(
this
.
imageList
);
this
.
$emit
(
'formitemvaluechange'
,
{
name
:
this
.
formItemName
,
value
:
value
});
}
}).
catch
(
error
=>
{
// 提示删除失败
Message
.
error
(
"删除图片失败:"
+
error
);
});
}
});
}
},
/**
* 批量更新文件表的ownerid
*/
updateFileBatch
(
files
)
{
// 拼接url
const
updateUrl
=
'/net-disk/files/'
+
this
.
getFolder
+
'?ownertype='
+
this
.
getOwnertype
+
"&ownerid="
+
this
.
getOwnerid
;
// requestBody参数
let
requestBody
=
[];
if
(
files
)
{
requestBody
=
files
;
}
// 发送post请求
Axios
.
post
(
updateUrl
,
requestBody
,
{
headers
:
{
"Content-Type"
:
"application/json;charset=UTF-8"
},
timeout
:
2000
}).
then
(
response
=>
{
if
(
!
response
||
response
.
status
!=
200
)
{
Message
.
error
(
"批量更新文件失败!"
);
return
;
}
}).
catch
(
error
=>
{
Message
.
error
(
"批量更新文件失败:"
+
error
);
});
},
},
}
</
script
>
<
style
scoped
>
</
style
>
\ No newline at end of file
This diff is collapsed.
Click to expand it.
编辑
预览
Markdown
格式
0%
请重试
or
添加新附件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
先完成此消息的编辑!
取消
想要评论请
注册
或
登录