Skip to content

Commit e9491d8

Browse files
author
Taois
committed
feat:访问超时设置
1 parent 5783973 commit e9491d8

File tree

7 files changed

+115
-14
lines changed

7 files changed

+115
-14
lines changed

dashboard/src/api/config.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,47 @@
33
* 定义接口相关的配置信息和常量
44
*/
55

6+
/**
7+
* 获取API超时时间(毫秒)
8+
* 从设置中读取,如果没有设置则使用默认值30秒
9+
*/
10+
export const getApiTimeout = () => {
11+
try {
12+
const addressSettings = JSON.parse(localStorage.getItem('addressSettings') || '{}')
13+
const timeoutSeconds = addressSettings.apiTimeout || 30
14+
return timeoutSeconds * 1000 // 转换为毫秒
15+
} catch (error) {
16+
console.warn('Failed to get API timeout from settings, using default:', error)
17+
return 30000 // 默认30秒
18+
}
19+
}
20+
21+
/**
22+
* 获取Action接口超时时间(毫秒)
23+
* Action接口通常需要更长的超时时间,默认100秒
24+
*/
25+
export const getActionTimeout = () => {
26+
try {
27+
const addressSettings = JSON.parse(localStorage.getItem('addressSettings') || '{}')
28+
const apiTimeoutSeconds = addressSettings.apiTimeout || 30
29+
// Action接口超时时间为普通接口的3倍,但最少100秒
30+
const actionTimeoutSeconds = Math.max(apiTimeoutSeconds * 3, 100)
31+
return actionTimeoutSeconds * 1000 // 转换为毫秒
32+
} catch (error) {
33+
console.warn('Failed to get Action timeout from settings, using default:', error)
34+
return 100000 // 默认100秒
35+
}
36+
}
37+
638
// 基础配置
739
export const API_CONFIG = {
840
// 基础URL配置
941
BASE_URL: import.meta.env.VITE_API_BASE_URL || 'http://localhost:5707',
1042

11-
// 超时配置
12-
TIMEOUT: 30000,
43+
// 超时配置(动态获取)
44+
get TIMEOUT() {
45+
return getApiTimeout()
46+
},
1347

1448
// 请求头配置
1549
HEADERS: {

dashboard/src/api/modules/module.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
import { get, post } from '../request'
7-
import { API_PATHS, MODULE_ACTIONS, PAGINATION } from '../config'
7+
import { API_PATHS, MODULE_ACTIONS, PAGINATION, API_CONFIG } from '../config'
88
import { processExtendParam } from '@/utils/apiUtils'
99
import axios from 'axios'
1010

@@ -68,7 +68,7 @@ const directApiCall = async (apiUrl, params = {}) => {
6868
try {
6969
const response = await axios.get(apiUrl, {
7070
params,
71-
timeout: 30000,
71+
timeout: API_CONFIG.TIMEOUT,
7272
headers: {
7373
'Accept': 'application/json'
7474
}
@@ -437,7 +437,7 @@ export const executeAction = async (module, data) => {
437437
// 如果是测试用的JSON文件,使用GET请求
438438
if (apiUrl.endsWith('.json')) {
439439
const response = await axios.get(apiUrl, {
440-
timeout: 30000,
440+
timeout: API_CONFIG.TIMEOUT,
441441
headers: {
442442
'Accept': 'application/json'
443443
}
@@ -447,7 +447,7 @@ export const executeAction = async (module, data) => {
447447
} else {
448448
// 否则使用POST请求
449449
const response = await axios.post(apiUrl, requestData, {
450-
timeout: 30000,
450+
timeout: API_CONFIG.TIMEOUT,
451451
headers: {
452452
'Accept': 'application/json',
453453
'Content-Type': 'application/json;charset=UTF-8',

dashboard/src/api/services/parser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import axios from 'axios'
7+
import { API_CONFIG } from '@/api/config'
78

89
class ParserService {
910
/**
@@ -174,7 +175,7 @@ class ParserService {
174175
'Referer': fullParseUrl, // 使用拼接后的解析地址作为Referer
175176
...parser.headers
176177
},
177-
timeout: 30000
178+
timeout: API_CONFIG.TIMEOUT
178179
}
179180

180181
const response = await axios(axiosConfig)
@@ -228,7 +229,7 @@ class ParserService {
228229
'Referer': data.referer || '',
229230
...parser.headers
230231
},
231-
timeout: 30000,
232+
timeout: API_CONFIG.TIMEOUT,
232233
maxRedirects: 5
233234
})
234235

dashboard/src/components/actions/InputAction.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ import { executeAction } from '@/api/modules/module.js'
264264
import { showToast } from '@/stores/toast.js'
265265
import siteService from '@/api/services/site'
266266
import { useRouter } from 'vue-router'
267+
import { getActionTimeout } from '@/api/config'
267268
268269
export default {
269270
name: 'InputAction',
@@ -957,7 +958,7 @@ export default {
957958
console.log('直接调用API:', props.apiUrl)
958959
const axios = (await import('axios')).default
959960
const response = await axios.post(props.apiUrl, actionData, {
960-
timeout: 30000,
961+
timeout: getActionTimeout(),
961962
headers: {
962963
'Accept': 'application/json',
963964
'Content-Type': 'application/json'

dashboard/src/components/actions/MenuAction.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ import {
263263
import { executeAction } from '@/api/modules/module.js'
264264
import { showToast } from '@/stores/toast.js'
265265
import { useRouter } from 'vue-router'
266+
import { getActionTimeout } from '@/api/config'
266267
267268
export default {
268269
name: 'MenuAction',
@@ -485,7 +486,7 @@ export default {
485486
console.log('直接调用API:', props.apiUrl)
486487
const axios = (await import('axios')).default
487488
const response = await axios.post(props.apiUrl, actionData, {
488-
timeout: 30000,
489+
timeout: getActionTimeout(),
489490
headers: {
490491
'Accept': 'application/json',
491492
'Content-Type': 'application/json'

dashboard/src/services/resetService.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ const DEFAULT_CONFIGS = {
1818
proxyPlayEnabled: false,
1919
proxySniff: 'http://localhost:57573/sniffer',
2020
proxySniffEnabled: false,
21-
snifferTimeout: 10
21+
snifferTimeout: 10,
22+
apiTimeout: 30
2223
},
2324

2425
// 应用设置默认值

dashboard/src/views/Settings.vue

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,62 @@
441441
</div>
442442
</div>
443443
</div>
444+
445+
<!-- 访问超时设置 -->
446+
<div class="address-config-item">
447+
<div class="address-config-row">
448+
<div class="address-config-info">
449+
<icon-clock-circle class="address-config-icon" />
450+
<div class="address-config-text">
451+
<div class="address-config-title">访问超时时间</div>
452+
<div class="address-config-desc">设置T4接口访问的超时时间(秒)</div>
453+
</div>
454+
</div>
455+
<div class="address-config-input-group">
456+
<a-input-number
457+
v-model="addressSettings.apiTimeout"
458+
placeholder="30"
459+
size="medium"
460+
class="address-config-input"
461+
:min="5"
462+
:max="120"
463+
:step="1"
464+
>
465+
<template #suffix>
466+
<span class="input-suffix">秒</span>
467+
</template>
468+
</a-input-number>
469+
<div class="address-config-actions">
470+
<a-button
471+
type="primary"
472+
@click="saveAddress('apiTimeout')"
473+
:loading="addressSaving.apiTimeout"
474+
size="medium"
475+
>
476+
<template #icon>
477+
<icon-save />
478+
</template>
479+
保存
480+
</a-button>
481+
</div>
482+
</div>
483+
</div>
484+
<div v-if="addressStatus.apiTimeout && addressStatus.apiTimeout.message" class="address-config-status">
485+
<div
486+
class="config-message"
487+
:class="{
488+
'config-message-success': addressStatus.apiTimeout.type === 'success',
489+
'config-message-error': addressStatus.apiTimeout.type === 'error',
490+
'config-message-warning': addressStatus.apiTimeout.type === 'warning'
491+
}"
492+
>
493+
<icon-check-circle v-if="addressStatus.apiTimeout.type === 'success'" class="config-icon" />
494+
<icon-exclamation-circle v-else-if="addressStatus.apiTimeout.type === 'error'" class="config-icon" />
495+
<icon-info-circle v-else class="config-icon" />
496+
<span class="config-text">{{ addressStatus.apiTimeout.message }}</span>
497+
</div>
498+
</div>
499+
</div>
444500
</div>
445501
</a-card>
446502

@@ -895,7 +951,8 @@ const addressSettings = reactive({
895951
proxyPlayEnabled: false,
896952
proxySniff: 'http://localhost:57573/sniffer',
897953
proxySniffEnabled: false,
898-
snifferTimeout: 10
954+
snifferTimeout: 10,
955+
apiTimeout: 30
899956
})
900957
901958
const addressSaving = reactive({
@@ -907,7 +964,8 @@ const addressSaving = reactive({
907964
proxyPlayReset: false,
908965
proxySniff: false,
909966
proxySniffReset: false,
910-
snifferTimeout: false
967+
snifferTimeout: false,
968+
apiTimeout: false
911969
})
912970
913971
const addressTesting = reactive({
@@ -920,7 +978,8 @@ const addressStatus = reactive({
920978
proxyAccess: null,
921979
proxyPlay: null,
922980
proxySniff: null,
923-
snifferTimeout: null
981+
snifferTimeout: null,
982+
apiTimeout: null
924983
})
925984
926985
// 播放器类型选项
@@ -1465,6 +1524,10 @@ const loadConfig = async () => {
14651524
if (!addressSettings.snifferTimeout) {
14661525
addressSettings.snifferTimeout = 10
14671526
}
1527+
// 确保访问超时有默认值
1528+
if (!addressSettings.apiTimeout) {
1529+
addressSettings.apiTimeout = 30
1530+
}
14681531
} catch (error) {
14691532
console.error('Failed to load address settings:', error)
14701533
}

0 commit comments

Comments
 (0)