Files
tripellet-merchant/common/http/index.js
2023-06-06 17:11:04 +08:00

58 lines
1.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 };