Skip to content

Commit

Permalink
feat: v3 (#7)
Browse files Browse the repository at this point in the history
Signed-off-by: ashing <[email protected]>
  • Loading branch information
ronething authored Sep 1, 2023
1 parent 71dad07 commit 6c98242
Show file tree
Hide file tree
Showing 14 changed files with 420 additions and 97 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Lint Check

on:
push:
branches:
- main
- release/**
pull_request:
branches:
- main
- release/**
permissions: read-all
jobs:
gofmt:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- uses: actions/checkout@v3
- name: Setup Go Environment
uses: actions/setup-go@v3
with:
go-version: '1.20.3'
- name: Run gofmt Check
working-directory: ./
run: |
diffs=`gofmt -l .`
if [[ -n $diffs ]]; then
echo "Files are not formatted by gofmt:"
echo $diffs
exit 1
fi
golint:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@v3
- name: Setup Go Environment
uses: actions/setup-go@v3
with:
go-version: '1.20.3'
- name: Install jq
run: sudo apt install -y jq
- uses: actions/setup-node@v3
with:
node-version: '16'
- name: Download golangci-lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.54.2
- name: Run Golang Linters
working-directory: ./
run: |
PATH=${PATH}:$(go env GOPATH)/bin make lint
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export GO111MODULE=on
export GOPROXY=https://goproxy.io

default: help
help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
.PHONY: help

lint: ## Apply go lint check
@golangci-lint run --timeout 10m ./...
.PHONY: lint

build: ## Build CLI for this project
@go mod tidy
@go build -o cli/showstart cli/main.go
.PHONY: build
7 changes: 0 additions & 7 deletions cli/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
# 构建脚本

export GO111MODULE=on
export GOPROXY=https://goproxy.io

.PHONY: build
build:
go mod tidy
go build -o showstart
15 changes: 8 additions & 7 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
)

var cfgFile string
var globalConfig *showstart.WapEncryptConfig
var globalConfig *showstart.WapEncryptConfigV3

//var globalClient = resty.New()

Expand Down Expand Up @@ -100,12 +100,13 @@ func initConfig() {
// If a config file is found, read it in.
if err := viper.ReadInConfig(); err == nil {
fmt.Println("使用的配置文件:", viper.ConfigFileUsed())
globalConfig = &showstart.WapEncryptConfig{
Sign: viper.GetString("sign"),
StFlpv: viper.GetString("st_flpv"),
Token: viper.GetString("token"),
UserId: viper.GetUint32("userId"),
AesKey: viper.GetString("aesKey"),
globalConfig = &showstart.WapEncryptConfigV3{
Sign: viper.GetString("sign"),
StFlpv: viper.GetString("st_flpv"),
Token: viper.GetString("token"),
UserId: viper.GetUint32("userId"),
AccessToken: viper.GetString("accessToken"),
IdToken: viper.GetString("idToken"),
}
log.Printf("globalConfig is %+v\n", globalConfig)
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
module xiudong

go 1.18
go 1.20

require (
github.com/forgoer/openssl v1.1.1
github.com/go-resty/resty/v2 v2.7.0
github.com/magiconair/properties v1.8.5
github.com/mitchellh/go-homedir v1.1.0
github.com/modood/table v0.0.0-20200225102042-88de94bb9876
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v1.3.0
github.com/spf13/viper v1.10.0
github.com/zeromicro/go-zero v1.3.2
Expand All @@ -17,7 +19,6 @@ require (
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/magiconair/properties v1.8.5 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/pelletier/go-toml v1.9.4 // indirect
github.com/smartystreets/goconvey v1.7.2 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi
github.com/pierrec/lz4 v2.6.1+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
164 changes: 98 additions & 66 deletions showstart/api.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package showstart

import (
"context"
"encoding/json"
"fmt"
"strings"

"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
"github.com/zeromicro/go-zero/core/logx"
)

Expand All @@ -16,55 +20,39 @@ type API interface {
}

type ShowStart struct {
Cookies *WapEncryptConfig
Cookies *WapEncryptConfigV3
Address *Address
Client *resty.Client
}

const OKShowstartState = "1"

func NewShowStart(cookies *WapEncryptConfig, client *resty.Client) API {
func NewShowStart(cookies *WapEncryptConfigV3, client *resty.Client) API {
if client == nil {
client = resty.New()
client = resty.New().SetRetryCount(2)
}
client.SetRetryCount(2)
return &ShowStart{Cookies: cookies, Client: client}
}

func (s *ShowStart) GetAddressList(pageNo int64) ([]Address, error) {
w, err := NewWapEncrypt(s.Cookies, &Source{
source := &SourceV3{
URL: "/wap/address/list.json",
Method: "POST",
Data: map[string]interface{}{
"pageNo": pageNo,
},
})
if err != nil {
return nil, err
}

// POST Map, default is JSON content type. No need to set one
resp, err := s.Client.R().
SetBody(w.Source.Data).
SetHeaders(w.Source.Headers).
SetResult(&AddressResp{}).
Post(w.GetRequestUrl())
var resp AddressResp
err := s.SendRequest(context.TODO(), source, &resp)
if err != nil {
logx.Errorf("GetAddress 请求发生错误: %v", err)
return nil, err
}

v, ok := resp.Result().(*AddressResp)
if !ok {
logx.Errorf("AddressResp 断言失败")
return nil, fmt.Errorf("AddressResp 断言失败")
}

logx.Infof("addressResp is %+v", v)
if v.State != OKShowstartState {
return nil, fmt.Errorf("showstart 状态码错误: %v", v.State)
}
logx.Infof("addressResp is %+v", resp)

return v.Result, nil
return resp.Result, nil
}

// GetAddress 获取用户地址列表
Expand All @@ -86,82 +74,126 @@ func (s *ShowStart) GetAddress() (*Address, error) {
func (s *ShowStart) GetCpList(pageNo int64) ([]CpItem, error) {
// 目前看来 pageNo 貌似没有特别的作用 例如传了 1 和 2 都是一样的返回结果 不知道秀动服务端如何处理的

w, err := NewWapEncrypt(s.Cookies, &Source{
source := &SourceV3{
URL: "/wap/cp/list.json",
Method: "POST",
Data: map[string]interface{}{
"pageNo": pageNo,
},
})
if err != nil {
return nil, err
}

// POST Map, default is JSON content type. No need to set one
resp, err := s.Client.R().
SetBody(w.Source.Data).
SetHeaders(w.Source.Headers).
SetResult(&CpResp{}).
Post(w.GetRequestUrl())
var resp CpResp
err := s.SendRequest(context.TODO(), source, &resp)
if err != nil {
logx.Errorf("GetCpList 请求发生错误: %v", err)
return nil, err
}

v, ok := resp.Result().(*CpResp)
if !ok {
logx.Errorf("CpResp 断言失败")
return nil, fmt.Errorf("CpResp 断言失败")
}

logx.Infof("GetCpList is %+v\n", v)
if v.State != OKShowstartState {
return nil, fmt.Errorf("showstart 状态码错误: %v", v.State)
}

return v.Result, nil
return resp.Result, nil
}

// GetTicketList 获取场次票种列表
func (s *ShowStart) GetTicketList(activityId string) ([]TicketListResult, error) {
w, err := NewWapEncrypt(s.Cookies, &Source{
source := &SourceV3{
URL: "/wap/activity/V2/ticket/list",
Method: "POST",
Data: map[string]interface{}{
"activityId": activityId,
"coupon": "", // 优惠卷 默认先为空吧
},
})
}

var resp TicketResp
err := s.SendRequest(context.TODO(), source, &resp)
if err != nil {
logx.Errorf("GetTicketList 请求发生错误: %v", err)
return nil, err
}

// POST Map, default is JSON content type. No need to set one
resp, err := s.Client.R().
SetBody(w.Source.Data).
SetHeaders(w.Source.Headers).
SetResult(&TicketResp{}).
Post(w.GetRequestUrl())
if len(resp.Result) == 0 {
return nil, fmt.Errorf("没有获取到对应场次票种列表")
}

return resp.Result, nil
}
func (s *ShowStart) GetWafToken() (*WafTokenResult, error) {
// reset
s.Cookies.AccessToken = ""
s.Cookies.IdToken = ""

source := &SourceV3{
URL: "/waf/gettoken",
Data: map[string]interface{}{
"sign": s.Cookies.Sign,
"st_flpv": s.Cookies.StFlpv,
},
}

var resp WafTokenResp
err := s.SendRequest(context.TODO(), source, &resp)
if err != nil {
logx.Errorf("GetTicketList 请求发生错误: %v", err)
logx.Errorf("GetWafToken 请求发生错误: %v\n", err)
return nil, err
}

v, ok := resp.Result().(*TicketResp)
if !ok {
logx.Errorf("TicketResp 断言失败")
return nil, fmt.Errorf("TicketResp 断言失败")
s.Cookies.AccessToken = resp.Result.AccessToken.AccessToken
s.Cookies.IdToken = resp.Result.IDToken.IDToken

return &resp.Result, nil
}

func (s *ShowStart) SendRequest(ctx context.Context, source *SourceV3, response interface{}) error {
request := s.Client.R().AddRetryCondition(func(resp *resty.Response, err error) bool {
if err != nil {
return false
}
var sr ResponseWrapper
if err = json.Unmarshal(resp.Body(), &sr); err != nil {
return false
}
// "state":"token-expire-at","sleep":0.4,"msg":"访问令牌已过期"
// check token-expire-at
if !strings.Contains(resp.Request.URL, "/waf/gettoken") && (getState(sr.State) == "token-expire-at" ||
getState(sr.State) == "token-clean-at") {
// get waf token then return true
wafTokenResp, err := s.GetWafToken()
if err != nil {
logx.Errorf("get waf token err: %v", err)
return false
}
//s.Infof("get waf token is: %+v", wafTokenResp)
s.Cookies.AccessToken = wafTokenResp.AccessToken.AccessToken
s.Cookies.IdToken = wafTokenResp.IDToken.IDToken
// regen request
w, err := NewWapEncryptV3(s.Cookies, source)
if err != nil {
logx.Errorf("retry gen encrypt v3 err: %v", err)
return false
}
// set request
resp.Request = resp.Request.SetBody(w.Source.Data).SetHeaders(w.Source.Headers)
return true
}
return false
})

w, err := NewWapEncryptV3(s.Cookies, source)
if err != nil {
return err
}

//logx.Infof("GetTicketList is %+v\n", v)
if v.State != OKShowstartState {
logx.Errorf("获取票种列表状态发生错误: %v", v.State) // 可以反应出是否是帐号的凭证过期之类
return nil, fmt.Errorf("showstart 状态码错误: %v", v.State)
resp, err := request.SetContext(ctx).SetHeaders(w.Source.Headers).SetBody(w.Source.Data).SetResult(&response).Post(w.GetRequestUrl())
if err != nil {
logx.Errorf("send request failed, resp: %v", resp.String())
return errors.Wrap(err, "failed to send post request")
}

if len(v.Result) == 0 {
return nil, fmt.Errorf("没有获取到对应场次票种列表")
var sr ResponseWrapper
if err = json.Unmarshal(resp.Body(), &sr); err != nil {
logx.Errorf("resp.Body is %v\n", resp.String())
return errors.Wrap(err, "json unmarshal")
}

return v.Result, nil
return checkState(sr.State)
}
Loading

0 comments on commit 6c98242

Please sign in to comment.