🦄 refactor:使用统一登录token鉴权

This commit is contained in:
wangsongsole 2023-04-20 15:02:29 +08:00
parent aeb028aa61
commit 337416c436
2 changed files with 9 additions and 66 deletions

View File

@ -185,12 +185,12 @@ const getData = (method, url, params, responseType) => {
};
const req = (method, url, params, responseType) => {
const token = `Bearer ${sessionStorage.getItem('authorization')}`;
var obj = null;
if (method == 'get') {
obj = {
headers: new Headers({
authorization: sessionStorage.getItem('authorization')
// Version: Version,
authorization: token
})
};
if (params) {
@ -206,23 +206,12 @@ const req = (method, url, params, responseType) => {
}
}
}
if (method === 'unified') {
const token = sessionStorage.getItem('unified-token');
obj = {
method: 'get',
headers: {
Authorization: `Bearer ${token}`
}
};
}
if (method == 'login') {
obj = {
method: 'post',
body: JSON.stringify(params),
headers: new Headers({
'Content-Type': 'application/json'
// Version: Version,
})
};
}
@ -233,8 +222,7 @@ const req = (method, url, params, responseType) => {
body: JSON.stringify(params),
headers: new Headers({
'Content-Type': 'application/json',
authorization: sessionStorage.getItem('authorization')
// Version: Version,
authorization: token
})
};
}
@ -274,53 +262,12 @@ const req = (method, url, params, responseType) => {
.catch((error) => console.error('Error:', error));
};
const getCodesHttp = (url) => {
var obj = null;
const token = sessionStorage.getItem('unified-token');
obj = {
method: 'get',
headers: {
Authorization: `Bearer ${token}`
}
};
let http = fetch(url, obj).then((res) => {
window.timeshow = res.headers.get('Date-Time');
if (res.status == 200) {
return res.text();
} else {
switch (res.status) {
case 401:
res
.json()
.then((response) => {
if (response.code == 401) {
Notify.clear();
Notify.error(response.message);
setTimeout(() => {
window.location.href = '/';
}, 2000);
}
})
.catch();
break;
default:
return;
}
}
});
return http
.then((response) => response)
.catch((error) => console.error('Error:', error));
};
export const handelResponse = (res, cb, errCb) => {
if (res && res.code == 200) {
cb(res.data, res.message);
} else {
if (res && typeof errCb == 'function') {
Notify.clear();
// Notify.error(res.message);
errCb(res.message);
}
}
@ -760,7 +707,7 @@ export const postLoginStandby = (data) => {
//获取权限codes
export const getCodes = () => {
return getCodesHttp(`${ApiUrl}/v1/menu/myCodes?systemId=2`);
return req('get', `${ApiUrl}/v1/menu/myCodes?systemId=2`);
};
// logout退出登录

View File

@ -36,9 +36,9 @@ export default class App extends Component {
/* 跳转统一登录平台 */
toUnifiedLogin() {
setTimeout(() => {
window.location.href = `${
window.unifiedUrl
}#/login?url=${encodeURIComponent(window.location.href)}`;
window.location.href = `${window.unifiedUrl}?url=${encodeURIComponent(
window.location.href
)}`;
}, 1500);
}
@ -59,10 +59,7 @@ export default class App extends Component {
handelResponse(
res,
(response, msg) => {
sessionStorage.setItem('authorization', response.authorization);
/* 统一登录得token */
sessionStorage.setItem('unified-token', authorization);
sessionStorage.setItem('authorization', authorization);
getAllEnum().then((res) => {
handelResponse(res, (response, msg) => {
sessionStorage.setItem('enmu', JSON.stringify(response));
@ -70,8 +67,7 @@ export default class App extends Component {
});
/* 获取codes */
getCodes().then((data) => {
const { codes } = JSON.parse(data);
getCodes().then(({ codes }) => {
let menuList = getMenus(codes);
Notify.clear();