update query login
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { query } from '@/modules/query';
|
||||
import { query, queryLogin } from '@/modules';
|
||||
import { create } from 'zustand';
|
||||
import { message } from '@/modules/message';
|
||||
export const getIsMac = async () => {
|
||||
@@ -69,10 +69,7 @@ export const useLayoutStore = create<LayoutStore>((set) => ({
|
||||
me: {},
|
||||
setMe: (me) => set({ me }),
|
||||
getMe: async () => {
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'me',
|
||||
});
|
||||
const res = await queryLogin.getMe();
|
||||
if (res.code === 200) {
|
||||
set({ me: res.data });
|
||||
}
|
||||
@@ -80,19 +77,12 @@ export const useLayoutStore = create<LayoutStore>((set) => ({
|
||||
openUser: false,
|
||||
setOpenUser: (openUser) => set({ openUser }),
|
||||
switchOrg: async (username?: string, type?: string) => {
|
||||
const res = await query.post({
|
||||
path: 'user',
|
||||
key: 'switchOrg',
|
||||
data: {
|
||||
username,
|
||||
type,
|
||||
},
|
||||
});
|
||||
const res = await queryLogin.switchUser(username || '');
|
||||
if (res.code === 200) {
|
||||
const { token } = res.data;
|
||||
query.saveToken(token);
|
||||
message.success('Switch success');
|
||||
window.location.reload();
|
||||
setTimeout(() => {
|
||||
window.location.reload();
|
||||
}, 1000);
|
||||
} else {
|
||||
message.error(res.message || 'Request failed');
|
||||
}
|
||||
|
||||
@@ -2,7 +2,27 @@ import { QueryClient } from '@kevisual/query';
|
||||
import { modal } from './redirect-to-login';
|
||||
import { create } from 'zustand';
|
||||
import { message } from './message';
|
||||
import { QueryLogin } from '@kevisual/query-login';
|
||||
|
||||
export const query = new QueryClient();
|
||||
|
||||
/**
|
||||
* 登录查询
|
||||
*/
|
||||
export const queryLogin = new QueryLogin({
|
||||
query,
|
||||
});
|
||||
// @ts-ignore
|
||||
if (window.context) {
|
||||
// @ts-ignore
|
||||
window.context.queryLogin = queryLogin;
|
||||
} else {
|
||||
// @ts-ignore
|
||||
window.context = {
|
||||
queryLogin,
|
||||
};
|
||||
}
|
||||
// 在请求之前,如果stop了
|
||||
query.beforeRequest = async (config) => {
|
||||
if (config.headers) {
|
||||
const token = localStorage.getItem('token');
|
||||
@@ -12,16 +32,21 @@ query.beforeRequest = async (config) => {
|
||||
}
|
||||
return config;
|
||||
};
|
||||
query.afterResponse = async (res) => {
|
||||
if (res.code === 401) {
|
||||
modal.setOpen(true);
|
||||
}
|
||||
if (res.code === 403) {
|
||||
if (!res?.message) {
|
||||
message.error('Unauthorized');
|
||||
query.afterResponse = async (res, ctx) => {
|
||||
if (res?.code === 401) {
|
||||
if (query?.stop) {
|
||||
return { code: 500, success: false, message: '登录已过期' };
|
||||
}
|
||||
query.stop = true;
|
||||
const queryLoginRes = await queryLogin.afterCheck401ToRefreshToken(res, ctx);
|
||||
query.stop = false;
|
||||
if (queryLoginRes.code === 401) {
|
||||
modal.setOpen(true);
|
||||
}
|
||||
return queryLoginRes;
|
||||
}
|
||||
return res;
|
||||
|
||||
return res as any;
|
||||
};
|
||||
export const request = query.post;
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { create } from 'zustand';
|
||||
import { query } from '@/modules';
|
||||
import { query, queryLogin } from '@/modules';
|
||||
import { message } from '@/modules/message';
|
||||
type UserStore = {
|
||||
showEdit: boolean;
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { query } from '@/modules';
|
||||
import { query, queryLogin } from '@/modules';
|
||||
import { basename } from '@/modules/basename';
|
||||
import { message } from '@/modules/message';
|
||||
import { create } from 'zustand';
|
||||
|
||||
// 如果自己是在iframe中登录,需要调用这个方法
|
||||
export const postLoginInIframe = (token: string) => {
|
||||
console.log('window.parent !== window', window.parent !== window);
|
||||
@@ -54,16 +55,14 @@ export const useLoginStore = create<LoginStore>((set, get) => {
|
||||
}
|
||||
set({ loading: true });
|
||||
const loaded = message.loading('loading...', 0);
|
||||
const res = await query.post({ path: 'user', key: 'login', username, password });
|
||||
const res = await queryLogin.login({ username, password });
|
||||
setTimeout(loaded, 200);
|
||||
if (res.code === 200) {
|
||||
const { token } = res.data;
|
||||
message.success('Success');
|
||||
set({ isLogin: true });
|
||||
query.saveToken(token);
|
||||
await new Promise((resolve) => setTimeout(resolve, 1000));
|
||||
if (window.parent !== window) {
|
||||
postLoginInIframe(token);
|
||||
postLoginInIframe(res.data?.accessToken || '');
|
||||
await new Promise((resolve) => setTimeout(resolve, 3000));
|
||||
}
|
||||
const search = new URLSearchParams(window.location.search);
|
||||
|
||||
Reference in New Issue
Block a user