diff --git a/common/lib.go b/common/lib.go index 58aa2fe..c3a4a77 100644 --- a/common/lib.go +++ b/common/lib.go @@ -2,12 +2,19 @@ package common import ( "bytes" + "errors" + "fmt" + "github.com/CodyGuo/win" + "github.com/makiuchi-d/gozxing" + "github.com/makiuchi-d/gozxing/qrcode" + goQrcode "github.com/skip2/go-qrcode" "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform" + "image" "io/ioutil" + "log" "math/rand" "os" - "os/exec" "runtime" "strconv" "time" @@ -95,21 +102,40 @@ func Exists(path string) bool { return true } -func OpenImage(file string) { - if runtime.GOOS=="windows" { - cmd:=exec.Command("start",file) - _=cmd.Start() - }else{ - if runtime.GOOS=="linux" { - cmd:=exec.Command("eog",file) - _=cmd.Start() - }else{ - cmd:=exec.Command("open",file) - _=cmd.Start() +func OpenImage(qrPath string) { + if "windows" == runtime.GOOS { // Windows系统 + cmd := "cmd /c rundll32.exe C:\\Windows\\System32\\shimgvw.dll,ImageView_Fullscreen " + qrPath + if err := ExecRun(cmd); err != nil { + log.Println(cmd) + log.Fatal(err) } + } else { // 非Windows系统(Linux等)输出控制台 + //解码二维码 + file, _ := os.Open(qrPath) + img, _, _ := image.Decode(file) + bmp, _ := gozxing.NewBinaryBitmapFromImage(img) + qrReader := qrcode.NewQRCodeReader() + res, _ := qrReader.Decode(bmp, nil) + + //输出控制台 + qr, err := goQrcode.New(res.String(), goQrcode.High) + if err != nil { + log.Println("二维码获取成功,请打开图片用京东APP扫描") + } + fmt.Println(qr.ToSmallString(false)) } } func Hour2Unix(hour string) (time.Time, error) { return time.ParseInLocation(DateTimeFormatStr, time.Now().Format(DateFormatStr) + " " + hour, time.Local) } + +func ExecRun(cmd string) error { + lpCmdLine := win.StringToBytePtr(cmd) + ret := win.WinExec(lpCmdLine, win.SW_HIDE) + if ret <= 31 { + return errors.New(winExecError[ret]) + + } + return nil +} diff --git a/common/var.go b/common/var.go index 69a776f..d6b86b4 100644 --- a/common/var.go +++ b/common/var.go @@ -19,3 +19,12 @@ var CookieJar *httpc.CookieJar var Config *goconfig.ConfigFile var SeckillStatus chan bool + +var ( + winExecError = map[uint32]string{ + 0: "The system is out of memory or resources.", + 2: "The .exe file is invalid.", + 3: "The specified file was not found.", + 11: "The specified path was not found.", + } +) \ No newline at end of file diff --git a/go.mod b/go.mod index 2d16b81..7360825 100644 --- a/go.mod +++ b/go.mod @@ -4,12 +4,15 @@ go 1.15 require ( github.com/Albert-Zhan/httpc v0.0.0-20190712132051-aed72924b5e6 + github.com/CodyGuo/win v0.0.0-20170113125346-08e6b7208274 github.com/PuerkitoBio/goquery v1.6.0 github.com/blinkbean/dingtalk v0.0.0-20201231030509-45a553a84503 github.com/chromedp/cdproto v0.0.0-20201204063249-be40c824ad18 github.com/chromedp/chromedp v0.5.4 github.com/gobwas/httphead v0.1.0 // indirect github.com/json-iterator/go v1.1.10 // indirect + github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da + github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/spf13/cobra v1.1.1 github.com/tidwall/gjson v1.6.7 github.com/unknwon/goconfig v0.0.0-20200908083735-df7de6a44db8 diff --git a/go.sum b/go.sum index be29fa4..859305d 100644 --- a/go.sum +++ b/go.sum @@ -15,6 +15,8 @@ github.com/Albert-Zhan/httpc v0.0.0-20190712132051-aed72924b5e6 h1:/YjYHOxa/xOxb github.com/Albert-Zhan/httpc v0.0.0-20190712132051-aed72924b5e6/go.mod h1:pUw0wQemWzufQn9HP5K3VPnSBYECMOvn1JHN3bjMo9g= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= +github.com/CodyGuo/win v0.0.0-20170113125346-08e6b7208274 h1:xZ+hO1TTdeXGco1FciZz6VZEk4lCtdEtvURu6rzkIAg= +github.com/CodyGuo/win v0.0.0-20170113125346-08e6b7208274/go.mod h1:WnZcB84JFhpps84rrej/HTM4u9RiT3tvT7zYjJVS6Yk= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/PuerkitoBio/goquery v1.6.0 h1:j7taAbelrdcsOlGeMenZxc2AWXD5fieT1/znArdnx94= github.com/PuerkitoBio/goquery v1.6.0/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc= @@ -29,6 +31,8 @@ github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24 github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs= github.com/bketelsen/crypt v0.0.3-0.20200106085610-5cbc8cc4026c/go.mod h1:MKsuJmJgSg28kpZDP6UIiPt0e0Oz0kqKNGyRaWEPv84= +github.com/blinkbean/dingtalk v0.0.0-20201231030509-45a553a84503 h1:F0ELue0asB0IvriIClt4RKNGPAOYBV2LxreivenNMfE= +github.com/blinkbean/dingtalk v0.0.0-20201231030509-45a553a84503/go.mod h1:9BaLuGSBqY3vT5hstValh48DbsKO7vaHaJnG9pXwbto= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/chromedp/cdproto v0.0.0-20201009231348-1c6a710e77de h1:cuPPanKjAp5XBwrD1RkeN4ILGRSffUhS69LKkFqKtIA= github.com/chromedp/cdproto v0.0.0-20201009231348-1c6a710e77de/go.mod h1:zx0YH7hi8sqkYXAa0LZZxpQLDsU8/a2jzbYbK79dQO8= @@ -84,6 +88,7 @@ github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Z github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= github.com/google/pprof v0.0.0-20190515194954-54271f7e092f/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc= @@ -120,7 +125,10 @@ github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANyt github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= +github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs= github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.10 h1:Kz6Cvnvv2wGdaG/V8yMvfkmNiXq9Ya2KUv4rouJJr68= +github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= @@ -135,6 +143,8 @@ github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czP github.com/mailru/easyjson v0.7.1/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs= github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA= github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc= +github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da h1:OgNu1PPD9EvZckyKDAc8DA4KymNXuc6vaCLsdOGyjOE= +github.com/makiuchi-d/gozxing v0.0.0-20200903113411-25f730ed83da/go.mod h1:WoI7z45M7ZNA5BJxiJHaB+x7+k8S/3phW5Y13IR4yWY= github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU= github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4= github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= @@ -147,7 +157,11 @@ github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS4 github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY= github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= @@ -173,6 +187,8 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e h1:MRM5ITcdelLK2j1vwZ3Je0FKVCfqOLp5zO6trqMLYs0= +github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e/go.mod h1:XV66xRDqSt+GTGFMVlhk3ULuV0y9ZmzeVGR4mloJI3M= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= @@ -274,7 +290,10 @@ golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtn golang.org/x/tools v0.0.0-20190911174233-4f2ddba30aff/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191012152004-8de300cfc20a/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.0.0-20191112195655-aa38f8e97acc/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE= google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M= google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg= diff --git a/jd_seckill/user.go b/jd_seckill/user.go index 2a86d06..fe0c557 100644 --- a/jd_seckill/user.go +++ b/jd_seckill/user.go @@ -10,127 +10,130 @@ import ( "log" "net/http" "os" + "path/filepath" "strconv" "time" ) type User struct { client *httpc.HttpClient - conf *goconfig.ConfigFile + conf *goconfig.ConfigFile } -func NewUser(client *httpc.HttpClient,conf *goconfig.ConfigFile) *User { - return &User{client: client,conf:conf } +func NewUser(client *httpc.HttpClient, conf *goconfig.ConfigFile) *User { + return &User{client: client, conf: conf} } func (this *User) getUserAgent() string { - return this.conf.MustValue("config","default_user_agent","") + return this.conf.MustValue("config", "default_user_agent", "") } func (this *User) loginPage() { - req:=httpc.NewRequest(this.client) - req.SetHeader("User-Agent",this.getUserAgent()) - req.SetHeader("Connection","keep-alive") - req.SetHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3") - _,_,_=req.SetUrl("https://passport.jd.com/new/login.aspx").SetMethod("get").Send().End() + req := httpc.NewRequest(this.client) + req.SetHeader("User-Agent", this.getUserAgent()) + req.SetHeader("Connection", "keep-alive") + req.SetHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3") + _, _, _ = req.SetUrl("https://passport.jd.com/new/login.aspx").SetMethod("get").Send().End() } -func (this *User) QrLogin() (string,error) { +func (this *User) QrLogin() (string, error) { //鐧诲綍椤甸潰 this.loginPage() //浜岀淮鐮佺櫥褰 - req:=httpc.NewRequest(this.client) - req.SetHeader("User-Agent",this.getUserAgent()) - req.SetHeader("Referer","https://passport.jd.com/new/login.aspx") - resp,err:=req.SetUrl("https://qr.m.jd.com/show?appid=133&size=300&t="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().EndFile("./","qr_code.png") - if err!=nil || resp.StatusCode!=http.StatusOK { + req := httpc.NewRequest(this.client) + req.SetHeader("User-Agent", this.getUserAgent()) + req.SetHeader("Referer", "https://passport.jd.com/new/login.aspx") + resp, err := req.SetUrl("https://qr.m.jd.com/show?appid=133&size=300&t="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().EndFile("./", "qr_code.png") + if err != nil || resp.StatusCode != http.StatusOK { log.Println("鑾峰彇浜岀淮鐮佸け璐") - return "",errors.New("鑾峰彇浜岀淮鐮佸け璐") + return "", errors.New("鑾峰彇浜岀淮鐮佸け璐") } - cookies:=resp.Cookies() - wlfstkSmdl:="" - for _,cookie:= range cookies { - if cookie.Name=="wlfstk_smdl" { - wlfstkSmdl=cookie.Value + cookies := resp.Cookies() + wlfstkSmdl := "" + for _, cookie := range cookies { + if cookie.Name == "wlfstk_smdl" { + wlfstkSmdl = cookie.Value break } } log.Println("浜岀淮鐮佽幏鍙栨垚鍔燂紝璇锋墦寮浜笢APP鎵弿") - dir,_:=os.Getwd() - common.OpenImage(dir+"/qr_code.png") - return wlfstkSmdl,nil + dir, _ := os.Getwd() + qrPath := filepath.Join(dir, `./qr_code.png`) + common.OpenImage(qrPath) + + return wlfstkSmdl, nil } -func (this *User) QrcodeTicket(wlfstkSmdl string) (string,error) { - req:=httpc.NewRequest(this.client) - req.SetHeader("User-Agent",this.getUserAgent()) - req.SetHeader("Referer","https://passport.jd.com/new/login.aspx") - resp,body,err:=req.SetUrl("https://qr.m.jd.com/check?appid=133&callback=jQuery"+strconv.Itoa(common.Rand(1000000,9999999))+"&token="+wlfstkSmdl+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End() - if err!=nil || resp.StatusCode!=http.StatusOK { +func (this *User) QrcodeTicket(wlfstkSmdl string) (string, error) { + req := httpc.NewRequest(this.client) + req.SetHeader("User-Agent", this.getUserAgent()) + req.SetHeader("Referer", "https://passport.jd.com/new/login.aspx") + resp, body, err := req.SetUrl("https://qr.m.jd.com/check?appid=133&callback=jQuery" + strconv.Itoa(common.Rand(1000000, 9999999)) + "&token=" + wlfstkSmdl + "&_=" + strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End() + if err != nil || resp.StatusCode != http.StatusOK { log.Println("鑾峰彇浜岀淮鐮佹壂鎻忕粨鏋滃紓甯") - return "",errors.New("鑾峰彇浜岀淮鐮佹壂鎻忕粨鏋滃紓甯") + return "", errors.New("鑾峰彇浜岀淮鐮佹壂鎻忕粨鏋滃紓甯") } - if gjson.Get(body,"code").Int()!=200 { - log.Printf("Code: %s, Message: %s",gjson.Get(body,"code").String(),gjson.Get(body,"msg").String()) - return "",errors.New(fmt.Sprintf("Code: %s, Message: %s",gjson.Get(body,"code").String(),gjson.Get(body,"msg").String())) + if gjson.Get(body, "code").Int() != 200 { + log.Printf("Code: %s, Message: %s", gjson.Get(body, "code").String(), gjson.Get(body, "msg").String()) + return "", errors.New(fmt.Sprintf("Code: %s, Message: %s", gjson.Get(body, "code").String(), gjson.Get(body, "msg").String())) } log.Println("宸插畬鎴愭墜鏈哄鎴风纭") - return gjson.Get(body,"ticket").String(),nil + return gjson.Get(body, "ticket").String(), nil } -func (this *User) TicketInfo(ticket string) (string,error) { - req:=httpc.NewRequest(this.client) - req.SetHeader("User-Agent",this.getUserAgent()) - req.SetHeader("Referer","https://passport.jd.com/uc/login?ltype=logout") - resp,body,err:=req.SetUrl("https://passport.jd.com/uc/qrCodeTicketValidation?t="+ticket).SetMethod("get").Send().End() - if err!=nil || resp.StatusCode!=http.StatusOK { +func (this *User) TicketInfo(ticket string) (string, error) { + req := httpc.NewRequest(this.client) + req.SetHeader("User-Agent", this.getUserAgent()) + req.SetHeader("Referer", "https://passport.jd.com/uc/login?ltype=logout") + resp, body, err := req.SetUrl("https://passport.jd.com/uc/qrCodeTicketValidation?t=" + ticket).SetMethod("get").Send().End() + if err != nil || resp.StatusCode != http.StatusOK { log.Println("浜岀淮鐮佷俊鎭牎楠屽け璐") - return "",errors.New("浜岀淮鐮佷俊鎭牎楠屽け璐") + return "", errors.New("浜岀淮鐮佷俊鎭牎楠屽け璐") } - if gjson.Get(body,"returnCode").Int()==0 { + if gjson.Get(body, "returnCode").Int() == 0 { log.Println("浜岀淮鐮佷俊鎭牎楠屾垚鍔") - return "",nil - }else{ + return "", nil + } else { log.Println("浜岀淮鐮佷俊鎭牎楠屽け璐") - return "",errors.New("浜岀淮鐮佷俊鎭牎楠屽け璐") + return "", errors.New("浜岀淮鐮佷俊鎭牎楠屽け璐") } } func (this *User) RefreshStatus() error { - client:=httpc.NewHttpClient() + client := httpc.NewHttpClient() client.SetCookieJar(common.CookieJar) client.SetRedirect(func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse }) - req:=httpc.NewRequest(client) - req.SetHeader("User-Agent",this.getUserAgent()) - resp,_,err:=req.SetUrl("https://order.jd.com/center/list.action?rid="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End() - if err==nil && resp.StatusCode==http.StatusOK { + req := httpc.NewRequest(client) + req.SetHeader("User-Agent", this.getUserAgent()) + resp, _, err := req.SetUrl("https://order.jd.com/center/list.action?rid=" + strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End() + if err == nil && resp.StatusCode == http.StatusOK { return nil - }else{ + } else { return errors.New("鐧诲綍澶辨晥") } } -func (this *User) GetUserInfo() (string,error) { - req:=httpc.NewRequest(this.client) - req.SetHeader("User-Agent",this.getUserAgent()) - req.SetHeader("Referer","https://order.jd.com/center/list.action") - errorCount:=5 - nickName:="" - for { - if errorCount>0 { - _,body,_:=req.SetUrl("https://passport.jd.com/user/petName/getUserInfoForMiniJd.action?callback="+strconv.Itoa(common.Rand(1000000,9999999))+"&_="+strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End() - if gjson.Get(body,"nickName").Exists() { - nickName=gjson.Get(body,"nickName").String() +func (this *User) GetUserInfo() (string, error) { + req := httpc.NewRequest(this.client) + req.SetHeader("User-Agent", this.getUserAgent()) + req.SetHeader("Referer", "https://order.jd.com/center/list.action") + errorCount := 5 + nickName := "" + for { + if errorCount > 0 { + _, body, _ := req.SetUrl("https://passport.jd.com/user/petName/getUserInfoForMiniJd.action?callback=" + strconv.Itoa(common.Rand(1000000, 9999999)) + "&_=" + strconv.Itoa(int(time.Now().Unix()*1000))).SetMethod("get").Send().End() + if gjson.Get(body, "nickName").Exists() { + nickName = gjson.Get(body, "nickName").String() break } - errorCount=errorCount-1 - time.Sleep(300*time.Millisecond) - }else{ + errorCount = errorCount - 1 + time.Sleep(300 * time.Millisecond) + } else { break } } - b,_:=common.GbkToUtf8([]byte(nickName)) + b, _ := common.GbkToUtf8([]byte(nickName)) return string(b), nil -} \ No newline at end of file +}