From 1900fafce625e471310b12e3d9eea0870575aba1 Mon Sep 17 00:00:00 2001 From: allen Date: Fri, 29 Apr 2022 11:42:54 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=BC=80=E6=94=BE=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E8=87=AA=E5=AE=9A=E4=B9=89=E8=AF=B7=E6=B1=82=E5=A4=B4?= =?UTF-8?q?=E5=92=8C=E8=AF=B7=E6=B1=82=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.example.yml | 9 +++---- internal/app/app.go | 8 +++--- internal/app/pkg/ddmc/session/session.go | 33 +++++++++++------------- internal/app/test/service_test.go | 21 +++++++++++---- 4 files changed, 39 insertions(+), 32 deletions(-) diff --git a/config.example.yml b/config.example.yml index 8065ea3..4e4d64d 100644 --- a/config.example.yml +++ b/config.example.yml @@ -2,10 +2,10 @@ name: 叮咚买菜助手 addr: :9999 # web服务端口号 多开需修改为其他端口号 base_concurrency: 1 # 除了提交订单的其他请求并发数, 默认为1 submit_concurrency: 2 # 最后提交订单的并发数, 默认为2 -snap_up: 3 # 抢购, 0 关闭, 1 六点抢, 2 八点半抢, 3 六点和八点半都抢 +snap_up: 0 # 抢购, 0 关闭, 1 六点抢, 2 八点半抢, 3 六点和八点半都抢 advance_time: 20 # 抢购提前进入时间, 单位:秒, 默认为20秒 pick_up_needed: false # 闲时捡漏开关, false 关闭, true 打开, 在抢购高峰期之外的时间捡漏 -monitor_needed: false # 监听开关, 监听是否有可配送时段 +monitor_needed: true # 监听开关, 监听是否有可配送时段 monitor_success_wait: 10 # 成功监听(发起捡漏或通知)之后的休息时间, 单位:分钟, 默认为10分钟 monitor_interval_min: 25 # 监听调用接口的最小时间间隔, 单位:秒, 默认为25秒 (防止被叮咚风控) monitor_interval_max: 35 # 监听调用接口的最大时间间隔, 单位:秒, 默认为35秒 @@ -17,9 +17,8 @@ headers: # 请求头 用户可以自定义, 自由增加删除参数 user-agent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.18(0x1800123f) NetType/WIFI Language/zh_CN' referer: https://servicewechat.com/wx1e113254eda17715/425/page-frame.html ddmc-app-client-id: 3 - ddmc-api-version: 9.44.0 - ddmc-build-version: 2.74.2 - ddmc-sdkversion: 2.13.2 + ddmc-api-version: 9.50.2 + ddmc-build-version: 2.85.3 ddmc-channel: undefined ddmc-os-version: undefined params: # 请求参数 用户可以自定义, 自由增加删除参数 diff --git a/internal/app/app.go b/internal/app/app.go index 1b57695..4d5f143 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -49,14 +49,14 @@ func Monitor() { if !conf.MonitorNeeded && !conf.PickUpNeeded { continue } - if isPeak() { - log.Println("当前高峰期或暂未营业") - continue - } now := time.Now() if now.Second() != 1 { continue } + if isPeak() { + log.Println("当前高峰期或暂未营业") + continue + } service.MonitorAndPickUp(cartMap) // <-time.After(time.Duration(duration) * time.Second) } diff --git a/internal/app/pkg/ddmc/session/session.go b/internal/app/pkg/ddmc/session/session.go index 3500c8d..d37da75 100644 --- a/internal/app/pkg/ddmc/session/session.go +++ b/internal/app/pkg/ddmc/session/session.go @@ -61,15 +61,15 @@ func InitializeMock() { Client: client, } - conf := config.Get() - s.UserID = conf.Mock["ddmc-uid"] + mock := config.Get().Mock + s.UserID = mock["ddmc-uid"] s.Address = address.Item{ - Id: conf.Mock["address_id"], - CityNumber: conf.Mock["ddmc-city-number"], - StationId: conf.Mock["ddmc-station-id"], + Id: mock["address_id"], + CityNumber: mock["ddmc-city-number"], + StationId: mock["ddmc-station-id"], } - longitude, _ := strconv.ParseFloat(conf.Mock["ddmc-longitude"], 64) - latitude, _ := strconv.ParseFloat(conf.Mock["ddmc-latitude"], 64) + longitude, _ := strconv.ParseFloat(mock["ddmc-longitude"], 64) + latitude, _ := strconv.ParseFloat(mock["ddmc-latitude"], 64) s.Address.Location.Location = []float64{longitude, latitude} }) } @@ -143,37 +143,35 @@ func chooseAddr() { index := textual.IndexOf(addr, options) s.Address = addrList[index] + log.Printf("Address => %#v", s.Address) log.Printf("已选择收货地址: %s %s %s", s.Address.Location.Address, s.Address.Location.Name, s.Address.AddrDetail) return } func GetHeaders() map[string]string { headers := map[string]string{ - // "accept": "application/json, text/plain, */*", // "accept-encoding": "gzip,compress,br,deflate", // 压缩可能有乱码 - // "accept-language": "zh-CN,zh-Hans;q=0.9", - // "content-type": "application/x-www-form-urlencoded", + "accept": "application/json, text/plain, */*", + "accept-language": "zh-CN,zh-Hans;q=0.9", "ddmc-city-number": s.Address.CityNumber, "ddmc-longitude": strconv.FormatFloat(s.Address.Location.Location[0], 'f', -1, 64), "ddmc-latitude": strconv.FormatFloat(s.Address.Location.Location[1], 'f', -1, 64), "ddmc-station-id": s.Address.StationId, "ddmc-uid": s.UserID, "ddmc-time": strconv.Itoa(int(time.Now().Unix())), - "ddmc-channel": "applet", - "ddmc-os-version": "[object Undefined]", + "ddmc-channel": "undefined", + "ddmc-os-version": "undefined", "ddmc-app-client-id": "3", - "ddmc-api-version": "9.44.0", - "ddmc-build-version": "2.74.2", - "ddmc-sdkversion": "2.13.2", + "ddmc-api-version": "9.50.2", + "ddmc-build-version": "2.85.3", "ddmc-ip": "", "ddmc-device-id": "", - "referer": "https://servicewechat.com/wx1e113254eda17715/425/page-frame.html", "user-agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 15_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/8.0.18(0x1800123f) NetType/WIFI Language/zh_CN", } h := config.Get().Headers // log.Printf("custom headers: %#v", h) for k, v := range h { - headers[k] = v + headers[strings.ToLower(k)] = v } return headers } @@ -195,7 +193,6 @@ func GetParams(headers map[string]string) map[string]string { "applet_source": "", "h5_source": "", "sharer_uid": "", - "device_token": "", } p := config.Get().Params for k, v := range p { diff --git a/internal/app/test/service_test.go b/internal/app/test/service_test.go index cd55125..85f9d42 100644 --- a/internal/app/test/service_test.go +++ b/internal/app/test/service_test.go @@ -5,7 +5,6 @@ import ( "time" _ "dingdong/internal/app/config" - "dingdong/internal/app/dto/reserve_time" "dingdong/internal/app/pkg/ddmc/session" "dingdong/internal/app/service" "dingdong/pkg/js" @@ -32,6 +31,7 @@ func TestGetHomeFlowDetail(t *testing.T) { list, err := service.GetHomeFlowDetail() if err != nil { t.Error(err) + return } t.Log(json.MustEncodePrettyString(list)) } @@ -40,6 +40,7 @@ func TestGetUser(t *testing.T) { user, err := session.GetUser() if err != nil { t.Error(err) + return } t.Log(user) } @@ -48,6 +49,7 @@ func TestGetAddress(t *testing.T) { list, err := session.GetAddress() if err != nil { t.Error(err) + return } t.Log(list) } @@ -56,13 +58,16 @@ func TestAllCheck(t *testing.T) { err := service.AllCheck() if err != nil { t.Error(err) + return } + t.Log("All check success") } func TestGetCart(t *testing.T) { cartMap, err := service.GetCart() if err != nil { t.Error(err) + return } t.Logf("%#v", cartMap) } @@ -70,11 +75,12 @@ func TestGetCart(t *testing.T) { func TestGetMultiReserveTime(t *testing.T) { cartMap := service.MockCartMap() now := time.Now() - _, err := service.GetMultiReserveTime(cartMap) + times, err := service.GetMultiReserveTime(cartMap) t.Logf("Millisecond => %d ms", time.Now().Sub(now).Milliseconds()) if err != nil { t.Error(err) } + t.Log(json.MustEncodePrettyString(times)) } // TestMockMultiReserveTime 模拟运力数据 @@ -84,27 +90,32 @@ func TestMockMultiReserveTime(t *testing.T) { } func TestCheckOrder(t *testing.T) { - reserveTimes := &reserve_time.GoTimes{} cartMap, err := service.GetCart() if err != nil { t.Error(err) + return } + reserveTimes := service.MockMultiReserveTime() orderMap, err := service.CheckOrder(cartMap, reserveTimes) if err != nil { t.Error(err) + return } t.Logf("%#v", orderMap) } func TestAddNewOrder(t *testing.T) { + err := service.AllCheck() + if err != nil { + t.Error(err) + return + } cartMap, err := service.GetCart() if err != nil { t.Error(err) return } - reserveTimes := service.MockMultiReserveTime() - orderMap, err := service.CheckOrder(cartMap, reserveTimes) if err != nil { t.Error(err)