初始化
This commit is contained in:
57
common/http/index.js
Normal file
57
common/http/index.js
Normal file
@@ -0,0 +1,57 @@
|
||||
import Request from "./luch-request/index";
|
||||
import $store from "@/common/store/index";
|
||||
import config from "@/common/config";
|
||||
import i18n from "@/common/i18n/lang/index";
|
||||
|
||||
const http = new Request();
|
||||
http.setConfig((conf) => {
|
||||
/* 设置全局配置 */
|
||||
conf.baseURL = config.baseUrl;
|
||||
conf.header = {
|
||||
...conf.header,
|
||||
};
|
||||
return conf;
|
||||
});
|
||||
|
||||
/* 请求之前拦截器。可以使用async await 做异步操作 */
|
||||
http.interceptors.request.use(
|
||||
(conf) => {
|
||||
let token = $store.getters.token;
|
||||
let lan = i18n.locale ? i18n.locale : "zh";
|
||||
conf.header = {
|
||||
...conf.header,
|
||||
accessToken: token,
|
||||
"Accept-Language": lan,
|
||||
};
|
||||
console.log("http.interceptors.request", conf);
|
||||
return conf;
|
||||
},
|
||||
(conf) => {
|
||||
return Promise.reject(conf);
|
||||
}
|
||||
);
|
||||
|
||||
/* 请求之后拦截器。可以使用async await 做异步操作 */
|
||||
http.interceptors.response.use(
|
||||
async (response) => {
|
||||
if (response.data.code !== 0) {
|
||||
// 服务端返回的状态码不等于0,则reject()
|
||||
|
||||
uni.showToast({
|
||||
title: response.data.msg,
|
||||
icon: "none",
|
||||
});
|
||||
return Promise.reject(response);
|
||||
// return Promise.reject(response)
|
||||
}
|
||||
return response;
|
||||
},
|
||||
(response) => {
|
||||
// 请求错误做点什么。可以使用async await 做异步操作
|
||||
console.log("http.interceptors.response", response);
|
||||
return Promise.reject(response);
|
||||
}
|
||||
);
|
||||
|
||||
export default http;
|
||||
export { http };
|
Reference in New Issue
Block a user