| | |
| | | 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"); |
| | | config.headers['Authorization'] = 'Basic c2FiZXI6c2FiZXJfc2VjcmV0' |
| | | if(token){ |
| | | config.headers[sysConfig.TOKEN_NAME] = sysConfig.TOKEN_PREFIX + token |
| | | } |
| | | 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; |
| | |
| | | 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('&'); |
| | | } |
| | | |
| | | var http = { |
| | | |
| | |
| | | }) |
| | | }, |
| | | |
| | | /** post 请求 |
| | | /** post 请求 request payload |
| | | * @param {string} url 接口地址 |
| | | * @param {object} data 请求参数 |
| | | * @param {object} config 参数 |
| | |
| | | }).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 参数 |
| | | */ |
| | | postJ: function(url, data={}, config={}) { |
| | | return new Promise((resolve, reject) => { |
| | | console.log(qsStringify(data)) |
| | | axios({ |
| | | method: 'post', |
| | | url: url, |
| | | headers: { 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'}, |
| | | data: qsStringify(data), |
| | | ...config |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | reject(error); |
| | | }) |
| | | }) |
| | | }, |
| | | |
| | | /** put 请求 |
| | | * @param {string} url 接口地址 |
| | | * @param {object} data 请求参数 |
| | |
| | | }).then((response) => { |
| | | resolve(response.data); |
| | | }).catch((error) => { |
| | | reject(error); |
| | | if(error.status == 400) { |
| | | ElMessage({ |
| | | message: error.data.msg, |
| | | type: 'warning', |
| | | }) |
| | | }else { |
| | | reject(error); |
| | | } |
| | | }) |
| | | }) |
| | | }, |