forked from yxgsir/yichahucha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jd_price_lite.js
247 lines (234 loc) · 9.03 KB
/
jd_price_lite.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
/*
README:https://github.com/yichahucha/surge/tree/master
*/
const path1 = "serverConfig";
const path2 = "wareBusiness";
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;
$done({ body: JSON.stringify(obj) });
}
if (url.indexOf(path2) != -1) {
$done({ body });
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) {
if (data.ok == 1 && data.single) {
const lower = lowerMsgs(data.single)
const detail = priceSummary(data)
const tip = data.PriceRemark.Tip + "(仅供参考)"
$tool.notify("", "", `${lower} ${tip}\n${detail}\n\n👉查看详情:http://tool.manmanbuy.com/historyLowest.aspx?url=${encodeURI(shareUrl)}`)
}
if (data.ok == 0 && data.msg.length > 0) {
$tool.notify("", "", `⚠️ ${data.msg}`)
}
}
})
}
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
listPriceDetail.pop()
let list = listPriceDetail.concat(historySummary(data.single))
list.forEach((item, index) => {
if (index == 2) {
item.Name = "双十一价格"
} else if (index == 3) {
item.Name = "六一八价格"
} else if (index == 4) {
item.Name = "三十天最低"
}
summary += `\n${item.Name} ${item.Price} ${item.Date} ${item.Difference}`
})
return summary
}
function historySummary(single) {
const rexMatch = /\[.*?\]/g;
const rexExec = /\[(.*),(.*),"(.*)"\]/;
let currentPrice, lowest60, 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
lowest60 = { 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 < 60 && price <= lowest60.price) {
lowest60.price = price
lowest60.Price = `¥${String(price)}`
lowest60.Date = date
lowest60.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 [lowest60, lowest180, lowest360];
}
function difference(currentPrice, price) {
let difference = strip(currentPrice - price)
if (difference == 0) {
return "-"
} else {
return `${difference > 0 ? "↑" : "↓"}${String(difference)}`
}
}
function strip(num, precision = 12) {
return +parseFloat(num.toPrecision(precision));
}
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 tool() {
const isSurge = typeof $httpClient != "undefined"
const isQuanX = typeof $task != "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 setCache = (value, key) => {
if (isQuanX) return $prefs.setValueForKey(value, key)
if (isSurge) return $persistentStore.write(value, key)
}
const getCache = (key) => {
if (isQuanX) return $prefs.valueForKey(key)
if (isSurge) return $persistentStore.read(key)
}
const adapterStatus = (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, notify, setCache, getCache, 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;
}