From cf00ad8e20ef0e80a991d4e5d41a313bd4351ce8 Mon Sep 17 00:00:00 2001 From: baiqi Date: Thu, 6 Jul 2023 09:36:05 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20useVisit-=E5=88=A4=E6=96=AD=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=98=AF=E5=90=A6=E9=A6=96=E6=AC=A1=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=9F=90=E4=B8=AA=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/hooks/useVisit.ts | 21 +++++++++++++++++++++ frontend/src/store/modules/app/visit.ts | 24 ++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 frontend/src/hooks/useVisit.ts create mode 100644 frontend/src/store/modules/app/visit.ts diff --git a/frontend/src/hooks/useVisit.ts b/frontend/src/hooks/useVisit.ts new file mode 100644 index 0000000000..8dab8726d6 --- /dev/null +++ b/frontend/src/hooks/useVisit.ts @@ -0,0 +1,21 @@ +import { useUserStore, useVisitStore } from '@/store'; + +/** + * 判断账户是否第一次访问 + * @param key 自定义 key + * @param needTimeStamp 是否需要加上时间戳 + */ +export default function useVisit(key: string, needTimeStamp = false) { + const userStore = useUserStore(); + const visitStore = useVisitStore(); + const localKey = `${userStore.accountId}-${key}-${needTimeStamp ? new Date().getTime() : ''}`; + const addVisited = () => { + visitStore.addVisitedKey(localKey); + }; + const getIsVisited = () => visitStore.getIsVisited(localKey); + + return { + addVisited, + getIsVisited, + }; +} diff --git a/frontend/src/store/modules/app/visit.ts b/frontend/src/store/modules/app/visit.ts new file mode 100644 index 0000000000..c09fb35465 --- /dev/null +++ b/frontend/src/store/modules/app/visit.ts @@ -0,0 +1,24 @@ +import { defineStore } from 'pinia'; + +// 用于记录用户是否第一次访问某些功能,会持久化存储 +const useVisitStore = defineStore('visit', { + state: () => ({ + visitedKeys: [] as string[], + }), + actions: { + addVisitedKey(key: string) { + this.visitedKeys.push(key); + }, + deleteVisitedKey(key: string) { + this.visitedKeys = this.visitedKeys.filter((item) => item !== key); + }, + getIsVisited(key: string): boolean { + return this.visitedKeys.includes(key); + }, + }, + persist: { + paths: ['visitedKeys'], + }, +}); + +export default useVisitStore;