feat: 添加用户登录功能并优化本地存储处理
- 在axios.js中设置默认的baseURL为本地API地址 - 在auth.js中添加对localStorage操作的异常处理 - 新增users.js文件,定义用户相关API接口 - 在LoginPage.vue中集成登录API并优化登录流程
Showing
4 changed files
with
58 additions
and
9 deletions
src/api/users.js
0 → 100644
| 1 | +/* | ||
| 2 | + * @Date: 2025-03-23 23:45:53 | ||
| 3 | + * @LastEditors: hookehuyr hookehuyr@gmail.com | ||
| 4 | + * @LastEditTime: 2025-03-23 23:45:54 | ||
| 5 | + * @FilePath: /mlaj/src/api/users.js | ||
| 6 | + * @Description: 用户相关接口 | ||
| 7 | + */ | ||
| 8 | +import { fn, fetch } from './fn'; | ||
| 9 | + | ||
| 10 | +const Api = { | ||
| 11 | + USER_LOGIN: '/users/login', | ||
| 12 | + USER_REGISTER: '/users/register', | ||
| 13 | + USER_INFO: '/users/info', | ||
| 14 | +} | ||
| 15 | + | ||
| 16 | +/** | ||
| 17 | + * @description: 用户登录 | ||
| 18 | + * @param: email 用户邮箱 | ||
| 19 | + * @param: password 用户密码 | ||
| 20 | + */ | ||
| 21 | +export const loginAPI = (params) => fn(fetch.post(Api.USER_LOGIN, params)); | ||
| 22 | + | ||
| 23 | +/** | ||
| 24 | + * @description: 用户注册 | ||
| 25 | + * @param: name 用户名称 | ||
| 26 | + * @param: email 用户邮箱 | ||
| 27 | + * @param: password 用户密码 | ||
| 28 | + */ | ||
| 29 | +export const registerAPI = (params) => fn(fetch.post(Api.USER_REGISTER, params)); | ||
| 30 | + | ||
| 31 | +/** | ||
| 32 | + * @description: 获取用户信息 | ||
| 33 | + */ | ||
| 34 | +export const getUserInfoAPI = () => fn(fetch.get(Api.USER_INFO)); |
| ... | @@ -47,8 +47,14 @@ export function provideAuth() { | ... | @@ -47,8 +47,14 @@ export function provideAuth() { |
| 47 | // 登录函数 | 47 | // 登录函数 |
| 48 | const login = (userData) => { | 48 | const login = (userData) => { |
| 49 | currentUser.value = userData | 49 | currentUser.value = userData |
| 50 | - localStorage.setItem('currentUser', JSON.stringify(userData)) | 50 | + if (currentUser.value) { |
| 51 | - localStorage.setItem('loginTimestamp', Date.now().toString()) | 51 | + try { |
| 52 | + localStorage.setItem('currentUser', JSON.stringify(userData)) | ||
| 53 | + localStorage.setItem('loginTimestamp', Date.now().toString()) | ||
| 54 | + } catch (error) { | ||
| 55 | + console.error('Failed to save user data to localStorage:', error) | ||
| 56 | + } | ||
| 57 | + } | ||
| 52 | return true | 58 | return true |
| 53 | } | 59 | } |
| 54 | 60 | ... | ... |
| ... | @@ -12,6 +12,7 @@ import qs from 'Qs' | ... | @@ -12,6 +12,7 @@ import qs from 'Qs' |
| 12 | import { strExist } from '@/utils/tools' | 12 | import { strExist } from '@/utils/tools' |
| 13 | // import { parseQueryString } from '@/utils/tools' | 13 | // import { parseQueryString } from '@/utils/tools' |
| 14 | 14 | ||
| 15 | +axios.defaults.baseURL = 'http://localhost:3000/api'; | ||
| 15 | axios.defaults.params = { | 16 | axios.defaults.params = { |
| 16 | f: 'good', | 17 | f: 'good', |
| 17 | }; | 18 | }; | ... | ... |
| ... | @@ -114,15 +114,16 @@ import { ref } from 'vue' | ... | @@ -114,15 +114,16 @@ import { ref } from 'vue' |
| 114 | import { useRoute, useRouter } from 'vue-router' | 114 | import { useRoute, useRouter } from 'vue-router' |
| 115 | import FrostedGlass from '@/components/ui/FrostedGlass.vue' | 115 | import FrostedGlass from '@/components/ui/FrostedGlass.vue' |
| 116 | import { useAuth } from '@/contexts/auth' | 116 | import { useAuth } from '@/contexts/auth' |
| 117 | -import { useTitle } from '@vueuse/core'; | 117 | +import { useTitle } from '@vueuse/core' |
| 118 | +import { loginAPI } from '@/api/users' | ||
| 118 | const $route = useRoute(); | 119 | const $route = useRoute(); |
| 119 | const $router = useRouter(); | 120 | const $router = useRouter(); |
| 120 | useTitle($route.meta.title); | 121 | useTitle($route.meta.title); |
| 121 | const router = useRouter() | 122 | const router = useRouter() |
| 122 | const { login } = useAuth() | 123 | const { login } = useAuth() |
| 123 | 124 | ||
| 124 | -const email = ref('') | 125 | +const email = ref('user1@example.com') |
| 125 | -const password = ref('') | 126 | +const password = ref('password123') |
| 126 | const error = ref('') | 127 | const error = ref('') |
| 127 | const loading = ref(false) | 128 | const loading = ref(false) |
| 128 | 129 | ||
| ... | @@ -136,13 +137,20 @@ const handleSubmit = async () => { | ... | @@ -136,13 +137,20 @@ const handleSubmit = async () => { |
| 136 | error.value = '' | 137 | error.value = '' |
| 137 | loading.value = true | 138 | loading.value = true |
| 138 | 139 | ||
| 139 | - // 使用auth.js中的login函数 | 140 | + // 调用登录接口 |
| 140 | - const success = login({ | 141 | + const response = await loginAPI({ |
| 141 | email: email.value, | 142 | email: email.value, |
| 142 | - name: '李玉红', | 143 | + password: password.value |
| 143 | - avatar: 'https://cdn.ipadbiz.cn/mlaj/images/user-avatar-1.jpg' | ||
| 144 | }) | 144 | }) |
| 145 | 145 | ||
| 146 | + if (response.code !== 1) { | ||
| 147 | + error.value = response.msg || '登录失败,请检查您的凭据' | ||
| 148 | + return | ||
| 149 | + } | ||
| 150 | + | ||
| 151 | + // 登录成功,更新auth状态 | ||
| 152 | + const success = login(response.data) | ||
| 153 | + | ||
| 146 | if (success) { | 154 | if (success) { |
| 147 | // 如果有重定向参数,登录成功后跳转到对应页面 | 155 | // 如果有重定向参数,登录成功后跳转到对应页面 |
| 148 | const redirect = $route.query.redirect | 156 | const redirect = $route.query.redirect | ... | ... |
-
Please register or login to post a comment