From dbbfdc722704ad8831a04bab1ce6cca90f39635e Mon Sep 17 00:00:00 2001 From: unitwk Date: Sun, 20 Aug 2023 23:38:22 +0800 Subject: [PATCH] Feat: vite dev server --- frontend/src/services/apiService.ts | 7 +++- frontend/src/services/apis.ts | 19 +++++++++- frontend/src/services/user.ts | 12 +++--- frontend/src/tools/commom.ts | 3 ++ frontend/src/views/Login.vue | 57 ++++++++++++++++------------- frontend/vite.config.ts | 32 ++++++++-------- 6 files changed, 80 insertions(+), 50 deletions(-) create mode 100644 frontend/src/tools/commom.ts diff --git a/frontend/src/services/apiService.ts b/frontend/src/services/apiService.ts index 79ae8c63..f97328ce 100644 --- a/frontend/src/services/apiService.ts +++ b/frontend/src/services/apiService.ts @@ -3,6 +3,9 @@ import axios from "axios"; import EventEmitter from "events"; import _ from "lodash"; +// Each request must carry the X-Requested-With: XMLHttpRequest header +axios.defaults.headers.common["X-Requested-With"] = "XMLHttpRequest"; + export interface RequestConfig extends AxiosRequestConfig { forceRequest?: boolean; } @@ -16,8 +19,8 @@ class ApiService { String(config.method), String(config.url), JSON.stringify(config.data ?? {}), - JSON.stringify(config.params ?? {}), - ].join(""), + JSON.stringify(config.params ?? {}) + ].join("") ); return new Promise((resolve, reject) => { diff --git a/frontend/src/services/apis.ts b/frontend/src/services/apis.ts index 0ad853f4..92a5d095 100644 --- a/frontend/src/services/apis.ts +++ b/frontend/src/services/apis.ts @@ -1,5 +1,22 @@ import { useDefineApi } from "@/stores/useApiCache"; +export const loginUser = useDefineApi< + { + // Post + data: { + username: string; + password: string; + }; + }, + // Response + { + id: number; + } +>({ + url: "/api/auth/login", + method: "POST" +}); + export const userInfoApi = useDefineApi< { // Query @@ -16,5 +33,5 @@ export const userInfoApi = useDefineApi< id: number; } >({ - url: "https://jsonplaceholder.typicode.com/todos/1", + url: "https://jsonplaceholder.typicode.com/todos/1" }); diff --git a/frontend/src/services/user.ts b/frontend/src/services/user.ts index 2ab032aa..f30c4e99 100644 --- a/frontend/src/services/user.ts +++ b/frontend/src/services/user.ts @@ -8,8 +8,8 @@ export const userService = createGlobalState(() => { url: "https://jsonplaceholder.typicode.com/todos/1", data: { username, - password, - }, + password + } }); return result.data; }; @@ -18,17 +18,17 @@ export const userService = createGlobalState(() => { const state = userInfoApi(); state.execute({ params: { - uid, + uid }, data: { - newName: "dsad", - }, + newName: "dsad" + } }); return state; }; return { login, - getUserInfo, + getUserInfo }; }); diff --git a/frontend/src/tools/commom.ts b/frontend/src/tools/commom.ts new file mode 100644 index 00000000..532e5c51 --- /dev/null +++ b/frontend/src/tools/commom.ts @@ -0,0 +1,3 @@ +export async function sleep(t: number): Promise { + return new Promise((resolve) => setTimeout(resolve, t)); +} diff --git a/frontend/src/views/Login.vue b/frontend/src/views/Login.vue index 36061f02..9e0c9e34 100644 --- a/frontend/src/views/Login.vue +++ b/frontend/src/views/Login.vue @@ -5,31 +5,47 @@ import { CheckCircleOutlined, LoadingOutlined, LockOutlined, - UserOutlined, + UserOutlined } from "@ant-design/icons-vue"; import { reactive, ref } from "vue"; import { router } from "@/config/router"; +import { loginUser } from "@/services/apis"; +import { sleep } from "@/tools/commom"; const formData = reactive({ username: "", - password: "", + password: "" }); +const { execute: login } = loginUser(); + const loginStep = ref(0); -const handleLogin = () => { +const handleLogin = async () => { loginStep.value++; - setTimeout(() => { - // loginStep.value = 0; - loginStep.value++; - setTimeout(() => loginSuccess(), 1200); - }, 3000); + await sleep(1500); + + try { + const res = await login({ + data: { + username: "1", + password: "2" + } + }); + console.log("resresres:", res); + } catch (error) { + console.log("LOGIN ERROR:", error); + } + + loginStep.value++; + await sleep(1200); + loginSuccess(); }; const loginSuccess = () => { loginStep.value++; router.push({ - path: "/", + path: "/" }); }; @@ -108,7 +124,7 @@ const loginSuccess = () => {
-