|
@@ -1,154 +1,84 @@
|
|
|
-
|
|
|
- * 请求拦截、相应拦截、错误统一处理
|
|
|
- */
|
|
|
-import BASE_URL from './config.js';
|
|
|
-import axios from 'axios';
|
|
|
-import QS from 'qs';
|
|
|
-import { Toast } from 'vant';
|
|
|
-import store from '../store/index';
|
|
|
-import * as $auth from "./auth"
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-axios.defaults.baseURL =BASE_URL;
|
|
|
-
|
|
|
-
|
|
|
-axios.defaults.timeout = 10000;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-axios.defaults.headers.post['Content-Type'] = 'application/json; charset=utf-8';
|
|
|
-
|
|
|
-
|
|
|
-axios.interceptors.request.use(
|
|
|
- config => {
|
|
|
-
|
|
|
-
|
|
|
- const token = store.state.token;
|
|
|
- token && (config.headers.Authorization = token);
|
|
|
- return config;
|
|
|
- },
|
|
|
- error => {
|
|
|
- return Promise.error(error);
|
|
|
- })
|
|
|
-
|
|
|
-
|
|
|
-axios.interceptors.response.use(
|
|
|
- response => {
|
|
|
-
|
|
|
- if (response.statusCode === 200) {
|
|
|
- return Promise.resolve(response);
|
|
|
- } else {
|
|
|
- return Promise.reject(response);
|
|
|
- }
|
|
|
+import axios from "axios";
|
|
|
+import sysConfig from '@/config'
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+axios.defaults.baseURL = sysConfig.baseURL
|
|
|
+axios.defaults.timeout = sysConfig.timeout
|
|
|
+const ContentType = sysConfig.ContentType
|
|
|
+axios.defaults.headers["Content-Type"] = "application/json";
|
|
|
+
|
|
|
+
|
|
|
+axios.interceptors.request.use(
|
|
|
+ (config) => {
|
|
|
+ config.params.api_key = sysConfig.api_key
|
|
|
+ return config
|
|
|
},
|
|
|
-
|
|
|
- error => {
|
|
|
- if (error.response.statusCode) {
|
|
|
- switch (error.response.statusCode) {
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- case 401:
|
|
|
- router.replace({
|
|
|
- path: '/login',
|
|
|
- query: { redirect: router.currentRoute.fullPath }
|
|
|
- });
|
|
|
- break;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- case 408:
|
|
|
- Toast({
|
|
|
- message: '登录过期,请重新登录',
|
|
|
- duration: 1000,
|
|
|
- forbidClick: true
|
|
|
- });
|
|
|
-
|
|
|
- localStorage.removeItem('token');
|
|
|
- store.commit('loginSuccess', null);
|
|
|
-
|
|
|
- setTimeout(() => {
|
|
|
- router.replace({
|
|
|
- path: '/login',
|
|
|
- query: {
|
|
|
- redirect: router.currentRoute.fullPath
|
|
|
- }
|
|
|
- });
|
|
|
- }, 1000);
|
|
|
- break;
|
|
|
-
|
|
|
- case 404:
|
|
|
- Toast({
|
|
|
- message: '路径找不到',
|
|
|
- duration: 1500,
|
|
|
- forbidClick: true
|
|
|
- });
|
|
|
- break;
|
|
|
- case 503:
|
|
|
- Toast({
|
|
|
- message: '服务不可用',
|
|
|
- duration: 1500,
|
|
|
- forbidClick: true
|
|
|
- });
|
|
|
- break;
|
|
|
- case 504:
|
|
|
- Toast({
|
|
|
- message: '网络连接错误',
|
|
|
- duration: 1500,
|
|
|
- forbidClick: true
|
|
|
- });
|
|
|
- break;
|
|
|
-
|
|
|
- default:
|
|
|
- Toast({
|
|
|
- message: error.response.data.message,
|
|
|
- duration: 1500,
|
|
|
- forbidClick: true
|
|
|
- });
|
|
|
- }
|
|
|
- return Promise.reject(error.response);
|
|
|
- }
|
|
|
+ (error) => {
|
|
|
+ return error
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+axios.interceptors.response.use(
|
|
|
+ (response) => {
|
|
|
+ return response
|
|
|
+ },
|
|
|
+ (error) => {
|
|
|
+ return error
|
|
|
+ }
|
|
|
+)
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+var http = {
|
|
|
+
|
|
|
+ * get请求
|
|
|
+ * @param {*} url 请求地址
|
|
|
+ * @param {*} params 请求参数,拼接再url地址栏后面,无需求时忽略该参数
|
|
|
+ * @param {*} config 请求的config相关配置
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ get: function (url, params = {}, config = {}) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ axios({
|
|
|
+ url: url,
|
|
|
+ method: 'get',
|
|
|
+ params: params,
|
|
|
+ config: config
|
|
|
+ }).then((response) => {
|
|
|
+ resolve(response.data)
|
|
|
+ }).catch((error) => {
|
|
|
+ reject(error)
|
|
|
+ })
|
|
|
+ })
|
|
|
+
|
|
|
+ },
|
|
|
+
|
|
|
+
|
|
|
+ * post请求
|
|
|
+ * @param {*} url 请求地址
|
|
|
+ * @param {*} data 请求参数
|
|
|
+ * @param {*} params 请求参数,拼接再url地址栏后面,无需求时忽略该参数
|
|
|
+ * @param {*} config 请求的config相关配置
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+ post: function (url, data = {}, params = {}, config = {}) {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ axios({
|
|
|
+ url: url,
|
|
|
+ method: 'post',
|
|
|
+ data: data,
|
|
|
+ params: params,
|
|
|
+ config: config
|
|
|
+ }).then((response) => {
|
|
|
+ resolve(response.data)
|
|
|
+ }).catch((error) => {
|
|
|
+ reject(error)
|
|
|
+ })
|
|
|
+ })
|
|
|
}
|
|
|
-);
|
|
|
-
|
|
|
- * get方法,对应get请求
|
|
|
- * @param {String} url [请求的url地址]
|
|
|
- * @param {Object} params [请求时携带的参数]
|
|
|
- */
|
|
|
-export function get(url, params){
|
|
|
- return new Promise((resolve, reject) =>{
|
|
|
- axios.get(url, {
|
|
|
- params: params
|
|
|
- })
|
|
|
- .then(res => {
|
|
|
- resolve(res.data);
|
|
|
- })
|
|
|
- .catch(err => {
|
|
|
- reject(err.data)
|
|
|
- })
|
|
|
- });
|
|
|
-}
|
|
|
-
|
|
|
- * post方法,对应post请求
|
|
|
- * @param {String} url [请求的url地址]
|
|
|
- * @param {Object} params [请求时携带的参数]
|
|
|
- */
|
|
|
-export function post(url, params) {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- axios.post(url, QS.stringify(params))
|
|
|
- .then(res => {
|
|
|
- resolve(res.data);
|
|
|
- })
|
|
|
- .catch(err => {
|
|
|
- reject(err.data)
|
|
|
- })
|
|
|
- });
|
|
|
}
|
|
|
+
|
|
|
+export default http;
|
|
|
+
|