forked from zwf234/rules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jd_price.js
326 lines (309 loc) · 11.7 KB
/
jd_price.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
const path1 = "serverConfig";
const path2 = "wareBusiness";
const path3 = "basicConfig";
const consolelog = false;
const url = $request.url;
const body = $response.body;
const $tool = tool();
if (url.indexOf(path1) != -1) {
let obj = JSON.parse(body);
delete obj.serverConfig.httpdns;
delete obj.serverConfig.dnsvip;
delete obj.serverConfig.dnsvip_v6;
$done({ body: JSON.stringify(obj) });
}
if (url.indexOf(path3) != -1) {
let obj = JSON.parse(body);
let JDHttpToolKit = obj.data.JDHttpToolKit;
if (JDHttpToolKit) {
delete obj.data.JDHttpToolKit.httpdns;
delete obj.data.JDHttpToolKit.dnsvipV6;
}
$done({ body: JSON.stringify(obj) });
}
if (url.indexOf(path2) != -1) {
let obj = JSON.parse(body);
const floors = obj.floors;
const commodity_info = floors[floors.length - 1];
const shareUrl = commodity_info.data.property.shareUrl;
request_history_price(shareUrl, function (data) {
if (data) {
const lowerword = adword_obj();
lowerword.data.ad.textColor = "#fe0000";
let bestIndex = 0;
for (let index = 0; index < floors.length; index++) {
const element = floors[index];
if (element.mId == lowerword.mId) {
bestIndex = index + 1;
break;
} else {
if (element.sortId > lowerword.sortId) {
bestIndex = index;
break;
}
}
}
if (data.ok == 1 && data.single) {
const lower = lowerMsgs(data.single)
const detail = priceSummary(data)
const tip = data.PriceRemark.Tip + "(仅供参考)"
lowerword.data.ad.adword = `${lower} ${tip}\n${detail}`;
floors.insert(bestIndex, lowerword);
}
if (data.ok == 0 && data.msg.length > 0) {
lowerword.data.ad.adword = "" + data.msg;
floors.insert(bestIndex, lowerword);
}
$done({ body: JSON.stringify(obj) });
} else {
$done({ body });
}
})
}
function lowerMsgs(data) {
const lower = data.lowerPriceyh
const lowerDate = dateFormat(data.lowerDateyh)
const lowerMsg = "历史最低价格:¥" + String(lower) + ` (${lowerDate}) `
return lowerMsg
}
function priceSummary(data) {
let summary = ""
let listPriceDetail = data.PriceRemark.ListPriceDetail.slice(0,4)
let list = listPriceDetail.concat(historySummary(data.single))
list.forEach((item, index) => {
if (item.Name == "双11价格") {
item.Name = "双十一价格"
} else if (item.Name == "618价格") {
item.Name = "六一八价格"
}
summary += `\n${item.Name}${getSpace(8)}${item.Price}${getSpace(8)}${item.Date}${getSpace(8)}${item.Difference}`
})
return summary
}
function historySummary(single) {
const rexMatch = /\[.*?\]/g;
const rexExec = /\[(.*),(.*),"(.*)".*\]/;
let currentPrice, lowest30, lowest90, lowest180, lowest360
let list = single.jiagequshiyh.match(rexMatch);
list = list.reverse().slice(0, 360);
list.forEach((item, index) => {
if (item.length > 0) {
const result = rexExec.exec(item);
const dateUTC = new Date(eval(result[1]));
const date = dateUTC.format("yyyy-MM-dd");
let price = parseFloat(result[2]);
if (index == 0) {
currentPrice = price
lowest30 = { Name: "三十天最低", Price: `¥${String(price)}`, Date: date, Difference: difference(currentPrice, price), price }
lowest90 = { Name: "九十天最低", Price: `¥${String(price)}`, Date: date, Difference: difference(currentPrice, price), price }
lowest180 = { Name: "一百八最低", Price: `¥${String(price)}`, Date: date, Difference: difference(currentPrice, price), price }
lowest360 = { Name: "三百六最低", Price: `¥${String(price)}`, Date: date, Difference: difference(currentPrice, price), price }
}
if (index < 30 && price < lowest30.price) {
lowest30.price = price
lowest30.Price = `¥${String(price)}`
lowest30.Date = date
lowest30.Difference = difference(currentPrice, price)
}
if (index < 90 && price < lowest90.price) {
lowest90.price = price
lowest90.Price = `¥${String(price)}`
lowest90.Date = date
lowest90.Difference = difference(currentPrice, price)
}
if (index < 180 && price < lowest180.price) {
lowest180.price = price
lowest180.Price = `¥${String(price)}`
lowest180.Date = date
lowest180.Difference = difference(currentPrice, price)
}
if (index < 360 && price < lowest360.price) {
lowest360.price = price
lowest360.Price = `¥${String(price)}`
lowest360.Date = date
lowest360.Difference = difference(currentPrice, price)
}
}
});
return [lowest30, lowest90, lowest180, lowest360];
}
function difference(currentPrice, price) {
let difference = sub(currentPrice, price)
if (difference == 0) {
return "-"
} else {
return `${difference > 0 ? "↑" : "↓"}${String(difference)}`
}
}
function sub(arg1, arg2) {
return add(arg1, -Number(arg2), arguments[2]);
}
function add(arg1, arg2) {
arg1 = arg1.toString(), arg2 = arg2.toString();
var arg1Arr = arg1.split("."), arg2Arr = arg2.split("."), d1 = arg1Arr.length == 2 ? arg1Arr[1] : "", d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
var maxLen = Math.max(d1.length, d2.length);
var m = Math.pow(10, maxLen);
var result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
var d = arguments[2];
return typeof d === "number" ? Number((result).toFixed(d)) : result;
}
function request_history_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=getHistoryTrend&p_url=" + encodeURIComponent(share_url)
}
$tool.post(options, function (error, response, data) {
if (!error) {
callback(JSON.parse(data));
if (consolelog) console.log("Data:\n" + data);
} else {
callback(null, null);
if (consolelog) console.log("Error:\n" + error);
}
})
}
function dateFormat(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 getSpace(length) {
let blank = "";
for (let index = 0; index < length; index++) {
blank += " ";
}
return blank;
}
function adword_obj() {
return {
"bId": "eCustom_flo_199",
"cf": {
"bgc": "#ffffff",
"spl": "empty"
},
"data": {
"ad": {
"adword": "",
"textColor": "#8C8C8C",
"color": "#f23030",
"newALContent": true,
"hasFold": true,
"class": "com.jd.app.server.warecoresoa.domain.AdWordInfo.AdWordInfo",
"adLinkContent": "",
"adLink": ""
}
},
"mId": "bpAdword",
"refId": "eAdword_0000000028",
"sortId": 13
}
}
function tool() {
const isSurge = typeof $httpClient != "undefined"
const isQuanX = typeof $task != "undefined"
const isResponse = typeof $response != "undefined"
const node = (() => {
if (typeof require == "function") {
const request = require('request')
return ({ request })
} else {
return (null)
}
})()
const notify = (title, subtitle, message) => {
if (isQuanX) $notify(title, subtitle, message)
if (isSurge) $notification.post(title, subtitle, message)
if (node) console.log(JSON.stringify({ title, subtitle, message }));
}
const write = (value, key) => {
if (isQuanX) return $prefs.setValueForKey(value, key)
if (isSurge) return $persistentStore.write(value, key)
}
const read = (key) => {
if (isQuanX) return $prefs.valueForKey(key)
if (isSurge) return $persistentStore.read(key)
}
const adapterStatus = (response) => {
if (response) {
if (response.status) {
response["statusCode"] = response.status
} else if (response.statusCode) {
response["status"] = response.statusCode
}
}
return response
}
const get = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "GET"
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) $httpClient.get(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
if (node) {
node.request(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
}
const post = (options, callback) => {
if (isQuanX) {
if (typeof options == "string") options = { url: options }
options["method"] = "POST"
$task.fetch(options).then(response => {
callback(null, adapterStatus(response), response.body)
}, reason => callback(reason.error, null, null))
}
if (isSurge) {
$httpClient.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
if (node) {
node.request.post(options, (error, response, body) => {
callback(error, adapterStatus(response), body)
})
}
}
return { isQuanX, isSurge, isResponse, notify, write, read, get, post }
}
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;
}