1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- import axios from 'axios'
- import { Notification, MessageBox, Message } from 'element-ui'
- import store from '@/store'
- import { getToken } from '@/utils/auth'
- import errorCode from '@/utils/errorCode'
- import Cookies from "js-cookie";
- axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
- // 配置公共的根路径
- const services = axios.create({
-
- // 线上地址
- baseURL: 'http://172.4.5.232:12092',
- // 超时
- timeout: 10000
- })
- // 请求拦截器
- services.interceptors.request.use(config => {
- // 是否需要设置 token
- const isToken = (config.headers || {}).isToken === false
- if (getToken() && !isToken) {
- config.headers['Authorization'] = 'Bearer ' + Cookies.get("accessToken") // 让每个请求携带自定义token 请根据实际情况自行修改
- }
- // get请求映射params参数
- if (config.method === 'get' && config.params) {
- let url = config.url + '?';
- for (const propName of Object.keys(config.params)) {
- const value = config.params[propName];
- var part = encodeURIComponent(propName) + "=";
- if (value !== null && typeof (value) !== "undefined") {
- if (typeof value === 'object') {
- for (const key of Object.keys(value)) {
- if (value[key] !== null && typeof (value[key]) !== 'undefined') {
- let params = propName + '[' + key + ']';
- let subPart = encodeURIComponent(params) + '=';
- url += subPart + encodeURIComponent(value[key]) + '&';
- }
- }
- } else {
- url += part + encodeURIComponent(value) + "&";
- }
- }
- }
- url = url.slice(0, -1);
- config.params = {};
- config.url = url;
- }
- return config
- }, error => {
- console.log(error)
- Promise.reject(error)
- })
- export default services
|