配置已保存

WLW OpenAPI 测试工具

完整的 API 测试工具和 SDK 示例代码

nodejs SDK 示例

const crypto = require('crypto'); const https = require('https'); const http = require('http'); const { URL } = require('url'); const { v4: uuidv4 } = require('uuid'); class WLWClient { constructor(baseUrl, accessKey, secretKey) { this.baseUrl = baseUrl; this.accessKey = accessKey; this.secretKey = secretKey; this.timeout = 30000; } _generateSignature(method, path, params) { // 排序参数 const sortedKeys = Object.keys(params) .filter(key => key !== 'signature') .sort(); // 构造参数字符串 const paramStr = sortedKeys .map(key => `${key}=${params[key]}`) .join('&'); // 构造签名字符串 const signStr = `${method}${path}${params.timestamp}${params.nonce}${ paramStr ? paramStr : '' }`; // HMAC-SHA256 签名 return crypto .createHmac('sha256', this.secretKey) .update(signStr) .digest('hex'); } _makeRequest(method, endpoint, params = {}) { return new Promise((resolve, reject) => { const url = new URL(endpoint, this.baseUrl); // 添加时间戳和nonce params.access_key = this.accessKey; params.timestamp = Math.floor(Date.now() / 1000).toString(); params.nonce = uuidv4(); // 生成签名 params.signature = this._generateSignature(method, url.pathname, params); const options = { method: method, timeout: this.timeout, headers: { 'User-Agent': 'WLW-OpenAPI-NodeJS-SDK/1.0' } }; let postData = ''; if (method === 'GET') { url.search = new URLSearchParams(params).toString(); } else { postData = new URLSearchParams(params).toString(); options.headers['Content-Type'] = 'application/x-www-form-urlencoded'; options.headers['Content-Length'] = Buffer.byteLength(postData); } const client = url.protocol === 'https:' ? https : http; const req = client.request(url, options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { const result = JSON.parse(data); resolve(result); } catch (error) { reject(new Error(`解析响应失败: ${error.message}`)); } }); }); req.on('error', (error) => { reject(error); }); if (postData) { req.write(postData); } req.end(); }); } async heartbeat() { return this._makeRequest('GET', '/api/v1/heartbeat'); } async getCardInfo(cardNo) { return this._makeRequest('GET', '/api/v1/info', { card_no: cardNo }); } async restartCard(cardNo) { return this._makeRequest('POST', '/api/v1/start', { card_no: cardNo }); } async stopCard(cardNo) { return this._makeRequest('POST', '/api/v1/stop', { card_no: cardNo }); } async orderPackage(cardNo, packageId, strategy) { return this._makeRequest('POST', '/api/v1/orders', { card_no: cardNo, package_id: packageId, strategy: strategy }); } } // 使用示例 async function main() { const client = new WLWClient( 'http://localhost:8080', 'your_access_key', 'your_secret_key' ); try { // 心跳检测 const heartbeat = await client.heartbeat(); console.log('心跳检测成功:', heartbeat); // 查询卡板信息 const cardInfo = await client.getCardInfo('CARD123456789'); console.log('卡板信息:', cardInfo); // 卡板复机 const restartResult = await client.restartCard('CARD123456789'); console.log('卡板复机:', restartResult); // 卡板停机 const stopResult = await client.stopCard('CARD123456789'); console.log('卡板停机:', stopResult); // 订购套餐 const order = await client.orderPackage('CARD123456789', 100, 1); console.log('订购套餐:', order); } catch (error) { console.error('请求失败:', error.message); } } // 如果直接运行此文件 if (require.main === module) { main(); } module.exports = WLWClient;