@yyp92-cli/template-vue-mobile 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +15 -0
- package/template/.env.development +5 -0
- package/template/.env.production +5 -0
- package/template/.env.test +5 -0
- package/template/README.md +5 -0
- package/template/auto-imports.d.ts +10 -0
- package/template/components.d.ts +28 -0
- package/template/index.html +16 -0
- package/template/package.json +35 -0
- package/template/pnpm-lock.yaml +2007 -0
- package/template/public/vite.svg +1 -0
- package/template/src/App.vue +24 -0
- package/template/src/assets/vue.svg +1 -0
- package/template/src/components/layout/index.vue +68 -0
- package/template/src/components/layout/navBar/index.vue +35 -0
- package/template/src/components/layout/tabBar/index.vue +47 -0
- package/template/src/global/constants.ts +4 -0
- package/template/src/main.ts +12 -0
- package/template/src/router/index.ts +38 -0
- package/template/src/router/routes.tsx +83 -0
- package/template/src/service/api.ts +7 -0
- package/template/src/service/config.ts +9 -0
- package/template/src/service/index.ts +1 -0
- package/template/src/service/request/index.ts +270 -0
- package/template/src/service/request/type.ts +5 -0
- package/template/src/service/service.ts +27 -0
- package/template/src/store/index.ts +14 -0
- package/template/src/store/login/index.ts +67 -0
- package/template/src/styles/global.scss +32 -0
- package/template/src/styles/index.scss +4 -0
- package/template/src/styles/reset.scss +17 -0
- package/template/src/theme/darkTheme.scss +51 -0
- package/template/src/theme/lightTheme.scss +53 -0
- package/template/src/utils/cache.ts +44 -0
- package/template/src/utils/download.ts +27 -0
- package/template/src/utils/format.ts +10 -0
- package/template/src/utils/index.ts +22 -0
- package/template/src/views/403/index.vue +23 -0
- package/template/src/views/404/index.vue +34 -0
- package/template/src/views/detail/index.vue +19 -0
- package/template/src/views/home/index.vue +19 -0
- package/template/src/views/login/index.vue +81 -0
- package/template/src/views/message/index.vue +19 -0
- package/template/src/views/mine/index.vue +19 -0
- package/template/src/views/todo/index.vue +19 -0
- package/template/src/vite-env.d.ts +4 -0
- package/template/tsconfig.app.json +24 -0
- package/template/tsconfig.json +7 -0
- package/template/tsconfig.node.json +25 -0
- package/template/vite.config.ts +59 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<router-view></router-view>
|
|
3
|
+
</template>
|
|
4
|
+
|
|
5
|
+
<script
|
|
6
|
+
setup
|
|
7
|
+
lang="ts"
|
|
8
|
+
name="App"
|
|
9
|
+
>
|
|
10
|
+
import {onMounted} from "vue";
|
|
11
|
+
import { Locale } from 'vant'
|
|
12
|
+
import zhCN from 'vant/es/locale/lang/zh-CN'
|
|
13
|
+
import {changeTheme} from '@/utils'
|
|
14
|
+
|
|
15
|
+
onMounted(() => {
|
|
16
|
+
changeTheme(false)
|
|
17
|
+
Locale.use('zh-CN', zhCN)
|
|
18
|
+
})
|
|
19
|
+
</script>
|
|
20
|
+
|
|
21
|
+
<style
|
|
22
|
+
scoped
|
|
23
|
+
lang="scss"
|
|
24
|
+
></style>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="layout">
|
|
3
|
+
<template v-if="route.meta.showFullScreen">
|
|
4
|
+
<router-view></router-view>
|
|
5
|
+
</template>
|
|
6
|
+
|
|
7
|
+
<template v-else>
|
|
8
|
+
<div class="layout-inner">
|
|
9
|
+
<div class="header">
|
|
10
|
+
<MyNavBar
|
|
11
|
+
:title="title"
|
|
12
|
+
:showLeftArrow="route.fullPath !== '/home'"
|
|
13
|
+
/>
|
|
14
|
+
</div>
|
|
15
|
+
|
|
16
|
+
<div class="content">
|
|
17
|
+
<router-view></router-view>
|
|
18
|
+
</div>
|
|
19
|
+
|
|
20
|
+
<div class="footer">
|
|
21
|
+
<MyTabBar />
|
|
22
|
+
</div>
|
|
23
|
+
</div>
|
|
24
|
+
</template>
|
|
25
|
+
</div>
|
|
26
|
+
</template>
|
|
27
|
+
|
|
28
|
+
<script
|
|
29
|
+
setup
|
|
30
|
+
lang="ts"
|
|
31
|
+
name="Layout"
|
|
32
|
+
>
|
|
33
|
+
import {ref, computed} from 'vue'
|
|
34
|
+
import { useRoute } from 'vue-router'
|
|
35
|
+
import MyNavBar from './navBar/index.vue'
|
|
36
|
+
import MyTabBar from './tabBar/index.vue'
|
|
37
|
+
|
|
38
|
+
const route = useRoute()
|
|
39
|
+
|
|
40
|
+
const title = computed(() => route.meta?.title || '首页')
|
|
41
|
+
</script>
|
|
42
|
+
|
|
43
|
+
<style
|
|
44
|
+
scoped
|
|
45
|
+
lang="scss"
|
|
46
|
+
>
|
|
47
|
+
.layout {
|
|
48
|
+
width: 100%;
|
|
49
|
+
height: 100%;
|
|
50
|
+
background: var(--bg-white-color);
|
|
51
|
+
|
|
52
|
+
.layout-inner {
|
|
53
|
+
width: 100%;
|
|
54
|
+
height: 100%;
|
|
55
|
+
display: flex;
|
|
56
|
+
flex-direction: column;
|
|
57
|
+
|
|
58
|
+
.header,
|
|
59
|
+
.footer {
|
|
60
|
+
flex-shrink: 0;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
.content {
|
|
64
|
+
flex: 1;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
</style>
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="nav-bar">
|
|
3
|
+
<van-nav-bar
|
|
4
|
+
:title="title"
|
|
5
|
+
:left-arrow="showLeftArrow"
|
|
6
|
+
@click-left="onClickLeft"
|
|
7
|
+
/>
|
|
8
|
+
</div>
|
|
9
|
+
</template>
|
|
10
|
+
|
|
11
|
+
<script
|
|
12
|
+
setup
|
|
13
|
+
lang="ts"
|
|
14
|
+
name="MyNavBar"
|
|
15
|
+
>
|
|
16
|
+
import {ref} from 'vue'
|
|
17
|
+
import {useRoute} from 'vue-router'
|
|
18
|
+
import router from '@/router'
|
|
19
|
+
|
|
20
|
+
defineProps([
|
|
21
|
+
'title',
|
|
22
|
+
'showLeftArrow'
|
|
23
|
+
])
|
|
24
|
+
|
|
25
|
+
function onClickLeft() {
|
|
26
|
+
router.back()
|
|
27
|
+
}
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<style
|
|
31
|
+
scoped
|
|
32
|
+
lang="scss"
|
|
33
|
+
>
|
|
34
|
+
.nav-bar {}
|
|
35
|
+
</style>
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="tab-bar">
|
|
3
|
+
<van-tabbar
|
|
4
|
+
v-model="active"
|
|
5
|
+
>
|
|
6
|
+
<template
|
|
7
|
+
v-for="item in loginStore.userMenus"
|
|
8
|
+
:key="item.path"
|
|
9
|
+
>
|
|
10
|
+
<van-tabbar-item
|
|
11
|
+
:to="item.path"
|
|
12
|
+
:icon="item.meta.icon"
|
|
13
|
+
>{{item.meta.title}}</van-tabbar-item>
|
|
14
|
+
</template>
|
|
15
|
+
</van-tabbar>
|
|
16
|
+
</div>
|
|
17
|
+
</template>
|
|
18
|
+
|
|
19
|
+
<script
|
|
20
|
+
setup
|
|
21
|
+
lang="ts"
|
|
22
|
+
name="MyTabBar"
|
|
23
|
+
>
|
|
24
|
+
import {ref, onMounted} from 'vue'
|
|
25
|
+
import {useRoute} from 'vue-router'
|
|
26
|
+
import useLoginStore from '@/store/login'
|
|
27
|
+
|
|
28
|
+
const route = useRoute()
|
|
29
|
+
const loginStore = useLoginStore()
|
|
30
|
+
const active = ref(0)
|
|
31
|
+
console.log('--loginStore.userMenus', loginStore.userMenus)
|
|
32
|
+
|
|
33
|
+
onMounted(() => {
|
|
34
|
+
const index = loginStore.userMenus.findIndex((item: any) => item.path === route.path)
|
|
35
|
+
active.value = index || 0
|
|
36
|
+
})
|
|
37
|
+
</script>
|
|
38
|
+
|
|
39
|
+
<style
|
|
40
|
+
scoped
|
|
41
|
+
lang="scss"
|
|
42
|
+
>
|
|
43
|
+
.tab-bar {
|
|
44
|
+
width: 100%;
|
|
45
|
+
height: 50px;
|
|
46
|
+
}
|
|
47
|
+
</style>
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { LOGIN_TOKEN, PERMISSION, MENUS} from '@/global/constants'
|
|
2
|
+
import { localCache } from '@/utils/cache'
|
|
3
|
+
import { createRouter, createWebHistory } from 'vue-router'
|
|
4
|
+
// import { getFirstMenu, getMenusRedirect, localRoutes } from '@/utils/map-menus'
|
|
5
|
+
import {getLocalPromission} from '@/utils'
|
|
6
|
+
import { routes } from './routes'
|
|
7
|
+
|
|
8
|
+
const router = createRouter({
|
|
9
|
+
history: createWebHistory(),
|
|
10
|
+
routes: routes
|
|
11
|
+
})
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* * 导航守卫
|
|
15
|
+
* 参数: to(跳转到的位置),from(从哪里跳转过来的),next: 路由的放行函数
|
|
16
|
+
* 返回值:返回值决定导航的路径(不返回或者返回 undefined, 默认跳转)
|
|
17
|
+
*/
|
|
18
|
+
router.beforeEach((to, from, next) => {
|
|
19
|
+
const permission = localCache.getCache(PERMISSION) ?? []
|
|
20
|
+
const menus = localCache.getCache(MENUS) ?? []
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
if (to.path === '/') {
|
|
24
|
+
next({
|
|
25
|
+
path: menus[0]?.path || '/'
|
|
26
|
+
})
|
|
27
|
+
}
|
|
28
|
+
else if (!['/403', '/login'].includes(to.path) && !permission.includes(to.path) && getLocalPromission(routes).includes(to.path)) {
|
|
29
|
+
return next('/403')
|
|
30
|
+
}
|
|
31
|
+
else {
|
|
32
|
+
next()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
next()
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
export default router
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
|
|
2
|
+
export const routes: any[] = [
|
|
3
|
+
{
|
|
4
|
+
path: '/',
|
|
5
|
+
name: 'main',
|
|
6
|
+
component: () => import('@/components/layout/index.vue'),
|
|
7
|
+
children: [
|
|
8
|
+
{
|
|
9
|
+
path: '/home',
|
|
10
|
+
name: 'home',
|
|
11
|
+
component: () => import('@/views/home/index.vue'),
|
|
12
|
+
meta: {
|
|
13
|
+
title: '首页',
|
|
14
|
+
icon: 'wap-home-o',
|
|
15
|
+
hideMenu: false,
|
|
16
|
+
showFullScreen: false
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
path: '/message',
|
|
21
|
+
name: 'message',
|
|
22
|
+
component: () => import('@/views/message/index.vue'),
|
|
23
|
+
meta: {
|
|
24
|
+
title: '消息',
|
|
25
|
+
icon: 'chat-o',
|
|
26
|
+
hideMenu: false,
|
|
27
|
+
showFullScreen: false
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
path: '/todo',
|
|
32
|
+
name: 'todo',
|
|
33
|
+
component: () => import('@/views/todo/index.vue'),
|
|
34
|
+
meta: {
|
|
35
|
+
title: '代办',
|
|
36
|
+
icon: 'label-o',
|
|
37
|
+
hideMenu: false,
|
|
38
|
+
showFullScreen: false
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
path: '/mine',
|
|
43
|
+
name: 'mine',
|
|
44
|
+
component: () => import('@/views/mine/index.vue'),
|
|
45
|
+
meta: {
|
|
46
|
+
title: '我的',
|
|
47
|
+
icon: 'user-o',
|
|
48
|
+
hideMenu: false,
|
|
49
|
+
showFullScreen: false
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
path: '/detail',
|
|
54
|
+
name: 'detail',
|
|
55
|
+
component: () => import('@/views/detail/index.vue'),
|
|
56
|
+
meta: {
|
|
57
|
+
title: '详情',
|
|
58
|
+
icon: '',
|
|
59
|
+
hideMenu: true,
|
|
60
|
+
showFullScreen: true
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
{
|
|
68
|
+
path: '/login',
|
|
69
|
+
component: () => import('@/views/login/index.vue')
|
|
70
|
+
},
|
|
71
|
+
|
|
72
|
+
{
|
|
73
|
+
path: '/403',
|
|
74
|
+
component: () => import('@/views/403/index.vue')
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
{
|
|
78
|
+
path: '/:pathMatch(.*)',
|
|
79
|
+
component: () => import('@/views/404/index.vue')
|
|
80
|
+
}
|
|
81
|
+
]
|
|
82
|
+
|
|
83
|
+
export default routes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './service'
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import axios from 'axios'
|
|
2
|
+
import { Toast } from 'vant'
|
|
3
|
+
import type { AxiosRequestConfig, AxiosError, AxiosResponse } from 'axios'
|
|
4
|
+
import type { MyInternalAxiosRequestConfig } from './type'
|
|
5
|
+
import { localCache } from '@/utils/cache'
|
|
6
|
+
import { LOGIN_TOKEN, USER_INFO, MENUS, PERMISSION } from '@/global/constants'
|
|
7
|
+
import router from '@/router'
|
|
8
|
+
|
|
9
|
+
// * 存储待取消的请求:key 是请求唯一标识,value 是 AbortController 实例
|
|
10
|
+
const pendingRequests = new Map()
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* * 生成请求唯一标识
|
|
14
|
+
* @param config - Axios 请求配置
|
|
15
|
+
* @returns 唯一标识字符串
|
|
16
|
+
*/
|
|
17
|
+
const generateKey = (config: AxiosRequestConfig) => {
|
|
18
|
+
const {
|
|
19
|
+
method,
|
|
20
|
+
url,
|
|
21
|
+
params,
|
|
22
|
+
data
|
|
23
|
+
} = config
|
|
24
|
+
|
|
25
|
+
// 拼接方法、URL、参数(params 是 URL 参数,data 是请求体参数)
|
|
26
|
+
return [
|
|
27
|
+
method?.toUpperCase(),
|
|
28
|
+
url,
|
|
29
|
+
JSON.stringify(params || {}),
|
|
30
|
+
JSON.stringify(data || {})
|
|
31
|
+
].join('&')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* * 添加请求到待取消列表(若存在重复请求则先取消)
|
|
36
|
+
* @param config - Axios 请求配置
|
|
37
|
+
*/
|
|
38
|
+
const addPendingRequest = (config: AxiosRequestConfig) => {
|
|
39
|
+
const requestKey = generateKey(config)
|
|
40
|
+
|
|
41
|
+
// 若存在重复请求,先取消旧请求
|
|
42
|
+
if (pendingRequests.has(requestKey)) {
|
|
43
|
+
const controller = pendingRequests.get(requestKey)
|
|
44
|
+
controller.abort(`取消重复请求:${requestKey}`)
|
|
45
|
+
pendingRequests.delete(requestKey)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// 创建新的 AbortController 并关联到请求
|
|
49
|
+
const controller = new AbortController()
|
|
50
|
+
// 将 signal 绑定到请求配置
|
|
51
|
+
config.signal = controller.signal
|
|
52
|
+
pendingRequests.set(requestKey, controller)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* * 从待取消列表中移除请求(请求完成/失败/取消时调用)
|
|
57
|
+
* @param config - Axios 请求配置
|
|
58
|
+
*/
|
|
59
|
+
const removePendingRequest = (config: AxiosRequestConfig) => {
|
|
60
|
+
const requestKey = generateKey(config)
|
|
61
|
+
|
|
62
|
+
if (pendingRequests.has(requestKey)) {
|
|
63
|
+
pendingRequests.delete(requestKey)
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// * Token 过期处理函数
|
|
68
|
+
function handleTokenExpired() {
|
|
69
|
+
// 1. 清除本地存储的 Token(避免死循环)
|
|
70
|
+
localCache.removeCache(LOGIN_TOKEN)
|
|
71
|
+
localCache.removeCache(USER_INFO)
|
|
72
|
+
localCache.removeCache(MENUS)
|
|
73
|
+
localCache.removeCache(PERMISSION)
|
|
74
|
+
|
|
75
|
+
// 2. 跳转到登录页,记录当前路径(方便登录后跳转回来)
|
|
76
|
+
const currentPath = router?.currentRoute?.value?.fullPath
|
|
77
|
+
|
|
78
|
+
if (currentPath !== '/login') {
|
|
79
|
+
router.push({
|
|
80
|
+
path: '/login',
|
|
81
|
+
// 携带当前路径作为跳转参数
|
|
82
|
+
query: {
|
|
83
|
+
redirect: currentPath
|
|
84
|
+
}
|
|
85
|
+
})
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// 3. 可选:显示提示信息
|
|
89
|
+
Toast.show({
|
|
90
|
+
icon: 'fail',
|
|
91
|
+
content: '登录已过期,请重新登录',
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
// * 创建 Axios 实例
|
|
97
|
+
const instance = axios.create({
|
|
98
|
+
// 从环境变量取 baseURL
|
|
99
|
+
baseURL: import.meta.env.VITE_APP_BASE_API,
|
|
100
|
+
timeout: 5000
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
// * 请求拦截器:添加取消逻辑
|
|
104
|
+
instance.interceptors.request.use(
|
|
105
|
+
(config: MyInternalAxiosRequestConfig) => {
|
|
106
|
+
// 允许通过配置关闭取消功能(例如某些特殊长轮询请求)
|
|
107
|
+
if (config.cancelable !== false) {
|
|
108
|
+
addPendingRequest(config)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const token = localCache.getCache(LOGIN_TOKEN)
|
|
112
|
+
|
|
113
|
+
if (config.headers && token) {
|
|
114
|
+
config.headers.Authorization = `Bearer ${token}`
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return config
|
|
118
|
+
},
|
|
119
|
+
(error) => Promise.reject(error)
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
// * 响应拦截器:清理已完成的请求
|
|
123
|
+
instance.interceptors.response.use(
|
|
124
|
+
(response: AxiosResponse) => {
|
|
125
|
+
const {
|
|
126
|
+
config,
|
|
127
|
+
data: responseData
|
|
128
|
+
} = response ?? {}
|
|
129
|
+
const {
|
|
130
|
+
code,
|
|
131
|
+
message
|
|
132
|
+
} = responseData ?? {}
|
|
133
|
+
|
|
134
|
+
removePendingRequest(config)
|
|
135
|
+
|
|
136
|
+
// * 统一处理接口成功后,返回的 code !== 0 的错误,这样业务代码就不用写错误的逻辑了
|
|
137
|
+
if (code !== 0) {
|
|
138
|
+
// handleTokenExpired()
|
|
139
|
+
message.error(message)
|
|
140
|
+
return Promise.reject(message)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// 直接返回响应体数据
|
|
144
|
+
return responseData
|
|
145
|
+
},
|
|
146
|
+
(error: AxiosError) => {
|
|
147
|
+
const {
|
|
148
|
+
config,
|
|
149
|
+
response
|
|
150
|
+
} = error ?? {}
|
|
151
|
+
const { status, statusText } = response ?? {}
|
|
152
|
+
|
|
153
|
+
// 移除已失败/取消的请求
|
|
154
|
+
if (error.config) {
|
|
155
|
+
removePendingRequest(error.config)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// 区分取消错误和其他错误
|
|
159
|
+
if (axios.isCancel(error)) {
|
|
160
|
+
// 取消请求不视为错误,返回 null
|
|
161
|
+
return Promise.resolve(null)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// * token 过期, 后端返回 401 状态码
|
|
165
|
+
if (status === 401) {
|
|
166
|
+
handleTokenExpired()
|
|
167
|
+
|
|
168
|
+
return Promise.reject(error)
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// 异常处理程序
|
|
172
|
+
if (response && status) {
|
|
173
|
+
Toast.show({
|
|
174
|
+
icon: 'fail',
|
|
175
|
+
content: statusText,
|
|
176
|
+
})
|
|
177
|
+
}
|
|
178
|
+
else if (!response) {
|
|
179
|
+
Toast.show({
|
|
180
|
+
icon: 'fail',
|
|
181
|
+
content: '您的网络发生异常,无法连接服务器'
|
|
182
|
+
})
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// 其他错误处理(如网络错误、404等)
|
|
186
|
+
return Promise.reject(error)
|
|
187
|
+
}
|
|
188
|
+
)
|
|
189
|
+
|
|
190
|
+
// 扩展实例:添加手动取消方法
|
|
191
|
+
const requestFn = {
|
|
192
|
+
...instance,
|
|
193
|
+
|
|
194
|
+
request<T = any>(config: AxiosRequestConfig) {
|
|
195
|
+
// 返回 promise
|
|
196
|
+
return new Promise<T>((resolve, reject) => {
|
|
197
|
+
instance
|
|
198
|
+
.request<any, T>(config)
|
|
199
|
+
.then((res) => {
|
|
200
|
+
resolve(res)
|
|
201
|
+
})
|
|
202
|
+
.catch((err) => {
|
|
203
|
+
reject(err)
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
get(config: AxiosRequestConfig) {
|
|
209
|
+
return this.request({
|
|
210
|
+
...config,
|
|
211
|
+
method: 'GET'
|
|
212
|
+
})
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
post(config: AxiosRequestConfig) {
|
|
216
|
+
return this.request({
|
|
217
|
+
...config,
|
|
218
|
+
method: 'POST'
|
|
219
|
+
})
|
|
220
|
+
},
|
|
221
|
+
|
|
222
|
+
delete(config: AxiosRequestConfig) {
|
|
223
|
+
return this.request({
|
|
224
|
+
...config,
|
|
225
|
+
method: 'DELETE'
|
|
226
|
+
})
|
|
227
|
+
},
|
|
228
|
+
|
|
229
|
+
patch(config: AxiosRequestConfig) {
|
|
230
|
+
return this.request({
|
|
231
|
+
...config,
|
|
232
|
+
method: 'PATCH'
|
|
233
|
+
})
|
|
234
|
+
},
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* 手动取消单个请求
|
|
238
|
+
* @param config - 请求配置(需包含 method、url,可选 params/data)
|
|
239
|
+
* @param message - 取消原因
|
|
240
|
+
* @returns 是否取消成功
|
|
241
|
+
*/
|
|
242
|
+
cancelOne: (
|
|
243
|
+
config: AxiosRequestConfig,
|
|
244
|
+
message: string = '手动取消请求'
|
|
245
|
+
) => {
|
|
246
|
+
const key = generateKey(config)
|
|
247
|
+
|
|
248
|
+
if (pendingRequests.has(key)) {
|
|
249
|
+
pendingRequests.get(key).abort(message)
|
|
250
|
+
pendingRequests.delete(key)
|
|
251
|
+
|
|
252
|
+
return true
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return false
|
|
256
|
+
},
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* 取消所有待处理的请求
|
|
260
|
+
* @param message - 取消原因
|
|
261
|
+
*/
|
|
262
|
+
cancelAll: (message: string = '取消所有请求') => {
|
|
263
|
+
pendingRequests.forEach((controller, key) => {
|
|
264
|
+
controller.abort(`${message}:${key}`)
|
|
265
|
+
pendingRequests.delete(key)
|
|
266
|
+
})
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export default requestFn
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import requestFn from './request'
|
|
2
|
+
import {apiUrl} from './api'
|
|
3
|
+
|
|
4
|
+
// 具体的接口
|
|
5
|
+
export const getDemo = () => {
|
|
6
|
+
const queryData = {}
|
|
7
|
+
|
|
8
|
+
return requestFn.get({
|
|
9
|
+
url: apiUrl.demoUrl,
|
|
10
|
+
params: {
|
|
11
|
+
name: '111',
|
|
12
|
+
age: 20
|
|
13
|
+
}
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const postDemo = () => {
|
|
18
|
+
const queryData = {}
|
|
19
|
+
|
|
20
|
+
return requestFn.post({
|
|
21
|
+
url: apiUrl.demoUrl1,
|
|
22
|
+
data: {
|
|
23
|
+
name: '111222',
|
|
24
|
+
age: 20
|
|
25
|
+
}
|
|
26
|
+
})
|
|
27
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import {createPinia} from 'pinia'
|
|
2
|
+
import useLoginStore from './login'
|
|
3
|
+
import type { App } from 'vue'
|
|
4
|
+
|
|
5
|
+
const pinia = createPinia()
|
|
6
|
+
|
|
7
|
+
function registerStore(app: App<Element>) {
|
|
8
|
+
app.use(pinia)
|
|
9
|
+
|
|
10
|
+
const loginStore = useLoginStore()
|
|
11
|
+
loginStore.loadLocalCacheAction()
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default registerStore
|