2024-10-20 02:25:09 +08:00

60 lines
1.5 KiB
JavaScript

import { store } from 'https://kevisual.xiongxiao.me/system/lib';
import { query } from './query.js';
export const meStore = store.create((set, get) => {
return {
open: false,
setOpen: (open) => set({ open }),
loading: false,
setLoading: (loading) => set({ loading }),
mount: false,
setMount: (mount) => set({ mount }),
me: null,
setMe: (me) => set({ me }),
getMe: async () => {
const res = await query.post({
path: 'user',
key: 'me',
});
if (res.code === 200) {
set({ me: res.data });
}
},
init: async () => {
const { loading, mount } = get();
const isLogin = location.pathname === '/user/login';
if (isLogin) return;
if (loading || mount) return;
set({ loading: true });
const res = await query.post({
path: 'user',
key: 'me',
});
set({ loading: false });
if (res.code === 200) {
set({ me: res.data, mount: true });
}
},
switchOrg: async (username, type = undefined) => {
const res = await query.post({
path: 'user',
key: 'switchOrg',
data: {
username,
type,
},
});
if (res.code === 200) {
const { token } = res.data;
query.saveToken(token);
// message.success('Switch success');
// window.location.reload();
} else {
message.error(res.message || 'Request failed');
}
},
};
}, 'me');