diff --git a/src/api/user.ts b/src/api/user.ts index 6639c95..dafc55e 100644 --- a/src/api/user.ts +++ b/src/api/user.ts @@ -53,12 +53,9 @@ import type { ApiResponse, PageResult } from './common/types' */ export interface LoginParams { - activeTab: string - username: string - password: string phone: string - email: string - code: string + password: string + verifyCode?: string } export interface RegisterParams { @@ -104,24 +101,27 @@ export interface UserQueryParams { // 用户API接口 export const userApi = { - //注册 + register(data: RegisterParams) { return request.post('/user/register', data) }, - // 登录 + // 手机号密码登录 login(data: LoginParams) { - return request.post>('/user/login', data) + return request.post>('/auth/login', data) }, + + + // 发送验证码 sendVerifyEmailCode(email: string) { return request.post('/user/getEmailCode', { email }) }, // 发送验证码 sendVerifyPhoneCode(phone: string) { - return request.post('/user/getPhoneCode', { phone }) + return request.post('/user/getphoneCode', { phone }) }, // 获取当前登录用户信息 diff --git a/src/components/Topbar.vue b/src/components/Topbar.vue new file mode 100644 index 0000000..fc72e23 --- /dev/null +++ b/src/components/Topbar.vue @@ -0,0 +1,116 @@ + + + + + \ No newline at end of file diff --git a/src/stores/user.ts b/src/stores/user.ts new file mode 100644 index 0000000..387090a --- /dev/null +++ b/src/stores/user.ts @@ -0,0 +1,42 @@ +import { defineStore } from 'pinia' +import { ref } from 'vue' +import type { UserInfo } from '@/api/user' +import { userApi } from '@/api/user' + +export const useUserStore = defineStore('user', () => { + const userInfo = ref(null) + const token = ref(null) + + // 获取用户信息 + const getUserInfo = async () => { + try { + const data = await userApi.getCurrentUser() + userInfo.value = data + return data + } catch (error) { + console.error('获取用户信息失败:', error) + return null + } + } + + // 设置token + const setToken = (newToken: string) => { + token.value = newToken + localStorage.setItem('token', newToken) + } + + // 清除用户信息 + const clearUserInfo = () => { + userInfo.value = null + token.value = null + localStorage.removeItem('token') + } + + return { + userInfo, + token, + getUserInfo, + setToken, + clearUserInfo + } +}) \ No newline at end of file diff --git a/src/views/LoginView.vue b/src/views/LoginView.vue index a6d9696..d1579d0 100644 --- a/src/views/LoginView.vue +++ b/src/views/LoginView.vue @@ -189,6 +189,7 @@ import { ElMessage } from 'element-plus' import { User, Lock, Phone, Message, Key, Avatar } from '@element-plus/icons-vue' import type { FormInstance } from 'element-plus' import { userApi } from '@/api/user' +import { useUserStore } from '@/stores/user' const router = useRouter() @@ -196,6 +197,7 @@ const route = useRoute() const loading = ref(false) const rememberMe = ref(false) const activeTab = ref('account') +const userStore = useUserStore() // 账号密码登录表单 const accountFormRef = ref() @@ -309,25 +311,13 @@ const handleLogin = async () => { if (valid) { loading.value = true try { - - await userApi.login(formData) - - await new Promise(resolve => setTimeout(resolve, 1000)) - - // 保存token和用户信息 - localStorage.setItem('token', 'dummy-token') - if (rememberMe.value) { - localStorage.setItem('loginType', activeTab.value) - localStorage.setItem('loginData', JSON.stringify(formData)) - } - + const { token } = await userApi.login(formData) + userStore.setToken(token) + await userStore.getUserInfo() ElMessage.success('登录成功') - - // 跳转到之前的页面或首页 - const redirect = route.query.redirect as string - router.push(redirect || '/') + router.push('/') } catch (error) { - //ElMessage.error('登录失败,请检查输入信息') + console.error('登录失败:', error) } finally { loading.value = false }