c4eede1114
CI / changes (push) Successful in 1m32s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / analysis (push) Successful in 3m45s
CI / bff (push) Successful in 31s
CI / chore (push) Successful in 0s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 1s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 1s
CI / product (push) Successful in 0s
CI / sale (push) Successful in 0s
CI / record (push) Successful in 0s
CI / store (push) Successful in 0s
CI / support (push) Successful in 0s
CI / user (push) Successful in 1s
CI / task (push) Successful in 0s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
CI / ws (push) Successful in 0s
158 lines
4.2 KiB
Go
158 lines
4.2 KiB
Go
package analysislogic
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"lone-services/pkg/modelbase"
|
|
"lone-services/pkg/redis"
|
|
"lone-services/pkg/utils"
|
|
"lone-services/pkg/validate"
|
|
"lone-services/pkg/wechat/qrcode"
|
|
pkgws "lone-services/pkg/ws"
|
|
analysis "lone-services/rpc/analysis/pb"
|
|
chore "lone-services/rpc/chore/pb"
|
|
analysisconfig "lone-services/services/analysis/internal/config"
|
|
"lone-services/services/analysis/internal/dao"
|
|
"lone-services/services/analysis/internal/logic/common"
|
|
"lone-services/services/analysis/internal/model"
|
|
"lone-services/services/analysis/internal/svc"
|
|
"lone-services/services/analysis/validator"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
const qrShareTTL = 24 * time.Hour
|
|
|
|
type QrcodeShareLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewQrcodeShareLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QrcodeShareLogic {
|
|
return &QrcodeShareLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *QrcodeShareLogic) QrcodeShare(in *analysis.QrcodeShareReq) (*analysis.Response, error) {
|
|
var req validator.QrcodeShareValidator
|
|
if msg := validate.ValidateFromProto(in, &req); msg != utils.StringEmpty {
|
|
return outParams(msg), nil
|
|
}
|
|
login, fail := common.RequireLogin(l.ctx)
|
|
if fail != nil {
|
|
return fail, nil
|
|
}
|
|
|
|
owned, err := l.imagesOwned(req.Ids, login.BizId)
|
|
if err != nil {
|
|
l.Errorf("qrcode share check image: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
if !owned {
|
|
return common.NotYoursResponse(), nil
|
|
}
|
|
|
|
if redis.Client == nil {
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
for _, id := range req.Ids {
|
|
idStr := strconv.FormatInt(id, utils.NumberTen)
|
|
key := pkgws.RedisKeyQRShare + utils.MD5Encrypt(idStr)
|
|
if err = redis.Client.Set(l.ctx, key, idStr, qrShareTTL).Err(); err != nil {
|
|
l.Errorf("qrcode share redis: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
}
|
|
|
|
typeVal := utils.NumberOne
|
|
if len(req.Ids) > utils.NumberOne {
|
|
typeVal = utils.NumberTwo
|
|
}
|
|
wxCfg, err := analysisconfig.LoadWechatConfig()
|
|
if err != nil {
|
|
l.Errorf("qrcode share wechat config: %v", err)
|
|
return common.OutResponse(utils.Fail, "微信配置缺失"), nil
|
|
}
|
|
path := fmt.Sprintf("%s?type=%d&ids=%s", wxCfg.ReportPage, typeVal, joinIds(req.Ids))
|
|
img, err := wxCfg.NewQrcodeClient().CreateWxaCode(l.ctx, &qrcode.WxaCodeRequest{
|
|
Path: path,
|
|
Width: wxCfg.Width,
|
|
EnvVersion: wxCfg.EnvVersion,
|
|
AutoColor: false,
|
|
LineColor: &qrcode.LineColor{R: 255, G: 255, B: 255},
|
|
IsHyaline: true,
|
|
})
|
|
if err != nil {
|
|
l.Errorf("qrcode share wechat: %v", err)
|
|
return common.FailResponse(utils.Fail), nil
|
|
}
|
|
qrURL, err := l.uploadQrcode(img)
|
|
if err != nil {
|
|
l.Errorf("qrcode share upload: %v", err)
|
|
return common.OutResponse(utils.Fail, "upload qrcode failed"), nil
|
|
}
|
|
return common.OkResponse(map[string]string{
|
|
"qrcode": qrURL,
|
|
"time": utils.Now().String(),
|
|
}), nil
|
|
}
|
|
|
|
func (l *QrcodeShareLogic) imagesOwned(ids []int64, userId int64) (bool, error) {
|
|
if userId < utils.NumberOne || len(ids) == utils.NumberZero {
|
|
return false, nil
|
|
}
|
|
var rows []dao.Image
|
|
err := model.ImageModel{}.Init().Items(modelbase.Params{
|
|
In: map[string][]string{"id": int64Strings(ids)},
|
|
Eq: map[string]string{
|
|
"user_id": strconv.FormatInt(userId, utils.NumberTen),
|
|
"status": strconv.Itoa(utils.StatusOk),
|
|
},
|
|
}, &rows)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
return len(rows) == len(ids), nil
|
|
}
|
|
|
|
func (l *QrcodeShareLogic) 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
|
|
}
|
|
|
|
func joinIds(ids []int64) string {
|
|
parts := int64Strings(ids)
|
|
out := ""
|
|
for i, part := range parts {
|
|
if i > 0 {
|
|
out += ","
|
|
}
|
|
out += part
|
|
}
|
|
return out
|
|
}
|