接入系统公司、部门、角色接口
This commit is contained in:
+89
-50
@@ -1,5 +1,5 @@
|
||||
import axios from 'axios';
|
||||
import { ElNotification, ElMessageBox } from 'element-plus';
|
||||
import { ElNotification, ElMessageBox, ElMessage } from 'element-plus';
|
||||
import sysConfig from "@/config";
|
||||
import tool from '@/utils/tool';
|
||||
import router from '@/router';
|
||||
@@ -33,61 +33,100 @@ let MessageBox_401_show = false
|
||||
// HTTP response 拦截器
|
||||
axios.interceptors.response.use(
|
||||
(response) => {
|
||||
return response;
|
||||
},
|
||||
(error) => {
|
||||
if (error.response) {
|
||||
if (error.response.status == 404) {
|
||||
ElNotification.error({
|
||||
title: '请求错误',
|
||||
message: "Status:404,正在请求不存在的服务器记录!"
|
||||
});
|
||||
} else if (error.response.status == 500) {
|
||||
ElNotification.error({
|
||||
title: '请求错误',
|
||||
message: error.response.data.message || "Status:500,服务器发生错误!"
|
||||
});
|
||||
} else if (error.response.status == 401) {
|
||||
if(!MessageBox_401_show){
|
||||
MessageBox_401_show = true
|
||||
ElMessageBox.confirm('当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。', '无权限访问', {
|
||||
type: 'error',
|
||||
closeOnClickModal: false,
|
||||
center: true,
|
||||
confirmButtonText: '重新登录',
|
||||
beforeClose: (action, instance, done) => {
|
||||
MessageBox_401_show = false
|
||||
done()
|
||||
}
|
||||
}).then(() => {
|
||||
router.replace({path: '/login'});
|
||||
}).catch(() => {})
|
||||
}
|
||||
} else {
|
||||
ElNotification.error({
|
||||
title: '请求错误',
|
||||
message: error.message || `Status:${error.response.status},未知错误!`
|
||||
});
|
||||
const token = response.headers.authorization;
|
||||
if (token) {
|
||||
let reToken = token.replace(/Bearer /g, "");
|
||||
tool.cookie.set("TOKEN", reToken);
|
||||
}
|
||||
const api_code = response.data.code;
|
||||
if (api_code === 401) {
|
||||
if (!MessageBox_401_show) {
|
||||
MessageBox_401_show = true;
|
||||
ElMessageBox.confirm("当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。","无权限访问",{
|
||||
type: "error",
|
||||
closeOnClickModal: false,
|
||||
center: true,
|
||||
confirmButtonText: "重新登录",
|
||||
beforeClose: (action, instance, done) => {
|
||||
MessageBox_401_show = false;
|
||||
done();
|
||||
},
|
||||
}).then(() => {
|
||||
// closeSock(true);
|
||||
tool.cookie.remove("TOKEN");
|
||||
router.replace({ path: "/login" });
|
||||
})
|
||||
.catch(() => {});
|
||||
}
|
||||
} else {
|
||||
} else if (api_code === 404) {
|
||||
ElNotification.error({
|
||||
title: '请求错误',
|
||||
message: "请求服务器无响应!"
|
||||
title: "请求错误",
|
||||
message: "Status:404,正在请求不存在的接口!",
|
||||
});
|
||||
} else if (api_code === 500) {
|
||||
ElNotification.error({
|
||||
title: "请求错误",
|
||||
message: response.data.msg || "Status:500,服务器发生错误!",
|
||||
});
|
||||
} else if (api_code === 0 || api_code !== 200) {
|
||||
ElMessage.warning(response.data.message);
|
||||
}
|
||||
|
||||
return response;
|
||||
|
||||
},
|
||||
(error) => {
|
||||
// if (error.response) {
|
||||
// if (error.response.status == 404) {
|
||||
// ElNotification.error({
|
||||
// title: '请求错误',
|
||||
// message: "Status:404,正在请求不存在的服务器记录!"
|
||||
// });
|
||||
// } else if (error.response.status == 500) {
|
||||
// ElNotification.error({
|
||||
// title: '请求错误',
|
||||
// message: error.response.data.message || "Status:500,服务器发生错误!"
|
||||
// });
|
||||
// } else if (error.response.status == 401) {
|
||||
// if(!MessageBox_401_show){
|
||||
// MessageBox_401_show = true
|
||||
// ElMessageBox.confirm('当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。', '无权限访问', {
|
||||
// type: 'error',
|
||||
// closeOnClickModal: false,
|
||||
// center: true,
|
||||
// confirmButtonText: '重新登录',
|
||||
// beforeClose: (action, instance, done) => {
|
||||
// MessageBox_401_show = false
|
||||
// done()
|
||||
// }
|
||||
// }).then(() => {
|
||||
// router.replace({path: '/login'});
|
||||
// }).catch(() => {})
|
||||
// }
|
||||
// } else {
|
||||
// ElNotification.error({
|
||||
// title: '请求错误',
|
||||
// message: error.message || `Status:${error.response.status},未知错误!`
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
|
||||
ElNotification.error({
|
||||
title: '请求错误',
|
||||
message: "请求服务器无响应!"
|
||||
});
|
||||
return Promise.reject(error.response);
|
||||
}
|
||||
);
|
||||
|
||||
var http = {
|
||||
const http = {
|
||||
|
||||
/** get 请求
|
||||
* @param {string} url 接口地址
|
||||
* @param {object} params 请求参数
|
||||
* @param {object} config 参数
|
||||
*/
|
||||
get: function(url, params={}, config={}) {
|
||||
get: function (url, params = {}, config = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios({
|
||||
method: 'get',
|
||||
@@ -107,7 +146,7 @@ var http = {
|
||||
* @param {object} data 请求参数
|
||||
* @param {object} config 参数
|
||||
*/
|
||||
post: function(url, data={}, config={}) {
|
||||
post: function (url, data = {}, config = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios({
|
||||
method: 'post',
|
||||
@@ -127,7 +166,7 @@ var http = {
|
||||
* @param {{}} data
|
||||
* @param {{}} config
|
||||
*/
|
||||
oss:function (url, data = {}, config = {}){
|
||||
oss: function (url, data = {}, config = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios({
|
||||
method: "post",
|
||||
@@ -149,7 +188,7 @@ var http = {
|
||||
* @param {object} data 请求参数
|
||||
* @param {object} config 参数
|
||||
*/
|
||||
put: function(url, data={}, config={}) {
|
||||
put: function (url, data = {}, config = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios({
|
||||
method: 'put',
|
||||
@@ -169,7 +208,7 @@ var http = {
|
||||
* @param {object} data 请求参数
|
||||
* @param {object} config 参数
|
||||
*/
|
||||
patch: function(url, data={}, config={}) {
|
||||
patch: function (url, data = {}, config = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios({
|
||||
method: 'patch',
|
||||
@@ -189,7 +228,7 @@ var http = {
|
||||
* @param {object} data 请求参数
|
||||
* @param {object} config 参数
|
||||
*/
|
||||
delete: function(url, data={}, config={}) {
|
||||
delete: function (url, data = {}, config = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios({
|
||||
method: 'delete',
|
||||
@@ -208,25 +247,25 @@ var http = {
|
||||
* @param {string} url 接口地址
|
||||
* @param {string} name JSONP回调函数名称
|
||||
*/
|
||||
jsonp: function(url, name='jsonp'){
|
||||
jsonp: function (url, name = 'jsonp') {
|
||||
return new Promise((resolve) => {
|
||||
var script = document.createElement('script')
|
||||
var _id = `jsonp${Math.ceil(Math.random() * 1000000)}`
|
||||
script.id = _id
|
||||
script.type = 'text/javascript'
|
||||
script.src = url
|
||||
window[name] =(response) => {
|
||||
window[name] = (response) => {
|
||||
resolve(response)
|
||||
document.getElementsByTagName('head')[0].removeChild(script)
|
||||
try {
|
||||
delete window[name];
|
||||
}catch(e){
|
||||
} catch (e) {
|
||||
window[name] = undefined;
|
||||
}
|
||||
}
|
||||
document.getElementsByTagName('head')[0].appendChild(script)
|
||||
})
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default http;
|
||||
|
||||
@@ -218,4 +218,87 @@ tool.crypto = {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 监听本地存储值的变化
|
||||
* @param {number} type 1 表示 localStorage,2 表示 sessionStorage
|
||||
* @param {string} key 键名
|
||||
* @param {string} data 要存储的数据
|
||||
* @returns {void}
|
||||
*/
|
||||
tool.addStorageEvent = function (type,key,data){
|
||||
if (type === 1) {
|
||||
// 创建一个 StorageEvent 事件
|
||||
let newStorageEvent = document.createEvent('StorageEvent');
|
||||
// 定义一个模拟的 localStorage 对象
|
||||
const storage = {
|
||||
setItem: function (k, val) {
|
||||
// 设置 localStorage 的值
|
||||
localStorage.setItem(k, val);
|
||||
// 初始化创建的事件
|
||||
newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
|
||||
// 派发对象
|
||||
window.dispatchEvent(newStorageEvent);
|
||||
}
|
||||
};
|
||||
// 调用模拟的 setItem 方法
|
||||
return storage.setItem(key, data);
|
||||
} else {
|
||||
// 如果是 sessionStorage
|
||||
let newStorageEvent = document.createEvent('StorageEvent');
|
||||
// 定义一个模拟的 sessionStorage 对象
|
||||
const storage = {
|
||||
setItem: function (k, val) {
|
||||
// 设置 sessionStorage 的值
|
||||
sessionStorage.setItem(k, val);
|
||||
// 初始化创建的事件
|
||||
newStorageEvent.initStorageEvent('setItem', false, false, k, null, val, null, null);
|
||||
// 派发对象
|
||||
window.dispatchEvent(newStorageEvent);
|
||||
}
|
||||
};
|
||||
// 调用模拟的 setItem 方法
|
||||
return storage.setItem(key, data);
|
||||
}
|
||||
}
|
||||
tool.resetSetItem = function(key, newVal) {
|
||||
if (key === "watchStorage") {
|
||||
let newStorageEvent = document.createEvent("StorageEvent");
|
||||
const storage = {
|
||||
setItem: function(k, val) {
|
||||
sessionStorage.setItem(k, val);
|
||||
// 初始化创建的事件
|
||||
newStorageEvent.initStorageEvent(
|
||||
"setItem",
|
||||
false,
|
||||
false,
|
||||
k,
|
||||
null,
|
||||
val,
|
||||
null,
|
||||
null
|
||||
);
|
||||
// 派发对象
|
||||
window.dispatchEvent(newStorageEvent);
|
||||
},
|
||||
};
|
||||
return storage.setItem(key, newVal);
|
||||
}
|
||||
}
|
||||
/* 获取地址栏参数 */
|
||||
tool.getParameterByName = function(name, url) {
|
||||
if (!url) url = window.location.href;
|
||||
// eslint-disable-next-line
|
||||
let nameText = name.replace(/[\[\]]/g, '$&');
|
||||
let regex = new RegExp('[?&]' + nameText + '(=([^&#]*)|&|#|$)');
|
||||
let results = regex.exec(url);
|
||||
if (!results) return null;
|
||||
if (!results[2]) return '';
|
||||
// 解析为 JavaScript 对象以去除双引号
|
||||
let decodedValue = decodeURIComponent(results[2].replace(/\+/g, ' '));
|
||||
|
||||
// 处理非 JSON 字符串,例如去除双引号
|
||||
decodedValue = decodedValue.replace(/^"(.*)"$/, '$1');
|
||||
return decodedValue;
|
||||
}
|
||||
|
||||
export default tool
|
||||
|
||||
Reference in New Issue
Block a user