@@ -28,42 +28,42 @@ import (
28
28
var ErrEmptyData = errors .New ("空数据" )
29
29
30
30
type jdSecKill struct {
31
- ctx context.Context
32
- cancel context.CancelFunc
33
- bCtx context.Context
34
- bWorksCtx []context.Context
35
- isLogin bool
36
- isClose bool
37
- mu sync.Mutex
38
- userAgent string
39
- UserInfo gjson.Result
40
- SkuId string
41
- SecKillUrl string
42
- SecKillNum int
31
+ ctx context.Context
32
+ cancel context.CancelFunc
33
+ bCtx context.Context
34
+ bWorksCtx []context.Context
35
+ isLogin bool
36
+ isClose bool
37
+ mu sync.Mutex
38
+ userAgent string
39
+ UserInfo gjson.Result
40
+ SkuId string
41
+ SecKillUrl string
42
+ SecKillNum int
43
43
SecKillInfo gjson.Result
44
- eid string
45
- fp string
46
- Works int
47
- IsOkChan chan struct {}
48
- IsOk bool
49
- StartTime time.Time
50
- DiffTime int64
44
+ eid string
45
+ fp string
46
+ Works int
47
+ IsOkChan chan struct {}
48
+ IsOk bool
49
+ StartTime time.Time
50
+ DiffTime int64
51
51
}
52
52
53
53
func NewJdSecKill (execPath string , skuId string , num , works int ) * jdSecKill {
54
54
if works < 0 {
55
55
works = 1
56
56
}
57
- jsk := & jdSecKill {
58
- ctx : nil ,
59
- isLogin : false ,
60
- isClose : false ,
61
- userAgent :chromedpEngine .GetRandUserAgent (),
62
- SkuId :skuId ,
63
- SecKillNum :num ,
64
- Works :works ,
65
- IsOk :false ,
66
- IsOkChan :make (chan struct {}, 1 ),
57
+ jsk := & jdSecKill {
58
+ ctx : nil ,
59
+ isLogin : false ,
60
+ isClose : false ,
61
+ userAgent : chromedpEngine .GetRandUserAgent (),
62
+ SkuId : skuId ,
63
+ SecKillNum : num ,
64
+ Works : works ,
65
+ IsOk : false ,
66
+ IsOkChan : make (chan struct {}, 1 ),
67
67
}
68
68
jsk .ctx , jsk .cancel = chromedpEngine .NewExecCtx (chromedp .ExecPath (execPath ), chromedp .UserAgent (jsk .userAgent ))
69
69
return jsk
@@ -118,7 +118,7 @@ func (jsk *jdSecKill) GetReq(reqUrl string, params map[string]string, referer st
118
118
defer resp .Body .Close ()
119
119
b , _ := ioutil .ReadAll (resp .Body )
120
120
logs .PrintlnSuccess ("Get请求接口:" , req .URL )
121
- // logs.PrintlnSuccess(string(b))
121
+ // logs.PrintlnSuccess(string(b))
122
122
logs .PrintlnInfo ("=======================" )
123
123
r := FormatJdResponse (b , req .URL .String (), false )
124
124
if r .Raw == "null" || r .Raw == "" {
@@ -223,13 +223,13 @@ func (jsk *jdSecKill) Run() error {
223
223
logs .PrintlnInfo ("等待登陆......" )
224
224
for {
225
225
select {
226
- case <- jsk .ctx .Done ():
227
- logs .PrintErr ("浏览器被关闭,退出进程" )
228
- return nil
229
- case <- jsk .bCtx .Done ():
230
- logs .PrintErr ("浏览器被关闭,退出进程" )
231
- return nil
232
- default :
226
+ case <- jsk .ctx .Done ():
227
+ logs .PrintErr ("浏览器被关闭,退出进程" )
228
+ return nil
229
+ case <- jsk .bCtx .Done ():
230
+ logs .PrintErr ("浏览器被关闭,退出进程" )
231
+ return nil
232
+ default :
233
233
}
234
234
if jsk .isLogin {
235
235
logs .PrintlnSuccess (jsk .UserInfo .Get ("realName" ).String () + ", 登陆成功........" )
@@ -254,7 +254,7 @@ func (jsk *jdSecKill) Run() error {
254
254
logs .PrintErr ("抢购连接访问错误,正在重试:" , err )
255
255
}
256
256
//_, _, _, _ = page.Navigate(jsk.SecKillUrl).WithReferrer("https://item.jd.com/"+jsk.SkuId+".html").Do(ctx2)
257
- SecKillRE:
257
+ SecKillRE:
258
258
//请求抢购连接,提交订单
259
259
err := jsk .ReqSubmitSecKillOrder (ctx2 )
260
260
if err != nil {
@@ -265,11 +265,11 @@ func (jsk *jdSecKill) Run() error {
265
265
}(v )
266
266
}
267
267
select {
268
- case <- jsk .IsOkChan :
269
- logs .PrintlnInfo ("抢购成功。。。10s后关闭进程..." )
270
- _ = chromedp .Sleep (10 * time .Second ).Do (ctx )
271
- case <- jsk .ctx .Done ():
272
- case <- jsk .bCtx .Done ():
268
+ case <- jsk .IsOkChan :
269
+ logs .PrintlnInfo ("抢购成功。。。10s后关闭进程..." )
270
+ _ = chromedp .Sleep (10 * time .Second ).Do (ctx )
271
+ case <- jsk .ctx .Done ():
272
+ case <- jsk .bCtx .Done ():
273
273
}
274
274
return nil
275
275
}),
@@ -278,7 +278,7 @@ func (jsk *jdSecKill) Run() error {
278
278
279
279
func (jsk * jdSecKill ) WaitStart () chromedp.ActionFunc {
280
280
return func (ctx context.Context ) error {
281
- u := "https://item.jd.com/" + jsk .SkuId + ".html"
281
+ u := "https://item.jd.com/" + jsk .SkuId + ".html"
282
282
for i := 0 ; i < jsk .Works ; i ++ {
283
283
go func () {
284
284
tid , err := target .CreateTarget (u ).Do (ctx )
@@ -298,7 +298,7 @@ func (jsk *jdSecKill) WaitStart() chromedp.ActionFunc {
298
298
}
299
299
_ = chromedp .Navigate (u ).Do (ctx )
300
300
st := jsk .StartTime .UnixNano () / 1e6
301
- logs .PrintlnInfo ("等待时间到达" + jsk .StartTime .Format (global .DateTimeFormatStr )+ "...... 请勿关闭浏览器" )
301
+ logs .PrintlnInfo ("等待时间到达" + jsk .StartTime .Format (global .DateTimeFormatStr ) + "...... 请勿关闭浏览器" )
302
302
for {
303
303
select {
304
304
case <- jsk .ctx .Done ():
@@ -309,7 +309,7 @@ func (jsk *jdSecKill) WaitStart() chromedp.ActionFunc {
309
309
return nil
310
310
default :
311
311
}
312
- if global .UnixMilli () - jsk .DiffTime >= st {
312
+ if global .UnixMilli ()- jsk .DiffTime >= st {
313
313
logs .PrintlnInfo ("时间到达。。。。开始执行" )
314
314
break
315
315
}
@@ -326,7 +326,7 @@ func (jsk *jdSecKill) GetEidAndFp() chromedp.ActionFunc {
326
326
logs .PrintlnInfo ("eid : " , jsk .eid , "fp : " , jsk .fp )
327
327
return nil
328
328
}
329
- RE:
329
+ RE:
330
330
logs .PrintlnInfo ("正在获取eid和fp参数...." )
331
331
_ = chromedp .Navigate ("https://search.jd.com/Search?keyword=衣服" ).Do (ctx )
332
332
logs .PrintlnInfo ("等待页面更新完成...." )
@@ -338,14 +338,14 @@ func (jsk *jdSecKill) GetEidAndFp() chromedp.ActionFunc {
338
338
}
339
339
n := itemNodes [rand .Intn (len (itemNodes ))]
340
340
_ = dom .ScrollIntoViewIfNeeded ().WithNodeID (n .NodeID ).Do (ctx )
341
- _ , _ , _ , _ = page .Navigate ("https://item.jd.com/" + n .AttributeValue ("data-sku" )+ ".html" ).Do (ctx )
341
+ _ , _ , _ , _ = page .Navigate ("https://item.jd.com/" + n .AttributeValue ("data-sku" ) + ".html" ).Do (ctx )
342
342
343
343
logs .PrintlnInfo ("等待商品详情页更新完成...." )
344
344
_ = chromedp .WaitVisible ("#InitCartUrl" ).Do (ctx )
345
- _ = chromedp .Sleep (1 * time .Second ).Do (ctx );
345
+ _ = chromedp .Sleep (1 * time .Second ).Do (ctx )
346
346
_ = chromedp .Click ("#InitCartUrl" ).Do (ctx )
347
347
_ = chromedp .WaitVisible ("#GotoShoppingCart" ).Do (ctx )
348
- _ = chromedp .Sleep (1 * time .Second ).Do (ctx );
348
+ _ = chromedp .Sleep (1 * time .Second ).Do (ctx )
349
349
_ = chromedp .Click ("#GotoShoppingCart" ).Do (ctx )
350
350
//_ = chromedp.Navigate("https://cart.jd.com/cart_index/").Do(ctx)
351
351
_ = chromedp .WaitVisible ("#cart-body" ).Do (ctx )
@@ -373,7 +373,7 @@ func (jsk *jdSecKill) GetEidAndFp() chromedp.ActionFunc {
373
373
logs .PrintlnWarning ("获取参数失败,等待重试。。。 重试过程过久可手动刷新浏览器" )
374
374
goto RE
375
375
}
376
- logs .PrintlnInfo ("参数获取成功:eid【" + jsk .eid + "】, fp【" + jsk .fp + "】" )
376
+ logs .PrintlnInfo ("参数获取成功:eid【" + jsk .eid + "】, fp【" + jsk .fp + "】" )
377
377
378
378
return nil
379
379
}
@@ -428,7 +428,7 @@ func (jsk *jdSecKill) ReqSubmitSecKillOrder(ctx context.Context) error {
428
428
logs .PrintlnInfo ("提交抢购订单............." )
429
429
430
430
submitCount := 1
431
- RE:
431
+ RE:
432
432
r , err := jsk .PostReq ("https://marathon.jd.com/seckillnew/orderService/pc/submitOrder.action?skuId=" + jsk .SkuId + "" , orderData , skUrl , ctx , false )
433
433
if err != nil {
434
434
if submitCount < 10 {
@@ -475,40 +475,39 @@ func (jsk *jdSecKill) GetOrderReqData() url.Values {
475
475
}
476
476
invoiceInfo := jsk .SecKillInfo .Get ("invoiceInfo" )
477
477
r := url.Values {
478
- "skuId" :[]string {jsk .SkuId },
479
- "num" :[]string {strconv .Itoa (jsk .SecKillNum )},
480
- "addressId" :[]string {defaultAddress .Get ("id" ).String ()},
481
- "yuShou" :[]string {"true" },
482
- "isModifyAddress" :[]string {"false" },
483
- "name" :[]string {defaultAddress .Get ("name" ).String ()},
484
- "provinceId" :[]string {defaultAddress .Get ("provinceId" ).String ()},
485
- "cityId" :[]string {defaultAddress .Get ("cityId" ).String ()},
486
- "countyId" :[]string {defaultAddress .Get ("countyId" ).String ()},
487
- "townId" :[]string {defaultAddress .Get ("townId" ).String ()},
488
- "addressDetail" :[]string {defaultAddress .Get ("addressDetail" ).String ()},
489
- "mobile" :[]string {defaultAddress .Get ("mobile" ).String ()},
490
- "mobileKey" :[]string {defaultAddress .Get ("mobileKey" ).String ()},
491
- "email" :[]string {defaultAddress .Get ("email" ).String ()},
492
- "postCode" :[]string {"" },
493
- "invoiceTitle" :[]string {"" },
494
- "invoiceCompanyName" :[]string {"" },
495
- "invoiceContent" :[]string {},
496
- "invoiceTaxpayerNO" :[]string {"" },
497
- "invoiceEmail" :[]string {"" },
498
- "invoicePhone" :[]string {invoiceInfo .Get ("invoicePhone" ).String ()},
499
- "invoicePhoneKey" :[]string {invoiceInfo .Get ("invoicePhoneKey" ).String ()},
500
- "invoice" :[]string {"true" },
501
- "password" :[]string {"" },
502
- "codTimeType" :[]string {"3" },
503
- "paymentType" :[]string {"4" },
504
- "areaCode" :[]string {"" },
505
- "overseas" :[]string {"0" },
506
- "phone" :[]string {"" },
507
- "eid" :[]string {jsk .eid },
508
- "fp" :[]string {jsk .fp },
509
- "token" :[]string {jsk .SecKillInfo .Get ("token" ).String ()},
510
- "pru" :[]string {"" },
511
-
478
+ "skuId" : []string {jsk .SkuId },
479
+ "num" : []string {strconv .Itoa (jsk .SecKillNum )},
480
+ "addressId" : []string {defaultAddress .Get ("id" ).String ()},
481
+ "yuShou" : []string {"true" },
482
+ "isModifyAddress" : []string {"false" },
483
+ "name" : []string {defaultAddress .Get ("name" ).String ()},
484
+ "provinceId" : []string {defaultAddress .Get ("provinceId" ).String ()},
485
+ "cityId" : []string {defaultAddress .Get ("cityId" ).String ()},
486
+ "countyId" : []string {defaultAddress .Get ("countyId" ).String ()},
487
+ "townId" : []string {defaultAddress .Get ("townId" ).String ()},
488
+ "addressDetail" : []string {defaultAddress .Get ("addressDetail" ).String ()},
489
+ "mobile" : []string {defaultAddress .Get ("mobile" ).String ()},
490
+ "mobileKey" : []string {defaultAddress .Get ("mobileKey" ).String ()},
491
+ "email" : []string {defaultAddress .Get ("email" ).String ()},
492
+ "postCode" : []string {"" },
493
+ "invoiceTitle" : []string {"" },
494
+ "invoiceCompanyName" : []string {"" },
495
+ "invoiceContent" : []string {},
496
+ "invoiceTaxpayerNO" : []string {"" },
497
+ "invoiceEmail" : []string {"" },
498
+ "invoicePhone" : []string {invoiceInfo .Get ("invoicePhone" ).String ()},
499
+ "invoicePhoneKey" : []string {invoiceInfo .Get ("invoicePhoneKey" ).String ()},
500
+ "invoice" : []string {"true" },
501
+ "password" : []string {"" },
502
+ "codTimeType" : []string {"3" },
503
+ "paymentType" : []string {"4" },
504
+ "areaCode" : []string {"" },
505
+ "overseas" : []string {"0" },
506
+ "phone" : []string {"" },
507
+ "eid" : []string {jsk .eid },
508
+ "fp" : []string {jsk .fp },
509
+ "token" : []string {jsk .SecKillInfo .Get ("token" ).String ()},
510
+ "pru" : []string {"" },
512
511
}
513
512
514
513
if invoiceInfo .Raw == "" {
@@ -535,10 +534,10 @@ func (jsk *jdSecKill) GetOrderReqData() url.Values {
535
534
536
535
func (jsk * jdSecKill ) GetSecKillInitInfo (ctx context.Context ) error {
537
536
r , err := jsk .PostReq ("https://marathon.jd.com/seckillnew/orderService/pc/init.action" , url.Values {
538
- "sku" :[]string {jsk .SkuId },
539
- "num" :[]string {strconv .Itoa (jsk .SecKillNum )},
540
- "isModifyAddress" :[]string {"false" },
541
- }, fmt .Sprintf ("https://marathon.jd.com/seckill/seckill.action?=skuId=100012043978 &num=2 &rid=%d" , time .Now ().Unix ()), ctx , false )
537
+ "sku" : []string {jsk .SkuId },
538
+ "num" : []string {strconv .Itoa (jsk .SecKillNum )},
539
+ "isModifyAddress" : []string {"false" },
540
+ }, fmt .Sprintf ("https://marathon.jd.com/seckill/seckill.action?=skuId=%s &num=%d &rid=%d" , jsk . SkuId , jsk . SecKillNum , time .Now ().Unix ()), ctx , false )
542
541
if err != nil {
543
542
return err
544
543
}
@@ -552,10 +551,10 @@ func (jsk *jdSecKill) GetSecKillUrl() string {
552
551
req .Header .Add ("User-Agent" , jsk .userAgent )
553
552
req .Header .Add ("Referer" , "https://item.jd.com/" + jsk .SkuId + ".html" )
554
553
r , _ := jsk .GetReq ("https://itemko.jd.com/itemShowBtn" , map [string ]string {
555
- "callback" :"jQuery" + strconv .FormatInt (global .GenerateRangeNum (1000000 , 9999999 ), 10 ),
556
- "skuId" :jsk .SkuId ,
557
- "from" :"pc" ,
558
- "_" : strconv .FormatInt (time .Now ().Unix () * 1000 , 10 ),
554
+ "callback" : "jQuery" + strconv .FormatInt (global .GenerateRangeNum (1000000 , 9999999 ), 10 ),
555
+ "skuId" : jsk .SkuId ,
556
+ "from" : "pc" ,
557
+ "_" : strconv .FormatInt (time .Now ().Unix ()* 1000 , 10 ),
559
558
}, "https://item.jd.com/" + jsk .SkuId + ".html" , nil , false )
560
559
return r .Get ("url" ).String ()
561
560
}
0 commit comments