/**
* 封裝get方法
* @param url
* @param params
* @returns {Promise}
*/
export function fetch(url,params={}){
? return new Promise((resolve,reject) => {
? ? axios.get(url,{
? ? ? params:params
? ? })
? ? ? .then(response => {
? ? ? ? if(response.data.code === 200){
? ? ? ? ? resolve(response.data.data);
? ? ? ? }else if(response.data.code === 501){
? ? ? ? ? router.push({
? ? ? ? ? ? name:'login'//從哪個(gè)頁面跳轉(zhuǎn)
? ? ? ? ? })
? ? ? ? }else if(response.data.code === 503){
? ? ? ? ? // Vue.prototype.$message.error('請(qǐng)勿頻繁操作')
? ? ? ? }else{
? ? ? ? ? Vue.prototype.$message.error(response.data.msg)
? ? ? ? ? reject()
? ? ? ? }
? ? ? })
? ? ? .catch(err => {
? ? ? ? reject(err);
? ? ? ? let message = '請(qǐng)求失敗!請(qǐng)檢查網(wǎng)絡(luò)';
? ? ? ? if(err.response)message=err.response.data.message;
? ? ? ? Vue.prototype.$msgbox({
? ? ? ? ? title:'錯(cuò)誤贮懈!',
? ? ? ? ? message:message,
? ? ? ? ? type:'error',
? ? ? ? })
? ? ? })
? })
}
/**
* 封裝post請(qǐng)求
* @param url
* @param data
* @returns {Promise}
*/
export function post(url,data = {}){
? return new Promise((resolve,reject) => {
? ? axios.post(url,data)
? ? ? .then(response => {
? ? ? ? if(response.data.code === 200){
? ? ? ? ? resolve(response.data.data);
? ? ? ? }else if(response.data.code === 501){
? ? ? ? ? router.push({
? ? ? ? ? ? name:'login'//從哪個(gè)頁面跳轉(zhuǎn)
? ? ? ? ? })
? ? ? ? }else if(response.data.code === 503){
? ? ? ? ? // Vue.prototype.$message.error('請(qǐng)勿平凡操作')
? ? ? ? }else{
? ? ? ? ? Vue.prototype.$message.error(response.data.msg)
? ? ? ? }
? ? ? },err => {
? ? ? ? reject(err);
? ? ? ? let message = '請(qǐng)求失敹笸省麻削!請(qǐng)檢查網(wǎng)絡(luò)';
? ? ? ? if(err.response)message=err.response.data.message;
? ? ? ? Vue.prototype.$msgbox({
? ? ? ? ? title:'錯(cuò)誤锉桑!',
? ? ? ? ? message:message,
? ? ? ? ? type:'error',
? ? ? ? })
? ? ? })
? })
}
/**
* 封裝patch請(qǐng)求
* @param url
* @param data
* @returns {Promise}
*/
export function patch(url,data = {}){
? return new Promise((resolve,reject) => {
? ? axios.patch(url,data)
? ? ? .then(response => {
? ? ? ? if(response.data.code === 200){
? ? ? ? ? resolve(response.data.data);
? ? ? ? }else if(response.data.code === 501){
? ? ? ? ? router.push({
? ? ? ? ? ? name:'login'//從哪個(gè)頁面跳轉(zhuǎn)
? ? ? ? ? })
? ? ? ? }else if(response.data.code === 503){
? ? ? ? ? // Vue.prototype.$message.error('請(qǐng)勿平凡操作')
? ? ? ? }else{
? ? ? ? ? Vue.prototype.$message.error(response.data.msg)
? ? ? ? }
? ? ? },err => {
? ? ? ? reject(err);
? ? ? ? let message = '請(qǐng)求失敱锨础攻人!請(qǐng)檢查網(wǎng)絡(luò)';
? ? ? ? if(err.response)message=err.response.data.message;
? ? ? ? Vue.prototype.$msgbox({
? ? ? ? ? title:'錯(cuò)誤进副!',
? ? ? ? ? message:message,
? ? ? ? ? type:'error',
? ? ? ? })
? ? ? })
? })
}
/**
* 封裝put請(qǐng)求
* @param url
* @param data
* @returns {Promise}
*/
export function put(url,data = {}){
? return new Promise((resolve,reject) => {
? ? axios.put(url,data)
? ? ? .then(response => {
? ? ? ? if(response.data.code === 200){
? ? ? ? ? resolve(response.data.data);
? ? ? ? }else if(response.data.code === 501){
? ? ? ? ? router.push({
? ? ? ? ? ? name:'login'//從哪個(gè)頁面跳轉(zhuǎn)
? ? ? ? ? })
? ? ? ? }else if(response.data.code === 503){
? ? ? ? ? // Vue.prototype.$message.error('請(qǐng)勿平凡操作')
? ? ? ? }else{
? ? ? ? ? Vue.prototype.$message.error(response.data.msg)
? ? ? ? }
? ? ? },err => {
? ? ? ? reject(err);
? ? ? ? let message = '請(qǐng)求失斦獯А!請(qǐng)檢查網(wǎng)絡(luò)';
? ? ? ? if(err.response)message=err.response.data.message;
? ? ? ? Vue.prototype.$msgbox({
? ? ? ? ? title:'錯(cuò)誤敢会!',
? ? ? ? ? message:message,
? ? ? ? ? type:'error',
? ? ? ? })
? ? ? })
? })
}
export function del(url,data = {}){
? return new Promise((resolve,reject) => {
? ? axios.delete(url,data)
? ? ? .then(response => {
? ? ? ? if(response.data.code === 200){
? ? ? ? ? resolve(response.data.data);
? ? ? ? }else if(response.data.code === 501){
? ? ? ? ? router.push({
? ? ? ? ? ? name:'login'//從哪個(gè)頁面跳轉(zhuǎn)
? ? ? ? ? })
? ? ? ? }else if(response.data.code === 503){
? ? ? ? ? // Vue.prototype.$message.error('請(qǐng)勿平凡操作')
? ? ? ? }else{
? ? ? ? ? Vue.prototype.$message.error(response.data.msg)
? ? ? ? }
? ? ? },err => {
? ? ? ? reject(err);
? ? ? ? let message = '請(qǐng)求失斣颉!請(qǐng)檢查網(wǎng)絡(luò)';
? ? ? ? if(err.response)message=err.response.data.message;
? ? ? ? Vue.prototype.$msgbox({
? ? ? ? ? title:'錯(cuò)誤鸥昏!',
? ? ? ? ? message:message,
? ? ? ? ? type:'error',
? ? ? ? })
? ? ? })
? })
}