diff --git a/src/components/Home/Home.vue b/src/components/Home/Home.vue index 11f4e1d..0b749ed 100644 --- a/src/components/Home/Home.vue +++ b/src/components/Home/Home.vue @@ -1,24 +1,26 @@ diff --git a/src/components/Layout/ToolRecommend/ToolRecommend.vue b/src/components/Layout/ToolRecommend/ToolRecommend.vue new file mode 100644 index 0000000..771cb7b --- /dev/null +++ b/src/components/Layout/ToolRecommend/ToolRecommend.vue @@ -0,0 +1,93 @@ + + + + + \ No newline at end of file diff --git a/src/components/Tools/tools.ts b/src/components/Tools/tools.ts index af8bee3..4ce0786 100644 --- a/src/components/Tools/tools.ts +++ b/src/components/Tools/tools.ts @@ -720,7 +720,7 @@ export function urlKeyMap() { //获取工具 export function getTools(data: ToolsReqData) { //接收参数 - const { cateId, title } = data + const { cateId, title, route } = data //获取工具list let list = toolsList() //标题筛选 @@ -738,6 +738,15 @@ export function getTools(data: ToolsReqData) { return item.cateId == cateId; }); } + //路由筛选 + if (route) { + list = list.filter(item => { + // 移除末尾的斜杠后比较 + const itemUrl = item.url.replace(/\/$/, ''); + const routeUrl = route.replace(/\/$/, ''); + return itemUrl === routeUrl; + }); + } return list } diff --git a/src/store/modules/tools.ts b/src/store/modules/tools.ts index 6b4af7d..2a3ac9c 100644 --- a/src/store/modules/tools.ts +++ b/src/store/modules/tools.ts @@ -7,17 +7,19 @@ import type { IpReqData, IpInfo } from '@/api/ip/type' import type { WebInfo, WebInfoReqData } from '@/api/webinfo/type' import { fetchWebInfo } from '@/api/webinfo' +// 收藏工具列表存储键名 +const COLLECTED_TOOLS_KEY = 'collected_tools'; + export const useToolsStore = defineStore('tools', { //用来存放变量 state: () => ({ list: [] as ToolsInfo[], - toolInfo: {} as ToolsInfo, + currentTool: {} as ToolsInfo, cates: [] as any[], recommends: [] as ToolsInfo[], ipData: {} as IpInfo, webInfo: {} as WebInfo, - collect: [] as ToolsInfo[], - collectIds: [] as number[], + collectedTools: [] as ToolsInfo[], }), //方法 actions: { @@ -32,7 +34,7 @@ export const useToolsStore = defineStore('tools', { async getToolInfo(data: ToolsReqData) { //发送请求 const result: any = await getTools(data) - this.toolInfo = result + this.currentTool = result[0] || {} return result }, //获取tools cate @@ -60,5 +62,54 @@ export const useToolsStore = defineStore('tools', { return Promise.reject(new Error(result.message)) } }, + // 加载收藏工具 + loadCollectedTools() { + try { + const stored = localStorage.getItem(COLLECTED_TOOLS_KEY); + if (stored) { + this.collectedTools = JSON.parse(stored); + } + } catch (error) { + console.error('加载收藏工具失败:', error); + this.collectedTools = []; + } + }, + // 保存收藏工具到本地存储 + saveCollectedTools() { + try { + localStorage.setItem(COLLECTED_TOOLS_KEY, JSON.stringify(this.collectedTools)); + } catch (error) { + console.error('保存收藏工具失败:', error); + } + }, + // 添加收藏 + addCollect(tool: ToolsInfo) { + // 检查是否已经收藏 + const isCollected = this.collectedTools.some(t => t.url === tool.url); + if (!isCollected) { + this.collectedTools.push(tool); + this.saveCollectedTools(); + return true; + } + return false; + }, + // 移除收藏 + removeCollect(url: string) { + const index = this.collectedTools.findIndex(tool => tool.url === url); + if (index > -1) { + this.collectedTools.splice(index, 1); + this.saveCollectedTools(); + return true; + } + return false; + }, + // 检查是否已收藏 + isCollected(url: string) { + const normalizedUrl = url.replace(/\/$/, ''); + return this.collectedTools.some(tool => { + const toolUrl = tool.url.replace(/\/$/, ''); + return toolUrl === normalizedUrl; + }); + }, } }) \ No newline at end of file