mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-11-30 02:58:31 +08:00
feat: useVisit-判断用户是否首次访问某个功能
This commit is contained in:
parent
447146f67e
commit
cf00ad8e20
21
frontend/src/hooks/useVisit.ts
Normal file
21
frontend/src/hooks/useVisit.ts
Normal 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,
|
||||
};
|
||||
}
|
24
frontend/src/store/modules/app/visit.ts
Normal file
24
frontend/src/store/modules/app/visit.ts
Normal 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;
|
Loading…
Reference in New Issue
Block a user