forked from OtterMind/Chat2DB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
45 lines (38 loc) · 1.12 KB
/
Copy pathindex.ts
File metadata and controls
45 lines (38 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { StoreApi } from 'zustand';
import { UseBoundStoreWithEqualityFn, createWithEqualityFn } from 'zustand/traditional';
import { devtools } from 'zustand/middleware';
import { shallow } from 'zustand/shallow';
import { IUserVO } from '@/typings/user';
import { getUser } from '@/service/user';
export interface IUserStore {
curUser?: IUserVO | null;
}
const initUserStore: IUserStore = {
curUser: null,
};
/**
* 用户 store
*/
export const useUserStore: UseBoundStoreWithEqualityFn<StoreApi<IUserStore>> = createWithEqualityFn(
devtools(() => initUserStore),
shallow,
);
/**
*
* @param curUser 设置当前用户
*/
export const setCurUser = (curUser?: IUserVO) => {
useUserStore.setState({ curUser });
};
/**
* 获取当前用户
*/
export const queryCurUser = async () => {
// null 表示在padding,返回 void 0(undefined)表示未登录
const curUser = await getUser() || void 0;
useUserStore.setState({ curUser });
// 向cookie中写入当前用户id
const date = new Date('2030-12-30 12:30:00').toUTCString();
document.cookie = `CHAT2DB.USER_ID=${curUser?.id};Expires=${date}`;
return curUser
};