Merge branch 'develop' of https://gitee.com/fusenpack/fusenapi into develop

This commit is contained in:
eson
2023-06-08 18:55:22 +08:00
8 changed files with 228 additions and 0 deletions

View File

@@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
Path: "/qrcode-set/list",
Handler: GetQrCodeSetListHandler(serverCtx),
},
{
Method: http.MethodPost,
Path: "/upload/qrcode",
Handler: UploadQrcodeHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.Auth.AccessSecret),
)

View File

@@ -0,0 +1,37 @@
package handler
import (
"errors"
"net/http"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/httpx"
"fusenapi/server/data-transfer/internal/logic"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
)
func UploadQrcodeHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.UploadQrcodeReq
if err := httpx.Parse(r, &req); err != nil {
httpx.OkJsonCtx(r.Context(), w, &types.Response{
Code: 510,
Message: "parameter error",
})
logx.Info(err)
return
}
l := logic.NewUploadQrcodeLogic(r.Context(), svcCtx)
resp := l.UploadQrcode(&req)
if resp != nil {
httpx.OkJsonCtx(r.Context(), w, resp)
} else {
err := errors.New("server logic is error, resp must not be nil")
httpx.ErrorCtx(r.Context(), w, err)
logx.Error(err)
}
}
}

View File

@@ -0,0 +1,66 @@
package logic
import (
"context"
"errors"
"fmt"
"fusenapi/model"
"fusenapi/server/data-transfer/internal/svc"
"fusenapi/server/data-transfer/internal/types"
"fusenapi/utils/basic"
"fusenapi/utils/qrcode"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stores/sqlx"
"strings"
)
type UploadQrcodeLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewUploadQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UploadQrcodeLogic {
return &UploadQrcodeLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
// 生成base64二维码
func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq) (resp *types.Response) {
if req.Url == "" {
resp.SetStatus(basic.CodeApiErr, "param url is empty")
}
if req.QRcodeType <= 0 {
resp.SetStatus(basic.CodeApiErr, "param QRcodeType must large than 0")
}
//获取二维码模板信息
qrCodeModel := model.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeSet, err := qrCodeModel.FindOne(l.ctx, req.QRcodeType)
if err != nil && !errors.Is(err, sqlx.ErrNotFound) {
logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get qrcode setting")
}
if qrCodeSet == nil {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "qrcode setting is not exists")
}
qrType := qrCodeSet.SvgWebsite.String
if strings.Contains(req.Url, "www.instagram.com") {
qrType = qrCodeSet.SvgInstagram.String
} else if strings.Contains(req.Url, "www.facebook.com") {
qrType = qrCodeSet.SvgFacebook.String
}
//生成二维码
imgBase64, err := qrcode.CreateQrCodeBs64WithLogo(req.Url, "", "", 512, int(qrCodeSet.IndexX), int(qrCodeSet.IndexY), true)
if err != nil {
logx.Error(err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
}
fmt.Println(imgBase64)
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UploadQrcodeRsp{
Link: "",
D: qrType + " " + imgBase64,
})
}

View File

@@ -16,6 +16,16 @@ type GetQrCodeSetListRsp struct {
Name string `json:"name"`
}
type UploadQrcodeReq struct {
Url string `json:"url"`
QRcodeType int64 `json:"QRcodeType"`
}
type UploadQrcodeRsp struct {
Link string `json:"link"`
D string `json:"d"`
}
type Response struct {
Code int `json:"code"`
Message string `json:"msg"`