@be-link/ecommerce-trade-service-node-sdk 0.1.61 → 0.1.63

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -312,6 +312,9 @@ export declare namespace PosOrderQueryService {
312
312
  };
313
313
  user: {
314
314
  userId: string;
315
+ unionId: string;
316
+ /** 用户昵称 */
317
+ nickname?: string;
315
318
  memberLevel?: string;
316
319
  receiverName: string;
317
320
  receiverPhone: string;
@@ -326,6 +329,9 @@ export declare namespace PosOrderQueryService {
326
329
  };
327
330
  userDetail: {
328
331
  userId: string;
332
+ unionId: string;
333
+ /** 用户昵称 */
334
+ nickname?: string;
329
335
  memberLevel?: string;
330
336
  receiverName: string;
331
337
  receiverPhone: string;
@@ -4,4 +4,5 @@ export { RosOrderQueryService as RosOrderQueryTypes } from './orderQuery/types';
4
4
  export { orderCoreByUserService } from './orderCore/userService';
5
5
  export { orderCoreByWebService } from './orderCore/webService';
6
6
  export { orderCoreByJobService } from './orderCore/jobService';
7
+ export { orderCoreByInternalService } from './orderCore/internalService';
7
8
  export { RosOrderCoreService as RosOrderCoreTypes } from './orderCore/types';
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.orderCoreByJobService = exports.orderCoreByWebService = exports.orderCoreByUserService = exports.orderQueryByUserService = exports.orderQueryByWebService = void 0;
3
+ exports.orderCoreByInternalService = exports.orderCoreByJobService = exports.orderCoreByWebService = exports.orderCoreByUserService = exports.orderQueryByUserService = exports.orderQueryByWebService = void 0;
4
4
  var webService_1 = require("./orderQuery/webService");
5
5
  Object.defineProperty(exports, "orderQueryByWebService", { enumerable: true, get: function () { return webService_1.orderQueryByWebService; } });
6
6
  var userService_1 = require("./orderQuery/userService");
@@ -11,3 +11,5 @@ var webService_2 = require("./orderCore/webService");
11
11
  Object.defineProperty(exports, "orderCoreByWebService", { enumerable: true, get: function () { return webService_2.orderCoreByWebService; } });
12
12
  var jobService_1 = require("./orderCore/jobService");
13
13
  Object.defineProperty(exports, "orderCoreByJobService", { enumerable: true, get: function () { return jobService_1.orderCoreByJobService; } });
14
+ var internalService_1 = require("./orderCore/internalService");
15
+ Object.defineProperty(exports, "orderCoreByInternalService", { enumerable: true, get: function () { return internalService_1.orderCoreByInternalService; } });
@@ -0,0 +1,8 @@
1
+ import { RosOrderCoreService } from './types';
2
+ import BaseService from '../../BaseService';
3
+ declare class RosOrderCoreByInternalService extends BaseService implements RosOrderCoreService.CoreByInternalController {
4
+ protected prefixUrl: string;
5
+ orderCreateForVerifyCoupon(request: RosOrderCoreService.ByInternal.Request.IOrderCreateForVerifyCoupon): Promise<RosOrderCoreService.ByInternal.Response.IOrderCreateForVerifyCoupon>;
6
+ }
7
+ export declare const orderCoreByInternalService: RosOrderCoreByInternalService;
8
+ export default orderCoreByInternalService;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
9
+ return function (target, key) { decorator(target, key, paramIndex); }
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.orderCoreByInternalService = void 0;
16
+ const tsoa_1 = require("tsoa");
17
+ const http_1 = require("../../../utils/http");
18
+ const BaseService_1 = __importDefault(require("../../BaseService"));
19
+ let RosOrderCoreByInternalService = class RosOrderCoreByInternalService extends BaseService_1.default {
20
+ constructor() {
21
+ super(...arguments);
22
+ this.prefixUrl = '/ros/core/internal';
23
+ }
24
+ orderCreateForVerifyCoupon(request) {
25
+ return (0, http_1.callApi)(this.getApiUrl(this.orderCreateForVerifyCoupon), request);
26
+ }
27
+ };
28
+ __decorate([
29
+ (0, tsoa_1.OperationId)('创建核销订单的逆向订单'),
30
+ (0, tsoa_1.Post)('order-create-for-verify-coupon'),
31
+ __param(0, (0, tsoa_1.Body)())
32
+ ], RosOrderCoreByInternalService.prototype, "orderCreateForVerifyCoupon", null);
33
+ RosOrderCoreByInternalService = __decorate([
34
+ (0, tsoa_1.Route)('ros/core/internal'),
35
+ (0, tsoa_1.Tags)('RosOrderCore')
36
+ ], RosOrderCoreByInternalService);
37
+ exports.orderCoreByInternalService = new RosOrderCoreByInternalService();
38
+ exports.default = exports.orderCoreByInternalService;
@@ -76,6 +76,21 @@ export declare namespace RosOrderCoreService {
76
76
  }
77
77
  namespace Response { }
78
78
  }
79
+ namespace ByInternal {
80
+ namespace Request {
81
+ interface IOrderCreateForVerifyCoupon {
82
+ /** 用户ID */
83
+ userId: string;
84
+ /** 优惠券ID */
85
+ couponId: string;
86
+ /** 优惠券码 */
87
+ couponCode: string;
88
+ }
89
+ }
90
+ namespace Response {
91
+ type IOrderCreateForVerifyCoupon = void;
92
+ }
93
+ }
79
94
  interface CoreByUserController {
80
95
  orderCreate(request: ByUser.Request.IReverseOrderCreate, req: any): Promise<ByUser.Response.IReverseOrderCreate>;
81
96
  orderCancel(request: ByUser.Request.IReverseOrderCancel, req: any): Promise<ByUser.Response.IReverseOrderCancel>;
@@ -89,4 +104,8 @@ export declare namespace RosOrderCoreService {
89
104
  interface CoreByJobController {
90
105
  autoApprove(request: ByJob.Request.IAutoApprove, req: any): Promise<void>;
91
106
  }
107
+ interface CoreByInternalController {
108
+ /** 创建核销订单的逆向订单 */
109
+ orderCreateForVerifyCoupon(request: ByInternal.Request.IOrderCreateForVerifyCoupon, req: any): Promise<ByInternal.Response.IOrderCreateForVerifyCoupon>;
110
+ }
92
111
  }
@@ -6,16 +6,35 @@ export declare namespace RosOrderQueryService {
6
6
  interface IOrderList {
7
7
  /** 查询条件 */
8
8
  conditions?: {
9
- createdAt: string;
10
- operateTime: string;
11
- orderTime: string;
12
- status: ENUM.ReverseOrderStatus;
13
- arrivalStatusList: ENUM.ReverseArrivalStatus[];
14
- wayList: ENUM.ReverseRefundWay[];
15
- typeList: ENUM.ReverseRefundType[];
16
- userIds: string[];
17
- orderIds: string[];
18
- storeIds: string[];
9
+ /** 下单时间范围 */
10
+ createdAt?: {
11
+ start?: number;
12
+ end?: number;
13
+ };
14
+ /** 退款操作时间范围 */
15
+ operateTime?: {
16
+ start?: number;
17
+ end?: number;
18
+ };
19
+ /** 退款申请时间范围 */
20
+ orderTime?: {
21
+ start?: number;
22
+ end?: number;
23
+ };
24
+ /** 退款订单状态列表 */
25
+ statusList?: ENUM.ReverseOrderStatus[];
26
+ /** 退款到账状态列表 */
27
+ arrivalStatusList?: ENUM.ReverseArrivalStatus[];
28
+ /** 退款方式列表 */
29
+ wayList?: ENUM.ReverseRefundWay[];
30
+ /** 退款类型列表 */
31
+ typeList?: ENUM.ReverseRefundType[];
32
+ /** 用户ID列表 */
33
+ userIds?: string[];
34
+ /** 正向订单ID列表 */
35
+ orderIds?: string[];
36
+ /** 门店ID列表 */
37
+ storeIds?: string[];
19
38
  };
20
39
  /** 分页 */
21
40
  pagination: {
@@ -40,6 +59,8 @@ export declare namespace RosOrderQueryService {
40
59
  reverseOrderIds?: string[];
41
60
  /** 订单ID列表 */
42
61
  orderIds?: string[];
62
+ /** 订单类型 */
63
+ orderTypeList?: ENUM.OrderType[];
43
64
  /** 用户ID列表 */
44
65
  userIds?: string[];
45
66
  /** 门店ID列表 */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@be-link/ecommerce-trade-service-node-sdk",
3
- "version": "0.1.61",
3
+ "version": "0.1.63",
4
4
  "description": "EcommerceTradeService Node.js SDK",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /** 正向订单 */
2
2
  /** 基础信息 */
3
3
  import { ENUM } from './enums';
4
- import { PRODUCT_SERVICE_ENUM } from '@be-link/ecommerce-product-service-node-sdk';
4
+ import { PRODUCT_SERVICE_ENUM, ProductServiceTypes } from '@be-link/ecommerce-product-service-node-sdk';
5
5
  export interface ITradeOrder {
6
6
  id: string;
7
7
  userId: string;
@@ -44,13 +44,36 @@ export interface ITradeOrderSkuInfo {
44
44
  id: string;
45
45
  orderId: string;
46
46
  quantity: number;
47
- productSnapshot: string;
48
- skuSnapshot: any;
47
+ /** 商品快照信息 */
48
+ productSnapshot: ProductServiceTypes.Response.getProductSkuInfo['productInfo'];
49
+ /** 商品SKU快照信息 */
50
+ skuSnapshot: ProductServiceTypes.Response.getProductSkuInfo['skuList'][0];
51
+ /** 商品Id */
49
52
  productId: string | null;
53
+ /** 商品名称 */
50
54
  productName: string | null;
55
+ /** skuId */
51
56
  skuId: string | null;
57
+ /** 商品规格编码 */
52
58
  skuCode: string | null;
59
+ /** 商品主图URL */
60
+ productImg: string | null;
61
+ /** 商品单价(分) */
53
62
  productPrice: number | null;
63
+ /** 商品规格列表 */
64
+ productSpec: ProductServiceTypes.Attr[] | null;
65
+ /** 商品提货方式 */
66
+ productPick: PRODUCT_SERVICE_ENUM.PRODUCT_ENUM.PICK_TYPE | null;
67
+ /** 商品发货方式 */
68
+ productDispatch: PRODUCT_SERVICE_ENUM.PRODUCT_ENUM.DISPATCH_TYPE | null;
69
+ /** 子商品SKU信息列表 */
70
+ subProductSkuInfos: {
71
+ productId: string;
72
+ skuId: string;
73
+ quantity: number;
74
+ productSnapshot: ProductServiceTypes.Response.getProductSkuInfo['productInfo'];
75
+ skuSnapshot: ProductServiceTypes.Response.getProductSkuInfo['skuList'][0];
76
+ }[] | null;
54
77
  createdAt: number;
55
78
  updatedAt: number;
56
79
  }
@@ -154,6 +177,7 @@ export interface ITradeReverseOrder {
154
177
  isFullRefund: number;
155
178
  createdAt: number;
156
179
  updatedAt: number;
180
+ orderItemIds: string[];
157
181
  }
158
182
  /** 资源扩展信息类型 */
159
183
  export interface IBizExtraInfo {
package/utils/http.js CHANGED
@@ -1,4 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
37
  };
@@ -8,109 +41,28 @@ const axios_1 = __importDefault(require("axios"));
8
41
  const uuid_1 = require("uuid");
9
42
  const axios_retry_1 = __importDefault(require("axios-retry"));
10
43
  const request_context_1 = require("@fastify/request-context");
11
- const http_1 = __importDefault(require("http"));
12
- const https_1 = __importDefault(require("https"));
13
44
  const safe_stable_stringify_1 = __importDefault(require("safe-stable-stringify"));
14
- // 针对高并发优化配置 - 支持2000并发
15
- const HTTP_CONFIG = {
16
- maxSockets: 3000, // 每个主机的最大socket连接数(支持2000并发)
17
- maxFreeSockets: 1000, // 空闲socket保留数(50%复用率,提高连接复用效率)
18
- maxTotalSockets: 10000, // 所有主机总socket数(单后端场景优化)
19
- keepAliveMsecs: 30000, // 保持连接30秒(平衡资源占用和连接复用)
20
- timeout: 5000, // socket超时3秒(高并发场景快速失败)
21
- requestTimeout: 5000, // 请求超时5秒(快速释放资源,避免慢请求占用连接)
22
- retryBaseDelay: 200, // 基础重试延迟100ms(给服务恢复时间)
23
- logThreshold: 1000, // 日志采样率(每1000个请求采样1次,降低日志开销)
24
- monitorInterval: 5000, // 监控日志间隔(毫秒)
25
- };
26
- // 配置 HTTP/HTTPS Agent 以支持高并发连接池和 keepAlive
27
- const httpAgent = new http_1.default.Agent({
28
- keepAlive: true,
29
- keepAliveMsecs: HTTP_CONFIG.keepAliveMsecs,
30
- maxSockets: HTTP_CONFIG.maxSockets,
31
- maxFreeSockets: HTTP_CONFIG.maxFreeSockets,
32
- maxTotalSockets: HTTP_CONFIG.maxTotalSockets,
33
- timeout: HTTP_CONFIG.timeout,
34
- scheduling: 'fifo', // 先进先出调度
35
- });
36
- const httpsAgent = new https_1.default.Agent({
37
- keepAlive: true,
38
- keepAliveMsecs: HTTP_CONFIG.keepAliveMsecs,
39
- maxSockets: HTTP_CONFIG.maxSockets,
40
- maxFreeSockets: HTTP_CONFIG.maxFreeSockets,
41
- maxTotalSockets: HTTP_CONFIG.maxTotalSockets,
42
- timeout: HTTP_CONFIG.timeout,
43
- scheduling: 'fifo',
44
- });
45
- // 配置 axios 默认使用这些 agent
46
- axios_1.default.defaults.httpAgent = httpAgent;
47
- axios_1.default.defaults.httpsAgent = httpsAgent;
48
- axios_1.default.defaults.timeout = HTTP_CONFIG.requestTimeout;
49
- axios_1.default.defaults.maxRedirects = 3; // 限制重定向次数
50
- axios_1.default.defaults.maxContentLength = 50 * 1024 * 1024; // 50MB 最大响应大小
51
- // 高并发场景下的智能重试配置
52
45
  (0, axios_retry_1.default)(axios_1.default, {
46
+ retries: 1,
53
47
  retryCondition(error) {
54
- // 重试临时错误和特定的网络错误
55
- const status = error.response?.status;
56
- const isNetworkError = error.code === 'ECONNRESET' || error.code === 'ETIMEDOUT';
57
- const isServerError = status === 502 || status === 503 || status === 504;
58
- const isRateLimited = status === 429;
59
- return isNetworkError || isServerError || isRateLimited;
48
+ return error.response?.status === 502;
60
49
  },
61
- retryDelay: (retryCount, error) => {
62
- // 指数退避 + 抖动
63
- const status = error.response?.status;
64
- // 如果是限流错误,延迟更长
65
- if (status === 429) {
66
- const retryAfter = error.response?.headers['retry-after'];
67
- if (retryAfter) {
68
- return parseInt(retryAfter) * 1000;
69
- }
70
- return 1000 + Math.random() * 1000; // 1-2秒
71
- }
72
- // 指数退避:100ms, 200ms, 400ms...
73
- const exponentialDelay = HTTP_CONFIG.retryBaseDelay * Math.pow(2, retryCount - 1);
74
- const jitter = Math.random() * 50; // 0-50ms 抖动
75
- return Math.min(exponentialDelay + jitter, 2000); // 最多延迟2秒
50
+ retryDelay: (retryCount) => {
51
+ console.info(`retryCount: ${retryCount}, retryDelay: ${retryCount * 500}`);
52
+ return retryCount * 500;
53
+ },
54
+ onRetry(retryCount, error, requestConfig) {
55
+ console.info(`retryCount: ${retryCount}, onRetry: ${error.message}, requestHeader: ${(0, safe_stable_stringify_1.default)(requestConfig.headers)}`);
76
56
  },
77
- shouldResetTimeout: true, // 重试时重置超时
78
57
  });
79
- // 连接池状态监控
80
- let requestCount = 0;
81
- let activeRequests = 0;
82
- let peakActiveRequests = 0; // 峰值并发
83
- // 注意:已移除应用层并发队列限制,完全依赖底层socket连接池控制
84
- // 这样可以避免应用层排队等待,充分利用socket连接池的并发能力
85
58
  async function callApi(url, request) {
86
- // 不再等待应用层队列,直接发起请求,由底层socket连接池控制并发
87
- const currentRequestId = ++requestCount;
88
- activeRequests++;
89
- // 记录峰值并发
90
- if (activeRequests > peakActiveRequests) {
91
- peakActiveRequests = activeRequests;
92
- }
93
- // 批量获取请求上下文,减少函数调用开销
94
- const ctx = request_context_1.requestContext.get('requestId') ? request_context_1.requestContext : null;
95
- const requestId = ctx?.get('requestId') || ctx?.get('traceMessageId') || (0, uuid_1.v4)();
96
- const pandoraUserId = ctx?.get('pandoraUserId') || '';
97
- const beLinkUserId = ctx?.get('beLinkUserId') || '';
98
- const pandoraRoleId = ctx?.get('pandoraRoleId') || '';
99
- const realIp = ctx?.get('realIp') || '';
100
- // 采样日志决策(提前计算,避免重复判断)
101
- const shouldLog = currentRequestId % HTTP_CONFIG.logThreshold === 0;
102
- const startTime = shouldLog ? Date.now() : 0;
59
+ const requestId = request_context_1.requestContext.get('requestId') || request_context_1.requestContext.get('traceMessageId') || (0, uuid_1.v4)();
60
+ const pandoraUserId = request_context_1.requestContext.get('pandoraUserId') || '';
61
+ const beLinkUserId = request_context_1.requestContext.get('beLinkUserId') || '';
62
+ const pandoraRoleId = request_context_1.requestContext.get('pandoraRoleId') || '';
63
+ const realIp = request_context_1.requestContext.get('realIp') || '';
103
64
  try {
104
- if (shouldLog) {
105
- console.log((0, safe_stable_stringify_1.default)({
106
- message: '发起HTTP请求',
107
- currentRequestId,
108
- activeRequests,
109
- requestId,
110
- url,
111
- type: 'http_request_start',
112
- }));
113
- }
65
+ console.info(`准备发起ecommerce-trade-service请求[${requestId}]: ${url}, 参数: ${(0, safe_stable_stringify_1.default)(request)}`);
114
66
  const response = await axios_1.default.post(url, request, {
115
67
  headers: {
116
68
  'x-request-id': requestId,
@@ -118,181 +70,42 @@ async function callApi(url, request) {
118
70
  'x-belink-userid': beLinkUserId,
119
71
  'x-belink-pandora-roleid': pandoraRoleId,
120
72
  'x-real-ip': realIp,
121
- Connection: 'keep-alive',
122
73
  },
123
- timeout: HTTP_CONFIG.requestTimeout,
124
- httpAgent,
125
- httpsAgent,
126
74
  });
127
- if (shouldLog) {
128
- const duration = Date.now() - startTime;
129
- if (duration > 3000) {
130
- console.log((0, safe_stable_stringify_1.default)({
131
- message: '请求完成',
132
- currentRequestId,
133
- duration: `${duration}ms`,
134
- activeRequests,
135
- type: 'http_request_complete',
136
- }));
137
- }
138
- }
139
75
  const responseData = response.data;
140
76
  return responseData.data;
141
77
  }
142
78
  catch (error) {
143
79
  const axiosError = error;
144
- const duration = startTime ? Date.now() - startTime : 0;
145
- // 错误始终记录(但简化输出)
146
- console.error((0, safe_stable_stringify_1.default)({
147
- message: '请求失败',
148
- currentRequestId,
149
- url,
150
- duration: `${duration}ms`,
151
- activeRequests,
152
- errorMessage: axiosError.message,
153
- type: 'http_request_failed',
154
- }));
155
80
  if (axiosError.response) {
156
81
  const response = axiosError.response;
157
82
  const data = response.data;
158
- console.error((0, safe_stable_stringify_1.default)({
159
- message: '响应异常',
160
- status: response.status,
161
- errorMessage: data?.message || axiosError.message,
162
- type: 'http_response_error',
163
- }));
83
+ console.error(`ecommerce-trade-service 异常: ${axiosError.message},requestId: ${requestId}`);
84
+ console.info('响应信息', data.message);
85
+ console.error('异常堆栈', (0, safe_stable_stringify_1.default)(error.stack));
86
+ // throw new Error(data.errorType + ' - ' + data.message)
87
+ throw error;
164
88
  }
89
+ // 调用dns模块解析url
90
+ const dns = await Promise.resolve().then(() => __importStar(require('dns')));
91
+ const dnsPromise = new Promise((resolve, reject) => {
92
+ const lookupRes = dns.lookup(url, (err, address) => {
93
+ if (err) {
94
+ console.error(err.message);
95
+ reject(err);
96
+ }
97
+ console.info(`lookup: ${(0, safe_stable_stringify_1.default)(lookupRes)}`);
98
+ resolve(address);
99
+ });
100
+ });
101
+ try {
102
+ const address = await dnsPromise;
103
+ console.info(`address: ${(0, safe_stable_stringify_1.default)(address)}`);
104
+ }
105
+ catch (error) {
106
+ console.info(`error: ${(0, safe_stable_stringify_1.default)(error)}`);
107
+ }
108
+ console.error(`ecommerce-trade-service 未知异常: ${axiosError.message}`, error.stack);
165
109
  throw error;
166
110
  }
167
- finally {
168
- // 记录请求完成,减少活跃计数
169
- activeRequests--;
170
- }
171
111
  }
172
- // 导出监控函数
173
- // export function getConnectionStats() {
174
- // const stats = {
175
- // // 当前状态
176
- // activeRequests,
177
- // totalRequests: requestCount,
178
- // // 峰值指标
179
- // peakActiveRequests,
180
- // // Socket连接池状态(这是真正的并发控制点)
181
- // httpSockets: {
182
- // total: Object.keys(httpAgent.sockets).reduce(
183
- // (sum, host) => sum + (httpAgent.sockets[host]?.length || 0),
184
- // 0,
185
- // ),
186
- // hosts: Object.keys(httpAgent.sockets).length,
187
- // details: httpAgent.sockets,
188
- // },
189
- // httpsSockets: {
190
- // total: Object.keys(httpsAgent.sockets).reduce(
191
- // (sum, host) => sum + (httpsAgent.sockets[host]?.length || 0),
192
- // 0,
193
- // ),
194
- // hosts: Object.keys(httpsAgent.sockets).length,
195
- // details: httpsAgent.sockets,
196
- // },
197
- // // 配置信息
198
- // config: {
199
- // maxSockets: HTTP_CONFIG.maxSockets,
200
- // maxTotalSockets: HTTP_CONFIG.maxTotalSockets,
201
- // requestTimeout: HTTP_CONFIG.requestTimeout,
202
- // note: '已移除应用层并发队列限制,完全依赖socket连接池',
203
- // },
204
- // }
205
- // return stats
206
- // }
207
- // 重置峰值统计(可用于定期重置)
208
- // export function resetPeakStats() {
209
- // peakActiveRequests = activeRequests
210
- // console.log(
211
- // stringify({
212
- // message: '峰值统计已重置',
213
- // type: 'stats_reset',
214
- // }),
215
- // )
216
- // }
217
- // // 定期监控日志
218
- // let monitorTimer: NodeJS.Timeout | null = null
219
- // export function startMonitoring(interval: number = HTTP_CONFIG.monitorInterval) {
220
- // if (monitorTimer) {
221
- // console.log(
222
- // stringify({
223
- // message: '监控已在运行中',
224
- // type: 'monitor_already_running',
225
- // }),
226
- // )
227
- // return
228
- // }
229
- // console.log(
230
- // stringify({
231
- // message: '启动并发监控',
232
- // interval: `${interval}ms`,
233
- // type: 'monitor_start',
234
- // }),
235
- // )
236
- // monitorTimer = setInterval(() => {
237
- // const stats = getConnectionStats()
238
- // // 只在有活动时打印
239
- // if (stats.totalRequests === 0) {
240
- // return
241
- // }
242
- // // 检测异常情况(基于socket连接池使用率)
243
- // const httpSocketsUsage = stats.httpSockets.total / HTTP_CONFIG.maxSockets
244
- // const httpsSocketsUsage = stats.httpsSockets.total / HTTP_CONFIG.maxSockets
245
- // const maxSocketUsage = Math.max(httpSocketsUsage, httpsSocketsUsage)
246
- // // Socket连接池使用率超过80%视为瓶颈
247
- // const hasIssue = maxSocketUsage > 0.8
248
- // if (hasIssue) {
249
- // console.warn(
250
- // stringify({
251
- // message: '并发监控检测到瓶颈',
252
- // 当前并发: stats.activeRequests,
253
- // 峰值并发: stats.peakActiveRequests,
254
- // 总请求数: stats.totalRequests,
255
- // HTTP_Sockets: `${stats.httpSockets.total}/${HTTP_CONFIG.maxSockets} (${Math.round(
256
- // httpSocketsUsage * 100,
257
- // )}%)`,
258
- // HTTPS_Sockets: `${stats.httpsSockets.total}/${HTTP_CONFIG.maxSockets} (${Math.round(
259
- // httpsSocketsUsage * 100,
260
- // )}%)`,
261
- // 建议: 'Socket连接池接近饱和,考虑提高maxSockets配置',
262
- // type: 'monitor_bottleneck_detected',
263
- // }),
264
- // )
265
- // } else if (stats.totalRequests % 10000 === 0) {
266
- // // 每1万次请求打印一次正常状态
267
- // console.log(
268
- // stringify({
269
- // message: '并发监控运行正常',
270
- // 总请求: stats.totalRequests,
271
- // 当前并发: stats.activeRequests,
272
- // 峰值并发: stats.peakActiveRequests,
273
- // HTTP_Sockets使用率: `${Math.round(httpSocketsUsage * 100)}%`,
274
- // HTTPS_Sockets使用率: `${Math.round(httpsSocketsUsage * 100)}%`,
275
- // type: 'monitor_normal',
276
- // }),
277
- // )
278
- // }
279
- // }, interval)
280
- // }
281
- // export function stopMonitoring() {
282
- // if (monitorTimer) {
283
- // clearInterval(monitorTimer)
284
- // monitorTimer = null
285
- // console.log(
286
- // stringify({
287
- // message: '监控已停止',
288
- // type: 'monitor_stop',
289
- // }),
290
- // )
291
- // }
292
- // }
293
- // // 自动启动监控(可选,如果不想自动启动可以注释掉)
294
- // if (process.env.NODE_ENV !== 'test') {
295
- // setTimeout(() => {
296
- // startMonitoring()
297
- // }, 3000) // 延迟3秒启动,避免服务启动时的干扰
298
- // }