-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
307 lines (268 loc) · 10.9 KB
/
script.js
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
function togglePrintingOptions() {
var printingOptions = document.getElementById("printingOptions");
var fileUploadArea = document.getElementById("fileUploadArea");
var button = document.querySelector("#functionArea button");
var container = document.querySelector('.container');
container.classList.toggle('flip');
if (printingOptions.style.display === "none") {
printingOptions.style.display = "block";
fileUploadArea.style.display = "none";
button.innerHTML = "只需要上传文件点这切换";
} else {
printingOptions.style.display = "none";
fileUploadArea.style.display = "block";
button.innerHTML = "需要打印PDF点这切换";
}
}
var helpButton = document.getElementById("helpButton");
var helpDialog = document.getElementById("helpDialog");
var closeHelpButton = document.getElementById("closeHelpButton");
helpButton.addEventListener("click", function() {
helpDialog.style.display = "block";
});
closeHelpButton.addEventListener("click", function() {
helpDialog.style.display = "none";
});
var paymentButton = document.getElementById("paymentButton");
var paymentDialog = document.getElementById("paymentDialog");
var closePaymentButton = document.getElementById("closePaymentButton");
paymentButton.addEventListener("click", function() {
paymentDialog.style.display = "block";
});
closePaymentButton.addEventListener("click", function() {
paymentDialog.style.display = "none";
});
function handleFileSelect(event) {
var file = event.target.files[0];
var allowedExtensions = /(\.pdf|\.txt|\.jpg|\.jpeg|\.png|\.doc|\.docx|\.xls|\.xlsx|\.ppt|\.pptx)$/i;
var maxFileSize = 100 * 1024 * 1024; // 100MB
if (!allowedExtensions.exec(file.name)) {
alert('仅支持上传后缀为 .pdf, .txt, .jpg, .jpeg, .png, .doc, .docx, .xls, .xlsx, .ppt, .pptx 的文件');
event.target.value = '';
return false;
}
if (file.name.split('.').length - 1 > 1) {
alert('文件名中不应包含多个"."');
event.target.value = '';
return false;
}
var fileSize = file.size;
if (fileSize > maxFileSize) {
alert('文件大小不能超过 20MB');
event.target.value = '';
return false;
}
}
function uploadFile() {
var fileInput = document.getElementById('fileInput2'); // 修改这里的id为fileInput2
var file = fileInput.files[0];
if (!file) {
alert('请先选择仅需上传的文件');
return false;
}
// 执行上传
alert('正在上传请稍等\n外网速度仅为128KB/s');
var formData = new FormData();
formData.append('file', file);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'upload2.php', true);
xhr.onload = function () {
if (xhr.status !== 500) {
console.log(xhr.responseText);
if (xhr.responseText.includes('成功')) {
var filename = file.name;
logDeviceInfo2(filename);
var currentDate = new Date();
var currentHour = currentDate.getHours();
var message;
if (currentHour >= 0 && currentHour < 4) {
message = '文件上传成功,当日凌晨4点前有效,请及时联系人工进行打印';
} else {
message = '文件上传成功,次日凌晨4点前有效,请及时联系人工进行打印';
}
alert(message);
} else {
alert('文件上传失败\n请检查文件内容及格式后重新上传');
}
} else {
alert('缓存耗尽!\n请联系管理员!');
}
};
xhr.send(formData);
}
function validateFileAndUpload() {
var fileInput = document.getElementById('fileInput');
var fileName = fileInput.value.trim().replace(/\s/g, ''); // 删除空格
var allowedExtensions = /(.pdf)$/i;
var maxFileSize = 10 * 1024 * 1024; // 10MB
if (!allowedExtensions.exec(fileName)) {
alert('仅支持上传后缀为 .pdf 的文件');
fileInput.value = '';
return false;
}
var fileSize = fileInput.files[0].size;
if (fileSize > maxFileSize) {
alert('文件大小不能超过 10MB');
fileInput.value = '';
return false;
}
// 文件验证通过,执行上传
var formData = new FormData();
formData.append('file', fileInput.files[0], fileName); // 传入处理后的文件名
var xhr = new XMLHttpRequest();
xhr.open('POST', 'upload.php', true);
alert('正在上传请稍等\n外网速度仅为128KB/s');
xhr.onload = function() {
if (xhr.status !== 500) {
console.log(xhr.responseText);
if (xhr.responseText.includes('成功')) {
// 读取并解析PDF文件,获取页数
var file = fileInput.files[0];
if (file.type === 'application/pdf') {
var fileReader = new FileReader();
fileReader.onload = function() {
var typedarray = new Uint8Array(this.result);
};
fileReader.readAsArrayBuffer(file);
}
// 提示用户只有五分钟用于打印
alert('您上传的文件将在五分钟后自动删除,请及时打印!');
// 设置定时器,在五分钟后提示文件已失效
setTimeout(function() {
alert('文件已失效,无法继续打印!');
window.location.reload(); // 刷新页面
}, 300000); // 五分钟后提示文件已失效
} else {
alert('文件上传失败\n请检查文件内容及格式后重新上传');
}
} else {
alert('缓存耗尽!\n请联系管理员!');
}
};
xhr.send(formData);
/*alert('寒假下班了');*/
}
function printFile() {
var fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert('请先选择要打印的文件');
return;
}
var filename = fileInput.files[0].name;
var encodedFilename = encodeURIComponent(filename); // 对文件名进行编码
var xhr = new XMLHttpRequest();
xhr.open('POST', 'print.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
logDeviceInfo(filename); // 记录设备信息和文件
alert(xhr.responseText); // 显示打印结果
} else {
alert('打印请求出错,确保内容并重新上传。');
}
window.location.reload(); // 刷新页面
}
};
xhr.send('filename=' + encodedFilename); // 发送编码后的文件名
/*alert('寒假下班了');*/
}
function printFile2() {
var fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert('请先选择要打印的文件');
return;
}
var filename = fileInput.files[0].name;
var encodedFilename = encodeURIComponent(filename); // 对文件名进行编码
var xhr = new XMLHttpRequest();
xhr.open('POST', 'print2.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
logDeviceInfo3(filename); // 记录设备信息和文件
alert(xhr.responseText); // 显示打印结果
} else {
alert('打印请求出错,确保内容并重新上传。');
}
window.location.reload(); // 刷新页面
}
};
xhr.send('filename=' + encodedFilename); // 发送编码后的文件名*/
/*alert('四月前仅可以全自动无人打印黑白内容!');*/
}
function printFile3() {
var fileInput = document.getElementById('fileInput');
if (fileInput.files.length === 0) {
alert('请先选择要打印的文件');
return;
}
var filename = fileInput.files[0].name;
var encodedFilename = encodeURIComponent(filename); // 对文件名进行编码
var xhr = new XMLHttpRequest();
xhr.open('POST', 'print3.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
if (xhr.status === 200) {
logDeviceInfo4(filename); // 记录设备信息和文件
alert(xhr.responseText); // 显示打印结果
} else {
alert('打印请求出错,确保内容并重新上传。');
}
window.location.reload(); // 刷新页面
}
};
xhr.send('filename=' + encodedFilename); // 发送编码后的文件名
/*alert('寒假下班了');*/
}
function logDeviceInfo(filename) {
var userAgent = navigator.userAgent;
var deviceInfo = {
userAgent: userAgent,
filename: filename
};
var xhr = new XMLHttpRequest();
xhr.open('POST', 'printer_log.php', true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(deviceInfo));
}
function logDeviceInfo2(filename) {
var userAgent = navigator.userAgent;
var deviceInfo = {
userAgent: userAgent,
filename: filename
};
var xhr = new XMLHttpRequest();
xhr.open('POST', 'upload_log.php', true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(deviceInfo));
}
function logDeviceInfo3(filename) {
var userAgent = navigator.userAgent;
var deviceInfo = {
userAgent: userAgent,
filename: filename
};
var xhr = new XMLHttpRequest();
xhr.open('POST', 'printer_log2.php', true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(deviceInfo));
}
function logDeviceInfo4(filename) {
var userAgent = navigator.userAgent;
var deviceInfo = {
userAgent: userAgent,
filename: filename
};
var xhr = new XMLHttpRequest();
xhr.open('POST', 'printer_log3.php', true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(deviceInfo));
}
var closePreviewButton = document.getElementById('closePreviewButton');
closePreviewButton.addEventListener('click', function() {
var previewDialog = document.getElementById('previewDialog');
previewDialog.style.display = 'none';
});