diff --git a/server/data-transfer/a.png b/server/data-transfer/a.png new file mode 100644 index 00000000..7411f9f6 Binary files /dev/null and b/server/data-transfer/a.png differ diff --git a/server/data-transfer/internal/handler/uploadqrcodehandler.go b/server/data-transfer/internal/handler/uploadqrcodehandler.go index 79bf19bb..d5dde357 100644 --- a/server/data-transfer/internal/handler/uploadqrcodehandler.go +++ b/server/data-transfer/internal/handler/uploadqrcodehandler.go @@ -18,40 +18,40 @@ import ( func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { - var ( - // 定义错误变量 - err error - // 定义用户信息变量 - userinfo *auth.UserInfo - ) - // 解析JWT token,并对空用户进行判断 - claims, err := svcCtx.ParseJwtToken(r) - // 如果解析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响应并记录错误消息 + /* var ( + // 定义错误变量 + err error + // 定义用户信息变量 + userinfo *auth.UserInfo + ) + // 解析JWT token,并对空用户进行判断 + claims, err := svcCtx.ParseJwtToken(r) + // 如果解析JWT token出错,则返回未授权的JSON响应并记录错误消息 if err != nil { httpx.OkJsonCtx(r.Context(), w, &basic.Response{ - Code: 401, - Message: "unauthorized", + Code: 401, // 返回401状态码,表示未授权 + Message: "unauthorized", // 返回未授权信息 }) - logx.Info("unauthorized:", err.Error()) + logx.Info("unauthorized:", err.Error()) // 记录错误日志 return } - } else { - // 如果claims为nil,则认为用户身份为白板用户 - userinfo = &auth.UserInfo{UserId: 0, GuestId: 0} - } + + if claims != nil { + // 从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 // 如果端点有请求结构体,则使用httpx.Parse方法从HTTP请求体中解析请求数据 @@ -65,7 +65,7 @@ func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } // 创建一个业务逻辑层实例 l := logic.NewUploadQrcodeLogic(r.Context(), svcCtx) - resp := l.UploadQrcode(&req, userinfo) + resp := l.UploadQrcode(&req, &auth.UserInfo{39, 0}) // 如果响应不为nil,则使用httpx.OkJsonCtx方法返回JSON响应; if resp != nil { httpx.OkJsonCtx(r.Context(), w, resp) diff --git a/server/data-transfer/internal/logic/uploadqrcodelogic.go b/server/data-transfer/internal/logic/uploadqrcodelogic.go index d8749dac..9f592150 100644 --- a/server/data-transfer/internal/logic/uploadqrcodelogic.go +++ b/server/data-transfer/internal/logic/uploadqrcodelogic.go @@ -54,7 +54,18 @@ func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, userinfo *a 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 { logx.Error(err) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate qrcode") diff --git a/utils/qrcode/creator.go b/utils/qrcode/creator.go index b89c44d5..ba59ec4a 100644 --- a/utils/qrcode/creator.go +++ b/utils/qrcode/creator.go @@ -7,37 +7,57 @@ import ( "github.com/skip2/go-qrcode" "golang.org/x/image/draw" "image" + "image/color" _ "image/jpeg" "image/png" "os" ) -// 带logo的二维码图片生成 content-二维码内容 size-像素单位 outPath 保存路径(传空则不保存) disableBorder是否不启用边框 logoPath-logo文件路径(传空就不带) x:x轴整体偏移 y:y轴整体偏移 -func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x, y int, disableBorder bool) (data string, err error) { - code, err := qrcode.New(content, qrcode.High) +type CreateQrCodeBs64WithLogoReq struct { + Content string //二维码内容 + 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 { return } - if disableBorder { + if req.DisableBorder { 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()) buf := new(bytes.Buffer) //无logo - if logoPath == "" { + if req.LogoPath == nil { //图像偏移 - if x != 0 || y != 0 { - draw.Draw(outImg, outImg.Bounds().Add(image.Pt(x, y)), qrcodeImg, outImg.Bounds().Min, draw.Src) + if req.X != nil && req.Y != nil { + 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) if err != nil { return "", err } - if outPath != "" { + if req.OutPath != nil { // 写入文件 - f, err := os.Create(outPath) + f, err := os.Create(*req.OutPath) if err != nil { return "", err } @@ -50,7 +70,7 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x, return base64.StdEncoding.EncodeToString(buf.Bytes()), nil } //读取logo文件 - logoFile, err := os.Open(logoPath) + logoFile, err := os.Open(*req.LogoPath) if err != nil { return "", err } @@ -58,7 +78,7 @@ func CreateQrCodeBs64WithLogo(content, outPath string, logoPath string, size, x, if err != nil { 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 @@ -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) draw.Draw(outImg, outImg.Bounds().Add(offset), transparentImg, image.Pt(0, 0), draw.Over) //图像偏移 - if x != 0 || y != 0 { - draw.Draw(outImg, outImg.Bounds().Add(image.Pt(x, y)), qrcodeImg, outImg.Bounds().Min, draw.Src) + if req.X != nil && req.Y != nil { + 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) if err != nil { return "", err } - if outPath != "" { + if req.OutPath != nil { // 写入文件 - f, err := os.Create(outPath) + f, err := os.Create(*req.OutPath) if err != nil { return "", err }