登录注册完成
This commit is contained in:
parent
0fab567259
commit
860d939add
|
@ -3,15 +3,17 @@ import { ref } from 'vue'
|
|||
import type { UserInfo } from '@/api/user'
|
||||
import { userApi } from '@/api/user'
|
||||
|
||||
|
||||
|
||||
export const useUserStore = defineStore('user', () => {
|
||||
const userInfo = ref<UserInfo | null>(null)
|
||||
let userInfo = ref<UserInfo | null>(null)
|
||||
const token = ref<string | null>(null)
|
||||
|
||||
// 获取用户信息
|
||||
const getUserInfo = async () => {
|
||||
try {
|
||||
const data = await userApi.getCurrentUser()
|
||||
userInfo.value = data
|
||||
userInfo = data
|
||||
return data
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error)
|
||||
|
@ -20,9 +22,8 @@ export const useUserStore = defineStore('user', () => {
|
|||
}
|
||||
|
||||
// 设置token
|
||||
const setToken = (newToken: string) => {
|
||||
token.value = newToken
|
||||
localStorage.setItem('token', newToken)
|
||||
const setToken = (token: string) => {
|
||||
localStorage.setItem('token', token)
|
||||
}
|
||||
|
||||
// 清除用户信息
|
||||
|
|
|
@ -312,7 +312,8 @@ const handleLogin = async () => {
|
|||
if (valid) {
|
||||
loading.value = true
|
||||
try {
|
||||
const { token } = await userApi.login(formData)
|
||||
const token = await userApi.login(formData)
|
||||
console.log(token)
|
||||
userStore.setToken(token)
|
||||
await userStore.getUserInfo()
|
||||
ElMessage.success('登录成功')
|
||||
|
|
Loading…
Reference in New Issue