Browse Source

fix: 去除登录相关逻辑

Lee 1 tuần trước cách đây
mục cha
commit
d98f324ef9
5 tập tin đã thay đổi với 6 bổ sung260 xóa
  1. 0 24
      app/constants/index.ts
  2. 0 105
      app/utils/auth.ts
  3. 1 12
      app/utils/fetch/index.ts
  4. 5 109
      app/utils/fetch/request.ts
  5. 0 10
      app/utils/fetch/whiteList.ts

+ 0 - 24
app/constants/index.ts

@@ -19,30 +19,6 @@ const config: Config = {
      */
     title: '绘家',
 
-    /**
-     * @type String
-     * @description accessToken
-     */
-    cookieTokenName: 'wctm__',
-
-    /**
-     * @type String
-     * @description refreshToken
-     */
-    cookieRefreshTokenName: 'wcRhtm__',
-
-    /**
-     * @type String
-     * @description 当前登录企业 ID
-     */
-    enterpriseUserIdName: 'wcEnI__',
-
-    /**
-     * @type String
-     * @description 当前登录uuid
-     */
-    uuidName: 'wcuutm__',
-
     /**
      * @type {boolean} true | false
      * @description Whether need tagsView

+ 0 - 105
app/utils/auth.ts

@@ -1,105 +0,0 @@
-/*
- * @Author: wjc
- * @Date: 2023-05-23 15:14:09
- * @LastEditors: ChenYaJin
- * @LastEditTime: 2023-11-08 17:53:06
- * @Description:
- */
-import defaultSettings from '~/constants'
-
-// accessToken 一天内有效
-const tokenName = defaultSettings.defaultSettings.cookieTokenName
-// refreshToken 一个月内有效
-const refreshTokenName = defaultSettings.defaultSettings.cookieRefreshTokenName
-// 企业ID - key
-const enterpriseUserIdName = defaultSettings.defaultSettings.enterpriseUserIdName
-// UUID - key
-const uuIdName = defaultSettings.defaultSettings.uuidName
-
-/**
- *accessToken
- * token刷新机制:accessToken失效则用refreshToken获取最新的token, refreshToken失效则需重新登陆
- */
-export function getToken() {
-  if (process.browser) {
-    return localStorage.getItem(tokenName)
-  }
-  return ''
-}
-
-export function setToken(token: string) {
-  if (process.browser) {
-    return localStorage.setItem(tokenName, token)
-  }
-  return ''
-}
-
-export function removeToken() {
-  if (process.browser) {
-    return localStorage.removeItem(tokenName)
-  }
-  return ''
-}
-
-/**
- *refreshToken
- */
-export function getRefreshToken() {
-  if (process.browser) {
-    return localStorage.getItem(refreshTokenName)
-  }
-  return ''
-}
-
-export function setRefreshToken(token: string) {
-  if (process.browser) {
-    return localStorage.setItem(refreshTokenName, token)
-  }
-  return ''
-}
-
-export function removeRefreshToken() {
-  if (process.browser) {
-    return localStorage.removeItem(refreshTokenName)
-  }
-  return ''
-}
-
-/**
- * 登录者企业ID
- */
-export function setEnterpriseUserIdSave(id: string) {
-  return localStorage.setItem(enterpriseUserIdName, id)
-}
-
-export function getEnterpriseUserIdSave() {
-  return localStorage.getItem(enterpriseUserIdName)
-}
-
-export function removeEnterpriseUserIdSave() {
-  return localStorage.removeItem(enterpriseUserIdName)
-}
-
-/**
- * 登录者UUID
- */
-export function setUuIdSave(id: string) {
-  return localStorage.setItem(uuIdName, id)
-}
-
-export function getUuIdSave() {
-  return localStorage.getItem(uuIdName)
-}
-
-export function removeUuIdSave() {
-  return localStorage.removeItem(uuIdName)
-}
-
-/**
- * 删除所有本地缓存信息
- */
-export function removeAllLocalStorage() {
-  removeToken()
-  removeUuIdSave()
-  removeEnterpriseUserIdSave()
-}

+ 1 - 12
app/utils/fetch/index.ts

@@ -9,24 +9,13 @@ import type { FetchContext, FetchResponse } from 'ofetch'
 import { apiBase } from '~/constants'
 import Request from './request'
 import { checkStatus } from './checkStatus'
-import whiteList from './whiteList'
 
 const http = new Request({
   baseURL: `${apiBase}`,
   interceptor: {
-    // 请求前钩子函数
-    onRequest({ request, options }: FetchContext) {
-      const isHas = whiteList.find((item) => (request as string).includes(item))
-      if (isHas) {
-        options.headers = {
-          ...options.headers,
-          Authorization: '',
-        } as any
-      }
-    },
     // 响应错误拦截
     onResponseError({ response }: FetchContext & { response: FetchResponse<any> }) {
-      if (response?.status && response?.status !== 401) {
+      if (response?.status) {
         checkStatus(response?.status, response._data?.message || '服务不可用,请稍候再试')
       }
     },

+ 5 - 109
app/utils/fetch/request.ts

@@ -1,19 +1,13 @@
 import type { UseFetchOptions } from '#app'
-import type { BasicResponse } from '~/models/common'
-import type { RequestQueueItem, IInterceptor, IConfig, IOption } from './interface'
-import { useLoginStore } from '~/stores/user/login'
-import { getToken, getRefreshToken } from '../auth'
+import type { IInterceptor, IConfig, IOption } from './interface'
+
 class Request {
   public baseURL: string
   public interceptor: IInterceptor
-  private isRefreshing = false // 是否正在刷新token,开启请求队列
-  private requestQueue: RequestQueueItem[] // 待请求队列
 
   constructor({ baseURL, interceptor }: IConfig) {
     this.baseURL = baseURL
     this.interceptor = interceptor as IInterceptor
-    this.isRefreshing = false
-    this.requestQueue = []
   }
 
   request<T = BasicResponse>({ url, method, params, data, options }: IOption<T>): Promise<T> {
@@ -28,110 +22,12 @@ class Request {
       onResponse: this.interceptor?.onResponse,
       onResponseError: this.interceptor?.onResponseError,
     }
-    return new Promise((resolve, reject) => {
-      this.requestPipeline(url, newOptions, resolve, reject)
-    })
+    return this.requestPipeline(url, newOptions)
   }
 
   // 请求管道处理具体细节
-  requestPipeline<T = BasicResponse>(
-    url: string,
-    options: UseFetchOptions<T>,
-    resolve: (data: T) => void,
-    reject: (data: unknown) => void
-  ): void {
-    const token = getToken()
-    const newOptions = {
-      ...options,
-      headers: {
-        Authorization: token ? `Bearer ${token}` : '',
-        ...options?.headers,
-      },
-    }
-    $fetch<T>(url, newOptions as any)
-      .then((res) => {
-        resolve(res as T)
-      })
-      .catch((error) => {
-        if (error.status === 401) {
-          if (!this.isRefreshing) {
-            this.isRefreshing = true
-            this.refreshToken()
-          }
-          this.addRequestQueueForRefreshToken<T>(url, options, resolve, reject)
-        } else {
-          reject(error)
-        }
-      })
-  }
-
-  // 刷新token
-  refreshToken() {
-    const loginStore = useLoginStore()
-    this.postRefreshTokenFunc()
-      .then((res) => {
-        if (res.data) {
-          const data = res.data
-          loginStore.updateToken(data)
-          this.requestQueueStartAfterRefreshToken()
-        }
-      })
-      .catch(() => {
-        loginStore.logout()
-        navigateTo({ path: '/' })
-
-        // ElMessage.error('登录已失效,需要重新登录')
-        // navigateTo({ path: '/login' })
-      })
-      .finally(() => {
-        this.isRefreshing = false
-      })
-  }
-  postRefreshTokenFunc(): Promise<BasicResponse> {
-    const data = {
-      clientId: getCanvasFingerprint(),
-    }
-    const token = getRefreshToken()
-    return $fetch(this.baseURL + '/auth/refresh', {
-      method: 'post',
-      body: data,
-      headers: {
-        Authorization: `Bearer ${token}`,
-        'Content-Type': 'application/json',
-      },
-    })
-  }
-
-  // 添加请求到等待队列
-  addRequestQueueForRefreshToken<T = BasicResponse>(
-    url: string,
-    options: UseFetchOptions<T>,
-    resolve: (data: T) => void,
-    reject: (data: unknown) => void
-  ): void {
-    this.requestQueue.push({
-      url,
-      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-      // @ts-ignore
-      options,
-      // eslint-disable-next-line @typescript-eslint/ban-ts-comment
-      // @ts-ignore
-      resolve,
-      reject,
-    })
-  }
-
-  // 刷新token成功等待队列开始请求
-  requestQueueStartAfterRefreshToken(): void {
-    let requestQueueItem = this.requestQueue.pop()
-
-    while (requestQueueItem) {
-      const { options, url, resolve, reject } = requestQueueItem
-
-      this.requestPipeline(url, options, resolve, reject)
-
-      requestQueueItem = this.requestQueue.pop()
-    }
+  requestPipeline<T = any>(url: string, options: UseFetchOptions<T>): Promise<T> {
+    return $fetch<T>(url, options as any)
   }
 }
 

+ 0 - 10
app/utils/fetch/whiteList.ts

@@ -1,10 +0,0 @@
-/*
- * @Author: wjc
- * @Date: 2023-12-07 11:46:49
- * @LastEditors: LiZhiWei
- * @LastEditTime: 2026-01-09 15:04:30
- * @Description:
- */
-const whiteList: string[] = []
-
-export default whiteList