This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
234 lines (203 loc) · 5.21 KB
/
main.go
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
package main
import (
"context"
"encoding/json"
"flag"
"sync"
"time"
"food/config"
"food/dd"
"food/logger"
"food/tasks"
)
// 捡漏用
var (
cartSleepTime = time.Millisecond * 3000
orderSleepTime = time.Millisecond * 3000
reserveSleepTime = time.Millisecond * 3000
refreshSleepTime = time.Millisecond * 5000
)
// 抢购用
// var (
// cartSleepTime = time.Millisecond * 100
// orderSleepTime = time.Millisecond * 100
// reserveSleepTime = time.Millisecond * 100
// refreshSleepTime = time.Millisecond * 2000
// )
var (
app *tasks.Task
session dd.DingdongSession
reserveTimeList []dd.ReserveTime
)
var (
conf config.Conf
confName string
)
func init() {
flag.StringVar(&confName, "conf", "", "")
}
func main() {
logger.Init()
flag.Parse()
if confName == "" {
logger.Log.Error("conf name is empty")
return
}
if _, err := conf.GetConf(confName); err != nil {
logger.Log.Error(err)
return
}
app = tasks.NewTask(context.Background())
session = dd.DingdongSession{}
if err := session.InitSession(dd.CommonHeader{
Cookie: conf.Cookie,
Uid: conf.Uid,
DeviceId: conf.DeviceId,
Longitude: conf.Longitude,
Latitude: conf.Latitude,
}, conf.BarkId); err != nil {
logger.Log.Error(err)
return
}
start()
}
func start() {
var task *tasks.Task
var cartHash string
cartTime := time.Now()
for {
select {
case <-app.Ctx.Done():
logger.Log.Info("抢购完毕,程序退出")
return
default:
if task != nil && time.Since(cartTime) < refreshSleepTime {
time.Sleep(time.Millisecond * 50)
continue
}
cartTime = time.Now()
if err := session.CheckCart(); err != nil {
logger.Log.Warnf("获取购物车失败 %s", err)
time.Sleep(cartSleepTime)
continue
}
if len(session.Cart.ProdList) == 0 {
logger.Log.Error("购物车中无有效商品,请先前往app添加或勾选!")
app.Cancel()
return
}
session.Order.Products = session.Cart.ProdList
hash, _ := json.Marshal(session.Cart)
// 直接字符串比较吧,懒得hash了
if string(hash) != cartHash {
if task != nil {
task.Cancel()
}
if cartHash != "" {
logger.Log.Info("--- 购物车发生变化,执行新的任务 ---")
}
for index, prod := range session.Cart.ProdList {
logger.Log.Infof("[%v] %s 数量:%v 总价:%s", index, prod.ProductName, prod.Count, prod.TotalPrice)
}
cartHash = string(hash)
task = tasks.NewTask(context.TODO())
go getReserveTime(task)
go createOrder(task)
}
}
}
}
func getReserveTime(task *tasks.Task) {
for {
select {
case <-task.Ctx.Done():
return
default:
logger.Log.Info("获取可预约时间")
err, list := session.GetMultiReserveTime()
if err != nil {
logger.Log.Warnf("获取预约时间失败 %s", err)
time.Sleep(reserveSleepTime)
} else {
reserveTimeList = list
count := len(reserveTimeList)
logger.Log.Infof("共 %v 个时间段可预约", count)
// 请求成功后续就不用请求那么频繁了
time.Sleep(refreshSleepTime)
}
}
}
}
func createOrder(task *tasks.Task) {
// 这里直接使用全局的可预约时间,大概率不需要考虑商品发生变化后可预约时间也发生变化了
checkOrderSuccess := false
for {
select {
case <-task.Ctx.Done():
return
default:
if !checkOrderSuccess {
logger.Log.Infof("获取订单金额中...")
err := session.CheckOrder()
if err != nil {
logger.Log.Warnf("获取订单金额失败 %s", err)
time.Sleep(orderSleepTime)
continue
}
logger.Log.Infof("订单总金额:%v", session.Order.Price)
checkOrderSuccess = true
session.GeneratePackageOrder()
}
// 并发!!同时向所有未约满的时间提交
// 疫情期间大概每天4个时间段可以选,非疫情就多了,不能这么玩咯
if len(reserveTimeList) > 0 {
createTask := tasks.NewTask(context.TODO())
createSuccess := false
hasProdErr := false
go func() {
wg := sync.WaitGroup{}
for _, reserveTime := range reserveTimeList {
wg.Add(1)
go func(reserveTime dd.ReserveTime, s dd.DingdongSession) {
select {
case <-createTask.Ctx.Done():
wg.Done()
default:
logger.Log.Infof("提交订单中 预约时间 %s", reserveTime.SelectMsg)
s.UpdatePackageOrder(reserveTime)
err := s.AddNewOrder()
switch err {
case nil:
createTask.Cancel()
createSuccess = true
case dd.TimeExpireErr:
hasProdErr = true
logger.Log.Warnf("下单失败 预约时间%s %s", reserveTime.SelectMsg, err)
case dd.ProdInfoErr:
logger.Log.Warnf("下单失败 预约时间%s %s", reserveTime.SelectMsg, err)
default:
logger.Log.Warnf("下单失败 预约时间%s %s", reserveTime.SelectMsg, err)
}
wg.Done()
}
}(reserveTime, session)
}
wg.Wait()
createTask.Cancel()
}()
<-createTask.Ctx.Done()
if createSuccess {
logger.Log.Infof("抢购成功,请前往app付款!")
task.Cancel()
app.Cancel()
return
}
if hasProdErr {
task.Cancel()
return
}
time.Sleep(orderSleepTime)
}
}
}
}