mirror of
https://github.com/qaiu/netdisk-fast-download.git
synced 2026-06-11 07:57:28 +00:00
refactor: 前端代码质量清理
- fix: Home.vue 组件名 'App' → 'Home' - fix: DirectoryTree selectAll 补 parserUrl 空值检查 - fix: 提取 previewBaseUrl 到 utils/constants.js,解除 ShowFile 对 Home 的耦合 - fix: Home.vue focus 事件监听器改为命名函数,加 beforeUnmount 移除 - fix: Playground.vue MutationObserver 保存引用,onUnmounted 中 disconnect - chore: 删除未使用的 api.js - chore: 删除 ClientLinks.vue 死代码 downloadClient/shouldShowDownloadButton - chore: 删除 DirectoryTree.vue 死代码 buildTree
This commit is contained in:
@@ -487,10 +487,6 @@ export default {
|
|||||||
}
|
}
|
||||||
return `${baseUrl}?${params.toString()}`
|
return `${baseUrl}?${params.toString()}`
|
||||||
},
|
},
|
||||||
// 文件树与窗格同源:直接返回当前目录数据
|
|
||||||
buildTree(list) {
|
|
||||||
return list || []
|
|
||||||
},
|
|
||||||
// 懒加载子节点
|
// 懒加载子节点
|
||||||
loadNode(node, resolve) {
|
loadNode(node, resolve) {
|
||||||
if (node.level === 0) {
|
if (node.level === 0) {
|
||||||
@@ -852,7 +848,7 @@ export default {
|
|||||||
this.toggleFileSelect(file)
|
this.toggleFileSelect(file)
|
||||||
},
|
},
|
||||||
selectAll() {
|
selectAll() {
|
||||||
this.selectedFiles = this.currentFileList.filter(f => f.fileType !== 'folder')
|
this.selectedFiles = this.currentFileList.filter(f => f.fileType !== 'folder' && f.parserUrl)
|
||||||
},
|
},
|
||||||
deselectAll() {
|
deselectAll() {
|
||||||
this.selectedFiles = []
|
this.selectedFiles = []
|
||||||
|
|||||||
@@ -1,125 +0,0 @@
|
|||||||
import axios from 'axios'
|
|
||||||
|
|
||||||
// 创建 axios 实例
|
|
||||||
const api = axios.create({
|
|
||||||
baseURL: process.env.VUE_APP_API_BASE_URL || '',
|
|
||||||
timeout: 30000,
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json'
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 请求拦截器
|
|
||||||
api.interceptors.request.use(
|
|
||||||
config => {
|
|
||||||
// 可以在这里添加认证token等
|
|
||||||
return config
|
|
||||||
},
|
|
||||||
error => {
|
|
||||||
return Promise.reject(error)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// 响应拦截器
|
|
||||||
api.interceptors.response.use(
|
|
||||||
response => {
|
|
||||||
return response.data
|
|
||||||
},
|
|
||||||
error => {
|
|
||||||
console.error('API请求错误:', error)
|
|
||||||
|
|
||||||
if (error.response) {
|
|
||||||
// 服务器返回错误状态码
|
|
||||||
const message = error.response.data?.message || error.response.data?.error || '服务器错误'
|
|
||||||
return Promise.reject(new Error(message))
|
|
||||||
} else if (error.request) {
|
|
||||||
// 网络错误
|
|
||||||
return Promise.reject(new Error('网络连接失败,请检查网络设置'))
|
|
||||||
} else {
|
|
||||||
// 其他错误
|
|
||||||
return Promise.reject(new Error(error.message || '请求失败'))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
// 客户端链接 API
|
|
||||||
export const clientLinksApi = {
|
|
||||||
/**
|
|
||||||
* 获取所有客户端下载链接
|
|
||||||
* @param {string} shareUrl - 分享链接
|
|
||||||
* @param {string} password - 提取码(可选)
|
|
||||||
* @returns {Promise} 客户端链接响应
|
|
||||||
*/
|
|
||||||
async getClientLinks(shareUrl, password = '') {
|
|
||||||
const params = new URLSearchParams()
|
|
||||||
params.append('url', shareUrl)
|
|
||||||
if (password) {
|
|
||||||
params.append('pwd', password)
|
|
||||||
}
|
|
||||||
|
|
||||||
return await api.get(`/v2/clientLinks?${params.toString()}`)
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取指定类型的客户端下载链接
|
|
||||||
* @param {string} shareUrl - 分享链接
|
|
||||||
* @param {string} password - 提取码(可选)
|
|
||||||
* @param {string} clientType - 客户端类型
|
|
||||||
* @returns {Promise} 指定类型的客户端链接
|
|
||||||
*/
|
|
||||||
async getClientLink(shareUrl, password = '', clientType) {
|
|
||||||
const params = new URLSearchParams()
|
|
||||||
params.append('url', shareUrl)
|
|
||||||
if (password) {
|
|
||||||
params.append('pwd', password)
|
|
||||||
}
|
|
||||||
params.append('clientType', clientType)
|
|
||||||
|
|
||||||
return await api.get(`/v2/clientLink?${params.toString()}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 其他 API(如果需要的话)
|
|
||||||
export const parserApi = {
|
|
||||||
/**
|
|
||||||
* 解析分享链接
|
|
||||||
* @param {string} shareUrl - 分享链接
|
|
||||||
* @param {string} password - 提取码(可选)
|
|
||||||
* @returns {Promise} 解析结果
|
|
||||||
*/
|
|
||||||
async parseLink(shareUrl, password = '') {
|
|
||||||
const params = new URLSearchParams()
|
|
||||||
params.append('url', shareUrl)
|
|
||||||
if (password) {
|
|
||||||
params.append('pwd', password)
|
|
||||||
}
|
|
||||||
|
|
||||||
return await api.get(`/v2/linkInfo?${params.toString()}`)
|
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 获取文件列表
|
|
||||||
* @param {string} shareUrl - 分享链接
|
|
||||||
* @param {string} password - 提取码(可选)
|
|
||||||
* @param {string} dirId - 目录ID(可选)
|
|
||||||
* @param {string} uuid - UUID(可选)
|
|
||||||
* @returns {Promise} 文件列表
|
|
||||||
*/
|
|
||||||
async getFileList(shareUrl, password = '', dirId = '', uuid = '') {
|
|
||||||
const params = new URLSearchParams()
|
|
||||||
params.append('url', shareUrl)
|
|
||||||
if (password) {
|
|
||||||
params.append('pwd', password)
|
|
||||||
}
|
|
||||||
if (dirId) {
|
|
||||||
params.append('dirId', dirId)
|
|
||||||
}
|
|
||||||
if (uuid) {
|
|
||||||
params.append('uuid', uuid)
|
|
||||||
}
|
|
||||||
|
|
||||||
return await api.get(`/v2/getFileList?${params.toString()}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default api
|
|
||||||
6
web-front/src/utils/constants.js
Normal file
6
web-front/src/utils/constants.js
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
/**
|
||||||
|
* 前端全局常量
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** 预览服务基础 URL */
|
||||||
|
export const PREVIEW_BASE_URL = 'https://nfd-parser.github.io/nfd-preview/preview.html?src='
|
||||||
@@ -293,24 +293,6 @@ export default {
|
|||||||
return clientConfig[type]?.downloadUrl || '#'
|
return clientConfig[type]?.downloadUrl || '#'
|
||||||
}
|
}
|
||||||
|
|
||||||
// 判断是否应该显示下载客户端按钮
|
|
||||||
const shouldShowDownloadButton = (type) => {
|
|
||||||
const os = getOSInfo()
|
|
||||||
switch (type) {
|
|
||||||
case 'CURL':
|
|
||||||
// cURL 在 Windows 上可能需要安装
|
|
||||||
return os === 'windows'
|
|
||||||
case 'ARIA2':
|
|
||||||
// Aria2 需要手动安装
|
|
||||||
return true
|
|
||||||
case 'THUNDER':
|
|
||||||
// 迅雷主要在 Windows 上使用
|
|
||||||
return os === 'windows'
|
|
||||||
default:
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// 获取操作系统信息
|
// 获取操作系统信息
|
||||||
const getOSInfo = () => {
|
const getOSInfo = () => {
|
||||||
const userAgent = navigator.userAgent.toLowerCase()
|
const userAgent = navigator.userAgent.toLowerCase()
|
||||||
@@ -383,13 +365,6 @@ export default {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 下载客户端
|
|
||||||
const downloadClient = (type) => {
|
|
||||||
const url = getClientDownloadUrl(type)
|
|
||||||
window.open(url, '_blank', 'noopener,noreferrer')
|
|
||||||
ElMessage.success(`正在跳转到 ${getClientDisplayName(type)} 下载页面`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// 格式化文件大小
|
// 格式化文件大小
|
||||||
const formatFileSize = (bytes) => {
|
const formatFileSize = (bytes) => {
|
||||||
if (!bytes) return '未知'
|
if (!bytes) return '未知'
|
||||||
@@ -440,9 +415,7 @@ export default {
|
|||||||
getTextareaRows,
|
getTextareaRows,
|
||||||
goBack,
|
goBack,
|
||||||
getClientLogo,
|
getClientLogo,
|
||||||
downloadClient,
|
|
||||||
handleImageError,
|
handleImageError,
|
||||||
shouldShowDownloadButton,
|
|
||||||
getClientSupportsCookie,
|
getClientSupportsCookie,
|
||||||
goToAuthConfig
|
goToAuthConfig
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -595,11 +595,10 @@ import fileTypeUtils from '@/utils/fileTypeUtils'
|
|||||||
import { ElMessage, ElMessageBox } from 'element-plus'
|
import { ElMessage, ElMessageBox } from 'element-plus'
|
||||||
import { playgroundApi } from '@/utils/playgroundApi'
|
import { playgroundApi } from '@/utils/playgroundApi'
|
||||||
import { testConnection, autoDetect, addDownload, getConfig, saveConfig } from '@/utils/downloaderService'
|
import { testConnection, autoDetect, addDownload, getConfig, saveConfig } from '@/utils/downloaderService'
|
||||||
|
import { PREVIEW_BASE_URL } from '@/utils/constants'
|
||||||
export const previewBaseUrl = 'https://nfd-parser.github.io/nfd-preview/preview.html?src=';
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'App',
|
name: 'Home',
|
||||||
components: { DarkMode, DirectoryTree, DownloadDialog },
|
components: { DarkMode, DirectoryTree, DownloadDialog },
|
||||||
mixins: [fileTypeUtils],
|
mixins: [fileTypeUtils],
|
||||||
data() {
|
data() {
|
||||||
@@ -617,7 +616,7 @@ export default {
|
|||||||
parseResult: {},
|
parseResult: {},
|
||||||
downloadUrl: null,
|
downloadUrl: null,
|
||||||
directLink: '',
|
directLink: '',
|
||||||
previewBaseUrl,
|
previewBaseUrl: PREVIEW_BASE_URL,
|
||||||
|
|
||||||
// 功能结果
|
// 功能结果
|
||||||
markdownText: '',
|
markdownText: '',
|
||||||
@@ -1791,12 +1790,13 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 监听窗口焦点事件
|
// 监听窗口焦点事件
|
||||||
window.addEventListener('focus', () => {
|
this._onFocusHandler = () => {
|
||||||
if (this.autoReadClipboard) {
|
if (this.autoReadClipboard) {
|
||||||
this.hasClipboardSuccessTip = false // 聚焦时重置,只提示一次
|
this.hasClipboardSuccessTip = false // 聚焦时重置,只提示一次
|
||||||
this.getPaste()
|
this.getPaste()
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
window.addEventListener('focus', this._onFocusHandler)
|
||||||
|
|
||||||
// 首次打开页面弹出风险提示
|
// 首次打开页面弹出风险提示
|
||||||
if (!window.localStorage.getItem('nfd_risk_ack')) {
|
if (!window.localStorage.getItem('nfd_risk_ack')) {
|
||||||
@@ -1804,6 +1804,12 @@ export default {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
beforeUnmount() {
|
||||||
|
if (this._onFocusHandler) {
|
||||||
|
window.removeEventListener('focus', this._onFocusHandler)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
watch: {
|
watch: {
|
||||||
downloadUrl(val) {
|
downloadUrl(val) {
|
||||||
if (!val) {
|
if (!val) {
|
||||||
|
|||||||
@@ -2220,6 +2220,8 @@ curl "${baseUrl}/json/parser?url=${encodeURIComponent(exampleUrl)}"</pre>
|
|||||||
}, 100);
|
}, 100);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let themeObserver = null;
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
// 初始化移动端检测
|
// 初始化移动端检测
|
||||||
updateIsMobile();
|
updateIsMobile();
|
||||||
@@ -2246,10 +2248,10 @@ curl "${baseUrl}/json/parser?url=${encodeURIComponent(exampleUrl)}"</pre>
|
|||||||
const html = document.documentElement;
|
const html = document.documentElement;
|
||||||
if (html && html.classList) {
|
if (html && html.classList) {
|
||||||
try {
|
try {
|
||||||
const observer = new MutationObserver(() => {
|
themeObserver = new MutationObserver(() => {
|
||||||
checkDarkMode();
|
checkDarkMode();
|
||||||
});
|
});
|
||||||
observer.observe(html, {
|
themeObserver.observe(html, {
|
||||||
attributes: true,
|
attributes: true,
|
||||||
attributeFilter: ['class', 'data-theme']
|
attributeFilter: ['class', 'data-theme']
|
||||||
});
|
});
|
||||||
@@ -2266,6 +2268,7 @@ curl "${baseUrl}/json/parser?url=${encodeURIComponent(exampleUrl)}"</pre>
|
|||||||
window.removeEventListener('resize', updateIsMobile);
|
window.removeEventListener('resize', updateIsMobile);
|
||||||
// 移除页面关闭/刷新前的提示
|
// 移除页面关闭/刷新前的提示
|
||||||
window.removeEventListener('beforeunload', handleBeforeUnload);
|
window.removeEventListener('beforeunload', handleBeforeUnload);
|
||||||
|
themeObserver?.disconnect();
|
||||||
});
|
});
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import fileTypeUtils from '@/utils/fileTypeUtils'
|
import fileTypeUtils from '@/utils/fileTypeUtils'
|
||||||
import { previewBaseUrl } from '@/views/Home.vue'
|
import { PREVIEW_BASE_URL } from '@/utils/constants'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: 'ShowFile',
|
name: 'ShowFile',
|
||||||
@@ -44,7 +44,7 @@ export default {
|
|||||||
downloadUrl: '',
|
downloadUrl: '',
|
||||||
shareUrl: '', // 添加原始分享链接
|
shareUrl: '', // 添加原始分享链接
|
||||||
fileTypeUtils,
|
fileTypeUtils,
|
||||||
previewBaseUrl
|
previewBaseUrl: PREVIEW_BASE_URL
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
|||||||
Reference in New Issue
Block a user