From 6652953b1a0a0f91a22121ee140b1d62bd3776de Mon Sep 17 00:00:00 2001 From: betasecond Date: Wed, 18 Jun 2025 16:03:28 +0800 Subject: [PATCH] feat: implement user and admin login flow --- pages/admin.vue | 9 +++++++++ pages/index.vue | 29 +++++++++++++++++++++++++++-- pages/user.vue | 9 +++++++++ server/api/login/admin.post.ts | 21 +++++++++++++++++++++ server/api/login/user.post.ts | 22 ++++++++++++++++++++++ 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 pages/admin.vue create mode 100644 pages/user.vue create mode 100644 server/api/login/admin.post.ts create mode 100644 server/api/login/user.post.ts diff --git a/pages/admin.vue b/pages/admin.vue new file mode 100644 index 0000000..221b74e --- /dev/null +++ b/pages/admin.vue @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/pages/index.vue b/pages/index.vue index f61dc48..62a190b 100644 --- a/pages/index.vue +++ b/pages/index.vue @@ -113,8 +113,33 @@ function showLogin() { } async function handleLogin() { - // TODO: Implement login logic - console.log('Logging in with role:', role.value, 'and credentials:', loginForm); + try { + if (role.value === 'user') { + const response = await $fetch('/api/login/user', { + method: 'POST', + body: { + contact: loginForm.contact, + password: loginForm.password + } + }); + message.value = response.message; + if (response.customerId) { + localStorage.setItem('customerId', response.customerId); + await navigateTo('/user'); + } + } else { // admin + const response = await $fetch('/api/login/admin', { + method: 'POST', + body: { + password: loginForm.password + } + }); + message.value = response.message; + await navigateTo('/admin'); + } + } catch (error: any) { + message.value = error.data?.message || '登录失败'; + } } async function handleRegister() { diff --git a/pages/user.vue b/pages/user.vue new file mode 100644 index 0000000..83ae3f9 --- /dev/null +++ b/pages/user.vue @@ -0,0 +1,9 @@ + + + \ No newline at end of file diff --git a/server/api/login/admin.post.ts b/server/api/login/admin.post.ts new file mode 100644 index 0000000..353ae58 --- /dev/null +++ b/server/api/login/admin.post.ts @@ -0,0 +1,21 @@ +import { defineEventHandler, readBody, setResponseStatus } from 'h3'; + +export default defineEventHandler(async (event) => { + const body = await readBody(event); + const { password } = body; + + if (!password) { + setResponseStatus(event, 400); + return { message: '请填写密码' }; + } + + // TODO: Replace with a more secure admin authentication method + if (password === 'adminpassword') { + return { + message: '管理员登录成功!', + }; + } else { + setResponseStatus(event, 401); + return { message: '密码错误' }; + } +}); \ No newline at end of file diff --git a/server/api/login/user.post.ts b/server/api/login/user.post.ts new file mode 100644 index 0000000..622aa4c --- /dev/null +++ b/server/api/login/user.post.ts @@ -0,0 +1,22 @@ +import { defineEventHandler, readBody, setResponseStatus } from 'h3'; + +export default defineEventHandler(async (event) => { + const body = await readBody(event); + const { contact, password } = body; + + if (!contact || !password) { + setResponseStatus(event, 400); + return { message: '请填写手机号和密码' }; + } + + // TODO: Replace with database user lookup and password verification + if (contact === '1234567890' && password === 'password') { + return { + message: '登录成功!', + customerId: 'dummy-customer-id-123', + }; + } else { + setResponseStatus(event, 401); + return { message: '手机号或密码错误' }; + } +}); \ No newline at end of file