Files
bdqr-server/app/main/api/internal/logic/agent/getinviteposterlogic.go

75 lines
2.1 KiB
Go
Raw Normal View History

2026-02-09 18:10:23 +08:00
package agent
import (
"context"
"encoding/base64"
2026-03-02 16:34:47 +08:00
"fmt"
2026-02-09 18:10:23 +08:00
"strings"
"bdqr-server/app/main/model"
"bdqr-server/common/ctxdata"
"bdqr-server/common/xerr"
"github.com/pkg/errors"
"bdqr-server/app/main/api/internal/svc"
"bdqr-server/app/main/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetInvitePosterLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetInvitePosterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetInvitePosterLogic {
return &GetInvitePosterLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetInvitePosterLogic) GetInvitePoster(req *types.GetInvitePosterReq) (resp *types.GetInvitePosterResp, err error) {
// 1. 需登录且为代理
userID, err := ctxdata.GetUidFromCtx(l.ctx)
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "获取用户信息失败, %v", err)
}
_, err = l.svcCtx.AgentModel.FindOneByUserId(l.ctx, userID)
if err != nil {
if errors.Is(err, model.ErrNotFound) {
return nil, errors.Wrapf(xerr.NewErrMsg("您不是代理"), "")
}
return nil, errors.Wrapf(xerr.NewErrCode(xerr.DB_ERROR), "查询代理信息失败, %v", err)
}
// 2. 邀请链接不能为空
inviteLink := strings.TrimSpace(req.InviteLink)
if inviteLink == "" {
return nil, errors.Wrapf(xerr.NewErrMsg("邀请链接不能为空"), "")
}
2026-03-02 16:34:47 +08:00
// 3. 确定模板类型1-4 对应 invitation_01 ~ invitation_04
templateIndex := req.TemplateIndex
if templateIndex < 1 || templateIndex > 4 {
templateIndex = 1
}
qrcodeType := fmt.Sprintf("invitation_%02d", templateIndex)
// 4. 调用 ImageService 生成邀请海报(背景图 + 二维码)
imgData, _, err := l.svcCtx.ImageService.ProcessImageWithQRCode(qrcodeType, inviteLink)
2026-02-09 18:10:23 +08:00
if err != nil {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成海报失败: %v", err)
}
2026-03-02 16:34:47 +08:00
// 5. 返回 base64
2026-02-09 18:10:23 +08:00
posterBase64 := base64.StdEncoding.EncodeToString(imgData)
return &types.GetInvitePosterResp{
PosterBase64: posterBase64,
}, nil
}