yxxh5/src/request/index.js

52 lines
1.1 KiB
JavaScript

import http from './interface'
/**
* 将业务所有接口统一起来便于维护
* 如果项目很大可以将 url 独立成文件,接口分成不同的模块
*
*/
export const request = (url,data,method,isback = false, isJson = false) => {
//设置请求前拦截器
http.interceptor.request = (config) => {
if(isJson){
config.header = {
'Content-Type':'application/x-www-form-urlencoded'
}
}else{
config.header = {
'Content-Type':'application/json;charset=UTF-8'
}
}
try {
const token = uni.getStorageSync('token');
// const Authorization = uni.getStorageSync('Authorization');
if(token){
config.header.Authorization = "Bearer "+ token
// config.header['x-token']= token
}else{
config.header.Authorization = ""
// config.header.token = ""
}
} catch (e) {
// error
}
}
//设置请求结束后拦截器
http.interceptor.response = (response) => {
//判断返回状态 执行相应操作
return response;
}
return http.request({
url,
method,
data,
isback
})
}
// 默认全部导出 import api from '@/common/vmeitime-http/'
export default {
request
}