2022-08-04 18:59:32 +08:00
|
|
|
<!--
|
|
|
|
|
* @Description:头像上传组件
|
|
|
|
|
* @Author: czw (725551805@qq.com)
|
|
|
|
|
* @Date: 2022-03-03 18:50:30
|
|
|
|
|
* @LastEditors: czw (725551805@qq.com)
|
|
|
|
|
* @LastEditTime: 2022-03-14 09:14:24
|
|
|
|
|
* @FilePath: /glxt/src/views/home/home/index.vue
|
|
|
|
|
-->
|
|
|
|
|
<template>
|
|
|
|
|
<div>
|
2022-08-12 18:26:27 +08:00
|
|
|
<el-upload
|
|
|
|
|
name="image"
|
|
|
|
|
:action="uploadAction"
|
|
|
|
|
:headers="uploadHeaders"
|
|
|
|
|
:on-preview="handleChange"
|
|
|
|
|
:on-remove="beforeRemove"
|
|
|
|
|
:on-success="good"
|
|
|
|
|
:file-list="img"
|
|
|
|
|
:limit="number"
|
|
|
|
|
:before-upload="handleBeforeUpload"
|
|
|
|
|
:on-change="handleEditChange"
|
|
|
|
|
accept=".png,.jpg"
|
|
|
|
|
list-type="picture-card"
|
|
|
|
|
:class="hideUploadEdit ? 'hide' : ''"
|
|
|
|
|
:show-file-list="true"
|
|
|
|
|
>
|
2022-08-04 18:59:32 +08:00
|
|
|
<i class="el-icon-plus"></i>
|
|
|
|
|
</el-upload>
|
|
|
|
|
|
2022-08-12 18:26:27 +08:00
|
|
|
<div class="demo-image__preview" v-if="imageUrl">
|
|
|
|
|
<el-image
|
|
|
|
|
class="hidden__el-image"
|
|
|
|
|
ref="elImage"
|
|
|
|
|
:src="imageUrl"
|
|
|
|
|
:preview-src-list="previewSrcList"
|
|
|
|
|
>
|
2022-08-04 18:59:32 +08:00
|
|
|
</el-image>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script>
|
2022-08-12 18:26:27 +08:00
|
|
|
import { getToken } from "@/util/auth";
|
2022-08-04 18:59:32 +08:00
|
|
|
export default {
|
2022-08-12 18:26:27 +08:00
|
|
|
name: "", // 页面名称
|
2022-08-04 18:59:32 +08:00
|
|
|
components: {}, // 挂载组件
|
|
|
|
|
props: {
|
|
|
|
|
// 上传图片数量
|
|
|
|
|
number: {
|
|
|
|
|
type: Number,
|
|
|
|
|
default: 1,
|
|
|
|
|
},
|
|
|
|
|
file: {
|
|
|
|
|
type: Array,
|
|
|
|
|
default: () => [],
|
|
|
|
|
},
|
|
|
|
|
}, // 组件传值
|
|
|
|
|
data() {
|
|
|
|
|
return {
|
|
|
|
|
hideUploadEdit: false, // 判断显示图片上传限制隐藏
|
2022-08-12 18:26:27 +08:00
|
|
|
url: "/api/upload", // 请求接口
|
|
|
|
|
imageUrl: "", // 图片展示
|
2022-08-04 18:59:32 +08:00
|
|
|
previewSrcList: [], // 展示图片列表
|
|
|
|
|
list: [], // 上传图片列表
|
|
|
|
|
img: [], // 实际展示的图片
|
2022-08-12 18:26:27 +08:00
|
|
|
};
|
2022-08-04 18:59:32 +08:00
|
|
|
},
|
|
|
|
|
computed: {
|
|
|
|
|
// 上传的地址
|
|
|
|
|
uploadAction() {
|
2022-08-12 18:26:27 +08:00
|
|
|
return "https://ct-upimg.yx090.com" + this.url;
|
2022-08-04 18:59:32 +08:00
|
|
|
},
|
|
|
|
|
// 设置上传的请求头部
|
|
|
|
|
uploadHeaders() {
|
|
|
|
|
return {
|
|
|
|
|
// authorization: 'Bearer' + getToken(),
|
2022-08-12 18:26:27 +08:00
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
|
};
|
2022-08-04 18:59:32 +08:00
|
|
|
},
|
|
|
|
|
}, // 计算机属性 类似与data概念
|
|
|
|
|
watch: {
|
|
|
|
|
file: {
|
|
|
|
|
handler(val) {
|
|
|
|
|
if (val) {
|
2022-08-12 18:26:27 +08:00
|
|
|
// console.log(val, 'ppp')
|
|
|
|
|
var imges = [];
|
2022-08-04 18:59:32 +08:00
|
|
|
val.forEach((element) => {
|
2022-08-12 18:26:27 +08:00
|
|
|
imges = element.url.split(",");
|
|
|
|
|
this.list = imges;
|
|
|
|
|
});
|
2022-08-04 18:59:32 +08:00
|
|
|
imges.forEach((element) => {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.img.push({ url: this.computedGetPictureSrc(element) });
|
|
|
|
|
});
|
2022-08-04 18:59:32 +08:00
|
|
|
const previewSrcList = imges.map((ele) => {
|
2022-08-12 18:26:27 +08:00
|
|
|
return this.computedGetPictureSrc(ele);
|
|
|
|
|
});
|
|
|
|
|
this.previewSrcList = previewSrcList;
|
2022-08-04 18:59:32 +08:00
|
|
|
if (val.length === this.number) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.hideUploadEdit = true;
|
2022-08-04 18:59:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
deep: true,
|
|
|
|
|
immediate: true,
|
|
|
|
|
},
|
|
|
|
|
}, // 监控data中数据变化
|
|
|
|
|
methods: {
|
|
|
|
|
computedGetPictureSrc(src) {
|
2022-08-12 18:26:27 +08:00
|
|
|
return "https://ct-upimg.yx090.com/" + src;
|
2022-08-04 18:59:32 +08:00
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* @author: czw (725551805@qq.com)
|
|
|
|
|
* @description: 判断显示图片上传限制隐藏
|
|
|
|
|
* @param {*} file
|
|
|
|
|
* @param {*} fileList
|
|
|
|
|
* @return {*}
|
|
|
|
|
* @Date: 2022-03-03 21:16:54
|
|
|
|
|
*/
|
|
|
|
|
handleEditChange(file, fileList) {
|
|
|
|
|
if (fileList.length === this.number) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.hideUploadEdit = true;
|
2022-08-04 18:59:32 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* @author: czw (725551805@qq.com)
|
|
|
|
|
* @description: 上传成功
|
|
|
|
|
* @param {*}
|
|
|
|
|
* @return {*}
|
|
|
|
|
* @Date: 2022-03-03 19:59:45
|
|
|
|
|
*/
|
|
|
|
|
good(file, fileList) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.previewSrcList.push(fileList.url);
|
|
|
|
|
console.log(this.previewSrcList, "------");
|
2022-08-04 18:59:32 +08:00
|
|
|
// file.data.forEach((element) => {
|
|
|
|
|
// this.list.push(element.url)
|
|
|
|
|
// })
|
|
|
|
|
|
2022-08-12 18:26:27 +08:00
|
|
|
this.$emit("urlimg", fileList.url);
|
2022-08-04 18:59:32 +08:00
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* @author: czw (725551805@qq.com)
|
|
|
|
|
* @description: 点击放大
|
|
|
|
|
* @param {*}
|
|
|
|
|
* @return {*}
|
|
|
|
|
* @Date: 2022-03-03 18:59:28
|
|
|
|
|
*/
|
|
|
|
|
handleChange(file) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.imageUrl = file.url;
|
2022-08-04 18:59:32 +08:00
|
|
|
this.$nextTick(() => {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.$refs.elImage.clickHandler();
|
|
|
|
|
});
|
2022-08-04 18:59:32 +08:00
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* @author: czw (725551805@qq.com)
|
|
|
|
|
* @description:删除
|
|
|
|
|
* @param {*}
|
|
|
|
|
* @return {*}
|
|
|
|
|
* @Date: 2022-03-03 18:59:40
|
|
|
|
|
*/
|
|
|
|
|
beforeRemove(file, fileList) {
|
|
|
|
|
if (fileList.length === this.number) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.hideUploadEdit = true;
|
2022-08-04 18:59:32 +08:00
|
|
|
} else {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.hideUploadEdit = false;
|
2022-08-04 18:59:32 +08:00
|
|
|
}
|
|
|
|
|
if (file.response) {
|
|
|
|
|
file.response.data.forEach((element) => {
|
|
|
|
|
for (var i = 0; i < this.list.length; i++) {
|
|
|
|
|
if (element.url === this.list[i]) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.list.splice(i, 1);
|
2022-08-04 18:59:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
2022-08-12 18:26:27 +08:00
|
|
|
});
|
2022-08-04 18:59:32 +08:00
|
|
|
} else {
|
2022-08-12 18:26:27 +08:00
|
|
|
console.log(file, "iiii");
|
2022-08-04 18:59:32 +08:00
|
|
|
for (var i = 0; i < this.list.length; i++) {
|
|
|
|
|
if (file.url === this.computedGetPictureSrc(this.list[i])) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.list.splice(i, 1);
|
2022-08-04 18:59:32 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
/**
|
|
|
|
|
* @author: czw (725551805@qq.com)
|
|
|
|
|
* @description:自动删除
|
|
|
|
|
* @param {*}
|
|
|
|
|
* @return {*}
|
|
|
|
|
* @Date: 2022-03-03 19:53:25
|
|
|
|
|
*/
|
|
|
|
|
handleBeforeUpload(file) {
|
2022-08-12 18:26:27 +08:00
|
|
|
const isLt2M = file.size / 1024 / 1024 < 2;
|
2022-08-04 18:59:32 +08:00
|
|
|
if (!isLt2M) {
|
2022-08-12 18:26:27 +08:00
|
|
|
this.$message.error("上传头像图片大小不能超过 2MB!");
|
|
|
|
|
return false;
|
2022-08-04 18:59:32 +08:00
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}, // 挂载一些方法
|
2022-08-12 18:26:27 +08:00
|
|
|
};
|
2022-08-04 18:59:32 +08:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
|
.demo-image__preview {
|
|
|
|
|
height: 0;
|
|
|
|
|
}
|
|
|
|
|
.hidden__el-image {
|
|
|
|
|
width: 0;
|
|
|
|
|
height: 0;
|
|
|
|
|
::v-deep .el-image__preview {
|
|
|
|
|
display: none;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
.hide .el-upload--picture-card {
|
|
|
|
|
display: none;
|
|
|
|
|
}
|
|
|
|
|
</style>
|