瀏覽代碼

更新

tags/v1.6.1^2
huangjiajun 1 年之前
父節點
當前提交
80a0e5836b
共有 6 個檔案被更改,包括 210 行新增1 行删除
  1. +2
    -0
      go.mod
  2. +8
    -1
      lib/local_wechat/api.go
  3. +33
    -0
      utils/qrcode/decodeFile.go
  4. +43
    -0
      utils/qrcode/getBase64.go
  5. +85
    -0
      utils/qrcode/saveFile.go
  6. +39
    -0
      utils/qrcode/writeWeb.go

+ 2
- 0
go.mod 查看文件

@@ -3,12 +3,14 @@ module code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git
go 1.15

require (
github.com/boombuler/barcode v1.0.1
github.com/forgoer/openssl v1.2.1
github.com/gin-gonic/gin v1.8.0
github.com/go-redis/redis v6.15.9+incompatible
github.com/go-sql-driver/mysql v1.6.0
github.com/gomodule/redigo v1.8.8
github.com/iGoogle-ink/gopay v1.5.36
github.com/makiuchi-d/gozxing v0.1.1
github.com/pkg/errors v0.9.1
github.com/shopspring/decimal v1.2.0
github.com/syyongx/php2go v0.9.7


+ 8
- 1
lib/local_wechat/api.go 查看文件

@@ -3,12 +3,14 @@ package local_wxpay
import (
zhios_pay_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils"
"code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils/logx"
"code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/utils/qrcode"
"fmt"
"github.com/iGoogle-ink/gopay"
"github.com/iGoogle-ink/gopay/pkg/util"
"github.com/iGoogle-ink/gopay/wechat"
v3 "github.com/iGoogle-ink/gopay/wechat/v3"
"strconv"
"strings"
"time"
)

@@ -142,8 +144,13 @@ func TradePcPay(client *wechat.Client, subject, orderID, amount, notifyUrl, prod
_ = zhios_pay_logx.Warn(err)
return nil, err
}
QRcode := ""
if wxRsp.CodeUrl != "" {
QRcode = qrcode.GetPNGBase64(wxRsp.CodeUrl)
}
QRcode = strings.ReplaceAll(QRcode, "\u0000", "")
r := map[string]string{
"payQrcode": wxRsp.CodeUrl,
"payQrcode": QRcode,
}
return r, nil
}


+ 33
- 0
utils/qrcode/decodeFile.go 查看文件

@@ -0,0 +1,33 @@
package qrcode

import (
"image"
_ "image/jpeg"
_ "image/png"
"os"

"github.com/makiuchi-d/gozxing"
"github.com/makiuchi-d/gozxing/qrcode"
)

func DecodeFile(fi string) (string, error) {
file, err := os.Open(fi)
if err != nil {
return "", err
}
img, _, err := image.Decode(file)
if err != nil {
return "", err
}
// prepare BinaryBitmap
bmp, err := gozxing.NewBinaryBitmapFromImage(img)
if err != nil {
return "", err
}
// decode image
result, err := qrcode.NewQRCodeReader().Decode(bmp, nil)
if err != nil {
return "", err
}
return result.String(), nil
}

+ 43
- 0
utils/qrcode/getBase64.go 查看文件

@@ -0,0 +1,43 @@
package qrcode

// 生成登录二维码图片, 方便在网页上显示

import (
"bytes"
"encoding/base64"
"image/jpeg"
"image/png"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)

func GetJPGBase64(content string, edges ...int) string {
edgeLen := 300
if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
edgeLen = edges[0]
}
img, _ := qr.Encode(content, qr.L, qr.Unicode)
img, _ = barcode.Scale(img, edgeLen, edgeLen)

emptyBuff := bytes.NewBuffer(nil) // 开辟一个新的空buff缓冲区
jpeg.Encode(emptyBuff, img, nil)
dist := make([]byte, 50000) // 开辟存储空间
base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) // buff转成base64
return "data:image/png;base64," + string(dist) // 输出图片base64(type = []byte)
}

func GetPNGBase64(content string, edges ...int) string {
edgeLen := 300
if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
edgeLen = edges[0]
}
img, _ := qr.Encode(content, qr.L, qr.Unicode)
img, _ = barcode.Scale(img, edgeLen, edgeLen)

emptyBuff := bytes.NewBuffer(nil) // 开辟一个新的空buff缓冲区
png.Encode(emptyBuff, img)
dist := make([]byte, 50000) // 开辟存储空间
base64.StdEncoding.Encode(dist, emptyBuff.Bytes()) // buff转成base64
return string(dist) // 输出图片base64(type = []byte)
}

+ 85
- 0
utils/qrcode/saveFile.go 查看文件

@@ -0,0 +1,85 @@
package qrcode

// 生成登录二维码图片

import (
"errors"
"image"
"image/jpeg"
"image/png"
"os"
"path/filepath"
"strings"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)

func SaveJpegFile(filePath, content string, edges ...int) error {
edgeLen := 300
if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
edgeLen = edges[0]
}
img, _ := qr.Encode(content, qr.L, qr.Unicode)
img, _ = barcode.Scale(img, edgeLen, edgeLen)

return writeFile(filePath, img, "jpg")
}

func SavePngFile(filePath, content string, edges ...int) error {
edgeLen := 300
if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
edgeLen = edges[0]
}
img, _ := qr.Encode(content, qr.L, qr.Unicode)
img, _ = barcode.Scale(img, edgeLen, edgeLen)

return writeFile(filePath, img, "png")
}

func writeFile(filePath string, img image.Image, format string) error {
if err := createDir(filePath); err != nil {
return err
}
file, err := os.Create(filePath)
defer file.Close()
if err != nil {
return err
}
switch strings.ToLower(format) {
case "png":
err = png.Encode(file, img)
break
case "jpg":
err = jpeg.Encode(file, img, nil)
default:
return errors.New("format not accept")
}
if err != nil {
return err
}
return nil
}

func createDir(filePath string) error {
var err error
// filePath, _ = filepath.Abs(filePath)
dirPath := filepath.Dir(filePath)
dirInfo, err := os.Stat(dirPath)
if err != nil {
if !os.IsExist(err) {
err = os.MkdirAll(dirPath, 0777)
if err != nil {
return err
}
} else {
return err
}
} else {
if dirInfo.IsDir() {
return nil
}
return errors.New("directory is a file")
}
return nil
}

+ 39
- 0
utils/qrcode/writeWeb.go 查看文件

@@ -0,0 +1,39 @@
package qrcode

import (
"bytes"
"image/jpeg"
"image/png"
"net/http"

"github.com/boombuler/barcode"
"github.com/boombuler/barcode/qr"
)

func WritePng(w http.ResponseWriter, content string, edges ...int) error {
edgeLen := 300
if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
edgeLen = edges[0]
}
img, _ := qr.Encode(content, qr.L, qr.Unicode)
img, _ = barcode.Scale(img, edgeLen, edgeLen)
buff := bytes.NewBuffer(nil)
png.Encode(buff, img)
w.Header().Set("Content-Type", "image/png")
_, err := w.Write(buff.Bytes())
return err
}

func WriteJpg(w http.ResponseWriter, content string, edges ...int) error {
edgeLen := 300
if len(edges) > 0 && edges[0] > 100 && edges[0] < 2000 {
edgeLen = edges[0]
}
img, _ := qr.Encode(content, qr.L, qr.Unicode)
img, _ = barcode.Scale(img, edgeLen, edgeLen)
buff := bytes.NewBuffer(nil)
jpeg.Encode(buff, img, nil)
w.Header().Set("Content-Type", "image/jpg")
_, err := w.Write(buff.Bytes())
return err
}

Loading…
取消
儲存