二维码/条形码的生成
This commit is contained in:
@@ -11,12 +11,25 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"errors"
|
||||
"image/png"
|
||||
"os"
|
||||
"encoding/base64"
|
||||
"git.rosy.net.cn/baseapi/platformapi"
|
||||
"git.rosy.net.cn/baseapi/utils"
|
||||
"git.rosy.net.cn/jx-callback/business/model"
|
||||
"git.rosy.net.cn/jx-callback/business/model/dao"
|
||||
"git.rosy.net.cn/jx-callback/globals"
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/qr"
|
||||
"github.com/boombuler/barcode/code128"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxCodeWidth = 400
|
||||
MaxCodeHeight = 400
|
||||
CodeTypeQr = "qr"
|
||||
CodeTypeBar = "bar"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -451,3 +464,82 @@ func CaculateSkuEarningPrice(shopPrice, salePrice int64, storePayPercentage int)
|
||||
earningPrice = earningPrice * int64(storePayPercentage) / 100
|
||||
return earningPrice
|
||||
}
|
||||
|
||||
func File2BytesByFileName(filename string) ([]byte, error) {
|
||||
file, err := os.Open(filename)
|
||||
defer file.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return File2BytesByFile(file)
|
||||
}
|
||||
|
||||
func File2BytesByFile(file *os.File) ([]byte, error) {
|
||||
stats, err := file.Stat()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data := make([]byte, stats.Size())
|
||||
_, err = file.Read(data)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func CreateCodeImage(qrCode barcode.Barcode) (fileName string, err error) {
|
||||
fileName = "qrcode.png"
|
||||
file, err := os.Create(fileName)
|
||||
defer file.Close()
|
||||
if err == nil {
|
||||
err = png.Encode(file, qrCode)
|
||||
}
|
||||
|
||||
return fileName, err
|
||||
}
|
||||
|
||||
func GetImgBase64(qrCode barcode.Barcode) (imgBase64 string, err error) {
|
||||
fileName, err := CreateCodeImage(qrCode)
|
||||
if err == nil {
|
||||
fileData, err := File2BytesByFileName(fileName)
|
||||
if err == nil {
|
||||
imgBase64 = "data:image/png;base64," + base64.StdEncoding.EncodeToString(fileData)
|
||||
}
|
||||
}
|
||||
|
||||
return imgBase64, err
|
||||
}
|
||||
|
||||
func CreateQrAndBarCode(width, height int, codetype, srcData string) (imgBase64 string, err error) {
|
||||
if width > MaxCodeWidth {
|
||||
width = MaxCodeWidth
|
||||
}
|
||||
if height > MaxCodeHeight {
|
||||
height = MaxCodeHeight
|
||||
}
|
||||
|
||||
if codetype == CodeTypeQr {
|
||||
qrCode, err := qr.Encode(srcData, qr.M, qr.Auto)
|
||||
if err == nil {
|
||||
qrCode, err = barcode.Scale(qrCode, width, height)
|
||||
if err == nil {
|
||||
imgBase64, err = GetImgBase64(qrCode)
|
||||
}
|
||||
}
|
||||
} else if codetype == CodeTypeBar {
|
||||
cs, err := code128.Encode(srcData)
|
||||
if err == nil {
|
||||
qrCode, err2 := barcode.Scale(cs, width, height)
|
||||
if err = err2; err == nil {
|
||||
imgBase64, err = GetImgBase64(qrCode)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
err = errors.New(fmt.Sprintf("未知编码类型:%s", codetype))
|
||||
}
|
||||
|
||||
return imgBase64, err
|
||||
}
|
||||
|
||||
@@ -279,3 +279,20 @@ func (c *CmsController) QueryConfigs() {
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
|
||||
// @Title 生成二维码或条形码
|
||||
// @Description 生成二维码或条形码
|
||||
// @Param token header string true "认证token"
|
||||
// @Param width formData int true "图片宽"
|
||||
// @Param height formData int true "图片高"
|
||||
// @Param codetype formData string true "编码类型(二维码:qr; 条形码:bar)"
|
||||
// @Param srcData formData string true "二维码/条形码数据源"
|
||||
// @Success 200 {object} controllers.CallResult
|
||||
// @Failure 200 {object} controllers.CallResult
|
||||
// @router /CreateQrAndBarCode [post]
|
||||
func (c *CmsController) CreateQrAndBarCode() {
|
||||
c.callCreateQrAndBarCode(func(params *tCmsCreateQrAndBarCodeParams) (retVal interface{}, errCode string, err error) {
|
||||
retVal, err = jxutils.CreateQrAndBarCode(params.Width, params.Height, params.Codetype, params.SrcData)
|
||||
return retVal, "", err
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user