feat: useVisit-判断用户是否首次访问某个功能

This commit is contained in:
baiqi 2023-07-06 09:36:05 +08:00 committed by fit2-zhao
parent 447146f67e
commit cf00ad8e20
2 changed files with 45 additions and 0 deletions

View File

@ -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,
};
}

View File

@ -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;