refactor(frontend): 重构前端代码以优化类型定义和组件逻辑
- 修改 ResponseData 类型定义,统一返回格式为数组类型 - 更新 API 函数签名以包含正确的返回类型注解 - 简化 AboutView 组件中的统计数据显示逻辑 - 移除未使用的变量和方法,精简组件代码 - 修复 HomeView 中的事件监听器类型定义问题 - 替换接口泛型中的 any 类型为 unknown 提高类型安全 - 注释掉未使用的全局变量避免潜在错误
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import request from "../utils/request.ts"
|
||||
|
||||
type ResponseData = Promise<{ code: number, message: Array<never> }>
|
||||
|
||||
export function book_download() {
|
||||
return request({
|
||||
url: "/book/download",
|
||||
@@ -24,7 +26,7 @@ export function video_hasTorrent() {
|
||||
})
|
||||
}
|
||||
|
||||
export function file_rename() {
|
||||
export function file_rename(): ResponseData {
|
||||
return request({
|
||||
url: "/file/rename",
|
||||
})
|
||||
@@ -60,7 +62,7 @@ export function test() {
|
||||
})
|
||||
}
|
||||
|
||||
export function torrent_statistics(){
|
||||
export function torrent_statistics(): ResponseData {
|
||||
return request({
|
||||
url: "/torrent/statistics",
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import axios, {
|
||||
} from "axios"
|
||||
|
||||
// 定义响应数据结构
|
||||
interface ResponseData<T = any> {
|
||||
interface ResponseData<T = unknown> {
|
||||
code: number
|
||||
msg: string
|
||||
data?: T
|
||||
@@ -18,8 +18,8 @@ interface RequestCache {
|
||||
time: number
|
||||
}
|
||||
|
||||
let downloadLoadingInstance: any
|
||||
export let isRelogin: { show: boolean } = {show: false}
|
||||
// let downloadLoadingInstance: any
|
||||
// export let isRelogin: { show: boolean } = {show: false}
|
||||
axios.defaults.headers["Content-Type"] = "application/json; charset=UTF-8"
|
||||
|
||||
const service: AxiosInstance = axios.create({
|
||||
|
||||
@@ -2,70 +2,22 @@
|
||||
import {onMounted, ref} from 'vue'
|
||||
import {torrent_statistics} from '@/api'
|
||||
|
||||
interface StatisticsData {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
let aa = ref<StatisticsData>({})
|
||||
let bb = ref("")
|
||||
let cc = ref("")
|
||||
let dd = ref("")
|
||||
|
||||
const aa = ref([])
|
||||
onMounted(async () => {
|
||||
const res = await torrent_statistics()
|
||||
console.log(res.message)
|
||||
aa.value = res.message
|
||||
})
|
||||
|
||||
const getSecondLevelOptions = () => {
|
||||
if (!bb.value || !aa.value[bb.value]) {
|
||||
return []
|
||||
}
|
||||
return aa.value[bb.value]
|
||||
}
|
||||
|
||||
const getThirdLevelOptions = () => {
|
||||
if (!bb.value || !cc.value || !aa.value[bb.value] || !aa.value[bb.value][cc.value]) {
|
||||
return []
|
||||
}
|
||||
console.log(aa.value[bb.value])
|
||||
return aa.value[bb.value][cc.value]
|
||||
}
|
||||
|
||||
</script>
|
||||
<template>
|
||||
<el-main>
|
||||
<el-card shadow="never">
|
||||
<template #default>
|
||||
<!-- <div v-for="(v,i) in getSecondLevelOptions()">-->
|
||||
<!-- <div>{{i}}</div>-->
|
||||
<!-- <el-button v-for="(v2,i2) in v" :key="i2">{{ v2 }}</el-button>-->
|
||||
<!-- </div>-->
|
||||
<div>
|
||||
<el-button v-for="(v,i) in aa" :key="i">{{ i }}</el-button>
|
||||
</div>
|
||||
</template>
|
||||
<template #footer>
|
||||
<el-select style="width: 120px" placeholder="请选择" v-model="bb">
|
||||
<el-option v-for="(v,i) in aa" :key="i" :label="i" :value="i"></el-option>
|
||||
</el-select>
|
||||
|
||||
<el-select style="width: 120px" placeholder="请选择" v-model="cc">
|
||||
<el-option v-for="(v,i) in getSecondLevelOptions()" :key="i" :label="i"
|
||||
:value="i"></el-option>
|
||||
</el-select>
|
||||
|
||||
<el-select style="width: 120px" placeholder="请选择" v-model="dd">
|
||||
<el-option v-for="(v,i) in getThirdLevelOptions()" :key="i" :label="v"
|
||||
:value="v"></el-option>
|
||||
</el-select>
|
||||
|
||||
<el-button type="primary" @click="">
|
||||
<el-icon>
|
||||
<Plus/>
|
||||
</el-icon>
|
||||
添加
|
||||
</el-button>
|
||||
</template>
|
||||
</el-card>
|
||||
</el-main>
|
||||
|
||||
@@ -20,10 +20,10 @@ interface messageItem {
|
||||
interface BtnItem {
|
||||
id: number
|
||||
title: string
|
||||
handle?: () => Promise<any>
|
||||
handle?: () => Promise<never>
|
||||
}
|
||||
|
||||
let loading = ref(false)
|
||||
const loading = ref(false)
|
||||
let eventSource: EventSource // 保存 EventSource 实例
|
||||
const status = ref('closed') // 连接状态:closed, connecting, connected
|
||||
const messages = ref<messageItem[]>([{id: Date.now(), text: '输出结果'}])
|
||||
@@ -74,7 +74,7 @@ const connect = (url: string) => {
|
||||
}
|
||||
eventSource = new EventSource(url)
|
||||
// 连接成功
|
||||
eventSource.addEventListener("open", (event) => {
|
||||
eventSource.addEventListener("open", () => {
|
||||
console.log('SSE 连接已打开')
|
||||
status.value = 'connected'
|
||||
})
|
||||
@@ -88,7 +88,7 @@ const connect = (url: string) => {
|
||||
closeConnection()
|
||||
})
|
||||
// 错误处理
|
||||
eventSource.addEventListener('error', (event) => {
|
||||
eventSource.addEventListener('error', () => {
|
||||
console.log('连接断开或失败', new Date());
|
||||
// 如果这里也频繁触发,确认了是断连重连
|
||||
if (eventSource.readyState === EventSource.CLOSED) {
|
||||
|
||||
Binary file not shown.
Reference in New Issue
Block a user