feat: analysis gen qrcode
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"lone-services/pkg/redis"
|
||||
"lone-services/pkg/utils"
|
||||
"lone-services/pkg/wechat/qrcode"
|
||||
analysis "lone-services/rpc/analysis/pb"
|
||||
chore "lone-services/rpc/chore/pb"
|
||||
"lone-services/services/analysis/internal/config"
|
||||
"lone-services/services/analysis/internal/dao"
|
||||
"lone-services/services/analysis/internal/svc"
|
||||
|
||||
"github.com/google/uuid"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GenerateQrcodeLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewGenerateQrcodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GenerateQrcodeLogic {
|
||||
return &GenerateQrcodeLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GenerateQrcodeLogic) GenerateQrcode(in *analysis.GenerateQrcodeRequest) (*analysis.Response, error) {
|
||||
sessionID, err := l.createSession()
|
||||
if err != nil {
|
||||
l.Errorf("generate qrcode session: %v", err)
|
||||
return outResponse(utils.Fail, "create qrcode session failed"), nil
|
||||
}
|
||||
|
||||
wxCfg, err := config.LoadWechatQrcodeConfig()
|
||||
if err != nil {
|
||||
l.Errorf("generate qrcode wechat config: %v", err)
|
||||
return outResponse(utils.Fail, "wechat config missing"), nil
|
||||
}
|
||||
|
||||
img, err := wxCfg.NewQrcodeClient().CreateWxaCode(l.ctx, &qrcode.WxaCodeRequest{
|
||||
Path: qrcode.BuildPath(wxCfg.Page, map[string]string{
|
||||
"session_id": sessionID,
|
||||
}),
|
||||
Width: wxCfg.Width,
|
||||
EnvVersion: wxCfg.EnvVersion,
|
||||
AutoColor: false,
|
||||
LineColor: &qrcode.LineColor{R: 255, G: 255, B: 255},
|
||||
IsHyaline: true,
|
||||
})
|
||||
if err != nil {
|
||||
l.Errorf("generate qrcode wechat: %v", err)
|
||||
return outResponse(utils.Fail, err.Error()), nil
|
||||
}
|
||||
|
||||
qrURL, err := l.uploadQrcode(img)
|
||||
if err != nil {
|
||||
l.Errorf("generate qrcode upload: %v", err)
|
||||
return outResponse(utils.Fail, "upload qrcode failed"), nil
|
||||
}
|
||||
|
||||
return okResponse(map[string]string{
|
||||
"session_id": sessionID,
|
||||
"qrcode": qrURL,
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (l *GenerateQrcodeLogic) createSession() (string, error) {
|
||||
if redis.Client == nil {
|
||||
return "", fmt.Errorf("redis not initialized")
|
||||
}
|
||||
|
||||
sessionID := uuid.New().String()
|
||||
payload, err := jsoniter.MarshalToString(dao.QrcodeSessionStatus{Status: dao.QrcodeSessionPending})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
key := dao.RedisKeyQRSession + sessionID
|
||||
if err := redis.Client.Set(l.ctx, key, payload, dao.QrcodeSessionTTL).Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return sessionID, nil
|
||||
}
|
||||
|
||||
func (l *GenerateQrcodeLogic) uploadQrcode(content []byte) (string, error) {
|
||||
if l.svcCtx.ChoreSvcName == utils.StringEmpty {
|
||||
return "", fmt.Errorf("chore service name empty")
|
||||
}
|
||||
|
||||
cli, err := svc.GetRpcClient(l.svcCtx.ChoreSvcName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
res, err := chore.NewChoreClient(cli.Conn()).InternalUpload(l.ctx, &chore.InternalUploadReq{
|
||||
Filename: uuid.New().String() + ".png",
|
||||
Content: content,
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if res == nil || res.Url == utils.StringEmpty {
|
||||
return "", fmt.Errorf("chore upload empty url")
|
||||
}
|
||||
return res.Url, nil
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"lone-services/services/analysis/analysis"
|
||||
"lone-services/services/analysis/internal/svc"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type PingLogic struct {
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
logx.Logger
|
||||
}
|
||||
|
||||
func NewPingLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PingLogic {
|
||||
return &PingLogic{
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
Logger: logx.WithContext(ctx),
|
||||
}
|
||||
}
|
||||
|
||||
func (l *PingLogic) Ping(in *analysis.Request) (*analysis.Response, error) {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return &analysis.Response{}, nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"lone-services/pkg/utils"
|
||||
analysis "lone-services/rpc/analysis/pb"
|
||||
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
)
|
||||
|
||||
func okResponse(data any) *analysis.Response {
|
||||
buf, _ := jsoniter.Marshal(data)
|
||||
return &analysis.Response{
|
||||
Code: utils.Ok.Code,
|
||||
Msg: utils.Ok.Msg,
|
||||
Data: string(buf),
|
||||
}
|
||||
}
|
||||
|
||||
func failResponse(status utils.Status) *analysis.Response {
|
||||
return &analysis.Response{
|
||||
Code: status.Code,
|
||||
Msg: status.Msg,
|
||||
}
|
||||
}
|
||||
|
||||
func outResponse(status utils.Status, msg string) *analysis.Response {
|
||||
return &analysis.Response{
|
||||
Code: status.Code,
|
||||
Msg: msg,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user