登录注册完成
This commit is contained in:
parent
860d939add
commit
6673f6eeea
|
@ -131,7 +131,7 @@ export const userApi = {
|
||||||
|
|
||||||
// 更新用户信息
|
// 更新用户信息
|
||||||
updateUserInfo(data: Partial<UserInfo>) {
|
updateUserInfo(data: Partial<UserInfo>) {
|
||||||
return request.put<ApiResponse<UserInfo>>('/user/info', data)
|
return request.put<ApiResponse>('/user/info', data)
|
||||||
},
|
},
|
||||||
|
|
||||||
// 更新用户头像
|
// 更新用户头像
|
||||||
|
|
|
@ -3,18 +3,15 @@ import { ref } from 'vue'
|
||||||
import type { UserInfo } from '@/api/user'
|
import type { UserInfo } from '@/api/user'
|
||||||
import { userApi } from '@/api/user'
|
import { userApi } from '@/api/user'
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
export const useUserStore = defineStore('user', () => {
|
export const useUserStore = defineStore('user', () => {
|
||||||
let userInfo = ref<UserInfo | null>(null)
|
const userInfo = ref<UserInfo | null>(null)
|
||||||
const token = ref<string | null>(null)
|
const token = ref<string | null>(null)
|
||||||
|
|
||||||
// 获取用户信息
|
// 获取用户信息
|
||||||
const getUserInfo = async () => {
|
const getUserInfo = async () => {
|
||||||
try {
|
try {
|
||||||
const data = await userApi.getCurrentUser()
|
console.log(1111)
|
||||||
userInfo = data
|
await userApi.getCurrentUser()
|
||||||
return data
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('获取用户信息失败:', error)
|
console.error('获取用户信息失败:', error)
|
||||||
return null
|
return null
|
||||||
|
@ -38,6 +35,6 @@ export const useUserStore = defineStore('user', () => {
|
||||||
token,
|
token,
|
||||||
getUserInfo,
|
getUserInfo,
|
||||||
setToken,
|
setToken,
|
||||||
clearUserInfo
|
clearUserInfo,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -6,7 +6,7 @@ import router from '@/router'
|
||||||
// 创建 axios 实例
|
// 创建 axios 实例
|
||||||
const service: AxiosInstance = axios.create({
|
const service: AxiosInstance = axios.create({
|
||||||
baseURL: import.meta.env.VITE_API_BASE_URL || '/api', // 从环境变量获取API基础路径
|
baseURL: import.meta.env.VITE_API_BASE_URL || '/api', // 从环境变量获取API基础路径
|
||||||
timeout: 15000, // 请求超时时间
|
timeout: 30000, // 请求超时时间
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,9 @@ service.interceptors.request.use(
|
||||||
if (token) {
|
if (token) {
|
||||||
config.headers = {
|
config.headers = {
|
||||||
...config.headers,
|
...config.headers,
|
||||||
Authorization: `Bearer ${token}`
|
// Authorization: `Bearer ${token}`
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
},
|
},
|
||||||
|
@ -45,8 +46,9 @@ service.interceptors.response.use(
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(error) => {
|
(error) => {
|
||||||
|
console.log(error.response) //undefined
|
||||||
const { status } = error.response || {}
|
const { status } = error.response || {}
|
||||||
|
|
||||||
// 处理常见错误
|
// 处理常见错误
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case 401:
|
case 401:
|
||||||
|
@ -66,9 +68,9 @@ service.interceptors.response.use(
|
||||||
default:
|
default:
|
||||||
ElMessage.error('网络错误')
|
ElMessage.error('网络错误')
|
||||||
}
|
}
|
||||||
|
|
||||||
return Promise.reject(error)
|
return Promise.reject(error)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
export default service
|
export default service
|
||||||
|
|
|
@ -315,9 +315,10 @@ const handleLogin = async () => {
|
||||||
const token = await userApi.login(formData)
|
const token = await userApi.login(formData)
|
||||||
console.log(token)
|
console.log(token)
|
||||||
userStore.setToken(token)
|
userStore.setToken(token)
|
||||||
await userStore.getUserInfo()
|
const res = await userApi.getCurrentUser()
|
||||||
|
//console.log(res)
|
||||||
ElMessage.success('登录成功')
|
ElMessage.success('登录成功')
|
||||||
router.push('/')
|
//router.push('/')
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('登录失败:', error)
|
console.error('登录失败:', error)
|
||||||
} finally {
|
} finally {
|
||||||
|
|
Loading…
Reference in New Issue