@liujitcn/kratos-uni-app-core 0.0.64 → 0.0.65

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/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "type": "git",
8
8
  "url": "git@github.com:liujitcn/kratos-admin.git"
9
9
  },
10
- "version": "0.0.64",
10
+ "version": "0.0.65",
11
11
  "type": "module",
12
12
  "files": [
13
13
  "dist",
@@ -3,6 +3,7 @@ import { computed, onMounted, ref, watch } from 'vue'
3
3
  import { useUserStore } from '../stores'
4
4
  import { navigateToLogin } from '../utils/navigation'
5
5
  import { getLanguageOptions, useI18n, type SupportedLocale } from '../locales'
6
+ import { isAuthExpiredError } from '../utils/http'
6
7
  import { defMfaService } from '../api/base/v1/mfa'
7
8
  import MfaManageDialog from './MfaManageDialog.vue'
8
9
 
@@ -15,14 +16,6 @@ defineSlots<{
15
16
 
16
17
  const userStore = useUserStore()
17
18
  const { locale, setLocale, t } = useI18n()
18
- /** 登录态失效时请求层已弹窗引导重新登录,页面侧不再叠加失败提示。 */
19
- const isAuthExpiredError = (error: unknown) => {
20
- if (error && typeof error === 'object') {
21
- const statusCode = (error as { statusCode?: number }).statusCode
22
- if (statusCode === 401 || statusCode === 403) return true
23
- }
24
- return error instanceof Error && /auth (required|expired)/.test(error.message)
25
- }
26
19
 
27
20
  const logoutLoading = ref(false)
28
21
  const mfaDialogVisible = ref(false)
package/src/utils/http.ts CHANGED
@@ -74,6 +74,23 @@ type HttpRequestOptions = UniApp.RequestOptions & {
74
74
  authMode?: AuthMode
75
75
  }
76
76
 
77
+ /** 认证缺失或失效错误:抛出前请求层已弹窗引导重新登录,页面侧无需重复提示。 */
78
+ export class AuthPromptError extends Error {
79
+ constructor() {
80
+ super(t('core.auth.session_expired'))
81
+ this.name = 'AuthPromptError'
82
+ }
83
+ }
84
+
85
+ /** 判断错误是否为登录态失效(请求层已弹窗提示,或响应为 401/403)。 */
86
+ export function isAuthExpiredError(error: unknown): boolean {
87
+ if (error && typeof error === 'object') {
88
+ const statusCode = (error as { statusCode?: number }).statusCode
89
+ if (statusCode === 401 || statusCode === 403) return true
90
+ }
91
+ return error instanceof AuthPromptError
92
+ }
93
+
77
94
  // 添加拦截器
78
95
  const httpInterceptor = {
79
96
  // 拦截前触发
@@ -296,7 +313,7 @@ async function getAccessTokenByMode(authMode: AuthMode) {
296
313
  if (!getToken()) {
297
314
  if (authMode === 'required') {
298
315
  await promptRelogin()
299
- throw new Error('auth required')
316
+ throw new AuthPromptError()
300
317
  }
301
318
  return ''
302
319
  }
@@ -315,7 +332,7 @@ async function getAccessTokenByMode(authMode: AuthMode) {
315
332
 
316
333
  if (authMode === 'required') {
317
334
  await promptRelogin()
318
- throw new Error('auth expired')
335
+ throw new AuthPromptError()
319
336
  }
320
337
 
321
338
  silentClearAuthData()