| | |
| | | // 封装网络请求 |
| | | import axios from 'axios' |
| | | import { Message } from 'element-ui' |
| | | import axios from "axios"; |
| | | import { Message } from "element-ui"; |
| | | export const baseUrl = process.env.VUE_APP_API_URL; |
| | | // 创建请求实例 |
| | | const req = axios.create({ |
| | | // baseURL: '/api' |
| | | // baseURL: '/account/verification' |
| | | baseURL: 'http://icmm.test.xyinde.com/api', |
| | | timeout: 10000 |
| | | }) |
| | | // baseURL: '/api' |
| | | // baseURL: '/account/verification' |
| | | baseURL: baseUrl, |
| | | timeout: 10000 |
| | | }); |
| | | |
| | | // 请求拦截 |
| | | req.interceptors.request.use((config) => { |
| | | // 携带token参数,一般会加载header Authorization |
| | | config.headers.Authorization = localStorage.getItem('access_token') |
| | | return config |
| | | }, (error) => { |
| | | return Promise.reject(error) |
| | | }) |
| | | req.interceptors.request.use( |
| | | (config) => { |
| | | // 携带token参数,一般会加载header Authorization |
| | | config.headers.Authorization = localStorage.getItem("access_token"); |
| | | config.headers.token = localStorage.getItem("access_token"); |
| | | return config; |
| | | }, |
| | | (error) => { |
| | | return Promise.reject(error); |
| | | } |
| | | ); |
| | | |
| | | // 响应拦截 |
| | | req.interceptors.response.use( |
| | | (res) => { |
| | | // 判断token过期时的一些处理 |
| | | if (res.data.success ==false||res.data.errorCode == '11') { |
| | | console.log('token过期了'); |
| | | Message.error(res.data.errorText || '') |
| | | // 清楚token |
| | | uni.getStorageSync('access_token'); |
| | | uni.clearStorageSync(); |
| | | uni.showToast({ |
| | | title: '请重新登录', |
| | | icon: 'none' |
| | | }); |
| | | uni.reLaunch({ |
| | | url: "/pages/Login/Login" |
| | | }); |
| | | } |
| | | console.log(res, '相应拦截1') |
| | | return res.data |
| | | }, |
| | | (error) => { |
| | | // state 400 500 |
| | | // 错误处理 |
| | | console.log(error, '相应拦截2') |
| | | // 跳登陆页面 |
| | | } |
| | | ) |
| | | (res) => { |
| | | // 判断token过期时的一些处理 |
| | | if ( |
| | | res.data.success == false && |
| | | res.data.errorText == "需要验证身份" && |
| | | res.data.errorCode == "11" |
| | | ) { |
| | | console.log("token过期了"); |
| | | Message.error(res.data.errorText || ""); |
| | | // 清楚token |
| | | uni.getStorageSync("access_token"); |
| | | uni.clearStorageSync(); |
| | | uni.showToast({ |
| | | title: "请重新登录", |
| | | icon: "none" |
| | | }); |
| | | uni.reLaunch({ |
| | | url: "/pages/Login/Login" |
| | | }); |
| | | } |
| | | return res.data; |
| | | }, |
| | | (error) => { |
| | | // state 400 500 |
| | | // 错误处理 |
| | | // 跳登陆页面 |
| | | } |
| | | ); |
| | | |
| | | export default req |
| | | export default req; |