fusenapi/server/upload/internal/logic/uploadqrcodelogic.go
laodaming 9bcb1d9377 fix
2023-07-11 11:33:43 +08:00

81 lines
2.2 KiB
Go

package logic
import (
"context"
"errors"
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/qrcode"
"gorm.io/gorm"
"strings"
"fusenapi/server/upload/internal/svc"
"fusenapi/server/upload/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
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,
}
}
func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please login first")
}
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 := gmodel.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
qrCodeSet, err := qrCodeModel.FindOne(l.ctx, req.QRcodeType)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "qrcode setting is not exists")
}
logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get qrcode setting")
}
qrType := *qrCodeSet.SvgWebsite
if strings.Contains(req.Url, "www.instagram.com") {
qrType = *qrCodeSet.SvgInstagram
} else if strings.Contains(req.Url, "www.facebook.com") {
qrType = *qrCodeSet.SvgFacebook
}
//生成二维码
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")
}
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UploadQrcodeRsp{
Link: "",
Data: qrType + " " + imgBase64,
})
}