@gt6/sdk 1.0.18 → 1.0.20

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.
@@ -35,12 +35,14 @@ class GT6Client {
35
35
  try {
36
36
  const controller = new AbortController();
37
37
  const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
38
+ // 对于FormData,不设置Content-Type,让浏览器自动设置
39
+ const headers = new Headers(options.headers);
40
+ if (!(options.body instanceof FormData)) {
41
+ headers.set('Content-Type', 'application/json');
42
+ }
38
43
  const response = await fetch(url, {
39
44
  ...options,
40
- headers: {
41
- 'Content-Type': 'application/json',
42
- ...options.headers,
43
- },
45
+ headers,
44
46
  signal: controller.signal
45
47
  });
46
48
  clearTimeout(timeoutId);
@@ -1886,6 +1888,83 @@ class TransactionsAPI {
1886
1888
  }
1887
1889
  return null;
1888
1890
  }
1891
+ /**
1892
+ * 创建支付记录
1893
+ * @param paymentData 支付记录数据
1894
+ * @returns 创建支付记录响应
1895
+ */
1896
+ async createPaymentRecord(paymentData) {
1897
+ try {
1898
+ // 获取用户token
1899
+ const token = this.getToken();
1900
+ if (!token) {
1901
+ return {
1902
+ success: false,
1903
+ message: '用户未登录'
1904
+ };
1905
+ }
1906
+ const response = await this.client.request('/web/payment/record/create', {
1907
+ method: 'POST',
1908
+ headers: {
1909
+ 'Authorization': `Bearer ${token}`,
1910
+ 'Content-Type': 'application/json'
1911
+ },
1912
+ body: JSON.stringify(paymentData)
1913
+ });
1914
+ // 检查响应格式
1915
+ if (response && typeof response === 'object') {
1916
+ // 如果响应包含 code 字段,说明是标准格式
1917
+ if ('code' in response) {
1918
+ const responseData = response;
1919
+ return {
1920
+ success: responseData.code === 0,
1921
+ message: responseData.message || (responseData.code === 0 ? '支付申请已提交' : '提交失败'),
1922
+ code: responseData.code,
1923
+ data: responseData.data
1924
+ };
1925
+ }
1926
+ // 如果没有 code 字段,可能是其他格式
1927
+ return {
1928
+ success: true,
1929
+ message: '支付申请已提交',
1930
+ data: response
1931
+ };
1932
+ }
1933
+ return {
1934
+ success: false,
1935
+ message: '响应格式错误'
1936
+ };
1937
+ }
1938
+ catch (error) {
1939
+ // 如果是HTTP错误,尝试解析响应体
1940
+ if (error.statusCode && error.message) {
1941
+ // 对于401错误,返回认证失败信息
1942
+ if (error.statusCode === 401) {
1943
+ return {
1944
+ success: false,
1945
+ message: '认证失败,请重新登录'
1946
+ };
1947
+ }
1948
+ // 对于400错误,可能是数据有误
1949
+ if (error.statusCode === 400) {
1950
+ return {
1951
+ success: false,
1952
+ message: error.message || '支付数据有误,请检查输入'
1953
+ };
1954
+ }
1955
+ // 对于其他HTTP错误,返回状态码信息
1956
+ return {
1957
+ success: false,
1958
+ message: error.message || `HTTP ${error.statusCode} 错误`
1959
+ };
1960
+ }
1961
+ // 对于网络错误或其他错误
1962
+ return {
1963
+ success: false,
1964
+ message: error instanceof Error ? error.message : '创建支付记录失败'
1965
+ };
1966
+ }
1967
+ }
1889
1968
  }
1890
1969
 
1891
1970
  /**
@@ -2274,6 +2353,12 @@ class GT6SDK {
2274
2353
  async createRetailOrder(orderData) {
2275
2354
  return this.transactions.createRetailOrder(orderData);
2276
2355
  }
2356
+ /**
2357
+ * 便捷方法:创建支付记录
2358
+ */
2359
+ async createPaymentRecord(paymentData) {
2360
+ return this.transactions.createPaymentRecord(paymentData);
2361
+ }
2277
2362
  }
2278
2363
 
2279
2364
  export { ArticlesAPI, FormsAPI, GT6Client, GT6Error, GT6SDK, ProductsAPI, SettingsAPI, TransactionsAPI, UsersAPI, GT6SDK as default };