| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- <template>
- <div
- class="multi-uploader"
- :class="fileList && fileList.length >= limit ? 'multi-uploader-limit' : ''"
- >
- <el-upload
- action="/upload_image"
- list-type="picture-card"
- :limit="limit"
- accept=".jpg,.jpeg,.png,.JPG,.PNG,.JPEG"
- ref="multiUploader"
- with-credentials
- :file-list="fileList"
- :before-upload="beforeUpload"
- :on-preview="handlePictureCardPreview"
- :on-remove="handleRemove"
- :on-success="handleSuccess"
- >
- <i class="el-icon-plus"></i>
- </el-upload>
- <el-dialog :visible.sync="dialogVisible">
- <img width="100%" :src="dialogImageUrl" alt />
- </el-dialog>
- <div
- slot="tip"
- v-if="showTips"
- class="el-upload__tip"
- :class="fileList.length ? 'el-upload__tip-active' : ''"
- >
- {{ tips }}
- </div>
- </div>
- </template>
- <script>
- export default {
- name: "multi-uploader",
- props: {
- value: {
- type: Array
- },
- showTips: {
- type: Boolean,
- default: true
- },
- limit: {
- type: Number,
- default: 9
- },
- tips: {
- type: String,
- default: "最多添加9张作品图片"
- },
- fileSize: {
- type: Number,
- default: 5
- }
- },
- model: {
- prop: "value",
- event: "change"
- },
- components: {},
- data() {
- return {
- uploading: false,
- fileList: [],
- dialogImageUrl: "",
- dialogVisible: false
- };
- },
- watch: {
- value: {
- //深度监听,可监听到对象、数组的变化
- handler(val, oldVal) {
- console.log("watch1:", val);
- this.fileList = val;
- },
- deep: true //true 深度监听
- },
- fileList: {
- //深度监听,可监听到对象、数组的变化
- handler(val, oldVal) {
- console.log("watch2:", val);
- this.$emit("change", val);
- },
- deep: true //true 深度监听
- }
- },
- computed: {},
- mounted() {
- this.fileList = this.value;
- },
- methods: {
- beforeUpload(file) {
- console.log("beforeUpload", file);
- console.log("before", this.$refs.multiUploader);
- let fileType = file.type ? file.type : file.raw ? file.raw.type : "";
- if (
- fileType !== "image/png" &&
- fileType !== "image/jpg" &&
- fileType !== "image/jpeg" &&
- fileType !== "image/gif"
- ) {
- this.$message.error("请上传png,jp(e)g,gif格式的图片");
- return false;
- }
- if (file.size / 1024 / 1024 > this.fileSize) {
- this.$message.error("图片大小不可超过5M");
- return false;
- }
- return true;
- },
- handleRemove(file, fileList) {
- console.log(file, fileList);
- this.fileList = fileList;
- },
- handlePictureCardPreview(file) {
- this.dialogImageUrl = file.url;
- this.dialogVisible = true;
- },
- handleSuccess(response, file, fileList) {
- console.log(response, file, fileList, this.fileList);
- if (!response.error && response.filename) {
- console.log("上传成功");
- const list = [];
- fileList.forEach(fileItem => {
- list.push({
- name: fileItem.name,
- url: fileItem.response ? fileItem.response.filename : fileItem.url,
- uid: fileItem.uid,
- status: "success"
- });
- });
- this.fileList = list;
- } else {
- this.$refs.multiUploader.abort(file);
- const list = [];
- fileList.forEach(tmpFile => {
- if (file.uid !== tmpFile.uid) {
- list.push({
- name: tmpFile.name,
- url: tmpFile.response ? tmpFile.response.filename : tmpFile.url,
- uid: tmpFile.uid,
- status: "success"
- });
- }
- });
- this.fileList = list;
- console.log("上传失败 abort");
- }
- }
- }
- };
- </script>
- <style lang="scss">
- .multi-uploader {
- position: relative;
- background: #fff;
- }
- .multi-uploader-limit {
- .el-upload--picture-card {
- display: none;
- }
- }
- .el-upload__tip {
- line-height: 20px;
- // margin-top: 10px;
- font-size: 14px;
- color: #999999;
- }
- .el-upload__tip-active {
- margin-top: -1px;
- }
- </style>
|