瀏覽代碼

二维码打开优化

main
ztino 4 年前
父節點
當前提交
768276bb0b
  1. 32
      common/lib.go
  2. 2
      go.mod
  3. 4
      jd_seckill/user.go

32
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))
}
}

2
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

4
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
}

載入中…
取消
儲存