-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
124 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VITE_BASE_URL = 'http://127.0.0.1:5000' | ||
VITE_DESIGN_URL = 'http://127.0.0.1:3000' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VITE_BASE_URL = 'https://api.formily.top' | ||
VITE_DESIGN_URL = 'http://design.formily.top' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import axios from "axios"; | ||
import { ElMessage } from "element-plus"; | ||
// import { useGlobalStore } from "@/stores/global"; | ||
|
||
const NOT_LOGIN_CODE = -1; | ||
// const { accountInfo } = useGlobalStore(); | ||
|
||
console.log("axios base url", import.meta.env.VITE_BASE_URL); | ||
// console.log("axios base url", process.env.VUE_APP_BASE_URL); | ||
const instance = axios.create({ | ||
baseURL: import.meta.env.VITE_BASE_URL, | ||
timeout: 60000, | ||
// headers: { token: accountInfo.token }, | ||
}); | ||
|
||
// axios.interceptors.request.use(resolve func, reject func) | ||
// Add a request interceptor | ||
instance.interceptors.request.use( | ||
async function (config) { | ||
// Do something before request is sent | ||
console.log("request 拦截: ", config); | ||
|
||
// 为所有请求加一个时间戳参数 | ||
config.url += (config?.url?.includes("?") ? "&" : "?") + "t=" + +new Date(); | ||
|
||
// config.headers = { token: accountInfo.token }; | ||
// 防止刷新后,状态管理数据清空,导致找不到 token | ||
config.headers = { | ||
token: localStorage.getItem("config-fe-token"), | ||
}; | ||
return config; // 用来请求的参数 | ||
}, | ||
function (error: any) { | ||
// Do something with request error | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
instance.interceptors.response.use( | ||
function (response) { | ||
// Any status code that lie within the range of 2xx cause this function to trigger | ||
// Do something with response data | ||
console.log("响应拦截", response); | ||
|
||
// 如果身份校验失败,返回登录页 | ||
// response.data.code === 111 && (window.location.href = response.data); | ||
|
||
const { code, msg, plainMsg } = response.data; | ||
|
||
if (code !== 0) { | ||
ElMessage.error(plainMsg ? `${msg}: ${plainMsg}` : msg); | ||
// 如果是没有登录,跳转到登录页面 | ||
if (code === NOT_LOGIN_CODE) { | ||
// router.push("/login"); | ||
// TODO: 弹窗或跳转到登录页面 | ||
} | ||
} | ||
|
||
return response?.data?.data; // 过滤掉除data参数外的其它参数,响应接收到的值。 | ||
// return response; | ||
}, | ||
function (error) { | ||
// Any status codes that falls outside the range of 2xx cause this function to trigger | ||
// Do something with response error | ||
return Promise.reject(error); | ||
} | ||
); | ||
|
||
export default instance; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters