package query import ( "context" "encoding/json" "time" "tyc-server/common/xerr" "github.com/pkg/errors" "tyc-server/app/main/api/internal/svc" "tyc-server/app/main/api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type QuerySingleTestLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewQuerySingleTestLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QuerySingleTestLogic { return &QuerySingleTestLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *QuerySingleTestLogic) QuerySingleTest(req *types.QuerySingleTestReq) (resp *types.QuerySingleTestResp, err error) { // _, err = l.svcCtx.FeatureModel.FindOneByApiId(l.ctx, req.Api) // if err != nil { // return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 获取接口失败 : %d", err) // } marshalParams, err := json.Marshal(req.Params) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 序列化参数失败 : %d", err) } // 创建一个30分钟超时的上下文 timeoutCtx, cancel := context.WithTimeout(l.ctx, 30*time.Minute) defer cancel() // 确保在函数返回时取消上下文,防止资源泄漏 // 使用新的超时上下文 apiResp, err := l.svcCtx.ApiRequestService.PreprocessRequestApi(timeoutCtx, marshalParams, req.Api) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 接口请求失败 : %d", err) } var respData interface{} err = json.Unmarshal(apiResp.Data, &respData) if err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.SERVER_COMMON_ERROR), "单查测试, 反序列化接口失败 : %d", err) } return &types.QuerySingleTestResp{ Data: respData, Api: req.Api, }, nil }