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) return resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to generate qrcode") } return resp.SetStatusWithMessage(basic.CodeOK, "success", types.UploadQrcodeRsp{ Link: "", D: qrType + " " + imgBase64, }) }