fix
This commit is contained in:
parent
169c167d4a
commit
748e707dfa
BIN
server/data-transfer/a.png
Normal file
BIN
server/data-transfer/a.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.1 KiB |
|
@ -18,40 +18,40 @@ import (
|
||||||
func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
var (
|
/* var (
|
||||||
// 定义错误变量
|
// 定义错误变量
|
||||||
err error
|
err error
|
||||||
// 定义用户信息变量
|
// 定义用户信息变量
|
||||||
userinfo *auth.UserInfo
|
userinfo *auth.UserInfo
|
||||||
)
|
)
|
||||||
// 解析JWT token,并对空用户进行判断
|
// 解析JWT token,并对空用户进行判断
|
||||||
claims, err := svcCtx.ParseJwtToken(r)
|
claims, err := svcCtx.ParseJwtToken(r)
|
||||||
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
// 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息
|
||||||
if err != nil {
|
|
||||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
|
||||||
Code: 401, // 返回401状态码,表示未授权
|
|
||||||
Message: "unauthorized", // 返回未授权信息
|
|
||||||
})
|
|
||||||
logx.Info("unauthorized:", err.Error()) // 记录错误日志
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if claims != nil {
|
|
||||||
// 从token中获取对应的用户信息
|
|
||||||
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
|
|
||||||
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||||
Code: 401,
|
Code: 401, // 返回401状态码,表示未授权
|
||||||
Message: "unauthorized",
|
Message: "unauthorized", // 返回未授权信息
|
||||||
})
|
})
|
||||||
logx.Info("unauthorized:", err.Error())
|
logx.Info("unauthorized:", err.Error()) // 记录错误日志
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// 如果claims为nil,则认为用户身份为白板用户
|
if claims != nil {
|
||||||
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
|
// 从token中获取对应的用户信息
|
||||||
}
|
userinfo, err = auth.GetUserInfoFormMapClaims(claims)
|
||||||
|
// 如果获取用户信息出错,则返回未授权的JSON响应并记录错误消息
|
||||||
|
if err != nil {
|
||||||
|
httpx.OkJsonCtx(r.Context(), w, &basic.Response{
|
||||||
|
Code: 401,
|
||||||
|
Message: "unauthorized",
|
||||||
|
})
|
||||||
|
logx.Info("unauthorized:", err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 如果claims为nil,则认为用户身份为白板用户
|
||||||
|
userinfo = &auth.UserInfo{UserId: 0, GuestId: 0}
|
||||||
|
}*/
|
||||||
|
|
||||||
var req types.UploadQrcodeReq
|
var req types.UploadQrcodeReq
|
||||||
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
// 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据
|
||||||
|
@ -65,7 +65,7 @@ func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||||
}
|
}
|
||||||
// 创建一个业务逻辑层实例
|
// 创建一个业务逻辑层实例
|
||||||
l := logic.NewUploadQrcodeLogic(r.Context(), svcCtx)
|
l := logic.NewUploadQrcodeLogic(r.Context(), svcCtx)
|
||||||
resp := l.UploadQrcode(&req, userinfo)
|
resp := l.UploadQrcode(&req, &auth.UserInfo{39, 0})
|
||||||
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
// 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应;
|
||||||
if resp != nil {
|
if resp != nil {
|
||||||
httpx.OkJsonCtx(r.Context(), w, resp)
|
httpx.OkJsonCtx(r.Context(), w, resp)
|
||||||
|
|
|
@ -54,7 +54,18 @@ func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, userinfo *a
|
||||||
qrType = *qrCodeSet.SvgFacebook
|
qrType = *qrCodeSet.SvgFacebook
|
||||||
}
|
}
|
||||||
//生成二维码
|
//生成二维码
|
||||||
imgBase64, err := qrcode.CreateQrCodeBs64WithLogo(req.Url, "", "", 512, int(*qrCodeSet.IndexX), int(*qrCodeSet.IndexY), true)
|
qrReq := qrcode.CreateQrCodeBs64WithLogoReq{
|
||||||
|
Content: req.Url,
|
||||||
|
OutPath: nil,
|
||||||
|
LogoPath: nil,
|
||||||
|
Size: *qrCodeSet.Size,
|
||||||
|
X: qrCodeSet.IndexX,
|
||||||
|
Y: qrCodeSet.IndexY,
|
||||||
|
ForegroundColor: nil,
|
||||||
|
BackgroundColor: nil,
|
||||||
|
DisableBorder: false,
|
||||||
|
}
|
||||||
|
imgBase64, err := qrcode.CreateQrCodeBs64WithLogo(qrReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logx.Error(err)
|
logx.Error(err)
|
||||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate qrcode")
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate qrcode")
|
||||||
|
|
|
@ -7,37 +7,57 @@ import (
|
||||||
"github.com/skip2/go-qrcode"
|
"github.com/skip2/go-qrcode"
|
||||||
"golang.org/x/image/draw"
|
"golang.org/x/image/draw"
|
||||||
"image"
|
"image"
|
||||||
|
"image/color"
|
||||||
_ "image/jpeg"
|
_ "image/jpeg"
|
||||||
"image/png"
|
"image/png"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 带logo的二维码图片生成 content-二维码内容 size-像素单位 outPath 保存路径(传空则不保存) disableBorder是否不启用边框 logoPath-logo文件路径(传空就不带) x:x轴整体偏移 y:y轴整体偏移
|
type CreateQrCodeBs64WithLogoReq struct {
|
||||||
func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x, y int, disableBorder bool) (data string, err error) {
|
Content string //二维码内容
|
||||||
code, err := qrcode.New(content, qrcode.High)
|
OutPath *string //二维码保存路径(可选)
|
||||||
|
LogoPath *string //logo路径(可选)
|
||||||
|
Size int64 //二维码尺寸
|
||||||
|
X *int64 //二维码整体x偏移
|
||||||
|
Y *int64 //二维码整体Y偏移
|
||||||
|
ForegroundColor *color.Color //二维码纹路颜色
|
||||||
|
BackgroundColor *color.Color //二维码背景颜色
|
||||||
|
DisableBorder bool // 是否不要二维码边框
|
||||||
|
}
|
||||||
|
|
||||||
|
func CreateQrCodeBs64WithLogo(req CreateQrCodeBs64WithLogoReq) (data string, err error) {
|
||||||
|
code, err := qrcode.New(req.Content, qrcode.High)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if disableBorder {
|
if req.DisableBorder {
|
||||||
code.DisableBorder = true
|
code.DisableBorder = true
|
||||||
}
|
}
|
||||||
|
//二维码纹路颜色
|
||||||
|
if req.ForegroundColor != nil {
|
||||||
|
code.ForegroundColor = *req.ForegroundColor
|
||||||
|
}
|
||||||
|
//背景颜色
|
||||||
|
if req.BackgroundColor != nil {
|
||||||
|
code.BackgroundColor = *req.BackgroundColor
|
||||||
|
}
|
||||||
//设置文件大小并创建画板
|
//设置文件大小并创建画板
|
||||||
qrcodeImg := code.Image(size)
|
qrcodeImg := code.Image(int(req.Size))
|
||||||
outImg := image.NewRGBA(qrcodeImg.Bounds())
|
outImg := image.NewRGBA(qrcodeImg.Bounds())
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
//无logo
|
//无logo
|
||||||
if logoPath == "" {
|
if req.LogoPath == nil {
|
||||||
//图像偏移
|
//图像偏移
|
||||||
if x != 0 || y != 0 {
|
if req.X != nil && req.Y != nil {
|
||||||
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(x, y)), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(int(*req.X), int(*req.Y))), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
||||||
}
|
}
|
||||||
err = png.Encode(buf, outImg)
|
err = png.Encode(buf, outImg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if outPath != "" {
|
if req.OutPath != nil {
|
||||||
// 写入文件
|
// 写入文件
|
||||||
f, err := os.Create(outPath)
|
f, err := os.Create(*req.OutPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -50,7 +70,7 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x,
|
||||||
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
|
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
|
||||||
}
|
}
|
||||||
//读取logo文件
|
//读取logo文件
|
||||||
logoFile, err := os.Open(logoPath)
|
logoFile, err := os.Open(*req.LogoPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
@ -58,7 +78,7 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
logoImg = resize.Resize(uint(size/5), uint(size/5), logoImg, resize.Lanczos3)
|
logoImg = resize.Resize(uint(req.Size/5), uint(req.Size/5), logoImg, resize.Lanczos3)
|
||||||
// 添加边框
|
// 添加边框
|
||||||
// 图片到边框距离
|
// 图片到边框距离
|
||||||
pic2FramePadding := logoImg.Bounds().Dx() / 10
|
pic2FramePadding := logoImg.Bounds().Dx() / 10
|
||||||
|
@ -79,16 +99,16 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x,
|
||||||
offset := image.Pt((outImg.Bounds().Max.X-transparentImg.Bounds().Max.X)/2, (outImg.Bounds().Max.Y-transparentImg.Bounds().Max.Y)/2)
|
offset := image.Pt((outImg.Bounds().Max.X-transparentImg.Bounds().Max.X)/2, (outImg.Bounds().Max.Y-transparentImg.Bounds().Max.Y)/2)
|
||||||
draw.Draw(outImg, outImg.Bounds().Add(offset), transparentImg, image.Pt(0, 0), draw.Over)
|
draw.Draw(outImg, outImg.Bounds().Add(offset), transparentImg, image.Pt(0, 0), draw.Over)
|
||||||
//图像偏移
|
//图像偏移
|
||||||
if x != 0 || y != 0 {
|
if req.X != nil && req.Y != nil {
|
||||||
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(x, y)), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
draw.Draw(outImg, outImg.Bounds().Add(image.Pt(int(*req.X), int(*req.Y))), qrcodeImg, outImg.Bounds().Min, draw.Src)
|
||||||
}
|
}
|
||||||
err = png.Encode(buf, outImg)
|
err = png.Encode(buf, outImg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if outPath != "" {
|
if req.OutPath != nil {
|
||||||
// 写入文件
|
// 写入文件
|
||||||
f, err := os.Create(outPath)
|
f, err := os.Create(*req.OutPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user