@newngapi/ngapi-npm
Version:
专为NG游戏平台设计的官方TypeScript SDK,让您能简单、高效地与NG游戏平台的API服务进行集成。
177 lines (154 loc) • 8.94 kB
text/typescript
import { Client, CurrencyCode } from '../src';
// 请替换为您的实际配置信息
const config = {
baseUrl: "https://api.ng777.net", // 替换为实际的 API 地址
sn: "YOUR_SN", // 替换为您的 sn
secretKey: "YOUR_SECRET_KEY", // 替换为您的 secretKey
};
/**
* 格式化并打印请求和响应的详细信息
* @param title - 示例标题
* @param endpoint - 请求的 API 路径
* @param params - 请求发送的参数
* @param response - API 返回的响应
*/
function printRequestDetails(title: string, endpoint: string, params: object, response: object) {
console.log(`\n--- ${title} ---`);
console.log(`➡️ Request Path: POST ${config.baseUrl}${endpoint}`);
console.log('➡️ Request Params:');
console.log(JSON.stringify(params, null, 2));
console.log('⬅️ API Response:');
console.log(JSON.stringify(response, null, 2));
console.log('---------------------------------\n');
}
// =================================================================
// 示例 1: 创建一个新玩家
// =================================================================
async function example1_createPlayer(client: Client) {
const playerId = 'test001';
const currency: CurrencyCode = 'CNY';
const endpoint = '/api/server/create';
const params = { platType: 'ag', playerId, currency };
const response = await client.createPlayer(params.platType, params.playerId, params.currency);
printRequestDetails('Example 1: Create a new player', endpoint, params, response);
if (response.code !== 10000) {
console.error('Player creation failed, subsequent examples might not work as expected.');
return null;
}
return { playerId, currency };
}
// =================================================================
// 示例 2: 查询玩家余额
// =================================================================
async function example2_getBalance(client: Client, playerId: string, currency: CurrencyCode) {
const endpoint = '/api/server/balance';
const params = { platType: 'ag', playerId, currency };
const response = await client.getBalance(params.platType, params.playerId, params.currency);
printRequestDetails('Example 2: Get player balance', endpoint, params, response);
}
// =================================================================
// 示例 3: 获取游戏链接
// =================================================================
async function example3_getGameUrl(client: Client, playerId: string, currency: CurrencyCode) {
const endpoint = '/api/server/gameUrl';
const params = { platType: 'ag', playerId, gameType: '1', currency, ingress: 'device1' };
const response = await client.getGameUrl(params.platType, params.playerId, params.gameType, params.currency, params.ingress);
printRequestDetails('Example 3: Get game URL', endpoint, params, response);
}
// =================================================================
// 示例 4: 查询全平台余额
// =================================================================
async function example4_getAllBalance(client: Client, playerId: string, currency: CurrencyCode) {
const endpoint = '/api/server/balanceAll';
const params = { playerId, currency };
const response = await client.getAllBalance(params.playerId, params.currency);
printRequestDetails('Example 4: Get all platform balance', endpoint, params, response);
}
// =================================================================
// 示例 5 & 6: 额度转换 - 转入, 并查询状态
// =================================================================
async function example5_6_depositAndCheckStatus(client: Client, playerId: string, currency: CurrencyCode) {
// 示例 5: 转入
const depositEndpoint = '/api/server/transfer';
const depositOrderId = `deposit_${Date.now()}`;
const depositParams = { platType: 'ag', playerId, currency, amount: '10.00', type: '1' as const, orderId: depositOrderId };
const depositResponse = await client.transfer(depositParams.platType, depositParams.playerId, depositParams.currency, depositParams.amount, depositParams.type, depositParams.orderId);
printRequestDetails('Example 5: Transfer - Deposit', depositEndpoint, depositParams, depositResponse);
// 示例 6: 查询状态
const statusEndpoint = '/api/server/transferStatus';
const statusParams = { playerId, currency, orderId: depositOrderId };
const statusResponse = await client.getTransferStatus(statusParams.playerId, statusParams.currency, statusParams.orderId);
printRequestDetails('Example 6: Get transfer status', statusEndpoint, statusParams, statusResponse);
}
// =================================================================
// 示例 7: 额度转换 - 转出
// =================================================================
async function example7_withdraw(client: Client, playerId: string, currency: CurrencyCode) {
const endpoint = '/api/server/transfer';
const orderId = `withdraw_${Date.now()}`;
const params = { platType: 'ag', playerId, currency, amount: '5.00', type: '2' as const, orderId };
const response = await client.transfer(params.platType, params.playerId, params.currency, params.amount, params.type, params.orderId);
printRequestDetails('Example 7: Transfer - Withdraw', endpoint, params, response);
}
// =================================================================
// 示例 8: 一键转出全平台额度
// =================================================================
async function example8_transferAll(client: Client, playerId: string, currency: CurrencyCode) {
const endpoint = '/api/server/transferAll';
const params = { playerId, currency };
const response = await client.transferAll(params.playerId, params.currency);
printRequestDetails('Example 8: Transfer all platform balance', endpoint, params, response);
}
// =================================================================
// 示例 9: 查询历史记录
// =================================================================
async function example9_getHistoryRecords(client: Client, currency: CurrencyCode) {
const endpoint = '/api/server/recordHistory';
const now = new Date();
const startTime = new Date(now.getTime() - 2 * 3600 * 1000).toISOString().slice(0, 19).replace('T', ' ');
const endTime = new Date(now.getTime() - 1 * 3600 * 1000).toISOString().slice(0, 19).replace('T', ' ');
const params = { currency, startTime, endTime, pageSize: '10' };
const response = await client.getHistoryRecords(params.currency, params.startTime, params.endTime, undefined, params.pageSize);
printRequestDetails('Example 9: Get history records', endpoint, params, response);
}
// =================================================================
// 示例 10: 查询实时记录
// =================================================================
async function example10_getRealTimeRecords(client: Client, currency: CurrencyCode) {
const endpoint = '/api/server/recordAll';
const params = { currency, pageSize: '10' };
const response = await client.getRealTimeRecords(params.currency, undefined, params.pageSize);
printRequestDetails('Example 10: Get real-time records', endpoint, params, response);
}
// =================================================================
// 示例 11: 查询商户余额
// =================================================================
async function example11_getQuota(client: Client) {
const endpoint = '/api/server/quota';
const params = {};
const response = await client.getQuota();
printRequestDetails('Example 11: Get merchant quota', endpoint, params, response);
}
async function main() {
console.log('🚀 --- NG API SDK Usage Example --- 🚀');
const client = new Client(config);
try {
const playerData = await example1_createPlayer(client);
if (playerData) {
const { playerId, currency } = playerData;
await example2_getBalance(client, playerId, currency);
await example3_getGameUrl(client, playerId, currency);
await example4_getAllBalance(client, playerId, currency);
await example5_6_depositAndCheckStatus(client, playerId, currency);
await example7_withdraw(client, playerId, currency);
await example8_transferAll(client, playerId, currency);
await example9_getHistoryRecords(client, currency);
await example10_getRealTimeRecords(client, currency);
await example11_getQuota(client);
}
} catch (error) {
console.error('\nAn unexpected error occurred:', error);
}
}
// 运行主函数
main();