vue中get和post请求
import axios from 'axios';
import router from '@/router';
import {
setSessionStorage,
getSessionStorage,
removeSessionStorage
} from '@/utils/mixin';
class Http {
constructor(){
this.Domain = 'http://*******';
}
require(options) {
if (!options.api) throw new Error('api 不能为空');
if (!options.param) { options.param = {} };
if (!options.methods) { options.methods = 'POST' }; //不传递方法默认为POST
};
return new Promise((resolve,reject) => {
let obj={
method: options.methods,
url: options.api,
baseURL: '/wap',
headers: {
'appname':'com.immortal.jeeqin',
'platform':'web',
'v':'1.0',
'lang':'zh-CN',
'country':'CN',
'token':getSessionStorage('token') || ''
},
params: options.param,//使用get方法时参数必须使用params
data:options.param
}
if(options.methods==='GET'){//判断方法是get时候 参数使用params
delete obj.data
}else{
delete obj.params
}
return axios(obj).then(response => {
const {state} = response.data;
//响应数据拦截处理
if (state == 2) {
//登录超时或互踢导致登录异常,跳转至登录页面
removeSessionStorage('token');
window.location.hash = '#/login';
} else {
//请求成功
return resolve(response.data);
}
},error => {
return reject()
}
})
}
export default Http;