提交 b3a20067 编写于 作者: laizhilong's avatar laizhilong

表单自定义图片上传组件:修改编写方式为typescript

上级 3dfdcb72
......@@ -5,11 +5,11 @@
multiple
:file-list="imageList"
list-type="picture-card"
:action="getAction"
:action="getAction()"
:headers="myHeaders"
:limit="limit"
:before-upload="beforeUpload"
:http-request="customUploadImage">
:http-request="customImageUpload">
<i class="el-icon-plus"></i>
<div slot="file" slot-scope="{file,index}">
<img class="el-upload-list__item-thumbnail" :src="file.url">
......@@ -41,127 +41,235 @@
</div>
</template>
<script>
import {Upload, Message, MessageBox} from 'element-ui';
<script lang="ts">
import {Component, Vue, Prop, Watch} from 'vue-property-decorator';
import {Message, MessageBox} from 'element-ui';
import Axios from 'axios';
import {Subject, Unsubscribable} from 'rxjs';
// 当前登录人token
var token = "Bearer " + localStorage.getItem('token');
@Component({})
export default class IbizImageUpload extends Vue {
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
* 当前表单对象
*
* @type {*}
* @memberof DiskImageUplaod
*/
getAction() {
return '/net-disk/upload/' + this.getFolder + '?ownertype=' + this.getOwnertype + '&ownerid=' + this.getOwnerid;
},
@Prop() public data!: any;
/**
* 当前属性名
*
* @type {string}
* @memberof DiskImageUplaod
*/
@Prop() public formItemName!: string;
/**
* 当前属性值
*
* @type {string}
* @memberof DiskImageUplaod
*/
@Prop() public value!: string;
/**
* 当前表单状态
*
* @type {*}
* @memberof DiskImageUplaod
*/
@Prop() public formState!: any;
/**
* 默认为当前实体名称,有指定则按表单参数
*
* @type {string}
* @memberof DiskImageUplaod
*/
@Prop() public folder!: string;
/**
* 默认为当前实体主键id,有指定则按表单参数
*
* @type {string}
* @memberof DiskImageUplaod
*/
@Prop() public ownerid!: string;
/**
* 默认为当前属性名,有指定则按表单参数
*
* @type {string}
* @memberof DiskImageUplaod
*/
@Prop() public ownertype!: string;
/**
* 持久化
*
* @type {boolean}
* @memberof DiskImageUplaod
*/
@Prop({default: false}) public persistence?: boolean;
/**
* 是否显示预览按钮
*
* @type {boolean}
* @memberof DiskImageUplaod
*/
@Prop({default: false}) public showPreview?: boolean;
/**
* 是否显示OCR按钮
*
* @type {boolean}
* @memberof DiskImageUplaod
*/
@Prop({default: false}) public showOcrview?: boolean;
/**
* 单文件大小
*
* @type {number}
* @memberof DiskImageUplaod
*/
@Prop({default: 1}) public size!: number;
/**
* 文件上传个数
*
* @type {number}
* @memberof DiskImageUplaod
*/
@Prop({default: 5}) public limit!: number;
/**
* 表单是否处于编辑状态(有真实主键,srfuf='1';srfuf='0'时处于新建未保存)
*
* @type {string}
* @memberof DiskImageUplaod
*/
public srfuf: string = '0';
/**
* 图片列表
*
* @type {Array<any>}
* @memberof DiskImageUplaod
*/
public imageList: Array<any> = [];
/**
* 当前登陆人的token
*
* @type {string}
* @memberof DiskImageUplaod
*/
public token: string = "Bearer " + localStorage.getItem('token');
/**
* 上传文件请求头
*
* @type {*}
* @memberof DiskImageUplaod
*/
public myHeaders: any = {Authorization: this.token};
/**
* 表单状态事件
*
* @type {*}
* @memberof DiskImageUplaod
*/
public formStateEvent: any | Unsubscribable | undefined;
/**
* 批量更新标识,false为不更新,true才可以更新
*
* @type {boolean}
* @memberof DiskImageUplaod
*/
public isUpdateBatch: boolean = true;
/**
* 新建状态标识,true为新建,false为编辑
*
* @type {boolean}
* @memberof DiskImageUplaod
*/
public isCreate: boolean = true;
/**
* 预览弹出框显示标识,true显示,false隐藏
*
* @type {boolean}
* @memberof DiskImageUplaod
*/
public dialogVisible: boolean = false;
/**
* 预览弹出框中的图片地址
*
* @type {string}
* @memberof DiskImageUplaod
*/
public dialogImageUrl: string = '';
/**
* 存放图片的fileid,用于图片列表定位
*
* @type {boolean}
* @memberof DiskImageUplaod
*/
public imageFileids: Array<any> = [];
/**
* 拼接上传路径
*
* @memberof DiskImageUplaod
*/
public getAction() {
return '/net-disk/upload/' + this.getFolder() + '?ownertype=' + this.getOwnertype() + '&ownerid=' + this.getOwnerid();
}
/**
* return folder
*
* @memberof DiskImageUplaod
*/
getFolder() {
public getFolder() {
return typeof this.folder == "string" ? this.folder : JSON.stringify(this.folder);
},
}
/**
* return ownertype
*
* @memberof DiskImageUplaod
*/
getOwnertype() {
public getOwnertype() {
return typeof this.ownertype == "string" ? this.ownertype : JSON.stringify(this.ownertype);
},
}
/**
* return ownerid
*
* @memberof DiskImageUplaod
*/
getOwnerid() {
public getOwnerid() {
return typeof this.ownerid == "string" ? this.ownerid : JSON.stringify(this.ownerid);
}
},
watch: {},
created() {
this.formStateEvent = this.formState.subscribe(($event) => {
/**
* vue生命周期create
*
* @memberof DiskImageUplaod
*/
public created() {
this.formStateEvent = this.formState.subscribe(($event: any) => {
// 表单加载完成
if (Object.is($event.type, 'load')) {
const data = JSON.parse(JSON.stringify($event.data));
......@@ -175,9 +283,9 @@
// 直接从表单的data数据里获取当前属性的值
if (data[this.formItemName] && this.imageList.length == 0) {
const files = JSON.parse(data[this.formItemName]);
files.forEach((item,i)=>{
files.forEach((item: any, i: number) => {
// 图片列表显示缩略图需要获取真实的图片信息
if (item.id && item.name){
if (item.id && item.name) {
this.getRealImageData(item);
}
});
......@@ -195,19 +303,21 @@
}
}
});
},
methods: {
}
/**
* 获取图片列表
*
* @memberof DiskImageUplaod
*/
getFiles() {
public getFiles() {
// 拼接url
const getUrl = '/net-disk/files/' + this.getFolder;
const getUrl = '/net-disk/files/' + this.getFolder();
// 发送get请求
Axios.get(getUrl, {
params: {
ownertype: this.getOwnertype,
ownerid: this.getOwnerid,
ownertype: this.getOwnertype(),
ownerid: this.getOwnerid(),
},
}).then(response => {
if (!response || response.status != 200) {
......@@ -218,9 +328,9 @@
if (response.data) {
const files = JSON.parse(JSON.stringify(response.data));
if (this.imageList.length == 0) {
files.forEach((item,i)=>{
files.forEach((item: any, i: number) => {
// 图片列表显示缩略图需要获取真实的图片信息
if (item.id && item.name){
if (item.id && item.name) {
this.getRealImageData(item);
}
});
......@@ -229,15 +339,17 @@
}).catch(error => {
Message.error("获取图片列表失败:" + error);
});
},
}
/**
* 获取真实的图片信息
* @param fileid
* @param file
* @memberof DiskImageUplaod
*/
getRealImageData(file){
public getRealImageData(file: any) {
let fileData = file;
// 拼接url,与下载一致
const downloadUrl = '/net-disk/download/' + this.getFolder + '/' + fileData.id + '/' + fileData.name;
const downloadUrl = '/net-disk/download/' + this.getFolder() + '/' + fileData.id + '/' + fileData.name;
// 发送get请求
Axios.get(downloadUrl, {
headers: {
......@@ -273,13 +385,16 @@
}).catch(error => {
Message.error("下载缩略图失败:" + error);
});
},
}
/**
* 上传之前
* 上传之前
* @param file
* @memberof DiskImageUplaod
*/
beforeUpload(file){
public beforeUpload(file: any) {
// 支持上传的图片格式
if (!file.name.match(/^.+\.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG|bmp|BMP)$/)){
if (!file.name.match(/^.+\.(gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG|bmp|BMP)$/)) {
Message.error(`上传失败,仅支持'gif,jpg,png,bmp'格式的图片!`);
return false;
}
......@@ -289,18 +404,21 @@
Message.error(`上传失败,单个图片不得超过${this.size}M!`);
return false;
}
},
}
/**
* 自定义上传图片
* 自定义图片上传
* @param param
* @memberof DiskImageUplaod
*/
customUploadImage(param){
public customImageUpload(param: any) {
// 上传的文件
let file = param.file;
// formData传参
let formData = new FormData();
formData.append('file', file);
// 拼接url
const uploadUrl = this.getAction;
const uploadUrl = this.getAction();
// 发送post请求
Axios.post(uploadUrl, formData, {timeout: 2000}).then(response => {
if (!response || response.status != 200) {
......@@ -310,7 +428,7 @@
if (response.data) {
let returnData = response.data;
// 拼接缩略图下载url
const downloadUrl = '/net-disk/download/' + this.getFolder + '/' + returnData.id + '/' + returnData.name;
const downloadUrl = '/net-disk/download/' + this.getFolder() + '/' + returnData.id + '/' + returnData.name;
// 发送get请求
Axios.get(downloadUrl, {
headers: {
......@@ -360,36 +478,47 @@
}).catch(err => {
Message.error('上传图片失败:' + err);
});
},
}
/**
* 预览
* @param file
* @memberof DiskImageUplaod
*/
onPreview(file){
public onPreview(file: any) {
if (file.url) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
} else {
Message.error("图片url不存在");
}
}
/**
* Ocr识别
* @param file
* @memberof DiskImageUplaod
*/
onOcr(file) {
public onOcr(file: any) {
// 拼接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;
const ocrUrl = '/net-disk/ocrview/' + this.getFolder() + '/' + id + '/' + name + '?authcode=' + file.authcode;
// 新窗口打开url
window.open(ocrUrl);
},
}
/**
* 下载
* @param file
* @memberof DiskImageUplaod
*/
onDownload(file) {
public onDownload(file: any) {
// 拼接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;
const downloadUrl = '/net-disk/download/' + this.getFolder() + '/' + id + '/' + name;
// 发送get请求
Axios.get(downloadUrl, {
headers: {
......@@ -428,19 +557,22 @@
}).catch(error => {
Message.error("下载图片失败:" + error);
});
},
}
/**
* 删除
* @param file
* @memberof DiskImageUplaod
*/
onRemove(file) {
public onRemove(file: any) {
if (file) {
MessageBox.confirm('此操作将永久删除该文件, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
}).then(() => {
if (this.imageFileids.indexOf(file.id)!=-1) {
if (this.imageFileids.indexOf(file.id) != -1) {
// 要删除的图片在图片列表中的下标
const index = this.imageFileids.indexOf(file.id);
// 拼接url
......@@ -451,7 +583,7 @@
Message.error("删除图片失败!");
}
// 从fileid数组中删除
this.imageFileids.splice(index,1);
this.imageFileids.splice(index, 1);
// 从图片列表中删除
this.imageList.splice(index, 1);
// persistence=true,时需要持久化表单属性
......@@ -466,13 +598,17 @@
}
});
}
},
}
/**
* 批量更新文件表的ownerid
* @param files
* @memberof DiskImageUplaod
*/
updateFileBatch(files) {
public updateFileBatch(files: any) {
// 拼接url
const updateUrl = '/net-disk/files/' + this.getFolder + '?ownertype=' + this.getOwnertype + "&ownerid=" + this.getOwnerid;
const updateUrl = '/net-disk/files/' + this.getFolder() + '?ownertype=' + this.getOwnertype() + "&ownerid=" + this.getOwnerid();
// requestBody参数
let requestBody = [];
if (files) {
......@@ -492,13 +628,12 @@
}).catch(error => {
Message.error("批量更新文件失败:" + error);
});
},
},
}
}
</script>
<style scoped>
</style>
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册