不需要token
export function requestNoSesion(parameter,method,url){
return new Promise((resolve,reject) => {
if(method=='GET'){
var contentType="application/json"
}else{
// var contentType="application/x-www-form-urlencoded"
var contentType="application/json;charset=UTF-8"
}
uni.request({
url: `xxxx.com${url}`,
method:method,
data:parameter,
header: {
"content-type": contentType,
"X-Requested-With": "XMLHttpRequest"
},
async:true,
complete: (res) => {
resolve(res)
}
})
})
}
需要token
token在登录之后存入缓存中
let res = await login({参数}, 'GET', '接口地址');
uni.setStorageSync('token',res.data.token)
export const service_url = 'xxx.com';
export function axios(parameter, method, url, headerOptions){
const header = {
'content-type': "application/json;charset=UTF-8",
'X-Requested-With': 'XMLHttpRequest',
};
const token = uni.getStorageSync('token') || '';
if(token !== ''){
header['Authorization'] = `Bearer ${token}`;
}
return new Promise((resolve,reject) => {
uni.request({
url:`${service_url}${url}`,
method:method,
data:parameter,
header: {...header, ...headerOptions},
async:true,
complete: (res) => {
resolve(res)
}
})
})
}