-
Notifications
You must be signed in to change notification settings - Fork 1
/
customsStatus.js
340 lines (330 loc) · 11 KB
/
customsStatus.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
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
// ==UserScript==
// @name 报关单状态查询
// @namespace customs
// @version 1.3
// @updateURL https://tampermonkey.isaacxu.com/customsStatus.js
// @license LGPL-3.0
// @description 报关单状态查询
// @supportURL https://github.com/meterXu/mtMonkeyJs
// @author isaac
// @match http://query.customs.gov.cn/HYW2007DataQuery/FormStatusQuery.aspx
// @require https://code.jquery.com/jquery-2.0.0.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery.form/4.2.2/jquery.form.min.js
// @run-at document-idle
// @grant unsafeWindow
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
let state = true;
let dataNum = 8;
let dataIndex = 0;
let backUrl = 'http://work.isaacxu.com/yyjkwebapi/api/';
let resArray = [];
let waitTime = 0;
let requireData = null;
let defaultWaitTime = waitTime;
let isResetViewSate = true;
function startRobot(time) {
if (state) {
// 判断是否可以开始
wirteLog('开始爬网');
if ($("#submitBtn").length === 1) {
$.ajax({
url: backUrl + 'Cus/GetNoList',
type: 'post',
dataType: 'json',
data: {
dataNum: dataNum
},
success: function (res) {
if (res) {
$("#totalCrawl").text(parseInt(res.crawled)+parseInt(res.unCrawl));
$("#crawled").text(res.crawled);
$("#unCrawl").text(res.unCrawl);
if(res.data.length > 0){
requireData=res.data;
window.setTimeout(c=>realPost(),waitTime*1000);
}
}
},
error: function () {
wirteLog('请求GetNoList出错');
startRobot(time||1000);
}
})
} else {
let reTime = new RegExp('(?<=\\().*(?=秒后)','g');
let execsTime = reTime.exec($('body').text());
if(execsTime){
waitTime = waitTime =parseFloat(execsTime[0]);
}else{
waitTime = 60;
}
wirteLog('发现页面不可爬,轮询等待'+waitTime+'秒');
window.setTimeout(c => {
window.location.href = window.location.href;
}, waitTime*1000)
}
} else {
startRobot(time||1000);
}
}
function realPost() {
if(!isResetViewSate){
resetViewState();
}
if(isResetViewSate){
isResetViewSate=false;
wirteLog('开始爬取报关单号:' + requireData[dataIndex]['CS_NO']);
$("#txtDeclareFormNo").val(requireData[dataIndex]['CS_NO']);
var imgUrl = $("#verifyIdentityImage img").attr("src");
$.ajax({
url: backUrl + "Cus/GetVcode",
data: {
imgurl: 'http://query.customs.gov.cn/' + imgUrl
},
async:true,
type: 'post',
dataType: 'json',
beforeSend: function () {
wirteLog('开始获取验证码');
},
success: function (res) {
if (res.code && res.code.length === 6) {
wirteLog('获取验证码为:' + res.code);
$("#txtVerifyNumber").val(res.code);
window.setTimeout(c => {
$("#submitBtn").click();
}, 0)
} else {
wirteLog('['+(dataIndex+1)+']验证码不正确:' + res.code);
resArray.push({
cusNo: $("#txtDeclareFormNo").val(),
state: null
});
rungoon();
}
},
error: function () {
wirteLog('['+(dataIndex+1)+']获取验证码失败');
resArray.push({
cusNo: $("#txtDeclareFormNo").val(),
state: null
});
rungoon();
}
})
}else{
wirteLog('等待'+waitTime+'秒');
window.setTimeout(c => realPost(),waitTime*1000);
}
}
function wirteLog(text) {
if($("#dc_log").val()&&$("#dc_log").val().length>=5000){
$("#dc_log").val("");
}else{
let ndt = new Date();
let msg = ndt.toLocaleDateString() + ' ' + ndt.toLocaleTimeString() + ":" + text + '\r\n';
$("#dc_log").val($("#dc_log").val()+msg);
$("#dc_log")[0].scrollTop = $("#dc_log")[0].scrollHeight;
}
}
function initElement() {
$("body").append("<div id='dc_contorl'><div>" +
"<div><label>总共:</label><label id='totalCrawl'></label><label>,已爬取:</label><label id='crawled'></label><label>,未爬取:</label><label id='unCrawl'></label></div>" +
"<div><button id='dc_btn'></button></div>" +
"<div><textarea id='dc_log'></textarea></div>" +
"</div>");
$("#dc_contorl div").css({
"margin-bottom":"10px"
});
$("#crawled").css({
'color':'red'
});
$("#unCrawl").css({
'color':'green'
});
$("#dc_contorl").css({
width: '450px',
position: 'absolute',
top: '50px',
right: '50px'
});
$("#dc_log").css({
width: '100%',
height: '400px',
overflow: 'auto',
resize: 'none',
'white-space': 'nowrap'
});
$('#dc_btn').text(state?'暂停':'开始');
$("#dc_btn").on({
'click': function () {
if (state) {
state = false;
$(this).text(state?'暂停':'开始');
wirteLog('暂停');
} else {
state = true;
$(this).text(state?'暂停':'开始');
wirteLog('开始');
}
}
});
}
function resetViewState() {
$.ajax({
url: 'http://query.customs.gov.cn/HYW2007DataQuery/FormStatusQuery.aspx',
type: 'get',
async: false,
beforeSend:function(){
wirteLog('开始重置viewState');
},
success: function (data) {
setViewState(data)
}
});
}
function setViewState(data) {
let reImg = /(?<=\<img src\=\").*(?=\" align=\"absmiddle\")/g;
let reView = new RegExp('(?<=__VIEWSTATE" value=").*(?=" />)', 'g');
var reAction = new RegExp('(?<=__EVENTVALIDATION" value=").*(?=" />)', 'g');
let execs = reImg.exec(data);
let viewExecs = reView.exec(data);
let actionExecs = reAction.exec(data);
if (execs && viewExecs && actionExecs) {
wirteLog('重置viewState成功');
isResetViewSate=true;
waitTime=defaultWaitTime;
let newUrl = execs[0];
newUrl = newUrl.replace(/&/g, "&");
$("img[align='absmiddle']").attr('src', newUrl);
$("#__VIEWSTATE").val(viewExecs[0]);
$("#__EVENTVALIDATION").val(actionExecs[0]);
} else {
wirteLog('重置viewState失败');
wirteLog(data);
isResetViewSate=false;
let reTime = new RegExp('(?<=\\().*(?=秒后)','g');
let execsTime = reTime.exec(data);
if(execsTime){
waitTime=parseFloat(execsTime[0]);
}else{
waitTime=60;
}
}
}
function setForm() {
let reResult = new RegExp('(?<=lblResult" class="color04">).*(?=</span)', 'g');
$('#form1').append('<input type="hidden" name="submitBtn" value="查询"/>');
$('#form1').on('submit', function (e) {
e.preventDefault(); // prevent native submit
$(this).ajaxSubmit({
success: function (data) {
let execRe = reResult.exec(data);
if (execRe) {
wirteLog('['+(dataIndex+1)+']爬取到结果:' + execRe);
resArray.push({
cusNo: $("#txtDeclareFormNo").val(),
state: execRe
});
setViewState(data)
} else {
let execRe = reResult.exec(data);
if (execRe) {
wirteLog('['+(dataIndex+1)+']爬取到结果:' + execRe);
resArray.push({
cusNo: $("#txtDeclareFormNo").val(),
state: execRe
});
setViewState(data)
}else {
wirteLog('['+(dataIndex+1)+']未爬取到结果');
resArray.push({
cusNo: $("#txtDeclareFormNo").val(),
state: null
});
if (data.indexOf('验证码错误') > -1) {
wirteLog('验证码错误')
setViewState(data)
} else {
if (data.length < 300) {
wirteLog(data)
}
}
}
}
rungoon();
},
error: function () {
wirteLog('提交表单失败');
resArray.push({
cusNo: $("#txtDeclareFormNo").val(),
state: null
});
rungoon();
}
})
});
}
function rungoon() {
if (resArray.length === dataNum) {
saveDataToDb();
}else{
dataIndex++;
if (dataIndex < dataNum) {
wirteLog('等待'+waitTime+'秒');
window.setTimeout(c => realPost(requireData),waitTime*1000);
}
}
}
function saveDataToDb(callback) {
wirteLog('该批次准备入库');
let saveData = resArray.filter(c => c.state !== null);
let cusNos = [];
let states = [];
if (saveData.length > 0) {
for (let i in saveData) {
cusNos.push(saveData[i].cusNo);
states.push(saveData[i].state);
}
$.ajax({
url: backUrl + "Cus/SaveCusStatus",
data: {
cusNos: cusNos.join(','),
states: states.join(',')
},
type: 'post',
beforeSend: function () {
wirteLog('该批次开始入库');
},
success: function (res) {
wirteLog('该批次入库成功');
},
error: function () {
wirteLog('该批次入库失败');
},
complete:function () {
wirteLog('一个批次爬网结束');
dataIndex=0;
resArray=[];
requireData=null;
startRobot(waitTime*1000);
}
})
} else {
wirteLog('该批次入库数据条数为0');
wirteLog('一个批次爬网结束');
dataIndex=0;
resArray=[];
startRobot(waitTime*1000);
}
}
(function () {
'use strict';
$(function () {
initElement();
setForm();
startRobot();
});
})();