81 lines
2.6 KiB
Go
81 lines
2.6 KiB
Go
package admin_query
|
|
|
|
import (
|
|
"context"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"qnc-server/app/main/api/internal/svc"
|
|
"qnc-server/app/main/api/internal/types"
|
|
"qnc-server/app/main/model"
|
|
"qnc-server/common/xerr"
|
|
"qnc-server/pkg/lzkit/crypto"
|
|
|
|
"github.com/pkg/errors"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type AdminGenerateQueryShareLinkLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
func NewAdminGenerateQueryShareLinkLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AdminGenerateQueryShareLinkLogic {
|
|
return &AdminGenerateQueryShareLinkLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *AdminGenerateQueryShareLinkLogic) AdminGenerateQueryShareLink(req *types.AdminGenerateQueryShareLinkReq) (resp *types.QueryGenerateShareLinkResp, err error) {
|
|
if req.OrderId == "" {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("订单ID不能为空"), "")
|
|
}
|
|
|
|
order, err := l.svcCtx.OrderModel.FindOne(l.ctx, req.OrderId)
|
|
if err != nil {
|
|
if errors.Is(err, model.ErrNotFound) {
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("订单不存在"), "")
|
|
}
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成分享链接, 获取订单失败: %v", err)
|
|
}
|
|
|
|
if order.Status != model.OrderStatusPaid {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成分享链接, 订单未支付")
|
|
}
|
|
|
|
query, err := l.svcCtx.QueryModel.FindOneByOrderId(l.ctx, order.Id)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成分享链接, 获取查询失败: %v", err)
|
|
}
|
|
|
|
if query.QueryState != model.QueryStateSuccess {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成分享链接, 查询未成功")
|
|
}
|
|
|
|
expireAt := time.Now().Add(time.Duration(l.svcCtx.Config.Query.ShareLinkExpire) * time.Second)
|
|
payload := types.QueryShareLinkPayload{
|
|
OrderId: order.Id,
|
|
ExpireAt: expireAt.Unix(),
|
|
}
|
|
secretKey := l.svcCtx.Config.Encrypt.SecretKey
|
|
key, err := hex.DecodeString(secretKey)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成分享链接, 解密失败: %v", err)
|
|
}
|
|
payloadBytes, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成分享链接, 序列化失败: %v", err)
|
|
}
|
|
encryptedPayload, err := crypto.AesEncryptURL(payloadBytes, key)
|
|
if err != nil {
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "生成分享链接, 加密失败: %v", err)
|
|
}
|
|
return &types.QueryGenerateShareLinkResp{
|
|
ShareLink: encryptedPayload,
|
|
}, nil
|
|
}
|