2026-05-21 17:29:35 +08:00
|
|
|
|
package toolbox
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
|
|
"qnc-server/app/main/api/internal/svc"
|
|
|
|
|
|
"qnc-server/app/main/api/internal/types"
|
2026-05-23 16:13:30 +08:00
|
|
|
|
"qnc-server/app/main/model"
|
2026-05-21 17:29:35 +08:00
|
|
|
|
"qnc-server/common/xerr"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
2026-05-23 16:13:30 +08:00
|
|
|
|
"github.com/zeromicro/go-zero/core/threading"
|
2026-05-21 17:29:35 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type ToolboxQueryLogic struct {
|
|
|
|
|
|
logx.Logger
|
|
|
|
|
|
ctx context.Context
|
|
|
|
|
|
svcCtx *svc.ServiceContext
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewToolboxQueryLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ToolboxQueryLogic {
|
|
|
|
|
|
return &ToolboxQueryLogic{
|
|
|
|
|
|
Logger: logx.WithContext(ctx),
|
|
|
|
|
|
ctx: ctx,
|
|
|
|
|
|
svcCtx: svcCtx,
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (l *ToolboxQueryLogic) ToolboxQuery(req *types.ToolboxQueryReq) (*types.ToolboxQueryResp, error) {
|
|
|
|
|
|
if req.ToolKey == "" {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrMsg("tool_key 不能为空"), "")
|
|
|
|
|
|
}
|
|
|
|
|
|
if l.svcCtx.ToolboxService == nil {
|
|
|
|
|
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "工具箱服务未初始化")
|
|
|
|
|
|
}
|
|
|
|
|
|
params := req.Params
|
|
|
|
|
|
if params == nil {
|
|
|
|
|
|
params = map[string]interface{}{}
|
|
|
|
|
|
}
|
|
|
|
|
|
result, err := l.svcCtx.ToolboxService.Query(l.ctx, req.ToolKey, params)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return nil, err
|
|
|
|
|
|
}
|
2026-05-23 16:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
// 记录免费工具查询到动态展示表
|
|
|
|
|
|
l.recordFreeToolInquiry(req.ToolKey)
|
|
|
|
|
|
|
2026-05-21 17:29:35 +08:00
|
|
|
|
return &types.ToolboxQueryResp{
|
|
|
|
|
|
ToolKey: req.ToolKey,
|
|
|
|
|
|
Result: result,
|
|
|
|
|
|
}, nil
|
|
|
|
|
|
}
|
2026-05-23 16:13:30 +08:00
|
|
|
|
|
|
|
|
|
|
// recordFreeToolInquiry 记录免费工具查询
|
|
|
|
|
|
func (l *ToolboxQueryLogic) recordFreeToolInquiry(toolKey string) {
|
|
|
|
|
|
// 异步记录,不影响主流程
|
|
|
|
|
|
threading.GoSafe(func() {
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
// 1. 获取工具名称 (这里可以建立一个简单的映射,或者从前端传过来,或者查询配置)
|
|
|
|
|
|
// 为了简单和解耦,我们这里仅记录 ToolKey,前端展示时再映射名称,或者在这里硬编码一些常用名称
|
|
|
|
|
|
toolName := toolKey
|
|
|
|
|
|
// 如果能访问到 toolboxRegistry 的逻辑最好,但后端通常不包含前端配置
|
|
|
|
|
|
// 我们可以通过一个简单的 Switch 处理一些核心工具名
|
|
|
|
|
|
switch toolKey {
|
|
|
|
|
|
case "ip-location":
|
|
|
|
|
|
toolName = "IP地址查询"
|
|
|
|
|
|
case "idcard-info":
|
|
|
|
|
|
toolName = "身份证归属地"
|
|
|
|
|
|
case "oilprice":
|
|
|
|
|
|
toolName = "实时油价"
|
|
|
|
|
|
case "joke":
|
|
|
|
|
|
toolName = "趣味笑话"
|
|
|
|
|
|
case "dream":
|
|
|
|
|
|
toolName = "周公解梦"
|
|
|
|
|
|
case "constellation":
|
|
|
|
|
|
toolName = "星座运势"
|
|
|
|
|
|
case "garbage":
|
|
|
|
|
|
toolName = "垃圾分类"
|
|
|
|
|
|
default:
|
|
|
|
|
|
// 其他的就直接用 key 或保持原样
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
record := &model.InquiryRecord{
|
|
|
|
|
|
UserPhoneTail: "系统",
|
|
|
|
|
|
DisplayName: "游客用户",
|
|
|
|
|
|
VinMasked: "******",
|
|
|
|
|
|
CarModel: "免费工具查询",
|
|
|
|
|
|
InquiryTag: toolName,
|
|
|
|
|
|
Status: 1,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
_, _ = l.svcCtx.InquiryRecordModel.Insert(ctx, nil, record)
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|