| | |
| | | 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'; |
| | |
| | | axios.interceptors.request.use( |
| | | (config) => { |
| | | let token = tool.cookie.get("TOKEN"); |
| | | if(token){ |
| | | config.headers['Authorization'] = 'Basic c2FiZXI6c2FiZXJfc2VjcmV0' |
| | | if (token) { |
| | | config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + token |
| | | } |
| | | if(!sysConfig.REQUEST_CACHE && config.method == 'get'){ |
| | | if (!sysConfig.REQUEST_CACHE && config.method == 'get') { |
| | | config.params = config.params || {}; |
| | | config.params['_'] = new Date().getTime(); |
| | | // config.params['_'] = new Date().getTime(); |
| | | } |
| | | Object.assign(config.headers, sysConfig.HEADERS) |
| | | return config; |
| | |
| | | // HTTP response 拦截器 |
| | | axios.interceptors.response.use( |
| | | (response) => { |
| | | if (response.data.code && response.data.code !== 200) { |
| | | // ElMessage({ |
| | | // message: response.data.msg, |
| | | // type: 'warning', |
| | | // }) |
| | | // return new Error() |
| | | // return new Error(); |
| | | } |
| | | return response; |
| | | }, |
| | | (error) => { |
| | |
| | | message: error.response.data.message || "Status:500,服务器发生错误!" |
| | | }); |
| | | } else if (error.response.status == 401) { |
| | | if(!MessageBox_401_show){ |
| | | if (!MessageBox_401_show) { |
| | | MessageBox_401_show = true |
| | | ElMessageBox.confirm('当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。', '无权限访问', { |
| | | ElMessageBox.confirm(`${error?.response?.data?.msg || '当前用户已被登出或无权限访问当前资源,请尝试重新登录后再操作。'}`, '无权限访问', { |
| | | type: 'error', |
| | | closeOnClickModal: false, |
| | | center: true, |
| | |
| | | done() |
| | | } |
| | | }).then(() => { |
| | | router.replace({path: '/login'}); |
| | | }).catch(() => {}) |
| | | router.replace({ path: '/login' }); |
| | | }).catch(() => { }) |
| | | } |
| | | } else { |
| | | ElNotification.error({ |
| | | title: '请求错误', |
| | | message: error.message || `Status:${error.response.status},未知错误!` |
| | | }); |
| | | // ElNotification.error({ |
| | | // title: '请求错误', |
| | | // message: error.message || `Status:${error.response.status},未知错误!` |
| | | // }); |
| | | } |
| | | } else { |
| | | ElNotification.error({ |
| | | title: '请求错误', |
| | | message: "请求服务器无响应!" |
| | | }); |
| | | // ElNotification.error({ |
| | | // title: '请求错误', |
| | | // message: "请求服务器无响应!" |
| | | // }); |
| | | } |
| | | |
| | | return Promise.reject(error.response); |
| | | } |
| | | ); |
| | | function qsStringify(obj) { |
| | | return Object.keys(obj) |
| | | .map(key => { |
| | | if (Array.isArray(obj[key])) { |
| | | return obj[key] |
| | | .map(arrayValue => `${encodeURIComponent(key)}=${encodeURIComponent(arrayValue)}`) |
| | | .join('&'); |
| | | } |
| | | return `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`; |
| | | }) |
| | | .join('&'); |
| | | return Object.keys(obj) |
| | | .map(key => { |
| | | if (Array.isArray(obj[key])) { |
| | | return obj[key] |
| | | .map(arrayValue => `${encodeURIComponent(key)}=${encodeURIComponent(arrayValue)}`) |
| | | .join('&'); |
| | | } |
| | | return `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`; |
| | | }) |
| | | .join('&'); |
| | | } |
| | | |
| | | var http = { |
| | |
| | | * @param {object} params 请求参数 |
| | | * @param {object} config 参数 |
| | | */ |
| | | get: function(url, params={}, config={}) { |
| | | get: function (url, params = {}, config = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios({ |
| | | method: 'get', |
| | |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | if (error?.status == 400) { |
| | | ElMessage({ |
| | | message: error.data.msg, |
| | | type: 'warning', |
| | | }) |
| | | } else { |
| | | reject(error); |
| | | } |
| | | }) |
| | | }) |
| | | }, |
| | | /** post 请求 query string parameters |
| | | * @param {string} url 接口地址 |
| | | * @param {object} data 请求参数 |
| | | * @param {object} config 参数 |
| | | */ |
| | | getJ: function (url, params = {}, config = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios({ |
| | | method: 'post', |
| | | url: url, |
| | | headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, |
| | | params: params, |
| | | ...config |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | resolve(error.data); |
| | | reject(error); |
| | | }) |
| | | }) |
| | | }, |
| | | |
| | | /** post 请求 request payload |
| | | * @param {string} url 接口地址 |
| | | * @param {object} data 请求参数 |
| | | * @param {object} config 参数 |
| | | */ |
| | | post: function(url, data={}, config={}) { |
| | | post: function (url, data = {}, config = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios({ |
| | | method: 'post', |
| | |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | reject(error); |
| | | if (error?.status == 400) { |
| | | ElMessage({ |
| | | message: error.data.msg, |
| | | type: 'warning', |
| | | }) |
| | | } else { |
| | | reject(error); |
| | | } |
| | | }) |
| | | }) |
| | | }, |
| | |
| | | * @param {object} data 请求参数 |
| | | * @param {object} config 参数 |
| | | */ |
| | | postJ: function(url, data={}, config={}) { |
| | | postJ: function (url, data = {}, config = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios({ |
| | | method: 'post', |
| | | url: url, |
| | | headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8','Authorization': 'Basic c2FiZXI6c2FiZXJfc2VjcmV0'}, |
| | | headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8' }, |
| | | data: qsStringify(data), |
| | | ...config |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | resolve(error.data); |
| | | reject(error); |
| | | }) |
| | | }) |
| | | }, |
| | | |
| | | /** put 请求 |
| | | * @param {string} url 接口地址 |
| | | * @param {object} data 请求参数 |
| | | * @param {object} config 参数 |
| | | */ |
| | | put: function(url, data={}, config={}) { |
| | | put: function (url, data = {}, config = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios({ |
| | | method: 'put', |
| | |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | resolve(error.data); |
| | | reject(error); |
| | | }) |
| | | }) |
| | |
| | | * @param {object} data 请求参数 |
| | | * @param {object} config 参数 |
| | | */ |
| | | patch: function(url, data={}, config={}) { |
| | | patch: function (url, data = {}, config = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios({ |
| | | method: 'patch', |
| | |
| | | * @param {object} data 请求参数 |
| | | * @param {object} config 参数 |
| | | */ |
| | | delete: function(url, data={}, config={}) { |
| | | delete: function (url, data = {}, config = {}) { |
| | | return new Promise((resolve, reject) => { |
| | | axios({ |
| | | method: 'delete', |
| | |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | reject(error); |
| | | if (error.status == 400) { |
| | | ElMessage({ |
| | | message: error.data.msg, |
| | | type: 'warning', |
| | | }) |
| | | } else { |
| | | reject(error); |
| | | } |
| | | }) |
| | | }) |
| | | }, |
| | |
| | | * @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; |
| | | } |
| | | } |