init
This commit is contained in:
61
apps/store/deploy/me.js
Normal file
61
apps/store/deploy/me.js
Normal file
@@ -0,0 +1,61 @@
|
||||
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');
|
||||
|
||||
meStore.getState().init();
|
||||
20
apps/store/deploy/query.js
Normal file
20
apps/store/deploy/query.js
Normal file
@@ -0,0 +1,20 @@
|
||||
import { QueryClient } from 'https://kevisual.xiongxiao.me/system/lib';
|
||||
|
||||
const env = window.env || {};
|
||||
const isDev = location.hostname === 'localhost';
|
||||
const productionUrl = env?.productionUrl || 'https://envision.xiongxiao.me/api/router';
|
||||
const url = isDev ? 'http://localhost:4002/api/router' : productionUrl;
|
||||
export const query = new QueryClient({
|
||||
url,
|
||||
});
|
||||
|
||||
query.after((res) => {
|
||||
const pathname = window.location.pathname;
|
||||
if (res.code === 401 && !pathname.includes('login')) {
|
||||
console.log('401');
|
||||
if (env?.needLogin) {
|
||||
window.location.href = env?.needLoginUrl || '/user/login';
|
||||
}
|
||||
}
|
||||
return res;
|
||||
});
|
||||
Reference in New Issue
Block a user