fusenapi/server/upload/internal/logic/uploadqrcodelogic.go

81 lines
2.2 KiB
Go
Raw Normal View History

2023-06-08 09:02:38 +00:00
package logic
import (
"context"
2023-06-20 09:28:28 +00:00
"errors"
2023-06-12 09:00:37 +00:00
"fusenapi/model/gmodel"
"fusenapi/utils/auth"
2023-06-08 09:02:38 +00:00
"fusenapi/utils/basic"
"fusenapi/utils/qrcode"
2023-06-20 09:28:28 +00:00
"gorm.io/gorm"
2023-06-08 09:02:38 +00:00
"strings"
2023-07-11 03:33:43 +00:00
"fusenapi/server/upload/internal/svc"
"fusenapi/server/upload/internal/types"
"github.com/zeromicro/go-zero/core/logx"
2023-06-08 09:02:38 +00:00
)
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,
}
}
2023-06-15 04:00:32 +00:00
func (l *UploadQrcodeLogic) UploadQrcode(req *types.UploadQrcodeReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if userinfo.GetIdType() != auth.IDTYPE_User {
2023-09-15 02:39:08 +00:00
return resp.SetStatusWithMessage(basic.CodeServiceErr, "please sign in first")
2023-06-15 04:00:32 +00:00
}
2023-06-08 09:02:38 +00:00
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")
}
//获取二维码模板信息
2023-06-12 09:00:37 +00:00
qrCodeModel := gmodel.NewFsQrcodeSetModel(l.svcCtx.MysqlConn)
2023-06-08 09:02:38 +00:00
qrCodeSet, err := qrCodeModel.FindOne(l.ctx, req.QRcodeType)
2023-06-12 09:00:37 +00:00
if err != nil {
2023-06-20 09:28:28 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "qrcode setting is not exists")
}
2023-06-08 09:02:38 +00:00
logx.Error(err)
resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get qrcode setting")
}
2023-06-12 09:00:37 +00:00
qrType := *qrCodeSet.SvgWebsite
2023-06-08 09:02:38 +00:00
if strings.Contains(req.Url, "www.instagram.com") {
2023-06-12 09:00:37 +00:00
qrType = *qrCodeSet.SvgInstagram
2023-06-08 09:02:38 +00:00
} else if strings.Contains(req.Url, "www.facebook.com") {
2023-06-12 09:00:37 +00:00
qrType = *qrCodeSet.SvgFacebook
2023-06-08 09:02:38 +00:00
}
//生成二维码
2023-06-16 03:15:38 +00:00
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)
2023-06-08 09:02:38 +00:00
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
})
}