Przeglądaj źródła

✨ refactor(user): 简化登录逻辑并移除请求工具函数

陈雪 1 dzień temu
rodzic
commit
4e518dacf4
2 zmienionych plików z 2 dodań i 75 usunięć
  1. 2 9
      src/store/user.ts
  2. 0 66
      src/utils/request.ts

+ 2 - 9
src/store/user.ts

@@ -1,6 +1,4 @@
 import { LoginForm } from '@/interface/user'
-import router from '@/router'
-import { request } from '@/utils/request'
 import { defineStore } from 'pinia'
 
 export const useUserStore = defineStore('user', {
@@ -9,13 +7,8 @@ export const useUserStore = defineStore('user', {
   }),
   actions: {
     async login(data: LoginForm) {
-      const { token } = await request<{ token: string }>({
-        url: '/backendApi/login/cashRegisterDoLogin',
-        method: 'POST',
-        data,
-        noAuth: true,
-      })
-      this.token = token
+      console.log('data: ', data)
+      this.token = 'token'
     },
   },
   getters: {

+ 0 - 66
src/utils/request.ts

@@ -1,66 +0,0 @@
-import { useUserStore } from '@/store/user'
-import { fetch } from '@tauri-apps/plugin-http'
-
-type CommonOption = {
-  url: string
-  query?: Record<string, string>
-  noAuth?: boolean
-  headers?: Record<string, string>
-}
-
-type GetOption = {
-  method: 'GET' | 'DELETE'
-}
-
-type PostOption = {
-  method: 'POST' | 'PUT'
-  data?: unknown
-}
-
-type RequestOptions = CommonOption & (GetOption | PostOption)
-
-type Response<T> = {
-  code: number
-  data: T
-  msg: string
-}
-
-export const request = async <T = void>(config: RequestOptions) => {
-  const { noAuth, url, headers, method, query } = config
-  if (!noAuth) {
-    const { getToken } = useUserStore()
-    headers['Access-Token'] = getToken
-  }
-  const reqPath = new URL(url, import.meta.env.VITE_BASE_URL || '')
-  if (query) Object.keys(query).forEach((key) => reqPath.searchParams.append(key, query[key]))
-
-  try {
-    console.log('reqPath.href: ', reqPath.href)
-    let body = null
-    if (config.method === 'POST' || (config.method === 'PUT' && config.data)) {
-      body = JSON.stringify(config.data)
-    }
-
-    const response = await fetch(reqPath.href, {
-      method,
-      headers: {
-        'Content-Type': 'application/json',
-        ...headers,
-      },
-      body,
-    })
-
-    if (!response.ok) {
-      throw new Error(`HTTP error! status: ${response.status}`)
-    }
-
-    const { code, data, msg } = (await response.json()) as Response<T>
-    if (code !== 200) {
-      throw new Error(msg)
-    }
-    return data
-  } catch (error) {
-    console.error('Request failed', error)
-    throw error
-  }
-}