前端
This commit is contained in:
180
frontend/app/web-gold/src/api/tikhub/README.md
Normal file
180
frontend/app/web-gold/src/api/tikhub/README.md
Normal file
@@ -0,0 +1,180 @@
|
||||
# TikHub API 模块
|
||||
|
||||
本模块提供了统一的 TikHub 接口调用中间层,支持多种平台的 API 接口。
|
||||
|
||||
## 目录结构
|
||||
|
||||
```
|
||||
tikhub/
|
||||
├── types.js # 枚举定义:InterfaceType、MethodType
|
||||
├── tikhub.js # 核心服务类 TikhubService
|
||||
├── index.js # 统一导出入口
|
||||
├── example.js # 使用示例
|
||||
└── README.md # 本文档
|
||||
```
|
||||
|
||||
## 快速开始
|
||||
|
||||
### 1. 导入模块
|
||||
|
||||
```javascript
|
||||
import TikhubService, { InterfaceType, MethodType } from '@/api/tikhub'
|
||||
```
|
||||
|
||||
### 2. 调用接口
|
||||
|
||||
```javascript
|
||||
// 调用抖音热门搜索接口
|
||||
const response = await TikhubService.postTikHup(
|
||||
InterfaceType.DOUYIN_WEB_HOT_SEARCH, // 接口类型
|
||||
MethodType.GET, // HTTP 方法
|
||||
{ keyword: '测试关键词' } // 实际接口参数
|
||||
)
|
||||
```
|
||||
|
||||
## API 说明
|
||||
|
||||
### TikhubService.postTikHup(type, methodType, urlParams)
|
||||
|
||||
统一调用 TikHub 接口的中间层方法。
|
||||
|
||||
**参数:**
|
||||
- `type` (String) - 接口类型,使用 `InterfaceType` 枚举值
|
||||
- `methodType` (String) - HTTP 方法类型,使用 `MethodType` 枚举值
|
||||
- `urlParams` (Object|String) - 实际接口的参数
|
||||
|
||||
**返回:**
|
||||
- Promise - axios 响应对象
|
||||
|
||||
**示例:**
|
||||
|
||||
```javascript
|
||||
// 获取小红书热门榜单
|
||||
await TikhubService.postTikHup(
|
||||
InterfaceType.XIAOHONGSHU_WEB_HOT_LIST,
|
||||
MethodType.GET,
|
||||
{ page: 1, page_size: 20 }
|
||||
)
|
||||
|
||||
// 搜索抖音内容
|
||||
await TikhubService.postTikHup(
|
||||
InterfaceType.DOUYIN_GENERAL_SEARCH_V4,
|
||||
MethodType.POST,
|
||||
{
|
||||
keyword: '热门内容',
|
||||
sort_type: 0,
|
||||
publish_time: 0,
|
||||
}
|
||||
)
|
||||
```
|
||||
|
||||
## 枚举说明
|
||||
|
||||
### InterfaceType - 接口类型
|
||||
|
||||
支持的所有接口类型:
|
||||
|
||||
| 枚举值 | 说明 |
|
||||
|--------|------|
|
||||
| `XIAOHONGSHU_USER_INFO` | 小红书 - 获取用户信息 |
|
||||
| `DOUYIN_WEB_USER_POST_VIDEOS` | 抖音 - 网页端获取用户发布的视频 |
|
||||
| `DOUYIN_APP_USER_POST_VIDEOS` | 抖音 - APP端V3获取用户发布的视频 |
|
||||
| `DOUYIN_APP_GENERAL_SEARCH` | 抖音 - APP端V3通用搜索结果 |
|
||||
| `DOUYIN_SEARCH_SUGGEST` | 抖音 - 搜索建议 |
|
||||
| `DOUYIN_GENERAL_SEARCH_V4` | 抖音 - 通用搜索V4 |
|
||||
| `XIAOHONGSHU_WEB_HOT_LIST` | 小红书 - 网页端V2热门榜单 |
|
||||
| `DOUYIN_WEB_HOT_SEARCH` | 抖音 - 网页端热门搜索结果 |
|
||||
| `KUAISHOU_WEB_HOT_LIST` | 快手 - 网页端热门榜单V2 |
|
||||
| `BILIBILI_WEB_POPULAR` | B站 - 网页端流行内容 |
|
||||
| `WEIBO_WEB_HOT_SEARCH` | 微博 - 网页端V2热门搜索指数 |
|
||||
| `DOUYIN_WEB_GENERAL_SEARCH` | 抖音 - 网页端通用搜索结果 |
|
||||
|
||||
### MethodType - HTTP 方法
|
||||
|
||||
| 枚举值 | 说明 |
|
||||
|--------|------|
|
||||
| `GET` | GET 请求 |
|
||||
| `POST` | POST 请求 |
|
||||
| `PUT` | PUT 请求 |
|
||||
| `DELETE` | DELETE 请求 |
|
||||
| `PATCH` | PATCH 请求 |
|
||||
|
||||
## 使用示例
|
||||
|
||||
详细的使用示例请参考 `example.js` 文件。
|
||||
|
||||
### 示例 1:获取抖音热门搜索
|
||||
|
||||
```javascript
|
||||
import TikhubService, { InterfaceType, MethodType } from '@/api/tikhub'
|
||||
|
||||
async function getDouyinHotSearch() {
|
||||
try {
|
||||
const response = await TikhubService.postTikHup(
|
||||
InterfaceType.DOUYIN_WEB_HOT_SEARCH,
|
||||
MethodType.GET,
|
||||
{ keyword: '测试关键词' }
|
||||
)
|
||||
return response
|
||||
} catch (error) {
|
||||
console.error('调用失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 示例 2:获取用户发布的视频
|
||||
|
||||
```javascript
|
||||
async function getUserVideos() {
|
||||
return await TikhubService.postTikHup(
|
||||
InterfaceType.DOUYIN_WEB_USER_POST_VIDEOS,
|
||||
MethodType.GET,
|
||||
{
|
||||
sec_user_id: 'MS4wLjABAAAxxxxxxxx',
|
||||
count: 20,
|
||||
max_cursor: 0,
|
||||
}
|
||||
)
|
||||
}
|
||||
```
|
||||
|
||||
## 错误处理
|
||||
|
||||
```javascript
|
||||
try {
|
||||
const response = await TikhubService.postTikHup(
|
||||
InterfaceType.DOUYIN_WEB_HOT_SEARCH,
|
||||
MethodType.GET,
|
||||
{ keyword: '测试' }
|
||||
)
|
||||
console.log('成功:', response)
|
||||
} catch (error) {
|
||||
if (error.message.includes('无效的接口类型')) {
|
||||
console.error('接口类型错误')
|
||||
} else {
|
||||
console.error('请求失败:', error.message)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## 后端接口格式
|
||||
|
||||
本模块会调用后端接口 `/webApi/admin-api/ai/tikHup/post_tik_hup`,传递的参数格式为:
|
||||
|
||||
```json
|
||||
{
|
||||
"interface_type": "8",
|
||||
"method_type": "GET",
|
||||
"platform_url": "https://api.tikhub.io/api/v1/douyin/web/fetch_hot_search_result",
|
||||
"url_params": { "keyword": "测试" }
|
||||
}
|
||||
```
|
||||
|
||||
## 注意事项
|
||||
|
||||
1. 所有接口类型必须使用 `InterfaceType` 枚举,不能使用字符串数字
|
||||
2. HTTP 方法必须使用 `MethodType` 枚举
|
||||
3. `urlParams` 可以是对象或字符串,取决于实际接口的要求
|
||||
4. 后端会根据 `interface_type` 从数据库中获取对应的 `platform_token`
|
||||
|
||||
7
frontend/app/web-gold/src/api/tikhub/index.js
Normal file
7
frontend/app/web-gold/src/api/tikhub/index.js
Normal file
@@ -0,0 +1,7 @@
|
||||
/**
|
||||
* TikHub API 统一导出
|
||||
*/
|
||||
export { TikhubService } from './tikhub.js'
|
||||
export { default } from './tikhub.js'
|
||||
export { InterfaceType, MethodType, InterfaceUrlMap, ParamType } from './types.js'
|
||||
|
||||
55
frontend/app/web-gold/src/api/tikhub/tikhub.js
Normal file
55
frontend/app/web-gold/src/api/tikhub/tikhub.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import http from '@/api/http'
|
||||
import { InterfaceType, MethodType, InterfaceUrlMap, ParamType } from './types'
|
||||
import qs from 'qs'
|
||||
// 使用本地代理前缀 /tikhub,开发环境通过 Vite 代理到 https://api.tikhub.io
|
||||
const SERVER_TIKHUB = '/webApi/admin-api/ai/tikHup'
|
||||
|
||||
/**
|
||||
* TikHub API 服务类
|
||||
*/
|
||||
export const TikhubService = {
|
||||
/**
|
||||
* 统一调用 TikHub 接口的中间层方法
|
||||
* @param {String} type - 接口类型,使用 InterfaceType 枚举
|
||||
* @param {String} methodType - HTTP 方法类型,使用 MethodType 枚举
|
||||
* @param {Object|String} urlParams - 实际接口的参数
|
||||
* @returns {Promise} axios 响应
|
||||
*
|
||||
* @example
|
||||
* // 调用抖音热门搜索接口
|
||||
* TikhubService.postTikHup(
|
||||
* InterfaceType.DOUYIN_WEB_HOT_SEARCH,
|
||||
* MethodType.GET,
|
||||
* { keyword: '测试' }
|
||||
* )
|
||||
*/
|
||||
postTikHup({
|
||||
type,
|
||||
methodType,
|
||||
urlParams,
|
||||
paramType = '',
|
||||
}) {
|
||||
// 验证接口类型是否存在
|
||||
if (!InterfaceUrlMap[type]) {
|
||||
throw new Error(`无效的接口类型: ${type}`)
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 构造请求参数
|
||||
const requestData = {
|
||||
type: type,
|
||||
methodType: methodType,
|
||||
paramType,
|
||||
urlParams: paramType === ParamType.JSON ? JSON.stringify(urlParams) : qs.stringify(urlParams),
|
||||
}
|
||||
|
||||
return http.post(`${SERVER_TIKHUB}/postTikHup`, qs.stringify(requestData))
|
||||
},
|
||||
}
|
||||
|
||||
export default TikhubService
|
||||
|
||||
// 导出枚举,方便外部使用
|
||||
export { InterfaceType, MethodType, InterfaceUrlMap }
|
||||
|
||||
87
frontend/app/web-gold/src/api/tikhub/types.js
Normal file
87
frontend/app/web-gold/src/api/tikhub/types.js
Normal file
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* TikHub 接口类型枚举
|
||||
* 对应数据库 tik_token 表中的 interface_type 字段
|
||||
*/
|
||||
export const InterfaceType = {
|
||||
/** 小红书 - 获取用户信息 */
|
||||
XIAOHONGSHU_USER_INFO: '1',
|
||||
|
||||
/** 抖音 - 网页端获取用户发布的视频 */
|
||||
DOUYIN_WEB_USER_POST_VIDEOS: '2',
|
||||
|
||||
/** 抖音 - APP端V3获取用户发布的视频 */
|
||||
DOUYIN_APP_USER_POST_VIDEOS: '3',
|
||||
|
||||
/** 抖音 - APP端V3通用搜索结果 */
|
||||
DOUYIN_APP_GENERAL_SEARCH: '4',
|
||||
|
||||
/** 抖音 - 搜索建议 */
|
||||
DOUYIN_SEARCH_SUGGEST: '5',
|
||||
|
||||
/** 抖音 - 通用搜索V4 */
|
||||
DOUYIN_GENERAL_SEARCH_V4: '6',
|
||||
|
||||
/** 小红书 - 网页端V2热门榜单 */
|
||||
XIAOHONGSHU_WEB_HOT_LIST: '7',
|
||||
|
||||
/** 抖音 - 网页端热门搜索结果 */
|
||||
DOUYIN_WEB_HOT_SEARCH: '8',
|
||||
|
||||
/** 快手 - 网页端热门榜单V2 */
|
||||
KUAISHOU_WEB_HOT_LIST: '9',
|
||||
|
||||
/** B站 - 网页端流行内容 */
|
||||
BILIBILI_WEB_POPULAR: '10',
|
||||
|
||||
/** 微博 - 网页端V2热门搜索指数 */
|
||||
WEIBO_WEB_HOT_SEARCH: '11',
|
||||
|
||||
/** 抖音 - 网页端通用搜索结果 */
|
||||
DOUYIN_WEB_GENERAL_SEARCH: '12',
|
||||
/** 抖音 - APP通用搜索结果 */
|
||||
DOUYIN_SEARCH_GENERAL_SEARCH: '14',
|
||||
}
|
||||
|
||||
/**
|
||||
* HTTP 请求方法枚举
|
||||
*/
|
||||
export const MethodType = {
|
||||
GET: 'GET',
|
||||
POST: 'POST',
|
||||
PUT: 'PUT',
|
||||
DELETE: 'DELETE',
|
||||
PATCH: 'PATCH',
|
||||
}
|
||||
/**
|
||||
* 请求参数类型枚举
|
||||
*/
|
||||
export const ParamType = {
|
||||
JSON: 'json',
|
||||
FORM: 'form'
|
||||
}
|
||||
/**
|
||||
* 接口类型与平台 URL 的映射
|
||||
* 根据 tik_token 表中的数据生成
|
||||
*/
|
||||
export const InterfaceUrlMap = {
|
||||
[InterfaceType.XIAOHONGSHU_USER_INFO]: 'https://api.tikhub.io/api/v1/xiaohongshu/app/get_user_info',
|
||||
[InterfaceType.DOUYIN_WEB_USER_POST_VIDEOS]: 'https://api.tikhub.io/api/v1/douyin/web/fetch_user_post_videos',
|
||||
[InterfaceType.DOUYIN_APP_USER_POST_VIDEOS]: 'https://api.tikhub.io/api/v1/douyin/app/v3/fetch_user_post_videos',
|
||||
[InterfaceType.DOUYIN_APP_GENERAL_SEARCH]: 'https://api.tikhub.io/api/v1/douyin/app/v3/fetch_general_search_result',
|
||||
[InterfaceType.DOUYIN_SEARCH_GENERAL_SEARCH]: 'https://api.tikhub.io/api/v1/douyin/search/fetch_general_search_v4',
|
||||
[InterfaceType.DOUYIN_SEARCH_SUGGEST]: 'https://api.tikhub.io/api/v1/douyin/search/fetch_search_suggest',
|
||||
[InterfaceType.DOUYIN_GENERAL_SEARCH_V4]: 'https://api.tikhub.io/api/v1/douyin/search/fetch_general_search_v4',
|
||||
[InterfaceType.XIAOHONGSHU_WEB_HOT_LIST]: 'https://api.tikhub.io/api/v1/xiaohongshu/web_v2/fetch_hot_list',
|
||||
[InterfaceType.DOUYIN_WEB_HOT_SEARCH]: 'https://api.tikhub.io/api/v1/douyin/web/fetch_hot_search_result',
|
||||
[InterfaceType.KUAISHOU_WEB_HOT_LIST]: 'https://api.tikhub.io/api/v1/kuaishou/web/fetch_kuaishou_hot_list_v2',
|
||||
[InterfaceType.BILIBILI_WEB_POPULAR]: 'https://api.tikhub.io/api/v1/bilibili/web/fetch_com_popular',
|
||||
[InterfaceType.WEIBO_WEB_HOT_SEARCH]: 'https://api.tikhub.io/api/v1/weibo/web_v2/fetch_hot_search_index',
|
||||
[InterfaceType.DOUYIN_WEB_GENERAL_SEARCH]: 'https://api.tikhub.io/api/v1/douyin/web/fetch_general_search_result',
|
||||
}
|
||||
|
||||
export default {
|
||||
InterfaceType,
|
||||
MethodType,
|
||||
InterfaceUrlMap,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user