From 498fa0e9153bdc2b24660c9c07914313b3edefb7 Mon Sep 17 00:00:00 2001 From: ECRZ Date: Tue, 6 Jan 2026 11:29:54 +0800 Subject: [PATCH] =?UTF-8?q?modify=EF=BC=9A=E6=8E=A5=E5=8F=A3=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commonApi.js | 251 ++++++++++++++++++++++++++++++++++++++++++++------- 接口cURL.txt | 8 ++ 2 files changed, 228 insertions(+), 31 deletions(-) create mode 100644 接口cURL.txt diff --git a/commonApi.js b/commonApi.js index a9120e6..a627901 100644 --- a/commonApi.js +++ b/commonApi.js @@ -1,14 +1,133 @@ const axios = require('axios'); const login = require('./loginApi.js'); + +/** + * 钢材价格数据采集服务配置 + * @typedef {Object} SteelPriceConfig + * @property {string} baseURL - 基础 URL + * @property {string} pageId - 页面 ID + * @property {string} menuId - 菜单 ID + */ + +/** + * 预定义的接口配置 + * 根据接口cURL.txt中的三个接口定义 + */ +const API_ENDPOINTS = { + /** + * 接口 1: 默认钢材价格查询 + */ + DEFAULT: { + name: '默认钢材价格查询', + pageId: 'PG-D615-D8E2-2FD84B8D', + menuId: 'MK-A8B8-109E-13D34116' + }, + + /** + * 接口 2: 备用钢材价格查询 + */ + BACKUP: { + name: '备用钢材价格查询', + pageId: 'PG-B9F4-CDC8-6EDE4F14', + menuId: 'MK-0967-9408-F5D4430C' + }, + + /** + * 接口 3: 扩展钢材价格查询 + */ + EXTENDED: { + name: '扩展钢材价格查询', + pageId: 'PG-3705-8CD5-7B6F4EB8', + menuId: 'MK-32E1-6A12-66314921' + } +}; + +/** + * 默认配置 + * @type {SteelPriceConfig} + */ +const DEFAULT_CONFIG = { + baseURL: 'https://xdwlgyl.yciccloud.com', + pageId: 'PG-D615-D8E2-2FD84B8D', + menuId: 'MK-A8B8-109E-13D34116' +}; + /** * 钢材价格数据采集服务 * 从德钢云平台获取价格数据 + * @class SteelPriceCollector */ class SteelPriceCollector { - constructor() { - this.baseURL = 'https://xdwlgyl.yciccloud.com'; - this.pageId = 'PG-D615-D8E2-2FD84B8D'; - this.menuId = 'MK-A8B8-109E-13D34116'; + /** + * 构造函数 + * @param {SteelPriceConfig} config - 服务配置 + */ + constructor(config = {}) { + /** + * 服务配置 + * @type {SteelPriceConfig} + * @private + */ + this._config = { + baseURL: config.baseURL || DEFAULT_CONFIG.baseURL, + pageId: config.pageId || DEFAULT_CONFIG.pageId, + menuId: config.menuId || DEFAULT_CONFIG.menuId + }; + } + + /** + * 获取配置 + * @returns {SteelPriceConfig} 当前配置 + */ + getConfig() { + return { ...this._config }; + } + + /** + * 更新配置 + * @param {Partial} config - 要更新的配置 + */ + updateConfig(config) { + this._config = { + ...this._config, + ...config + }; + } + + /** + * 使用预定义的接口配置 + * @param {string} endpointKey - 接口键名 ('DEFAULT' | 'BACKUP' | 'EXTENDED') + */ + useEndpoint(endpointKey) { + const endpoint = API_ENDPOINTS[endpointKey]; + + if (!endpoint) { + throw new Error(`未知的接口端点: ${endpointKey}。可用选项: ${Object.keys(API_ENDPOINTS).join(', ')}`); + } + + console.log(`🔄 切换到接口: ${endpoint.name}`); + console.log(` Page ID: ${endpoint.pageId}`); + console.log(` Menu ID: ${endpoint.menuId}`); + + this.updateConfig({ + pageId: endpoint.pageId, + menuId: endpoint.menuId + }); + } + + /** + * 获取可用的接口列表 + * @returns {Object} 接口列表 + */ + getAvailableEndpoints() { + return Object.keys(API_ENDPOINTS).reduce((acc, key) => { + acc[key] = { + name: API_ENDPOINTS[key].name, + pageId: API_ENDPOINTS[key].pageId, + menuId: API_ENDPOINTS[key].menuId + }; + return acc; + }, {}); } /** @@ -19,6 +138,7 @@ class SteelPriceCollector { * @param {number} params.page - 页码,默认1 * @param {number} params.pageSize - 每页大小,默认1000 * @returns {Promise} Axios请求配置 + * @private */ async _buildConfig(params = {}) { const { @@ -69,7 +189,7 @@ class SteelPriceCollector { // 构建URL编码的请求体 const requestData = new URLSearchParams({ - pageId: this.pageId, + pageId: this._config.pageId, pageSize: pageSize.toString(), page: page.toString(), isPageNoChange: '0', @@ -84,16 +204,16 @@ class SteelPriceCollector { return { method: 'POST', - url: `${this.baseURL}/gdpaas/mainpage/findPage.htm?_p_=${this.pageId}`, + url: `${this._config.baseURL}/gdpaas/mainpage/findPage.htm?_p_=${this._config.pageId}`, headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 'Accept': 'application/json, text/javascript, */*; q=0.01', 'X-Requested-With': 'XMLHttpRequest', - 'Pageid': 'PG_D615_D8E2_2FD84B8D', - 'Menuid': this.menuId, + 'Pageid': this._config.pageId, + 'Menuid': this._config.menuId, 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36', - 'Referer': `${this.baseURL}/gdpaas/home/index.htm`, - 'Origin': this.baseURL, + 'Referer': `${this._config.baseURL}/gdpaas/home/index.htm`, + 'Origin': this._config.baseURL, 'Accept-Language': 'zh-CN,zh;q=0.9', 'Accept-Encoding': 'gzip, deflate, br', 'Sec-Fetch-Site': 'same-origin', @@ -139,33 +259,102 @@ class SteelPriceCollector { } /** - * 导出单例实例 + * 导出类和默认实例 */ -module.exports = new SteelPriceCollector(); +module.exports = SteelPriceCollector; +module.exports.default = new SteelPriceCollector(); +module.exports.API_ENDPOINTS = API_ENDPOINTS; /** * 如果直接运行此文件,执行示例请求 */ if (require.main === module) { - const collector = require('./commonApi.js'); + const SteelPriceCollector = require('./commonApi.js'); - // 示例:获取最近一年的价格数据 - collector.fetchPrices({ - startDate: '2025-01-06', - endDate: '2026-01-06', - page: 1, - pageSize: 1 - }) - .then(result => { - if (result.success) { - console.log('✅ 数据获取成功'); - console.log('📅 时间:', result.timestamp); - console.log('📊 数据预览:', JSON.stringify(result.data, null, 2)); - } else { - console.error('❌ 数据获取失败:', result.error); - } - }) - .catch(error => { - console.error('💥 未捕获的错误:', error); + async function testAllEndpoints() { + console.log('🚀 测试三个接口\n'); + + const collector = new SteelPriceCollector(); + + // 显示可用接口 + console.log('📋 可用接口列表:'); + const endpoints = collector.getAvailableEndpoints(); + Object.entries(endpoints).forEach(([key, info]) => { + console.log(` ${key}: ${info.name}`); + console.log(` Page ID: ${info.pageId}`); + console.log(` Menu ID: ${info.menuId}\n`); }); + + // 测试接口 1: DEFAULT + console.log('\n' + '='.repeat(60)); + console.log('测试接口 1: DEFAULT(默认钢材价格查询)'); + console.log('='.repeat(60) + '\n'); + + collector.useEndpoint('DEFAULT'); + + const result1 = await collector.fetchPrices({ + startDate: '2025-01-06', + endDate: '2026-01-06', + page: 1, + pageSize: 1 + }); + + if (result1.success) { + console.log('✅ 接口 1 调用成功'); + console.log('📊 返回数据:', JSON.stringify(result1.data, null, 2)); + } else { + console.error('❌ 接口 1 调用失败:', result1.error); + } + + // 测试接口 2: BACKUP + console.log('\n' + '='.repeat(60)); + console.log('测试接口 2: BACKUP(备用钢材价格查询)'); + console.log('='.repeat(60) + '\n'); + + collector.useEndpoint('BACKUP'); + + const result2 = await collector.fetchPrices({ + startDate: '2025-01-06', + endDate: '2026-01-06', + page: 1, + pageSize: 1 + }); + + if (result2.success) { + console.log('✅ 接口 2 调用成功'); + console.log('📊 返回数据:', JSON.stringify(result2.data, null, 2)); + } else { + console.error('❌ 接口 2 调用失败:', result2.error); + } + + // 测试接口 3: EXTENDED + console.log('\n' + '='.repeat(60)); + console.log('测试接口 3: EXTENDED(扩展钢材价格查询)'); + console.log('='.repeat(60) + '\n'); + + collector.useEndpoint('EXTENDED'); + + const result3 = await collector.fetchPrices({ + startDate: '2025-01-06', + endDate: '2026-01-06', + page: 1, + pageSize: 1 + }); + + if (result3.success) { + console.log('✅ 接口 3 调用成功'); + console.log('📊 返回数据:', JSON.stringify(result3.data, null, 2)); + } else { + console.error('❌ 接口 3 调用失败:', result3.error); + } + + console.log('\n' + '='.repeat(60)); + console.log('✅ 所有接口测试完成!'); + console.log('='.repeat(60) + '\n'); + } + + // 运行测试 + testAllEndpoints().catch(error => { + console.error('💥 测试失败:', error); + }); } diff --git a/接口cURL.txt b/接口cURL.txt new file mode 100644 index 0000000..64065a1 --- /dev/null +++ b/接口cURL.txt @@ -0,0 +1,8 @@ +curl -k -i --raw -o 0.dat -X POST --data-raw "pageId=PG-D615-D8E2-2FD84B8D&pageSize=1000&page=1&isPageNoChange=0&ctrlId=mainTable&json=%7B%22_PIRCE_DATE_DATE_START_%22%3A%222025-01-06%22%2C%22_PIRCE_DATE_DATE_END_%22%3A%222026-01-06%22%2C%22PARTSNAME_NAME%22%3Anull%2C%22_PARTSNAME_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PARTSNAME_NAME_IS_LIKE_%22%3A%221%22%2C%22GOODS_MATERIAL%22%3Anull%2C%22_GOODS_MATERIAL_ADVANCE_SEARCH_%22%3Anull%2C%22_GOODS_MATERIAL_IS_LIKE_%22%3A%221%22%2C%22GOODS_SPEC%22%3Anull%2C%22_GOODS_SPEC_ADVANCE_SEARCH_%22%3Anull%2C%22_GOODS_SPEC_IS_LIKE_%22%3A%221%22%2C%22PRODUCTAREA_NAME%22%3Anull%2C%22_PRODUCTAREA_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PRODUCTAREA_NAME_IS_LIKE_%22%3A%221%22%2C%22PNTREE_NAME%22%3Anull%2C%22_PNTREE_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PNTREE_NAME_IS_LIKE_%22%3A%221%22%2C%22OPERATOR_NAME%22%3Anull%2C%22_OPERATOR_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22PR_PRICE_REGION%22%3Anull%2C%22_PR_PRICE_REGION_ADVANCE_SEARCH_%22%3Anull%2C%22OPERATOR_CODE%22%3Anull%2C%22PRICE_ID%22%3Anull%2C%22_orderFields_%22%3A%5B%5D%7D&searchBoxId=searchForm&groupSumFields=%5B%5D" -H "host: xdwlgyl.yciccloud.com" -H "content-length: 1060" -H "parentid: " -H "sec-ch-ua: \"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"" -H "sec-ch-ua-mobile: ?0" -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" -H "content-type: application/x-www-form-urlencoded; charset=UTF-8" -H "accept: application/json, text/javascript, */*; q=0.01" -H "x-requested-with: XMLHttpRequest" -H "pageid: PG_D615_D8E2_2FD84B8D" -H "menuid: MK-A8B8-109E-13D34116" -H "sec-ch-ua-platform: \"Windows\"" -H "origin: https://xdwlgyl.yciccloud.com" -H "sec-fetch-site: same-origin" -H "sec-fetch-mode: cors" -H "sec-fetch-dest: empty" -H "referer: https://xdwlgyl.yciccloud.com/gdpaas/home/index.htm" -H "accept-encoding: gzip, deflate, br" -H "accept-language: zh-CN,zh;q=0.9" -H "cookie: SameSite=Lax; HWWAFSESTIME=1767596069712; HWWAFSESID=e2b2773ee2641b98e5; SameSite=Lax; SYS-A7EE-D0E8-BE614B80=1108@f1ee335282c947faaaf66db345a3df2a; JSESSIONID=1BB0C51760229A028C43B904D55686A8" "https://xdwlgyl.yciccloud.com/gdpaas/mainpage/findPage.htm?_p_=PG-D615-D8E2-2FD84B8D" + + +curl -k -i --raw -o 0.dat -X POST --data-raw "pageId=PG-B9F4-CDC8-6EDE4F14&pageSize=1000&page=1&isPageNoChange=0&ctrlId=mainTable&json=%7B%22_PIRCE_DATE_DATE_START_%22%3A%222025-01-06%22%2C%22_PIRCE_DATE_DATE_END_%22%3A%222026-01-06%22%2C%22PARTSNAME_NAME%22%3Anull%2C%22_PARTSNAME_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PARTSNAME_NAME_IS_LIKE_%22%3A%221%22%2C%22GOODS_MATERIAL%22%3Anull%2C%22_GOODS_MATERIAL_ADVANCE_SEARCH_%22%3Anull%2C%22_GOODS_MATERIAL_IS_LIKE_%22%3A%221%22%2C%22GOODS_SPEC%22%3Anull%2C%22_GOODS_SPEC_ADVANCE_SEARCH_%22%3Anull%2C%22_GOODS_SPEC_IS_LIKE_%22%3A%221%22%2C%22PRODUCTAREA_NAME%22%3Anull%2C%22_PRODUCTAREA_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PRODUCTAREA_NAME_IS_LIKE_%22%3A%221%22%2C%22PNTREE_NAME%22%3Anull%2C%22_PNTREE_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PNTREE_NAME_IS_LIKE_%22%3A%221%22%2C%22OPERATOR_NAME%22%3Anull%2C%22_OPERATOR_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22PR_PRICE_REGION%22%3Anull%2C%22_PR_PRICE_REGION_ADVANCE_SEARCH_%22%3Anull%2C%22OPERATOR_CODE%22%3Anull%2C%22PRICE_ID%22%3Anull%2C%22_orderFields_%22%3A%5B%5D%7D&searchBoxId=searchForm&groupSumFields=%5B%5D" -H "host: xdwlgyl.yciccloud.com" -H "content-length: 1060" -H "parentid: " -H "sec-ch-ua: \"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"" -H "sec-ch-ua-mobile: ?0" -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" -H "content-type: application/x-www-form-urlencoded; charset=UTF-8" -H "accept: application/json, text/javascript, */*; q=0.01" -H "x-requested-with: XMLHttpRequest" -H "pageid: PG_B9F4_CDC8_6EDE4F14" -H "menuid: MK-0967-9408-F5D4430C" -H "sec-ch-ua-platform: \"Windows\"" -H "origin: https://xdwlgyl.yciccloud.com" -H "sec-fetch-site: same-origin" -H "sec-fetch-mode: cors" -H "sec-fetch-dest: empty" -H "referer: https://xdwlgyl.yciccloud.com/gdpaas/home/index.htm" -H "accept-encoding: gzip, deflate, br" -H "accept-language: zh-CN,zh;q=0.9" -H "cookie: SameSite=Lax; HWWAFSESTIME=1767596069712; HWWAFSESID=e2b2773ee2641b98e5; SameSite=Lax; SYS-A7EE-D0E8-BE614B80=1108@f1ee335282c947faaaf66db345a3df2a; JSESSIONID=654D8A60A2F7E34CDCC47C7228A96BAF" "https://xdwlgyl.yciccloud.com/gdpaas/mainpage/findPage.htm?_p_=PG-B9F4-CDC8-6EDE4F14" + + +curl -k -i --raw -o 0.dat -X POST --data-raw "pageId=PG-3705-8CD5-7B6F4EB8&pageSize=1000&page=1&isPageNoChange=0&ctrlId=mainTable&json=%7B%22_PIRCE_DATE_DATE_START_%22%3A%222025-01-06%22%2C%22_PIRCE_DATE_DATE_END_%22%3A%222026-01-06%22%2C%22PARTSNAME_NAME%22%3Anull%2C%22_PARTSNAME_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PARTSNAME_NAME_IS_LIKE_%22%3A%221%22%2C%22GOODS_MATERIAL%22%3Anull%2C%22_GOODS_MATERIAL_ADVANCE_SEARCH_%22%3Anull%2C%22_GOODS_MATERIAL_IS_LIKE_%22%3A%221%22%2C%22GOODS_SPEC%22%3Anull%2C%22_GOODS_SPEC_ADVANCE_SEARCH_%22%3Anull%2C%22_GOODS_SPEC_IS_LIKE_%22%3A%221%22%2C%22PRODUCTAREA_NAME%22%3Anull%2C%22_PRODUCTAREA_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PRODUCTAREA_NAME_IS_LIKE_%22%3A%221%22%2C%22PNTREE_NAME%22%3Anull%2C%22_PNTREE_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22_PNTREE_NAME_IS_LIKE_%22%3A%221%22%2C%22OPERATOR_NAME%22%3Anull%2C%22_OPERATOR_NAME_ADVANCE_SEARCH_%22%3Anull%2C%22OPERATOR_CODE%22%3Anull%2C%22PRICE_ID%22%3Anull%2C%22PR_PRICE_REGION%22%3Anull%2C%22_orderFields_%22%3A%5B%5D%7D&searchBoxId=searchForm&groupSumFields=%5B%5D" -H "host: xdwlgyl.yciccloud.com" -H "content-length: 1012" -H "parentid: " -H "sec-ch-ua: \"Not_A Brand\";v=\"8\", \"Chromium\";v=\"120\", \"Google Chrome\";v=\"120\"" -H "sec-ch-ua-mobile: ?0" -H "user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" -H "content-type: application/x-www-form-urlencoded; charset=UTF-8" -H "accept: application/json, text/javascript, */*; q=0.01" -H "x-requested-with: XMLHttpRequest" -H "pageid: PG_3705_8CD5_7B6F4EB8" -H "menuid: MK-32E1-6A12-66314921" -H "sec-ch-ua-platform: \"Windows\"" -H "origin: https://xdwlgyl.yciccloud.com" -H "sec-fetch-site: same-origin" -H "sec-fetch-mode: cors" -H "sec-fetch-dest: empty" -H "referer: https://xdwlgyl.yciccloud.com/gdpaas/home/index.htm" -H "accept-encoding: gzip, deflate, br" -H "accept-language: zh-CN,zh;q=0.9" -H "cookie: SameSite=Lax; HWWAFSESTIME=1767596069712; HWWAFSESID=e2b2773ee2641b98e5; SameSite=Lax; SYS-A7EE-D0E8-BE614B80=1108@f1ee335282c947faaaf66db345a3df2a; JSESSIONID=D9348A40D7F2ED8B6CEB4E5494A64DE6" "https://xdwlgyl.yciccloud.com/gdpaas/mainpage/findPage.htm?_p_=PG-3705-8CD5-7B6F4EB8" +