修改登录逻辑,配合前后端联调

This commit is contained in:
spllzh
2025-08-11 19:33:08 +08:00
parent e764dfd2fe
commit 3b3a7d0f94
4 changed files with 68 additions and 41 deletions

View File

@@ -1,16 +1,22 @@
import request from '@/utils/request'
import { AUTH_API } from './config'
export function login(data) {
// 转换参数名称以匹配后端接口要求
const loginData = {
userName: data.username, // 后端需要 userName 而不是 username
password: data.password
}
return request({
url: '/vue-admin-template/user/login',
url: '/sys/auth/login',
method: 'post',
data
data: loginData
})
}
export function getInfo(token) {
return request({
url: '/vue-admin-template/user/info',
url: '/sys/auth/getUserInfoByToken',
method: 'get',
params: { token }
})
@@ -18,7 +24,7 @@ export function getInfo(token) {
export function logout() {
return request({
url: '/vue-admin-template/user/logout',
url: AUTH_API.LOGOUT,
method: 'post'
})
}

View File

@@ -5,7 +5,7 @@ import { getToken } from '@/utils/auth'
// create an axios instance
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
baseURL: '/api', // 统一使用 /api 前缀
// withCredentials: true, // send cookies when cross-domain requests
timeout: 5000 // request timeout
})
@@ -16,10 +16,8 @@ service.interceptors.request.use(
// do something before request is sent
if (store.getters.token) {
// let each request carry token
// ['X-Token'] is a custom headers key
// please modify it according to the actual situation
config.headers['X-Token'] = getToken()
// 使用Bearer token格式
config.headers['Authorization'] = `Bearer ${getToken()}`
}
return config
},
@@ -32,43 +30,58 @@ service.interceptors.request.use(
// response interceptor
service.interceptors.response.use(
/**
* If you want to get http information such as headers or status
* Please return response => response
*/
/**
* Determine the request status by custom code
* Here is just an example
* You can also judge the status by HTTP Status Code
*/
response => {
const res = response.data
console.log('response 0811 ', res)
// if the custom code is not 20000, it is judged as an error.
if (res.code !== 20000) {
// 适配后端响应格式
// 后端成功响应: { data: {...}, success: true, message: "..." }
// 前端期望格式: { code: 20000, data: {...}, message: "..." }
if (res.success === true) {
// 后端成功响应,转换为前端期望格式
return {
code: 20000,
data: res.data,
message: res.message || 'Success'
}
} else if (res.success === false) {
// 后端失败响应
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
// to re-login
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
confirmButtonText: 'Re-Login',
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
location.reload()
})
})
}
return Promise.reject(new Error(res.message || 'Error'))
} else {
} else if (res.code === 20000) {
// 已经是前端期望格式
return res
} else {
// 其他情况,按原逻辑处理
if (res.code !== 20000) {
Message({
message: res.message || 'Error',
type: 'error',
duration: 5 * 1000
})
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
// to re-login
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
confirmButtonText: 'Re-Login',
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
store.dispatch('user/resetToken').then(() => {
location.reload()
})
})
}
return Promise.reject(new Error(res.message || 'Error'))
} else {
return res
}
}
},
error => {

View File

@@ -43,8 +43,6 @@
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
</el-form>
</div>
</template>
@@ -72,7 +70,7 @@ export default {
return {
loginForm: {
username: 'admin',
password: '111111'
password: '123456'
},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],

View File

@@ -6,7 +6,7 @@ function resolve(dir) {
return path.join(__dirname, dir)
}
const name = defaultSettings.title || 'vue Admin Template' // page title
const name = defaultSettings.title || 'AI平台' // page title
// If your port is set to 80,
// use administrator privileges to execute the command line.
@@ -36,7 +36,17 @@ module.exports = {
warnings: false,
errors: true
},
before: require('./mock/mock-server.js')
// before: require('./mock/mock-server.js'), // 注释掉 mock 服务器
proxy: {
'/api': {
target: 'http://localhost:9060', // 从环境变量获取后端服务器地址
changeOrigin: true,
pathRewrite: {
'^/api': '/api' // 保持 /api 前缀
},
logLevel: 'debug' // 开发环境下显示代理日志
}
}
},
configureWebpack: {
// provide the app's title in webpack's name field, so that