From 768276bb0b68dba932a67426bbfd784e876ac7df Mon Sep 17 00:00:00 2001 From: ztino <> Date: Tue, 5 Jan 2021 23:16:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BA=8C=E7=BB=B4=E7=A0=81=E6=89=93=E5=BC=80?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- common/lib.go | 32 +++++++++++++++++++++----------- go.mod | 2 ++ jd_seckill/user.go | 4 +++- 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/common/lib.go b/common/lib.go index 74a0b3c..ddbcfe0 100644 --- a/common/lib.go +++ b/common/lib.go @@ -2,8 +2,13 @@ package common import ( "bytes" + "fmt" + "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" "math/rand" "os" @@ -95,17 +100,22 @@ func Exists(path string) bool { return true } -func OpenImage(file string) { - if runtime.GOOS == "windows" { - cmd := exec.Command("cmd", "/k", "start", file) +func OpenImage(qrPath string) { + if runtime.GOOS == "windows" {//windows + cmd := exec.Command("cmd", "/k", "start", qrPath) _ = cmd.Start() - } else { - if runtime.GOOS == "linux" { - cmd := exec.Command("eog", file) - _ = cmd.Start() - } else { - cmd := exec.Command("open", file) - _ = cmd.Start() - } + }else if runtime.GOOS == "darwin" {//Macos + cmd := exec.Command("open", qrPath) + _ = cmd.Start() + }else{ + //linux或者其他系统 + file, _ := os.Open(qrPath) + img, _, _ := image.Decode(file) + bmp, _ := gozxing.NewBinaryBitmapFromImage(img) + qrReader := qrcode.NewQRCodeReader() + res, _ := qrReader.Decode(bmp, nil) + //输出控制台 + qr, _ := goQrcode.New(res.String(), goQrcode.High) + fmt.Println(qr.ToSmallString(false)) } } diff --git a/go.mod b/go.mod index 349d92c..160049a 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,8 @@ require ( 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/jd_seckill/user.go b/jd_seckill/user.go index 2a86d06..3ba429e 100644 --- a/jd_seckill/user.go +++ b/jd_seckill/user.go @@ -10,6 +10,7 @@ import ( "log" "net/http" "os" + "path/filepath" "strconv" "time" ) @@ -57,7 +58,8 @@ func (this *User) QrLogin() (string,error) { } log.Println("二维码获取成功,请打开京东APP扫描") dir,_:=os.Getwd() - common.OpenImage(dir+"/qr_code.png") + qrPath := filepath.Join(dir, `./qr_code.png`) + common.OpenImage(qrPath) return wlfstkSmdl,nil }