|
@@ -0,0 +1,49 @@
|
|
|
|
|
+import axios from 'axios'
|
|
|
|
|
+import { getActivePinia } from "pinia";
|
|
|
|
|
+import userData from '@/utils/userdata'
|
|
|
|
|
+const userinfo = userData.getUserData() || ''
|
|
|
|
|
+
|
|
|
|
|
+// 全局loading
|
|
|
|
|
+const resetLoaing = (type: boolean) => {
|
|
|
|
|
+ const store = getActivePinia()
|
|
|
|
|
+ if (!store) return
|
|
|
|
|
+ const obj = store.state.value
|
|
|
|
|
+ for (const key in obj) {
|
|
|
|
|
+ const el = obj[key]
|
|
|
|
|
+ if ('loading' in el) {
|
|
|
|
|
+ el.loading = type
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// http request 拦截=>增加是否显示loading
|
|
|
|
|
+axios.interceptors.request.use(
|
|
|
|
|
+ (config: any) => {
|
|
|
|
|
+ config.baseURL = import.meta.env.VITE_SERVER_URL
|
|
|
|
|
+ const headers = config.headers || {}
|
|
|
|
|
+ headers['Authorization'] = 'Bearer ' + userinfo.token
|
|
|
|
|
+ const showLoading = config.showLoading || false
|
|
|
|
|
+ if (showLoading) resetLoaing(true)
|
|
|
|
|
+ return config
|
|
|
|
|
+ },
|
|
|
|
|
+ (err) => { throw err }
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+// http response 拦截
|
|
|
|
|
+axios.interceptors.response.use(
|
|
|
|
|
+ (response) => {
|
|
|
|
|
+ const data = response.data
|
|
|
|
|
+ if (data.code !== 200) {
|
|
|
|
|
+ if (data.code === 2002) {
|
|
|
|
|
+ // token 错误
|
|
|
|
|
+ userData.clear()
|
|
|
|
|
+ window.location.href = '/login.html'
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ resetLoaing(false)
|
|
|
|
|
+ return data
|
|
|
|
|
+ },
|
|
|
|
|
+ (error) => { throw error }
|
|
|
|
|
+)
|
|
|
|
|
+export default axios
|