<template>
<div class="tinypng-wrapper">
<div class="tips">
<p>1. 只能压缩 <span class="highlight">jpg/png</span> 格式图片;</p>
<p>2. 一次最多压缩<span class="highlight">100张</span>;</p>
<p>
3. 压缩后的文件会保存在<span class="highlight"
>选择的路径下的image-compress文件夹</span
>中, 请留意成功后的提示;
</p>
<p>
4. image-compress文件夹中如果有同名文件,将被<span class="highlight"
>覆盖</span
>;
</p>
<p>5. 图片处理需要时间,点击压缩后请耐心等待片刻。</p>
</div>
<div class="header">
<el-button>压缩图片质量</el-button>
<el-slider
class="slider"
v-model="quality"
:step="10"
:min="10"
show-stops
>
</el-slider>
</div>
<div class="header">
<input type="file" id="file" @change="fileChange" hidden webkitdirectory>
<el-input placeholder="保存文件目录" v-model="targetDir" disabled>
<template #prepend>图片保存目录</template>
<template #append>
<el-button icon="el-icon-folder" @click="btnChange"></el-button>
</template>
</el-input>
<el-button style="margin-left: 24px" type="primary" @click="handleSubmit"
>开始压缩</el-button
>
</div>
<div class="tinypng-content">
<el-upload
class="upload-demo"
ref="upload"
accept=".jpg,.png"
multiple
:auto-upload="false"
:limit="maxFileNum"
:file-list="fileList"
:on-exceed="handleExceed"
:on-preview="handlePictureCardPreview"
action=""
list-type="picture-card"
>
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog v-model="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="">
</el-dialog>
</div>
</div>
</template>
<style lang="scss" scoped>
.tinypng-wrapper{
width: 96%;
margin: 0 auto;
.tips{
padding-left: 20px;
border: 1px dashed rgb(187, 187, 187);
.highlight{
color: rgb(84, 162, 252);
}
}
.header{
display: flex;
align-items:center;
justify-content: space-between;
margin-top: 25px;
.slider{
width: 78%;
margin-right: 20px;
}
}
.tinypng-content{
margin: 20px 0;
padding: 20px;
border: 1px dashed rgb(187, 187, 187);
border-radius: 5px;
}
}
</style>
<script>
const { ipcRenderer } = window.require("electron")
const PATH = window.require("path");
import { onMounted, ref, onBeforeUnmount } from "vue";
import { ElMessage, ElNotification, ElLoading } from "element-plus";
let loadingInstance = null;
export default {
setup() {
const fileList = ref([]);
const maxFileNum = ref(100);
const upload = ref(null);
const targetDir = ref(null);
const quality = ref(50);
const dialogVisible = ref(false);
const dialogImageUrl = ref(null);
const marks = ref({
10: "10",
20: "20",
30: "30",
40: "40",
50: "50",
60: "60",
70: "70",
80: "80",
90: "90",
100: "100"
});
const handleExceed = (files, fileList) => {
ElMessage.warning({
message: `最多只能选择 ${ maxFileNum.value }个文件哦,当前选择了 ${files.length + fileList.length} 个文件`,
type: "warning"
});
};
const handleSubmit = () => {
const uploadFiles = upload.value.uploadFiles;
if (!uploadFiles.length) {
ElNotification({
title: "警告",
message: "请先选择文件!",
type: "warning"
});
return false;
}
if (!targetDir.value) {
ElNotification({
title: "警告",
message: "请先选择文件存放路径!",
type: "warning"
});
return false;
}
const dir = PATH.normalize(targetDir.value);
const fileList = [];
uploadFiles.map(item => item?.raw?.path && fileList.push(item.raw.path));
const data = {
fileList,
quality: quality.value,
targetDir: dir
};
loadingInstance = ElLoading.service( {
background: "rgba(255,255,255,0.5)"
});
ipcRenderer.send("compress-image", data);
};
const btnChange = () => {
var file = document.getElementById('file')
file.click()
};
const fileChange = () => {
try {
const fu = document.getElementById('file')
if (fu == null) return
targetDir.value = PATH.parse(fu.files[0].path).dir+ `${PATH.sep}image-compress`
console.log(targetDir.value)
} catch (error) {
console.debug('choice file err:', error)
}
};
const handlePictureCardPreview = file => {
console.log('file.url',file.url)
ElNotification({
title: "通知",
message: "预览图片功能暂不支持"
});
}
onBeforeUnmount(() => {
loadingInstance = null;
});
onMounted(() => {
ipcRenderer.on("compress-status", (event, arg) => {
ElNotification({
title: arg.success ? "成功" : "失败",
message: arg.success ? arg.msg : arg.reason,
type: arg.success ? "success" : "error"
});
loadingInstance.close();
if (arg.success) {
upload.value.uploadFiles = [];
fileList.value = [];
quality.value = 50;
}
});
});
return {
targetDir,
upload,
quality,
marks,
fileList,
maxFileNum,
handleExceed,
handleSubmit,
btnChange,
fileChange,
handlePictureCardPreview,
dialogVisible,
dialogImageUrl
};
}
};
</script>