import axios from 'axios';
import { Toast } from 'vant';
import store from '@/store';//倉庫
import router from '@/router'
axios.defaults.timeout = 60000;
axios.defaults.baseURL = 'this is api location';
const app_Id = "this is appId"
//開始loading劈猪,export導(dǎo)出去
export const loadingBegin = () => {
Toast.loading({
duration: 0, // 持續(xù)展示 toast
forbidClick: true, // 禁用背景點擊
loadingType: 'spinner',
message: '努力加載中'
});
}
//關(guān)閉loading
export const loadingFinish = () => {
Toast.clear();
}
//http request 攔截器 請求攔截(配置發(fā)送請求的信息)
axios.interceptors.request.use(config => {
// 處理請求之前的配置
let Token = window.localStorage.getItem('Token');
if (Token) {
config.headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + Token,
"appId": app_Id
}
} else {
// 請求失敗的處理
config.headers = {
'Content-Type': 'application/json',
"appId": app_Id
}
// router.replace({
// path:'/login',
// query:{
// redirect:router.currentRoute.fullpath
// }
// })
}
return config;
}, error => {
return Promise.reject(error);
}
);
//http response 攔截器 響應(yīng)攔截(配置請求回來的信息)
axios.interceptors.response.use(response => {
// 處理響應(yīng)數(shù)據(jù)
//if (response.data.code == 1000 || response.data.code == 1001) {
//Toast.fail('未授權(quán)宝穗,請重新登錄');
//window.localStorage.setItem("Token", "");
//router.push('my');
// }
return response;
}, error => {
// console.log('util.js error', error.response.data.code);
// 處理響應(yīng)失敗
// if (error.response.code == 1000 || error.response.code == 1001) {
//Toast.fail('未授權(quán)素邪,請重新登錄');
//router.push('my');
//}else if (error.response.status == 401) {
//Toast.fail('未授權(quán)值桩,請重新登錄');
// router.push('my');
// }
return Promise.reject(error)
}
)
/**
* 封裝get方法
* @param url
* @param data
* @returns {Promise}
*/
export function get(url, params = {}) {
return new Promise((resolve, reject) => {
axios.get(url, {
params: params
})
.then(response => {
resolve(response.data);
})
.catch(err => {
reject(err)
})
})
}
/**
* 封裝post請求
* @param url
* @param data
* @returns {Promise}
*/
export function post(url, data = {}) {
return new Promise((resolve, reject) => {
axios.post(url, data)
.then(response => {
resolve(response.data);
}, err => {
reject(err)
})
})
}
/**
* 封裝patch請求
* @param url
* @param data
* @returns {Promise}
*/
export function patch(url, data = {}) {
return new Promise((resolve, reject) => {
axios.patch(url, data)
.then(response => {
resolve(response.data);
}, err => {
reject(err)
})
})
}
/**
* 封裝put請求
* @param url
* @param data
* @returns {Promise}
*/
export function put(url, data = {}) {
return new Promise((resolve, reject) => {
axios.put(url, data)
.then(response => {
resolve(response.data);
}, err => {
reject(err)
})
})
}
/**
* 封裝delete請求
* @param url
* @param data
* @returns {Promise}
*/
export function del(url, params = {}) {
return new Promise((resolve, reject) => {
axios.delete(url, {
params: params
})
.then(response => {
resolve(response.data);
})
.catch(err => {
reject(err)
})
})
}
export const appId = app_Id
export const baseURL = axios.defaults.baseURL
使用
在API里引入
import { post, get, patch, put, del } from './util'
API.js
export default {
upImg(params, config){//
return post(`/upload/images/0005`, params, config)
},
code(phone, params) {//
return get(`/verificationcode/${phone}`, params)
},
escrowWallet(params) {
return post(`/escrowWallet`, params)
},
}
在main.js里引入
import api from '@/js/API'
Vue.prototype.$api = api;
頁面使用
let params = c;
let config = {
// headers: {
// a:"b"
//}
};
this.$api.upImg(params, config).then(res => {
//成功
}).catch(err => {
//失敗
});
this.$api.code(this.phone).then(res => {
}).catch(res => {
});
let data = {
//password: this.password,
//confirmPassword: this.confirmPassword
};
this.$api.escrowWallet(data).then(res => {
})