import axios from 'axios' import Vue from 'vue' let config = {} const instance = axios.create(config) // 3. 配置信息 config = { // 每次请求的协议、IP地址。 设置该配置后,每次请求路径都可以使用相对路径,例如"/admin/login" // baseURL: "http://127.0.0.1:5590", // 请求超时时间 timeout: 0, // 每次请求携带cookie withCredentials: true } // 请求拦截,后期可能会用到,先注册在此 instance.interceptors.request.use( function (config) { const token = localStorage.getItem('token') // const token = 'eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI0ODExMjEwOTM1MjUzNzcwMjQiLCJhdXRoIjoiUk9MRV9GQUNUT1JZX0RJUkNUT1IsUk9MRV9JTk5FUl9VU0VSLFJPTEVfSEFSRFdBUkUiLCJ0b2tlbklkIjoiMSIsImV4cCI6MTY5NzYxMzg1OH0.hgBlNeVkFzDnGjJFoEN91X968v4ecLUCKW_m7nnbA6TsQIISAJREv5xGGyQYSNlxbwsS35N2aa8MyrmZqJcHxQ' const site = JSON.parse(localStorage.getItem('site')) // let proxy_url = 'http://121.37.173.82:82'; //打包上线时请改用此处 const proxyUrl = process.env.VUE_APP_BASE_URL // 打包上线时请改用此处 // let proxy_url = '/proxy'//打包上线时此处请注释掉 config.url = proxyUrl + config.url const urls = ['http://122.112.250.253:7774/jbl/api/mes/login','http://121.36.142.167:7774/jbl/api/mes/login','http://122.112.250.253:7774/jbl/api/site/all/ignore-action?_allow_anonymous=true','http://121.36.142.167:7774/jbl/api/site/all/ignore-action?_allow_anonymous=true','http://clouddevice.qingyaokeji.com/api/inout','http://122.112.250.253:7774/jbl/api/module-data/dispatch_orders/dispatch_orders/diy/update_print_status','http://122.112.250.253:7774/jbl/api/module-data/box_orders/box_orders/diy/update_print_status','http://122.112.250.253:7774/jbl/api/module-data/process_flow_card/process_flow_card/diy/update_print_status'] if (urls.indexOf(config.data.url) !== -1) { config.data.header = ['Content-Type:application/json'] } else { config.data.header = [`Authorization:Bearer ${token}`, 'Content-Type:application/json', `Site:${site.site}`] } // config.data = JSON.stringify(data) return config }, function (error) { // 对请求错误做些什么 return Promise.reject(error) } ) // 2. 响应拦截 instance.interceptors.response.use( (res) => { if (res.status === 200) { if (res.data.code === 200) { return res.data } else { // 若code 非 200 if (res.data.code === 401) { localStorage.removeItem('mobile_token') return location.reload() } Vue.prototype.$Message.error(res.data.msg || '未知错误') return res.data || res } } else { return Vue.prototype.$Message.error('请求超时') } }, // 对于错误响应的处理 (err) => { Vue.prototype.$Notice.error({ title: '请求失败', desc: err }) return err } ) export default instance