109 lines
2.7 KiB
JavaScript
109 lines
2.7 KiB
JavaScript
import { defineStore } from 'pinia'
|
|
import { ref, computed } from 'vue'
|
|
import { proxiesAPI, statsAPI } from '../api'
|
|
|
|
export const useProxyStore = defineStore('proxy', () => {
|
|
const proxies = ref([])
|
|
const total = ref(0)
|
|
const loading = ref(false)
|
|
const stats = ref({})
|
|
|
|
const availableCount = computed(() => stats.value.available || 0)
|
|
const totalCount = computed(() => stats.value.total || 0)
|
|
|
|
async function fetchStats() {
|
|
try {
|
|
const response = await statsAPI.getStats()
|
|
if (response.code === 200) {
|
|
stats.value = response.data
|
|
}
|
|
} catch (error) {
|
|
console.error('获取统计信息失败:', error)
|
|
}
|
|
}
|
|
|
|
async function fetchProxies(params) {
|
|
loading.value = true
|
|
try {
|
|
const response = await proxiesAPI.getProxies(params)
|
|
if (response.code === 200) {
|
|
proxies.value = response.data.list
|
|
total.value = response.data.total
|
|
}
|
|
} catch (error) {
|
|
console.error('获取代理列表失败:', error)
|
|
} finally {
|
|
loading.value = false
|
|
}
|
|
}
|
|
|
|
async function deleteProxy(ip, port) {
|
|
try {
|
|
const response = await proxiesAPI.deleteProxy(ip, port)
|
|
if (response.code === 200) {
|
|
return true
|
|
}
|
|
} catch (error) {
|
|
console.error('删除代理失败:', error)
|
|
}
|
|
return false
|
|
}
|
|
|
|
async function batchDeleteProxies(proxyList) {
|
|
try {
|
|
const response = await proxiesAPI.batchDeleteProxies(proxyList)
|
|
if (response.code === 200) {
|
|
return response.data.deleted_count
|
|
}
|
|
} catch (error) {
|
|
console.error('批量删除代理失败:', error)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
async function cleanInvalidProxies() {
|
|
try {
|
|
const response = await proxiesAPI.cleanInvalidProxies()
|
|
if (response.code === 200) {
|
|
return response.data.deleted_count
|
|
}
|
|
} catch (error) {
|
|
console.error('清理无效代理失败:', error)
|
|
}
|
|
return 0
|
|
}
|
|
|
|
async function exportProxies(format, protocol) {
|
|
try {
|
|
const response = await proxiesAPI.exportProxies(format, protocol)
|
|
const url = window.URL.createObjectURL(new Blob([response]))
|
|
const link = document.createElement('a')
|
|
link.href = url
|
|
link.setAttribute('download', `proxies.${format}`)
|
|
document.body.appendChild(link)
|
|
link.click()
|
|
link.remove()
|
|
window.URL.revokeObjectURL(url)
|
|
return true
|
|
} catch (error) {
|
|
console.error('导出代理失败:', error)
|
|
}
|
|
return false
|
|
}
|
|
|
|
return {
|
|
proxies,
|
|
total,
|
|
loading,
|
|
stats,
|
|
availableCount,
|
|
totalCount,
|
|
fetchStats,
|
|
fetchProxies,
|
|
deleteProxy,
|
|
batchDeleteProxies,
|
|
cleanInvalidProxies,
|
|
exportProxies
|
|
}
|
|
})
|