package logic import ( "context" "fusenapi/model/gmodel" "fusenapi/server/data-transfer/internal/svc" "fusenapi/server/data-transfer/internal/types" "fusenapi/utils/auth" "fusenapi/utils/basic" "fusenapi/utils/qrcode" "github.com/zeromicro/go-zero/core/logx" "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, 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 { logx.Error(err) resp.SetStatusWithMessage(basic.CodeServiceErr, "failed to get qrcode setting") } if qrCodeSet.Id == 0 { return resp.SetStatusWithMessage(basic.CodeServiceErr, "qrcode setting is not exists") } 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, }) }