@gt6/sdk 1.0.15 → 1.0.16

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.
package/README.md CHANGED
@@ -334,6 +334,120 @@ new GT6SDK(config: GT6Config)
334
334
 
335
335
  - `getArticleStats(): Promise<ArticleStats>`
336
336
 
337
+ ### TransactionsAPI 类
338
+
339
+ #### 交易相关方法
340
+
341
+ - `uploadFile(file: File): Promise<UploadResponse>` - 上传文件
342
+ - `deductFunds(deductData: DeductFundsRequest): Promise<DeductFundsResponse>` - 资金扣款
343
+ - `createRetailOrder(orderData: CreateRetailOrderRequest): Promise<CreateRetailOrderResponse>` - 创建零售订单
344
+
345
+ #### 资金扣款
346
+
347
+ ```typescript
348
+ // 资金扣款
349
+ const deductResult = await sdk.transactions.deductFunds({
350
+ userId: 12345,
351
+ userType: 2,
352
+ amount: '100.00',
353
+ description: '购买商品扣款',
354
+ orderId: 'ORDER123456'
355
+ });
356
+
357
+ if (deductResult.success) {
358
+ console.log('扣款成功');
359
+ console.log('资金日志ID:', deductResult.fundLogId);
360
+ console.log('新余额:', deductResult.newBalance);
361
+ } else {
362
+ console.log('扣款失败:', deductResult.message);
363
+ }
364
+ ```
365
+
366
+ #### 创建零售订单
367
+
368
+ ```typescript
369
+ // 创建零售订单
370
+ const orderData = {
371
+ user_id: 12345,
372
+ user_type: 2,
373
+ total_amount: 150.00,
374
+ pay_way: 1, // 钱包支付
375
+ product_type: 1,
376
+ platform_id: 1747558688,
377
+ d1: 0,
378
+ d2: 0,
379
+ d3: 0,
380
+ products: [
381
+ {
382
+ product_id: 1001,
383
+ quantity: 2,
384
+ sku: 'PROD001',
385
+ price: 50.00,
386
+ region_id: 1,
387
+ address_id: 101,
388
+ shipping_fee: 10.00,
389
+ tax_fee: 5.00
390
+ }
391
+ ]
392
+ };
393
+
394
+ const orderResult = await sdk.transactions.createRetailOrder(orderData);
395
+
396
+ if (orderResult.success) {
397
+ console.log('订单创建成功');
398
+ console.log('订单ID:', orderResult.orderId);
399
+ console.log('订单状态:', orderResult.order?.status);
400
+ } else {
401
+ console.log('订单创建失败:', orderResult.message);
402
+ }
403
+ ```
404
+
405
+ #### 完整交易流程
406
+
407
+ ```typescript
408
+ // 完整的购买流程示例
409
+ async function completePurchase(userId: number, amount: number, products: any[]) {
410
+ // 第一步:扣款
411
+ const deductResult = await sdk.transactions.deductFunds({
412
+ userId,
413
+ userType: 2,
414
+ amount: amount.toString(),
415
+ description: '购买商品扣款'
416
+ });
417
+
418
+ if (!deductResult.success) {
419
+ throw new Error(`扣款失败: ${deductResult.message}`);
420
+ }
421
+
422
+ // 第二步:创建订单
423
+ const orderData = {
424
+ user_id: userId,
425
+ user_type: 2,
426
+ total_amount: amount,
427
+ pay_way: 1,
428
+ product_type: 1,
429
+ platform_id: 1747558688,
430
+ d1: 0,
431
+ d2: 0,
432
+ d3: 0,
433
+ products
434
+ };
435
+
436
+ const orderResult = await sdk.transactions.createRetailOrder(orderData);
437
+
438
+ if (!orderResult.success) {
439
+ // 注意:如果订单创建失败,需要考虑退款处理
440
+ throw new Error(`订单创建失败: ${orderResult.message}`);
441
+ }
442
+
443
+ return {
444
+ fundLogId: deductResult.fundLogId,
445
+ orderId: orderResult.orderId,
446
+ newBalance: deductResult.newBalance
447
+ };
448
+ }
449
+ ```
450
+
337
451
  ## 使用示例
338
452
 
339
453
  ### 根据分类获取文章
@@ -1752,6 +1752,146 @@ class UsersAPI {
1752
1752
  }
1753
1753
  }
1754
1754
 
1755
+ /**
1756
+ * 交易管理API模块
1757
+ * 提供资金扣款、订单创建等功能
1758
+ */
1759
+ class TransactionsAPI {
1760
+ constructor(client) {
1761
+ this.client = client;
1762
+ }
1763
+ /**
1764
+ * 资金扣款
1765
+ * @param deductData 扣款数据
1766
+ * @returns 扣款响应
1767
+ */
1768
+ async deductFunds(deductData) {
1769
+ try {
1770
+ // 获取用户token
1771
+ const token = this.getToken();
1772
+ if (!token) {
1773
+ return {
1774
+ success: false,
1775
+ message: '用户未登录'
1776
+ };
1777
+ }
1778
+ const response = await this.client.request('/web/user/fund/deduct', {
1779
+ method: 'POST',
1780
+ headers: {
1781
+ 'Authorization': `Bearer ${token}`,
1782
+ 'Content-Type': 'application/json'
1783
+ },
1784
+ body: JSON.stringify(deductData)
1785
+ });
1786
+ return response;
1787
+ }
1788
+ catch (error) {
1789
+ // 如果是HTTP错误,尝试解析响应体
1790
+ if (error.statusCode && error.message) {
1791
+ // 对于401错误,返回认证失败信息
1792
+ if (error.statusCode === 401) {
1793
+ return {
1794
+ success: false,
1795
+ message: '认证失败,请重新登录'
1796
+ };
1797
+ }
1798
+ // 对于400错误,可能是余额不足等业务错误
1799
+ if (error.statusCode === 400) {
1800
+ return {
1801
+ success: false,
1802
+ message: error.message || '扣款失败,请检查余额'
1803
+ };
1804
+ }
1805
+ // 对于其他HTTP错误,返回状态码信息
1806
+ return {
1807
+ success: false,
1808
+ message: error.message || `HTTP ${error.statusCode} 错误`
1809
+ };
1810
+ }
1811
+ // 对于网络错误或其他错误
1812
+ return {
1813
+ success: false,
1814
+ message: error instanceof Error ? error.message : '资金扣款失败'
1815
+ };
1816
+ }
1817
+ }
1818
+ /**
1819
+ * 创建零售订单
1820
+ * @param orderData 订单数据
1821
+ * @returns 创建订单响应
1822
+ */
1823
+ async createRetailOrder(orderData) {
1824
+ try {
1825
+ // 获取用户token
1826
+ const token = this.getToken();
1827
+ if (!token) {
1828
+ return {
1829
+ success: false,
1830
+ message: '用户未登录'
1831
+ };
1832
+ }
1833
+ const response = await this.client.request('/web/orders', {
1834
+ method: 'POST',
1835
+ headers: {
1836
+ 'Authorization': `Bearer ${token}`,
1837
+ 'Content-Type': 'application/json'
1838
+ },
1839
+ body: JSON.stringify(orderData)
1840
+ });
1841
+ return response;
1842
+ }
1843
+ catch (error) {
1844
+ // 如果是HTTP错误,尝试解析响应体
1845
+ if (error.statusCode && error.message) {
1846
+ // 对于401错误,返回认证失败信息
1847
+ if (error.statusCode === 401) {
1848
+ return {
1849
+ success: false,
1850
+ message: '认证失败,请重新登录'
1851
+ };
1852
+ }
1853
+ // 对于400错误,可能是订单数据有误
1854
+ if (error.statusCode === 400) {
1855
+ return {
1856
+ success: false,
1857
+ message: error.message || '订单数据有误,请检查输入'
1858
+ };
1859
+ }
1860
+ // 对于其他HTTP错误,返回状态码信息
1861
+ return {
1862
+ success: false,
1863
+ message: error.message || `HTTP ${error.statusCode} 错误`
1864
+ };
1865
+ }
1866
+ // 对于网络错误或其他错误
1867
+ return {
1868
+ success: false,
1869
+ message: error instanceof Error ? error.message : '创建订单失败'
1870
+ };
1871
+ }
1872
+ }
1873
+ /**
1874
+ * 获取用户token(从本地存储)
1875
+ * @returns token或null
1876
+ */
1877
+ getToken() {
1878
+ if (typeof window !== 'undefined' && window.localStorage) {
1879
+ const userDataStr = localStorage.getItem('userData');
1880
+ if (userDataStr) {
1881
+ try {
1882
+ const userData = JSON.parse(userDataStr);
1883
+ return userData.token || null;
1884
+ }
1885
+ catch (error) {
1886
+ console.warn('解析用户数据失败:', error);
1887
+ return null;
1888
+ }
1889
+ }
1890
+ }
1891
+ return null;
1892
+ }
1893
+ }
1894
+
1755
1895
  /**
1756
1896
  * GT6 SDK 主类
1757
1897
  * 提供文章、产品、设置、表单和用户管理相关的所有功能
@@ -1764,6 +1904,7 @@ class GT6SDK {
1764
1904
  this.settings = new SettingsAPI(client);
1765
1905
  this.forms = new FormsAPI(client);
1766
1906
  this.users = new UsersAPI(client);
1907
+ this.transactions = new TransactionsAPI(client);
1767
1908
  }
1768
1909
  /**
1769
1910
  * 获取客户端实例(用于高级用法)
@@ -2125,6 +2266,18 @@ class GT6SDK {
2125
2266
  async uploadFile(file) {
2126
2267
  return this.users.uploadFile(file);
2127
2268
  }
2269
+ /**
2270
+ * 便捷方法:资金扣款
2271
+ */
2272
+ async deductFunds(deductData) {
2273
+ return this.transactions.deductFunds(deductData);
2274
+ }
2275
+ /**
2276
+ * 便捷方法:创建零售订单
2277
+ */
2278
+ async createRetailOrder(orderData) {
2279
+ return this.transactions.createRetailOrder(orderData);
2280
+ }
2128
2281
  }
2129
2282
 
2130
2283
  exports.ArticlesAPI = ArticlesAPI;
@@ -2134,6 +2287,7 @@ exports.GT6Error = GT6Error;
2134
2287
  exports.GT6SDK = GT6SDK;
2135
2288
  exports.ProductsAPI = ProductsAPI;
2136
2289
  exports.SettingsAPI = SettingsAPI;
2290
+ exports.TransactionsAPI = TransactionsAPI;
2137
2291
  exports.UsersAPI = UsersAPI;
2138
2292
  exports.default = GT6SDK;
2139
2293
  //# sourceMappingURL=gt6-sdk.cjs.js.map