-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
354 lines (328 loc) · 14.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片压缩</title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
<!-- import Vue before Element -->
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<!-- import JavaScript -->
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<!-- 动画 -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.2/animate.min.css">
<!-- 基础样式 -->
<link rel="stylesheet" href="index.css">
</head>
<script>
</script>
<body>
<!-- <h1 class="animated fadeInDown faster">Example</h1> -->
<div class="content" id="vue">
<img class="settingIcon" src="./assets/settingIcon.png" @click="showSetting" />
<!-- modal弹窗 -->
<transition name="slide-fade"></transition>
<div class="modal " v-show="isShowModal">
<div class="content animated faster fadeInDown">
<div class="header">
设置
<img class="closeIcon" src="./assets/close.png" @click="closeSetting" />
</div>
<div class="lineDiv">
<span>是否覆盖原图片:</span>
<el-switch v-model="isCover" active-color="#13ce66" />
</div>
<div class="lineDiv">
<span>图片保存地址:</span>
<p class="saveUrl"> {{saveFileUrl}}/tinypng</p>
</div>
</div>
</div>
</transition>
<!-- 存在新的任务 -->
<div class="modal " v-show="isShowTipsModal">
<div class="tipsContent animated faster fadeInDown">
<div class="header">
提示
<img class="closeIcon" src="./assets/close.png" @click="closeTips" />
</div>
<div class="tipsLineDiv">
<span>当前有压缩任务正在进行中,是否重新开始压缩任务?</span>
</div>
<div class="tipsLineIconDiv">
<img class="tipsClose" src="./assets/tipsClose.png" @click="closeTips" />
<img class="affirmIcon" src="./assets/confirm.png" @click='coverNewFile' />
</div>
</div>
</div>
<!-- 显示处理的结果页面 -->
<div class="modal " v-show="isShowResultList">
<div class="resultContet animated faster fadeInDown">
<div class="header">
压缩完毕
<img class="closeIcon" src="./assets/close.png" @click="closeTips('result')" />
</div>
<div class="resultLineDiv">
<template v-for='item in newResult'>
<div class="resultList">
<div >
<img class ='resultImg' v-bind:src="item.url"/>
</div>
<div class="titleDiv">
<span class='img-title'>{{item.fileName}}</span>
<div>
<span class='img-title fileSize'>{{item.oldSize}}</span>
<span class='img-title fileSize'>{{item.newSize}}</span>
</div>
</div>
<span class='img-title ratio'>-{{item.ratio}}</span>
</div>
</template>
</div>
</div>
</div>
<div class="home">
<!-- 小火箭 -->
<div class="rocket-holder" v-if="!isLoading">
<img class='rocket' src="./assets/rocket.png" />
<img class="rainbow" src="./assets/rainbow.png" />
</div>
<div class="container" v-else>
<span class="girl"></span>
<div class="boys">
<span></span>
<span></span>
<span></span>
<span></span>
</div>
</div>
<!-- 提示文字 -->
<div class="progressDiv">
<div v-if="isLoading">
<p class="progress" v-if='!isEnd'>{{number}}/{{total}}</p>
<p class="progress" v-if='isEnd'>压缩完毕!</p>
<p class="progress" v-else>{{tipsTitle}}</p>
</div>
<div v-else>
<p class="progress">请拖入需要压缩的图片或者文件夹</p>
</div>
</div>
</div>
</div>
<script>
const TinyPng = require('web-tinypngjs');
//本地桌面地址
const path = require('path');
const saveFileUrl = path.join(require('os').homedir(), 'Desktop');
const {
shell,
Notification
} = require('electron')
const app = new Vue({
el: '#vue',
data: {
total: 0,
number: 0,
saveFileUrl: saveFileUrl,
isTemporaryFile: false,
isLoading: false, //是否在压缩
isEnd: false, //是否压缩结束
isShowModal: false, //显示modal
isShowTipsModal: false, //提示已经有任务
isCover: false, //是否覆盖原来的图片
tempInfo: null, //临时存放拖拽文件的地方
tipsTitle: "正在压缩中...", //提示语
imageResult: [], //存放处理后的文件信息
newResult: [], //通知过后 重新组装的数据
isShowResultList: false, //显示结果的列表页
},
created: function() {
this.dropInit();
},
methods: {
disposeTipsTitle: function() {
this.tipsTitle = '压缩后图片将保存在桌面tinypng文件中...'
setTimeout(() => {
this.tipsTitle = '正在压缩中...'
}, 5000)
},
//处理图片的方法-提取公共
disposeImage: async function(e) {
const fileInfo = e.dataTransfer.files[0];
this.isLoading = true;
let isCover = this.isCover;
const {
name,
path,
type
} = fileInfo;
//如果是文件夹
if (type === '') {
const imageList = await TinyPng.getAllImg(path);
//判断是否有store文件
if (imageList.length > 0) {
this.total = imageList.length
}
if (imageList.length === 0) {
alert('文件夹里面未搜索到图片哦~')
} else {
let isTemporaryFile = false;
imageList.find((item) => {
if (item.name === ".DS_Store") {
isTemporaryFile = true;
}
this.total = isTemporaryFile ? imageList.length - 1 : imageList.length;
this.isEnd = false;
this.isTemporaryFile = isTemporaryFile;
})
this.disposeTipsTitle();
//如果需要覆盖的话 输出地址不传
TinyPng.compress(path, isCover ? '' : `${this.saveFileUrl}/tinypng`, this.process);
}
} else if (type.includes("image")) {
this.isEnd = false;
this.number = 1;
this.total = 1;
this.disposeTipsTitle();
//如果需要覆盖的话 输出地址不传
TinyPng.compressImg(path, isCover ? "" : `${this.saveFileUrl}/tinypng/${name}`).then((res) => {
this.isEnd = true;
this.isLoading = false;
if (res) {
this.imageResult.push(res)
}
this.messageNotice();
});
} else {
alert("不是图片哦")
}
},
// 拖拽初始化
dropInit: function() {
document.addEventListener("drop", preventDe);
document.addEventListener("dragleave", preventDe);
document.addEventListener("dragover", preventDe);
document.addEventListener("dragenter", preventDe);
function preventDe(e) {
e.preventDefault();
}
document.addEventListener("drop", async(e) => {
e.preventDefault();
if (this.isLoading) {
this.isShowTipsModal = true;
this.tempInfo = e.dataTransfer.files[0];
} else {
this.disposeImage(e);
}
})
},
// 页面回调
process: function(res, number, total) {
//處理下文件的名稱
//如果有临时文件 数量-1
if (this.isTemporaryFile) {
--total;
--number;
}
if (res) {
this.imageResult.push(res)
}
this.number = number;
this.total = total;
if (number === total) {
this.isLoading = false;
this.isEnd = true;
this.total = 0;
this.number = 0;
this.messageNotice();
}
},
showSetting: function() {
this.isShowModal = true;
},
closeSetting: function() {
this.isShowModal = false;
},
closeTips: function(type) {
if (type === 'result') {
this.isShowResultList = false;
this.newResult = [];
} else {
this.isShowTipsModal = false;
}
},
coverNewFile: function() {
this.total = 0;
this.number = 0;
this.isShowTipsModal = false;
let tempInfo = this.tempInfo;
const data = {
dataTransfer: {
files: [tempInfo]
}
}
this.disposeImage(data)
},
//消息通知
messageNotice: function() {
let disposerNumber = 0;
let compressionSize = 0;
let compressionSizeDesc = '';
this.imageResult.map((item) => {
++disposerNumber;
compressionSize += (item.input.size - item.output.size);
compressionSizeDesc = this.getFileSize(compressionSize);
})
let option = {
title: "文件压缩成功!",
body: `本次共压缩了${disposerNumber}个文件夹,共计压缩${compressionSizeDesc}`,
icon: path.join(__dirname, './assets/programming.png')
};
// 创建通知并保存
let hhwNotication = new window.Notification(option.title, option);
//通知过后-重新组装数据下
let newResult = []
this.imageResult.map((item) => {
let oldSize = this.getFileSize(item.input.size);
let newSize = this.getFileSize(item.output.size);
let ratio = this.toPercent((item.input.size - item.output.size) / item.input.size);
let fileName = item.fileName.split(".")[0]
newResult.push({
url: item.output.url,
fileName: fileName,
oldSize,
newSize,
ratio
})
})
this.newResult = newResult;
this.isShowResultList = true;
this.imageResult = [];
// 当通知被点击时, 用默认浏览器打开链接
hhwNotication.onclick = function() {
console.log("点击了吧")
}
},
//获取文件多少kb
getFileSize: function(fileByte) {
var fileSizeByte = fileByte;
var fileSizeMsg = "";
if (fileSizeByte < 1048576) fileSizeMsg = (fileSizeByte / 1024).toFixed(2) + "KB";
else if (fileSizeByte == 1048576) fileSizeMsg = "1MB";
else if (fileSizeByte > 1048576 && fileSizeByte < 1073741824) fileSizeMsg = (fileSizeByte / (1024 * 1024)).toFixed(2) + "MB";
else if (fileSizeByte > 1048576 && fileSizeByte == 1073741824) fileSizeMsg = "1GB";
else if (fileSizeByte > 1073741824 && fileSizeByte < 1099511627776) fileSizeMsg = (fileSizeByte / (1024 * 1024 * 1024)).toFixed(2) + "GB";
else fileSizeMsg = "文件超过1TB";
return fileSizeMsg;
},
//转成百分数
toPercent: function(point) {
var str = Number(point * 100).toFixed(1);
str += "%";
return str;
}
}
})
</script>
</body>
</html>