douyin-api.service.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient, HttpHeaders } from '@angular/common/http';
  3. import { Observable, throwError } from 'rxjs';
  4. import { catchError, retry } from 'rxjs/operators';
  5. import { environment } from '../../environments/environment';
  6. // 抖音API接口定义
  7. interface DouyinSearchParams {
  8. keyword: string;
  9. cursor?: number | string; // 支持数字或字符串
  10. sort_type?: string;
  11. publish_time?: string;
  12. filter_duration?: string;
  13. content_type?: string;
  14. search_id?: string;
  15. }
  16. interface DouyinVideoDetailParams {
  17. aweme_id: string;
  18. }
  19. interface DouyinCommentsParams {
  20. aweme_id: string;
  21. cursor?: number | string;
  22. count?: number | string;
  23. }
  24. @Injectable({
  25. providedIn: 'root'
  26. })
  27. export class DouyinApiService {
  28. private readonly BASE_URL = environment.douyinApi.baseUrl;
  29. private readonly TIMEOUT = environment.douyinApi.timeout;
  30. private readonly MAX_RETRIES = environment.douyinApi.maxRetries;
  31. constructor(private http: HttpClient) {}
  32. /**
  33. * 抖音综合搜索
  34. * @param params 搜索参数
  35. * @returns 搜索结果
  36. */
  37. searchVideos(params: DouyinSearchParams): Observable<any> {
  38. const url = `${this.BASE_URL}/api/v1/douyin/search/fetch_challenge_search_v2`;
  39. const defaultParams = {
  40. cursor: 0,
  41. sort_type: '0',
  42. publish_time: '0',
  43. filter_duration: '0',
  44. content_type: '0',
  45. search_id: ''
  46. };
  47. const requestParams = {
  48. ...defaultParams,
  49. ...params,
  50. cursor: params.cursor || 0 // 保持cursor为数字,根据API文档
  51. };
  52. return this.http.post(url, requestParams, {
  53. headers: this.getHeaders(),
  54. timeout: this.TIMEOUT
  55. }).pipe(
  56. retry(this.MAX_RETRIES),
  57. catchError(this.handleError)
  58. );
  59. }
  60. /**
  61. * 获取单个视频详情
  62. * @param aweme_id 视频ID
  63. * @returns 视频详情
  64. */
  65. getVideoDetail(aweme_id: string): Observable<any> {
  66. const url = `${this.BASE_URL}/api/v1/douyin/app/v3/fetch_one_video_v3`;
  67. return this.http.get(url, {
  68. params: { aweme_id },
  69. headers: this.getHeaders(),
  70. timeout: this.TIMEOUT
  71. }).pipe(
  72. retry(this.MAX_RETRIES),
  73. catchError(this.handleError)
  74. );
  75. }
  76. /**
  77. * 获取视频评论
  78. * @param params 评论参数
  79. * @returns 评论列表
  80. */
  81. getVideoComments(params: DouyinCommentsParams): Observable<any> {
  82. const url = `${this.BASE_URL}/app/v3/fetch_video_comments`;
  83. const defaultParams = {
  84. cursor: 0,
  85. count: 20
  86. };
  87. const requestParams = {
  88. ...defaultParams,
  89. ...params,
  90. cursor: params.cursor || 0,
  91. count: params.count || 20
  92. };
  93. return this.http.get(url, {
  94. params: requestParams,
  95. headers: this.getHeaders(),
  96. timeout: this.TIMEOUT
  97. }).pipe(
  98. retry(this.MAX_RETRIES),
  99. catchError(this.handleError)
  100. );
  101. }
  102. /**
  103. * 获取评论回复
  104. * @param item_id 视频ID
  105. * @param comment_id 评论ID
  106. * @param cursor 游标
  107. * @returns 回复列表
  108. */
  109. getCommentReplies(item_id: string, comment_id: string, cursor: number = 0): Observable<any> {
  110. const url = `${this.BASE_URL}/comment/reply/list`;
  111. return this.http.post(url, {
  112. item_id,
  113. comment_id,
  114. cursor,
  115. count: 20
  116. }, {
  117. headers: this.getHeaders(),
  118. timeout: this.TIMEOUT
  119. }).pipe(
  120. retry(this.MAX_RETRIES),
  121. catchError(this.handleError)
  122. );
  123. }
  124. /**
  125. * 获取用户信息
  126. * @param sec_uid 用户sec_uid
  127. * @returns 用户信息
  128. */
  129. getUserProfile(sec_uid: string): Observable<any> {
  130. const url = `${this.BASE_URL}/app/v3/handler_user_profile`;
  131. return this.http.get(url, {
  132. params: { sec_user_id: sec_uid },
  133. headers: this.getHeaders(),
  134. timeout: this.TIMEOUT
  135. }).pipe(
  136. retry(this.MAX_RETRIES),
  137. catchError(this.handleError)
  138. );
  139. }
  140. /**
  141. * 获取请求头(使用环境配置)
  142. */
  143. private getHeaders(): HttpHeaders {
  144. return new HttpHeaders({
  145. 'Content-Type': 'application/json',
  146. 'Accept': 'application/json',
  147. 'Authorization': `Bearer ${environment.douyinApi.token}`
  148. });
  149. }
  150. /**
  151. * 错误处理
  152. */
  153. private handleError(error: any): Observable<never> {
  154. let errorMessage = '';
  155. if (error.error instanceof ErrorEvent) {
  156. // 客户端错误
  157. errorMessage = `网络错误: ${error.error.message}`;
  158. } else {
  159. // 服务端错误
  160. switch (error.status) {
  161. case 401:
  162. errorMessage = '认证失败 (401): Token无效或已过期';
  163. break;
  164. case 403:
  165. errorMessage = '访问被拒绝 (403): 权限不足';
  166. break;
  167. case 429:
  168. errorMessage = '请求过于频繁 (429): 请稍后重试';
  169. break;
  170. case 500:
  171. errorMessage = '服务器内部错误 (500): 请稍后重试';
  172. break;
  173. case 400:
  174. errorMessage = '请求参数错误 (400): 请检查请求参数格式';
  175. break;
  176. case 0:
  177. errorMessage = '网络连接失败: 请检查网络设置';
  178. break;
  179. default:
  180. errorMessage = `请求失败 (${error.status}): ${error.message || '未知错误'}`;
  181. }
  182. // 检查API返回的具体错误信息
  183. if (error.error && error.error.msg) {
  184. errorMessage += ` | API错误: ${error.error.msg}`;
  185. }
  186. }
  187. console.error('Douyin API Error:', errorMessage, error);
  188. return throwError(() => new Error(errorMessage));
  189. }
  190. }