-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtb_price_qx.js
232 lines (215 loc) · 7.65 KB
/
tb_price_qx.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
/*
README:https://github.com/yichahucha/surge/tree/master
*/
let isQuantumultX = $task !== undefined;
let isSurge = $httpClient !== undefined;
var $task = isQuantumultX ? $task : {};
var $httpClient = isSurge ? $httpClient : {};
if (isQuantumultX) {
var errorInfo = {
error: ''
};
$httpClient = {
get: (url, cb) => {
var urlObj;
if (typeof (url) == 'string') {
urlObj = {
url: url
}
} else {
urlObj = url;
}
$task.fetch(urlObj).then(response => {
cb(undefined, response, response.body)
}, reason => {
errorInfo.error = reason.error;
cb(errorInfo, response, '')
})
},
post: (url, cb) => {
var urlObj;
if (typeof (url) == 'string') {
urlObj = {
url: url
}
} else {
urlObj = url;
}
url.method = 'POST';
$task.fetch(urlObj).then(response => {
cb(undefined, response, response.body)
}, reason => {
errorInfo.error = reason.error;
cb(errorInfo, response, '')
})
}
}
}
const console_log = true
const url = $request.url
const body = $response.body
if (true) {
let obj = JSON.parse(body)
let apiStack = obj.data.apiStack[0]
let value = JSON.parse(apiStack.value)
let tradeConsumerProtection = value.global.data.tradeConsumerProtection
if (!tradeConsumerProtection) {
value.global.data["tradeConsumerProtection"] = customTradeConsumerProtection()
}
tradeConsumerProtection = value.global.data.tradeConsumerProtection
let service = tradeConsumerProtection.tradeConsumerService.service
let nonService = tradeConsumerProtection.tradeConsumerService.nonService
let item = obj.data.item
let shareUrl = `https://item.taobao.com/item.htm?id=${item.itemId}`
request_hsitory_price(shareUrl, function (data) {
if (data) {
let historyItem = getHistoryItem()
if (data.ok == 1 && data.single) {
const lower_price = lower_price_msg(data.single)
const result = history_price_item(data.single)
const tbitems = result[1]
service.items = service.items.concat(nonService.items)
historyItem.desc = lower_price
service.items.push(historyItem)
nonService.title = "价格走势"
nonService.items = tbitems
}
if (data.ok == 0 && data.msg.length > 0) {
historyItem.desc = data.msg
service.items.push(historyItem)
}
apiStack.value = JSON.stringify(value)
$done({ body: JSON.stringify(obj) })
} else {
$done({ body })
}
})
}
function lower_price_msg(data) {
const lower = data.lowerPriceyh;
const lower_date = changeDateFormat(data.lowerDateyh);
const lower_msg = "历史最低到手价: ¥" + String(lower) + " " + lower_date
const curret_msg = (data.currentPriceStatus ? " 当前价格" + data.currentPriceStatus : "") + " (仅供参考)";
return lower_msg + curret_msg;
}
function history_price_item(data) {
const rex_match = /\[.*?\]/g;
const rex_exec = /\[(.*),(.*),"(.*)"\]/;
const list = data.jiagequshiyh.match(rex_match);
let tbitems = [];
let start_date = "";
let end_date = "";
list.reverse().forEach((item, index) => {
if (item.length > 0) {
const result = rex_exec.exec(item);
const dateUTC = new Date(eval(result[1]));
const date = dateUTC.format("yyyy-MM-dd");
if (index == 0) {
end_date = date;
}
if (index == list.length - 1) {
start_date = date;
}
let price = result[2];
price = "¥" + String(parseFloat(price));
const msg = date + get_blank_space(50 - date.length) + price;
tbitem = {
icon: "https://s2.ax1x.com/2020/01/03/lU2AYD.png",
title: msg
}
tbitems.push(tbitem);
}
});
const date_range_msg = `(${start_date} ~ ${end_date})`;
return [date_range_msg, tbitems]
}
function request_hsitory_price(share_url, callback) {
const options = {
url: "https://apapia-history.manmanbuy.com/ChromeWidgetServices/WidgetServices.ashx",
headers: {
"Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 13_1_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 - mmbWebBrowse - ios"
},
body: "methodName=getBiJiaInfo_wxsmall&p_url=" + encodeURIComponent(share_url)
}
$httpClient.post(options, function (error, response, data) {
if (!error) {
callback(JSON.parse(data));
if (console_log) console.log("Data:\n" + data);
} else {
callback(null, null);
if (console_log) console.log("Error:\n" + error);
}
})
}
function changeDateFormat(cellval) {
const date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
const month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
const currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
return date.getFullYear() + "-" + month + "-" + currentDate;
}
function get_blank_space(length) {
let blank = "";
for (let index = 0; index < length; index++) {
blank += " ";
}
return blank;
}
function getHistoryItem() {
return {
icon: "https://s2.ax1x.com/2020/01/03/lU2Pw6.png",
title: "历史价格",
desc: ""
}
}
function customTradeConsumerProtection() {
return {
"tradeConsumerService": {
"service": {
"items": [
],
"icon": "",
"title": "基础服务"
},
"nonService": {
"items": [
],
"title": "其他"
}
},
"passValue": "all",
"url": "https://h5.m.taobao.com/app/detailsubpage/consumer/index.js",
"type": "0"
}
}
Array.prototype.insert = function (index, item) {
this.splice(index, 0, item);
};
Date.prototype.format = function (fmt) {
var o = {
"y+": this.getFullYear(),
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
for (var k in o) {
if (new RegExp("(" + k + ")").test(fmt)) {
if (k == "y+") {
fmt = fmt.replace(RegExp.$1, ("" + o[k]).substr(4 - RegExp.$1.length));
}
else if (k == "S+") {
var lens = RegExp.$1.length;
lens = lens == 1 ? 3 : lens;
fmt = fmt.replace(RegExp.$1, ("00" + o[k]).substr(("" + o[k]).length - 1, lens));
}
else {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
}
}
}
return fmt;
}