自己项目需要,封装了一个ajax接口,对接于Graphql接口,可以自己手动改一部分
import config from "./config.js"
let ajax = {
// 默认配置
config: {
baseURL: 'http://127.0.0.1:9904',
header: {
'Content-Type': 'application/json;charset=UTF-8',
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,POST,OPTIONS",
Authorization: "Bearer "
},
method: 'GET',
dataType: 'json',
/* 如设为json,会对返回的数据做一次 JSON.parse */
responseType: 'text',
},
//路由 不拦截登录的页面
filterPages: ['pages/login/login'],
//设置拦截器
interceptors: {
request: null,
response: null
},
//请求
request(options) {
let _this = this;
options.baseURL = options.baseURL || _this.config.baseURL;
options.dataType = options.dataType || _this.config.dataType;
options.data = options.data || {};
options.url = options.baseURL + options.url;
options.method = options.method || _this.config.method;
if (_this.config.header.Authorization.indexOf(options.token) == -1) {
_this.config.header.Authorization = "Bearer " + options.token
options.header = _this.config.header
} else {
options.header = _this.config.header
}
options.sslVerify = false
// console.log(options.data)
return new Promise((resolve, reject) => {
options.success = (res) => {
if (res.statusCode == 200) {
//但是 如果我当前的请求数据为空,说明token过期了或者其他
if (res.data.statusCode == 1) {
//那么就获取当前的路由
let pages = getCurrentPages()
let currentRouter = null;
//#ifdef H5
currentRouter = pages[0].$page.path
//#endif
//#ifdef APP-PLUS
currentRouter = pages[pages.length - 1].route
//#endif
//看我当前是不是在我拦截的路由上进行的请求,如果是那就没啥,如果不是的话,那么请求失败了就要默认跳转至登录页
if (_this.filterPages.indexOf(currentRouter) == -1) {
uni.clearStorageSync();
uni.redirectTo({
url: '/pages/login/login',
});
}
}
// console.log("资源中心接口请求畅通!")
// console.log(res)
resolve(res.data)
} else if (res.statusCode == 401) {
uni.showToast({
icon: 'none',
title: '未授权',
duration: 2000
});
uni.clearStorageSync();
uni.reLaunch({
url: '/pages/login/login'
});
} else if (res.statusCode == 403) {
uni.showToast({
icon: 'none',
title: '登录身份失效,请重新登录',
duration: 2000
});
uni.clearStorageSync();
uni.reLaunch({
url: '/pages/login/login'
});
} else if (res.statusCode == 405) {
uni.showToast({
icon: 'none',
title: '请求方法错误',
duration: 1500
});
uni.clearStorageSync();
uni.reLaunch({
url: '/pages/login/login'
});
} else {
uni.showToast({
icon: 'none',
title: '请求错误:' + res.statusCode,
duration: 1500
});
uni.clearStorageSync();
uni.reLaunch({
url: '/pages/login/login'
});
}
},
options.fail = (err) => {
uni.showToast({
icon: 'none',
title: '请求失败,请稍后再试',
duration: 2000
});
reject(err)
}
uni.request(options)
})
},
//下面接口自己根据实际场景写自己的需要参数
post(url, request = {}) {
let baseURL = null;
let options = {};
if (!request.params.object) {
return {
statusCode: 500011,
message: "请求对象有误,请告诉我要访问哪个类型的接口!",
result: null
};
}
//判断当前请求是哪一个网关的
if (request.params.object === "user-center") {
baseURL = config.domainUser
options.data = request.data;
}
if (request.params.object === "resource") {
baseURL = config.domainResource
options.data = request
}
if (request.params.object === "resource" && request.params.service_name==="api_data_service") {
baseURL = config.domainResource
options.data = request
}
if (!baseURL) {
return {
statusCode: 500012,
message: "当前接口有误,请联系研发人员!",
result: null
};
}
options.url = url
// console.log("资源中心ip:"+baseURL)
options.method = 'POST'
options.baseURL = baseURL
options.token = request.token ? request.token : uni.getStorageSync('token') ? uni.getStorageSync('token') : "";
return ajax.request(options)
}
}
export default ajax
我的调用
//获取当前组织的文件服务器地址
export const getOrganizationParams = async () => {
let uuid = uni.getStorageSync("currentOrganization").oiUuid
const query =
`{
a:allOrganizationParameters(
condition: {
oiUuid: "${uuid}",
}
) {
totalCount
nodes {
# 组织参数名称
opName
}
}
}`;
const request = {
params: {
object: "resource",
service_name: "public_data_service",
},
data: {
query
}
};
return await ajaxRes.post("/public_code_service", request).then((res) => {
return {
statusCode: res.statusCode,
message: res.message,
result: res.statusCode === 0 ? res.result.a : res.result
};
}).catch(function(error) {
console.log('发生错误!', error);
return {
statusCode: 402,
message: "网络超时",
result: null
};
});
}
网友评论