登录功能接入数据库
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
VITE_APP_ENV=development
|
||||
VITE_APP_DEBUG_MODE=true
|
||||
VITE_APP_TITLE=项目管理系统
|
||||
VITE_APP_TITLE=项目管理系统
|
||||
VITE_APP_API_BASE_URL=https://www.yunzer.cn/api
|
||||
|
||||
+12
-19
@@ -10,24 +10,9 @@ const api = axios.create({
|
||||
}
|
||||
})
|
||||
|
||||
// 调试信息
|
||||
console.log('API配置:', {
|
||||
baseURL: ENV_CONFIG.API_BASE_URL,
|
||||
timeout: ENV_CONFIG.REQUEST_TIMEOUT,
|
||||
'环境变量VITE_APP_API_BASE_URL': import.meta.env.VITE_APP_API_BASE_URL
|
||||
})
|
||||
|
||||
// 请求拦截器 - 添加token
|
||||
api.interceptors.request.use(
|
||||
(config) => {
|
||||
// 调试信息
|
||||
console.log('API请求:', {
|
||||
method: config.method,
|
||||
url: config.url,
|
||||
fullURL: (config.baseURL || '') + (config.url || ''),
|
||||
baseURL: config.baseURL
|
||||
})
|
||||
|
||||
const token = localStorage.getItem(ENV_CONFIG.TOKEN_KEY)
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
@@ -42,8 +27,16 @@ api.interceptors.request.use(
|
||||
// 响应拦截器 - 处理错误
|
||||
api.interceptors.response.use(
|
||||
(response) => {
|
||||
// 直接返回响应数据,而不是整个response对象
|
||||
return response.data
|
||||
const data = response.data
|
||||
|
||||
// 检查后端返回的状态码
|
||||
if (data.code === 0) {
|
||||
// 成功,返回data字段的内容
|
||||
return data.data
|
||||
} else {
|
||||
// 失败,抛出错误
|
||||
return Promise.reject(new Error(data.msg || '请求失败'))
|
||||
}
|
||||
},
|
||||
(error) => {
|
||||
if (error.response?.status === 401) {
|
||||
@@ -57,8 +50,8 @@ api.interceptors.response.use(
|
||||
)
|
||||
|
||||
// 用户登录接口
|
||||
export const login = (data: { username: string; password: string }) => {
|
||||
return api.post('/user/login', data)
|
||||
export const login = (data: { account: string; password: string }) => {
|
||||
return api.post('/admin/login', data)
|
||||
}
|
||||
|
||||
// 获取用户信息接口
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
const ENV_CONFIG = {
|
||||
// API配置
|
||||
API_BASE_URL: import.meta.env.VITE_APP_API_BASE_URL || 'https://www.yunzer.cn/api',
|
||||
API_BASE_URL: import.meta.env.VITE_APP_API_BASE_URL,
|
||||
REQUEST_TIMEOUT: 10000,
|
||||
|
||||
// 应用配置
|
||||
|
||||
@@ -3,27 +3,25 @@ import { login as loginApi, getUserInfo as getUserInfoApi, logout as logoutApi }
|
||||
import ENV_CONFIG from '@/config/env'
|
||||
|
||||
interface LoginForm {
|
||||
username: string
|
||||
account: string
|
||||
password: string
|
||||
}
|
||||
|
||||
interface UserInfo {
|
||||
id: number
|
||||
username: string
|
||||
account: string
|
||||
name: string
|
||||
avatar?: string
|
||||
role: string
|
||||
}
|
||||
|
||||
interface ApiResponse<T> {
|
||||
code: number
|
||||
message: string
|
||||
data: T
|
||||
phone?: string
|
||||
sex?: number
|
||||
qq?: string
|
||||
wechat?: string
|
||||
create_time?: number
|
||||
}
|
||||
|
||||
interface LoginResponse {
|
||||
token: string
|
||||
userInfo: UserInfo
|
||||
user_info: UserInfo
|
||||
}
|
||||
|
||||
const useUserStore = defineStore('user', {
|
||||
@@ -48,29 +46,29 @@ const useUserStore = defineStore('user', {
|
||||
// 用户登录
|
||||
async userLogin(loginForm: LoginForm) {
|
||||
try {
|
||||
const response = await loginApi(loginForm) as unknown as ApiResponse<LoginResponse>
|
||||
const response = await loginApi(loginForm) as unknown as LoginResponse
|
||||
|
||||
// 假设API返回格式为 { code: 200, data: { token: string, userInfo: UserInfo } }
|
||||
if (response.code === 200 && response.data) {
|
||||
const { token, userInfo } = response.data
|
||||
// 后端返回格式为 { token: string, user_info: UserInfo }
|
||||
if (response && response.token && response.user_info) {
|
||||
const { token, user_info } = response
|
||||
|
||||
// 保存登录状态
|
||||
this.token = token
|
||||
this.userInfo = userInfo
|
||||
this.userInfo = user_info
|
||||
this.isLogin = true
|
||||
|
||||
// 保存到 localStorage
|
||||
localStorage.setItem(ENV_CONFIG.TOKEN_KEY, token)
|
||||
localStorage.setItem(ENV_CONFIG.USER_INFO_KEY, JSON.stringify(userInfo))
|
||||
localStorage.setItem(ENV_CONFIG.USER_INFO_KEY, JSON.stringify(user_info))
|
||||
|
||||
return userInfo
|
||||
return user_info
|
||||
} else {
|
||||
throw new Error(response.message || '登录失败')
|
||||
throw new Error('登录响应数据格式错误')
|
||||
}
|
||||
} catch (error: any) {
|
||||
// 处理不同类型的错误
|
||||
if (error.response?.data?.message) {
|
||||
throw new Error(error.response.data.message)
|
||||
if (error.response?.data?.msg) {
|
||||
throw new Error(error.response.data.msg)
|
||||
} else if (error.message) {
|
||||
throw new Error(error.message)
|
||||
} else {
|
||||
@@ -104,10 +102,10 @@ const useUserStore = defineStore('user', {
|
||||
if (token && userInfoStr) {
|
||||
try {
|
||||
// 验证token是否有效
|
||||
const response = await getUserInfoApi() as unknown as ApiResponse<UserInfo>
|
||||
if (response.code === 200 && response.data) {
|
||||
const response = await getUserInfoApi() as unknown as UserInfo
|
||||
if (response && response.id) {
|
||||
this.token = token
|
||||
this.userInfo = response.data
|
||||
this.userInfo = response
|
||||
this.isLogin = true
|
||||
} else {
|
||||
// token无效,清除本地存储但不调用logout API
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
<el-form-item>
|
||||
<el-input
|
||||
:prefix-icon="User"
|
||||
v-model="loginForm.username"
|
||||
v-model="loginForm.account"
|
||||
></el-input>
|
||||
</el-form-item>
|
||||
<el-form-item>
|
||||
@@ -45,7 +45,7 @@ import { ElNotification } from 'element-plus'
|
||||
import useUserStore from '@/store/modules/user'
|
||||
|
||||
// 收集账号与密码数据
|
||||
const loginForm = reactive({ username: '', password: '' })
|
||||
const loginForm = reactive({ account: '', password: '' })
|
||||
// 按钮加载状态
|
||||
const loading = ref(false)
|
||||
// 获取路由实例
|
||||
|
||||
Reference in New Issue
Block a user