修改登录逻辑,配合前后端联调
This commit is contained in:
@@ -1,16 +1,22 @@
|
|||||||
import request from '@/utils/request'
|
import request from '@/utils/request'
|
||||||
|
import { AUTH_API } from './config'
|
||||||
|
|
||||||
export function login(data) {
|
export function login(data) {
|
||||||
|
// 转换参数名称以匹配后端接口要求
|
||||||
|
const loginData = {
|
||||||
|
userName: data.username, // 后端需要 userName 而不是 username
|
||||||
|
password: data.password
|
||||||
|
}
|
||||||
return request({
|
return request({
|
||||||
url: '/vue-admin-template/user/login',
|
url: '/sys/auth/login',
|
||||||
method: 'post',
|
method: 'post',
|
||||||
data
|
data: loginData
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getInfo(token) {
|
export function getInfo(token) {
|
||||||
return request({
|
return request({
|
||||||
url: '/vue-admin-template/user/info',
|
url: '/sys/auth/getUserInfoByToken',
|
||||||
method: 'get',
|
method: 'get',
|
||||||
params: { token }
|
params: { token }
|
||||||
})
|
})
|
||||||
@@ -18,7 +24,7 @@ export function getInfo(token) {
|
|||||||
|
|
||||||
export function logout() {
|
export function logout() {
|
||||||
return request({
|
return request({
|
||||||
url: '/vue-admin-template/user/logout',
|
url: AUTH_API.LOGOUT,
|
||||||
method: 'post'
|
method: 'post'
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { getToken } from '@/utils/auth'
|
|||||||
|
|
||||||
// create an axios instance
|
// create an axios instance
|
||||||
const service = axios.create({
|
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
|
// withCredentials: true, // send cookies when cross-domain requests
|
||||||
timeout: 5000 // request timeout
|
timeout: 5000 // request timeout
|
||||||
})
|
})
|
||||||
@@ -16,10 +16,8 @@ service.interceptors.request.use(
|
|||||||
// do something before request is sent
|
// do something before request is sent
|
||||||
|
|
||||||
if (store.getters.token) {
|
if (store.getters.token) {
|
||||||
// let each request carry token
|
// 使用Bearer token格式
|
||||||
// ['X-Token'] is a custom headers key
|
config.headers['Authorization'] = `Bearer ${getToken()}`
|
||||||
// please modify it according to the actual situation
|
|
||||||
config.headers['X-Token'] = getToken()
|
|
||||||
}
|
}
|
||||||
return config
|
return config
|
||||||
},
|
},
|
||||||
@@ -32,43 +30,58 @@ service.interceptors.request.use(
|
|||||||
|
|
||||||
// response interceptor
|
// response interceptor
|
||||||
service.interceptors.response.use(
|
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 => {
|
response => {
|
||||||
const res = response.data
|
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({
|
||||||
message: res.message || 'Error',
|
message: res.message || 'Error',
|
||||||
type: 'error',
|
type: 'error',
|
||||||
duration: 5 * 1000
|
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'))
|
return Promise.reject(new Error(res.message || 'Error'))
|
||||||
} else {
|
} else if (res.code === 20000) {
|
||||||
|
// 已经是前端期望格式
|
||||||
return res
|
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 => {
|
error => {
|
||||||
|
|||||||
@@ -43,8 +43,6 @@
|
|||||||
|
|
||||||
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
|
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登录</el-button>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</el-form>
|
</el-form>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
@@ -72,7 +70,7 @@ export default {
|
|||||||
return {
|
return {
|
||||||
loginForm: {
|
loginForm: {
|
||||||
username: 'admin',
|
username: 'admin',
|
||||||
password: '111111'
|
password: '123456'
|
||||||
},
|
},
|
||||||
loginRules: {
|
loginRules: {
|
||||||
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
|
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ function resolve(dir) {
|
|||||||
return path.join(__dirname, 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,
|
// If your port is set to 80,
|
||||||
// use administrator privileges to execute the command line.
|
// use administrator privileges to execute the command line.
|
||||||
@@ -36,7 +36,17 @@ module.exports = {
|
|||||||
warnings: false,
|
warnings: false,
|
||||||
errors: true
|
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: {
|
configureWebpack: {
|
||||||
// provide the app's title in webpack's name field, so that
|
// provide the app's title in webpack's name field, so that
|
||||||
|
|||||||
Reference in New Issue
Block a user