Vue.component('template-select-image', {
template: `
{{item.name}}
可选{{checkedLength}}/{{canSelectNum}}
确定选择
原名:{{bigImgOriginalName}}
将图片拖到此处上传,或点击选择上传
只能上传jpg/png/gif/ico及视频文件,每次最多选择100个
图片不得大于1MB,视频不得大于10MB
`,
props: {
'canSelectNum': {
type: Number
},
'lessHeight': {
type: Number
},
'usedImageList': {
type: Array
},
'selectFileType': {
type: Number,
default: 1,
},
},
data() {
return {
selectTypeListC: [],
selectTypeListD: [],
tempMerchantClass: [],
tempStoreClass: [],
showUpload: false,
searchKey: '',
selectTypeC: '',
selectTypeD: '',
fileType: 1,
bigImgName: '图片名称',
showBigImage: false,
bigImg: '',
bigType: 1,
page: 1,
limit: 15,
imgTotal: 0,
canLoad: true,
checkedLength: 0,
bigImgNameTop: '0px',
allFileList: [],
isDisabled: true,
bigImgOriginalName: '',
showGetMore: false,
allSuccess: '',
fileList: [],
isUploading: false,
loading: false,
actMenuIndex: 1,
}
},
methods: {
toUpload() {
if (!this.fileType) {
this.$message({
message: '请指定上传文件类型',
type: 'warning',
});
return;
}
this.showUpload = true;
},
getUploadPostData() {
return {
selectTypeC: this.selectTypeC,
selectTypeD: this.selectTypeD,
fileType: this.fileType,
};
},
beforeFileUpload(file) {
console.log(file);
if (this.fileType == 1) {
//上传图片
let imgExt = ['image/jpeg', 'image/png', 'image/gif', 'image/vnd.microsoft.icon', 'image/x-icon'];
if (imgExt.indexOf(file.type) == -1) {
this.$message.error('上传图片只能是 JPG、PNG、ico格式!');
return false;
}
if ((file.size / 1024 / 1024) > 1) {
this.$message.error('上传图片大小不能超过 1MB!');
return false;
}
} else {
//视频
if (file.type.split('/')[0] != 'video') {
this.$message.error('请上传视频文件');
return false;
}
if ((file.size / 1024 / 1024) > 10) {
this.$message.error('上传视频大小不能超过 10MB!');
return false;
}
}
return true;
},
uploadFileIng(event, file, fileList) {
this.isUploading = true;
},
uploadFileSuccess(response, file, fileList) {
this.isUploading = false;
if (response.status == 1) {
this.allSuccess++;
if (this.allSuccess == Object.keys(fileList).length) {
this.$message({
type: 'success',
message: '上传成功'
});
this.page = 1;
this.limit = 25;
this.$nextTick(() => {
this.canLoad = true;
this.getData();
})
}
} else {
this.$message({
message: response.msg,
type: 'error',
duration: 1500
});
}
},
uploadFileError(event, file, fileList) {
this.isUploading = false;
this.$message({
message: '上传失败,请重试',
type: 'error',
duration: 1500
});
},
beforeRemove() {
this.allSuccess--;
},
closeUpload() {
if (!this.isUploading) {
this.fileList = [];
this.allSuccess = '';
this.showUpload = false;
}
},
//选择图片
useIt(index) {
var allFileList = JSON.parse(JSON.stringify(this.allFileList));
if (allFileList[index].checked) {
allFileList[index].checked = false;
this.checkedLength = this.checkedLength - 1;
} else {
if (this.checkedLength < this.canSelectNum) {
allFileList[index].checked = true;
this.checkedLength = this.checkedLength + 1;
}
}
this.allFileList = [];
this.allFileList = allFileList;
},
//确认选择图片并提交
selectImage() {
var item = [];
// console.log(this.usedImageList);
var allFileList = JSON.parse(JSON.stringify(this.allFileList));
for (var i = 0; i < allFileList.length; i++) {
if (allFileList[i].checked) {
if (typeof this.usedImageList == "object") {
if (this.usedImageList.indexOf(allFileList[i].id) == -1) {
item.push(JSON.parse(JSON.stringify(allFileList[i])));
}
} else {
item.push(JSON.parse(JSON.stringify(allFileList[i])));
}
}
allFileList[i].checked = false;
}
this.allFileList = [];
this.allFileList = allFileList;
this.checkindex = [];
this.checkedLength = 0;
this.isDataTable = false;
this.$nextTick(() => {
this.$emit('selectfile', item);
})
},
//搜索图片
doSearch() {
this.page = 1;
this.canLoad = true;
this.$nextTick(() => {
this.getData();
});
},
changeSelectTypeC(val) {
if (val == -1 && this.tempMerchantClass.length) {
this.selectTypeListD = [];
this.selectTypeD = '';
this.selectTypeListD = JSON.parse(JSON.stringify(this.tempMerchantClass));
} else if (val > 0 && this.tempStoreClass.length) {
this.selectTypeListD = [];
this.selectTypeD = '';
this.selectTypeListD = JSON.parse(JSON.stringify(this.tempStoreClass));
} else {
this.selectTypeListD = [];
this.selectTypeD = '';
}
},
//查看大图
seeBig(item) {
this.bigImgName = item.name + '(大小:' + item.size + ')';
this.bigImgOriginalName = item.original_name;
this.bigImg = item.path;
this.bigType = item.type;
this.showBigImage = true;
var height = document.body.clientHeight | document.documentElement.clientHeight;
this.$nextTick(() => {
// console.log(height, this.$refs.bigImgName.offsetHeight)
var _height = (this.$refs.bigImgName.offsetHeight + 130) / 2;
console.log(_height, height)
if (height > _height) {
this.bigImgNameTop = 'calc(50vh - ' + _height + 'px)';
} else {
this.bigImgNameTop = '0px';
}
})
},
//修改页数
handleCurrentChange(val) {
this.page = val;
this.getData();
},
getData() {
if (!this.canLoad) {
return;
}
this.loading = true;
$.ajax({
url: "/index.php/Retail/Component/getGalleryImages",
type: 'post',
data: {
page: this.page,
limit: this.limit,
selectTypeC: this.selectTypeC,
selectTypeD: this.selectTypeD,
key: this.searchKey,
fileType: this.fileType,
},
success: (res) => {
if (res.status == 1) {
this.allFileList = [];
this.allFileList = res.list;
if (this.page == 1) {
this.imgTotal = res.count;
}
this.$nextTick(() => {
this.loading = false;
this.canLoad = true;
})
} else {
this.$confirm(res.msg, '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'error'
}).then(() => {
this.loading = false;
}).catch(() => {
this.loading = false;
});
}
}
})
},
},
watch: {
checkedLength() {
if (this.checkedLength > 0) {
this.isDisabled = false;
} else {
this.isDisabled = true;
}
},
usedImageList() {
this.checkedLength = 0;
for (var i = 0; i < this.allFileList.length; i++) {
if (this.usedImageList.indexOf(this.allFileList[i].id) != -1) {
this.allFileList[i].checked = true;
// this.checkedLength++;
} else {
this.allFileList[i].checked = false;
}
}
},
selectFileType(n) {
this.fileType = n;
this.doSearch();
}
},
mounted() {
},
created() {
$.ajax({
url: "/index.php/Retail/Component/getSelectClass",
type: 'post',
data: {},
success: res => {
if (res.status == 1) {
this.selectTypeListC = res.from_class;
this.tempMerchantClass = res.merchant_class;
this.tempStoreClass = res.store_class;
}
}
});
this.getData();
},
});