-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
205 lines (195 loc) · 5.74 KB
/
index.js
File metadata and controls
205 lines (195 loc) · 5.74 KB
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
const myAxios = require('./utils/myAxios')
const { userConfig, emailConfig, runConfig } = require('./config/config');
const { sendEmail } = require('./utils/sendEmail');
const { logger } = require('./utils/logger');
const { exit } = require('process');
const { getDateStr, getDateOfDay } = require('./utils/util');
const runningTask = null //运行定时器
let buyItems = [] //买到的商品列表
async function main () {
if (Object.values(userConfig).some(item => item === "")) {
logger(`请先到/config/config.js中完成所有配置`);
exit(0);
return;
}
const mode = runConfig.isLoop ? '循环执行' : `定时执行,${runConfig.maxTime}分钟后自动停止`
logger(`开始运行,[模式]${mode}`);
if (!runConfig.isLoop && runConfig.maxTime && runningTask === null) {
runningTask = setTimeout(() => {
logger(`定时执行结束`);
exit(0);
}, runConfig.maxTime * 60 * 1000);
}
// 下单流程:获取购物车商品=>获取配送时间=>下单
getCart()
}
let runcount = {
getCartCount: 0,
getCapacityDataCount: 0,
orderCount: 0
}
// 重试过多,重新开始下单流程
function isMaxCount (key) {
runcount = {
getCartCount: 0,
getCapacityDataCount: 0,
orderCount: 0
}
if (runcount[key] > runConfig.maxRunCount) {
setTimeout(() => {
main()
}, runConfig.runInterval)
return true
} else {
runcount[key]++
return false
}
}
//获取购物车信息
const getCart = async () => {
if (isMaxCount("getCartCount")) {
return
}
let url = 'https://api-sams.walmartmobile.cn/api/v1/sams/trade/cart/getUserCart';
let data = {
"uid": userConfig.uid,
"deliveryType": "0",
"deviceType": 'ios',
"storeList": userConfig.storeList,
"parentDeliveryType": 1,
"homePagelongitude": userConfig.longitude,
"homePagelatitude": userConfig.latitude
}
try {
let ret = await myAxios(url, data);
let { floorInfoList } = ret.data.data;
let { normalGoodsList, amount, quantity } = floorInfoList[0];
if (amount == 0) {
logger('【购物车】商品为空');
setTimeout(() => {
getCart();
}, runConfig.runInterval)
return;
}
logger('【购物车】商品获取成功');
logger(`【购物车】共 ${quantity} 件商品,共 ${amount / 100} 元`)
let goodsList = normalGoodsList.map(item => {
return {
"isSelected": true,
"quantity": item.quantity,
"spuId": item.spuId,
"storeId": item.storeId
}
});
//买到的商品
buyItems = normalGoodsList.map(item => item.goodsName)
getCapacityData(goodsList, amount);
} catch (e) {
console.error('【购物车】商品获取失败');
setTimeout(() => {
getCart();
}, runConfig.runInterval)
}
}
//获取配送时间
const getCapacityData = async (goodsList, amount) => {
let errorText = ""
if (isMaxCount("orderCount")) {
return
}
let url = 'https://api-sams.walmartmobile.cn/api/v1/sams/delivery/portal/getCapacityData';
let data = {
"perDateList": userConfig.perDateList,
"storeDeliveryTemplateId": userConfig.storeDeliveryTemplateId
}
try {
const ret = await myAxios(url, data);
let { capcityResponseList } = ret.data.data;
if (capcityResponseList[0].dateISFull) {
errorText = '时间已约满'
}
let time = capcityResponseList[0].list.filter(item => item.timeISFull === false)[0];
let {
startRealTime,
endRealTime,
closeDate,
startTime,
endTime
} = time;
logger(`【成功】获取配送时间:${capcityResponseList[0].strDate} ${startTime} - ${endTime}`);
logger(`【开始下单】`);
order(startRealTime, endRealTime, goodsList, amount)
} catch (error) {
logger(`【获取配送时间失败】`, errorText || error);
setTimeout(() => {
getCapacityData(goodsList, amount);
}, runConfig.runInterval)
}
}
//开始下单
const order = async (startRealTime, endRealTime, goodsList, amount) => {
if (isMaxCount("getCapacityDataCount")) {
return
}
let url = 'https://api-sams.walmartmobile.cn/api/v1/sams/trade/settlement/commitPay'
let data = {
"invoiceInfo": {},
"cartDeliveryType": 2,
"floorId": 1,
"amount": amount,
"purchaserName": "",
"tradeType": "APP",
"purchaserId": "",
"payType": 0,
"currency": "CNY",
"channel": "wechat",
"shortageId": 1,
"isSelfPickup": 0,
"orderType": 0,
"couponList": userConfig.couponList,
"uid": userConfig.uid,
"appId": "wx57364320cb03dfba",
"addressId": userConfig.addressId,
"deliveryInfoVO": {
"storeDeliveryTemplateId": userConfig.storeDeliveryTemplateId,
"deliveryModeId": "1003",
"storeType": "2"
},
"remark": "",
"storeInfo": userConfig.storeinfo,
"shortageDesc": "其他商品继续配送(缺货商品直接退款)",
"payMethodId": "1486659732",
goodsList: goodsList,
"settleDeliveryInfo": {
"expectArrivalTime": startRealTime,
"expectArrivalEndTime": endRealTime,
"deliveryType": 0
},
}
try {
let ret = await myAxios(url, data, userConfig.trackinfo);
let {
success,
msg
} = ret.data;
if (success) {
logger('【抢到菜了】')
//发送邮件
sendEmail({
title: `下单成功通知(${getDateStr()})`,
message: `山姆下单成功了,请及时付款!!!\n 买到的商品有:\n ${buyItems.join(' \n')}`
});
} else {
logger(msg);
setTimeout(() => {
order(startRealTime, endRealTime, goodsList, amount)
}, runConfig.runInterval)
}
} catch (e) {
logger('【下单失败了】')
setTimeout(() => {
order(startRealTime, endRealTime, goodsList, amount)
}, runConfig.runInterval)
}
}
main()