2023-06-08 09:02:38 +00:00
|
|
|
package logic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"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)
|
2023-06-08 11:29:08 +00:00
|
|
|
return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate qrcode")
|
2023-06-08 09:02:38 +00:00
|
|
|
}
|
|
|
|
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UploadQrcodeRsp{
|
|
|
|
Link: "",
|
2023-06-08 11:33:30 +00:00
|
|
|
Data: qrType + " " + imgBase64,
|
2023-06-08 09:02:38 +00:00
|
|
|
})
|
|
|
|
}
|