api-connectivity.service.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { Injectable } from '@angular/core';
  2. import { Observable, forkJoin, of } from 'rxjs';
  3. import { map, catchError, timeout } from 'rxjs/operators';
  4. import { HttpApiService } from './http-api.service';
  5. import { AmazonRawApiService } from './amazon-raw-api.service';
  6. import { SorftimeApiService } from './sorftime-api.service';
  7. import { API_CONFIG } from './api.config';
  8. export interface ConnectivityResult {
  9. service: string;
  10. status: 'ok' | 'error' | 'timeout';
  11. latency: number;
  12. message: string;
  13. }
  14. /**
  15. * API 连通性测试服务
  16. * 用于验证各平台 API 的可用性
  17. */
  18. @Injectable({ providedIn: 'root' })
  19. export class ApiConnectivityService {
  20. constructor(
  21. private api: HttpApiService,
  22. private amazonRaw: AmazonRawApiService,
  23. private sorftimeApi: SorftimeApiService
  24. ) { }
  25. /** 测试所有 API 连通性 */
  26. testAll(): Observable<ConnectivityResult[]> {
  27. return forkJoin([
  28. this.testAmazon(),
  29. this.testSorftime(),
  30. this.testTikHub()
  31. ]);
  32. }
  33. /** 测试所有 Sorftime 端点详细连通性 */
  34. testSorftimeEndpoints(): Observable<ConnectivityResult[]> {
  35. return forkJoin([
  36. this.testSorftime(),
  37. this.testSorftimeCategoryTree(),
  38. this.testSorftimeProductQuery(),
  39. this.testSorftimeKeywordQuery()
  40. ]);
  41. }
  42. /** Sorftime — CategoryTree 端点 */
  43. private testSorftimeCategoryTree(): Observable<ConnectivityResult> {
  44. const start = Date.now();
  45. return this.sorftimeApi.getCategoryTree().pipe(
  46. timeout(API_CONFIG.TIMEOUT),
  47. map((resp: any) => ({
  48. service: 'Sorftime CategoryTree',
  49. status: 'ok' as const,
  50. latency: Date.now() - start,
  51. message: `返回数据: ${Array.isArray(resp?.data) ? resp.data.length : '有'}条类目`
  52. })),
  53. catchError(err => of({
  54. service: 'Sorftime CategoryTree',
  55. status: (err?.name === 'TimeoutError' ? 'timeout' : 'error') as 'timeout' | 'error',
  56. latency: Date.now() - start,
  57. message: err?.message || '连接失败'
  58. }))
  59. );
  60. }
  61. /** Sorftime — ProductQuery 端点 */
  62. private testSorftimeProductQuery(): Observable<ConnectivityResult> {
  63. const start = Date.now();
  64. return this.sorftimeApi.searchProducts('test', { pageSize: 1 }).pipe(
  65. timeout(API_CONFIG.TIMEOUT),
  66. map((resp: any) => ({
  67. service: 'Sorftime ProductQuery',
  68. status: 'ok' as const,
  69. latency: Date.now() - start,
  70. message: `搜索正常`
  71. })),
  72. catchError(err => of({
  73. service: 'Sorftime ProductQuery',
  74. status: (err?.name === 'TimeoutError' ? 'timeout' : 'error') as 'timeout' | 'error',
  75. latency: Date.now() - start,
  76. message: err?.message || '连接失败'
  77. }))
  78. );
  79. }
  80. /** Sorftime — KeywordQuery 端点 */
  81. private testSorftimeKeywordQuery(): Observable<ConnectivityResult> {
  82. const start = Date.now();
  83. return this.sorftimeApi.searchKeywords('test').pipe(
  84. timeout(API_CONFIG.TIMEOUT),
  85. map((resp: any) => ({
  86. service: 'Sorftime KeywordQuery',
  87. status: 'ok' as const,
  88. latency: Date.now() - start,
  89. message: `关键词查询正常`
  90. })),
  91. catchError(err => of({
  92. service: 'Sorftime KeywordQuery',
  93. status: (err?.name === 'TimeoutError' ? 'timeout' : 'error') as 'timeout' | 'error',
  94. latency: Date.now() - start,
  95. message: err?.message || '连接失败'
  96. }))
  97. );
  98. }
  99. /** 测试 Amazon SP-API */
  100. testAmazon(): Observable<ConnectivityResult> {
  101. const start = Date.now();
  102. return this.amazonRaw.getMarketplaceParticipations().pipe(
  103. timeout(API_CONFIG.TIMEOUT),
  104. map(() => ({
  105. service: 'Amazon SP-API',
  106. status: 'ok' as const,
  107. latency: Date.now() - start,
  108. message: '连接正常'
  109. })),
  110. catchError(err => of({
  111. service: 'Amazon SP-API',
  112. status: (err?.name === 'TimeoutError' ? 'timeout' : 'error') as 'timeout' | 'error',
  113. latency: Date.now() - start,
  114. message: err?.message || '连接失败'
  115. }))
  116. );
  117. }
  118. /** 测试 Sorftime API */
  119. testSorftime(): Observable<ConnectivityResult> {
  120. const start = Date.now();
  121. return this.api.sorftimeForward<any>('/api/ProductQuery', {
  122. query: { domain: 1 },
  123. body: { Query: 1, QueryType: 7, Pattern: 'dress', Page: 1 }
  124. }).pipe(
  125. timeout(API_CONFIG.TIMEOUT),
  126. map(() => ({
  127. service: 'Sorftime API',
  128. status: 'ok' as const,
  129. latency: Date.now() - start,
  130. message: '连接正常'
  131. })),
  132. catchError(err => of({
  133. service: 'Sorftime API',
  134. status: (err?.name === 'TimeoutError' ? 'timeout' : 'error') as 'timeout' | 'error',
  135. latency: Date.now() - start,
  136. message: err?.message || '连接失败'
  137. }))
  138. );
  139. }
  140. /** 测试 TikHub API */
  141. testTikHub(): Observable<ConnectivityResult> {
  142. const start = Date.now();
  143. return this.api.tikhubGet<any>('/api/v1/tiktok/app/v3/fetch_video_search_result', {
  144. keyword: 'dress',
  145. sort_type: 0,
  146. publish_time: 0
  147. }).pipe(
  148. timeout(API_CONFIG.TIMEOUT),
  149. map(() => ({
  150. service: 'TikHub API',
  151. status: 'ok' as const,
  152. latency: Date.now() - start,
  153. message: '连接正常'
  154. })),
  155. catchError(err => of({
  156. service: 'TikHub API',
  157. status: (err?.name === 'TimeoutError' ? 'timeout' : 'error') as 'timeout' | 'error',
  158. latency: Date.now() - start,
  159. message: err?.message || '连接失败'
  160. }))
  161. );
  162. }
  163. }