Skip to content

Commit

Permalink
refactor: 开放用户自定义请求头和请求参数
Browse files Browse the repository at this point in the history
  • Loading branch information
allen committed Apr 29, 2022
1 parent 4c1060b commit 1900faf
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 32 deletions.
9 changes: 4 additions & 5 deletions config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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秒
Expand All @@ -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: # 请求参数 用户可以自定义, 自由增加删除参数
Expand Down
8 changes: 4 additions & 4 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
33 changes: 15 additions & 18 deletions internal/app/pkg/ddmc/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
})
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down
21 changes: 16 additions & 5 deletions internal/app/test/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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))
}
Expand All @@ -40,6 +40,7 @@ func TestGetUser(t *testing.T) {
user, err := session.GetUser()
if err != nil {
t.Error(err)
return
}
t.Log(user)
}
Expand All @@ -48,6 +49,7 @@ func TestGetAddress(t *testing.T) {
list, err := session.GetAddress()
if err != nil {
t.Error(err)
return
}
t.Log(list)
}
Expand All @@ -56,25 +58,29 @@ 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)
}

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 模拟运力数据
Expand All @@ -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)
Expand Down

0 comments on commit 1900faf

Please sign in to comment.