Commit 6544f84bc24b69ff3801cf77cbb4ade69f72d087
1 parent
2aa5e5da
feat(demo): add token expired example
添加token超时例子
Showing
4 changed files
with
42 additions
and
6 deletions
mock/demo/account.ts
1 | 1 | import { MockMethod } from 'vite-plugin-mock'; |
2 | 2 | import { resultSuccess, resultError } from '../_util'; |
3 | +import { ResultEnum } from '../../src/enums/httpEnum'; | |
3 | 4 | |
4 | 5 | const userInfo = { |
5 | 6 | name: 'Vben', |
... | ... | @@ -59,4 +60,12 @@ export default [ |
59 | 60 | return resultError(); |
60 | 61 | }, |
61 | 62 | }, |
63 | + { | |
64 | + url: '/basic-api/user/tokenExpired', | |
65 | + method: 'post', | |
66 | + statusCode: 200, | |
67 | + response: () => { | |
68 | + return resultError('Token Expired!', { code: ResultEnum.TIMEOUT as number }); | |
69 | + }, | |
70 | + }, | |
62 | 71 | ] as MockMethod[]; |
... | ... |
src/api/demo/account.ts
... | ... | @@ -4,6 +4,7 @@ import { GetAccountInfoModel } from './model/accountModel'; |
4 | 4 | enum Api { |
5 | 5 | ACCOUNT_INFO = '/account/getAccountInfo', |
6 | 6 | SESSION_TIMEOUT = '/user/sessionTimeout', |
7 | + TOKEN_EXPIRED = '/user/tokenExpired', | |
7 | 8 | } |
8 | 9 | |
9 | 10 | // Get personal center-basic settings |
... | ... | @@ -11,3 +12,5 @@ enum Api { |
11 | 12 | export const accountInfoApi = () => defHttp.get<GetAccountInfoModel>({ url: Api.ACCOUNT_INFO }); |
12 | 13 | |
13 | 14 | export const sessionTimeoutApi = () => defHttp.post<void>({ url: Api.SESSION_TIMEOUT }); |
15 | + | |
16 | +export const tokenExpiredApi = () => defHttp.post<void>({ url: Api.TOKEN_EXPIRED }); | |
... | ... |
src/utils/http/axios/index.ts
... | ... | @@ -15,6 +15,7 @@ import { setObjToUrlParams, deepMerge } from '/@/utils'; |
15 | 15 | import { useErrorLogStoreWithOut } from '/@/store/modules/errorLog'; |
16 | 16 | import { useI18n } from '/@/hooks/web/useI18n'; |
17 | 17 | import { joinTimestamp, formatRequestDate } from './helper'; |
18 | +import { useUserStoreWithOut } from '/@/store/modules/user'; | |
18 | 19 | |
19 | 20 | const globSetting = useGlobSetting(); |
20 | 21 | const urlPrefix = globSetting.urlPrefix; |
... | ... | @@ -61,6 +62,9 @@ const transform: AxiosTransform = { |
61 | 62 | switch (code) { |
62 | 63 | case ResultEnum.TIMEOUT: |
63 | 64 | timeoutMsg = t('sys.api.timeoutMessage'); |
65 | + const userStore = useUserStoreWithOut(); | |
66 | + userStore.setToken(undefined); | |
67 | + userStore.logout(true); | |
64 | 68 | break; |
65 | 69 | default: |
66 | 70 | if (message) { |
... | ... |
src/views/demo/feat/session-timeout/index.vue
... | ... | @@ -3,7 +3,15 @@ |
3 | 3 | title="登录过期示例" |
4 | 4 | content="用户登录过期示例,不再跳转登录页,直接生成页面覆盖当前页面,方便保持过期前的用户状态!" |
5 | 5 | > |
6 | - <a-button type="primary" @click="test">点击触发用户登录过期</a-button> | |
6 | + <a-card title="请点击下面的按钮访问测试接口" extra="所访问的接口会返回Token过期响应"> | |
7 | + <a-card-grid style="width: 50%; text-align: center"> | |
8 | + <a-button type="primary" @click="test1">HttpStatus == 401</a-button> | |
9 | + </a-card-grid> | |
10 | + <a-card-grid style="width: 50%; text-align: center"> | |
11 | + <span></span> | |
12 | + <a-button class="ml-4" type="primary" @click="test2">Response.code == 401</a-button> | |
13 | + </a-card-grid> | |
14 | + </a-card> | |
7 | 15 | </PageWrapper> |
8 | 16 | </template> |
9 | 17 | <script lang="ts"> |
... | ... | @@ -11,24 +19,36 @@ |
11 | 19 | import { PageWrapper } from '/@/components/Page'; |
12 | 20 | import { useUserStore } from '/@/store/modules/user'; |
13 | 21 | |
14 | - import { sessionTimeoutApi } from '/@/api/demo/account'; | |
22 | + import { sessionTimeoutApi, tokenExpiredApi } from '/@/api/demo/account'; | |
23 | + import { Card } from 'ant-design-vue'; | |
15 | 24 | |
16 | 25 | export default defineComponent({ |
17 | 26 | name: 'TestSessionTimeout', |
18 | - components: { PageWrapper }, | |
27 | + components: { ACardGrid: Card.Grid, ACard: Card, PageWrapper }, | |
19 | 28 | setup() { |
20 | 29 | const userStore = useUserStore(); |
21 | - async function test() { | |
22 | - // 示例网站生产环境用得是mock数据,所以不能返回401, | |
30 | + async function test1() { | |
31 | + // 示例网站生产环境用的是mock数据,不能返回Http状态码, | |
23 | 32 | // 所以在生产环境直接改变状态来达到测试效果 |
24 | 33 | if (import.meta.env.PROD) { |
25 | 34 | userStore.setToken(undefined); |
26 | 35 | userStore.setSessionTimeout(true); |
27 | 36 | } else { |
37 | + // 这个api会返回状态码为401的响应 | |
28 | 38 | await sessionTimeoutApi(); |
29 | 39 | } |
30 | 40 | } |
31 | - return { test }; | |
41 | + | |
42 | + async function test2() { | |
43 | + // 这个api会返回code为401的json数据,Http状态码为200 | |
44 | + try { | |
45 | + await tokenExpiredApi(); | |
46 | + } catch (err) { | |
47 | + console.log('接口访问错误:', (err as Error).message || '错误'); | |
48 | + } | |
49 | + } | |
50 | + | |
51 | + return { test1, test2 }; | |
32 | 52 | }, |
33 | 53 | }); |
34 | 54 | </script> |
... | ... |