🦄 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 req = (method, url, params, responseType) => {
const token = `Bearer ${sessionStorage.getItem('authorization')}`;
var obj = null; var obj = null;
if (method == 'get') { if (method == 'get') {
obj = { obj = {
headers: new Headers({ headers: new Headers({
authorization: sessionStorage.getItem('authorization') authorization: token
// Version: Version,
}) })
}; };
if (params) { 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') { if (method == 'login') {
obj = { obj = {
method: 'post', method: 'post',
body: JSON.stringify(params), body: JSON.stringify(params),
headers: new Headers({ headers: new Headers({
'Content-Type': 'application/json' 'Content-Type': 'application/json'
// Version: Version,
}) })
}; };
} }
@ -233,8 +222,7 @@ const req = (method, url, params, responseType) => {
body: JSON.stringify(params), body: JSON.stringify(params),
headers: new Headers({ headers: new Headers({
'Content-Type': 'application/json', 'Content-Type': 'application/json',
authorization: sessionStorage.getItem('authorization') authorization: token
// Version: Version,
}) })
}; };
} }
@ -274,53 +262,12 @@ const req = (method, url, params, responseType) => {
.catch((error) => console.error('Error:', error)); .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) => { export const handelResponse = (res, cb, errCb) => {
if (res && res.code == 200) { if (res && res.code == 200) {
cb(res.data, res.message); cb(res.data, res.message);
} else { } else {
if (res && typeof errCb == 'function') { if (res && typeof errCb == 'function') {
Notify.clear(); Notify.clear();
// Notify.error(res.message);
errCb(res.message); errCb(res.message);
} }
} }
@ -760,7 +707,7 @@ export const postLoginStandby = (data) => {
//获取权限codes //获取权限codes
export const getCodes = () => { export const getCodes = () => {
return getCodesHttp(`${ApiUrl}/v1/menu/myCodes?systemId=2`); return req('get', `${ApiUrl}/v1/menu/myCodes?systemId=2`);
}; };
// logout退出登录 // logout退出登录

View File

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