diff --git a/ah-jjzhgd-web/src/utils/request.js b/ah-jjzhgd-web/src/utils/request.js index fbd11a6..1b6aed5 100644 --- a/ah-jjzhgd-web/src/utils/request.js +++ b/ah-jjzhgd-web/src/utils/request.js @@ -1,7 +1,9 @@ import axios from 'axios' import { MessageBox, Message } from 'element-ui' import store from '@/store' -import { getToken } from '@/utils/auth' +import {getToken, removeToken} from '@/utils/auth' +import router from '@/router' // @表示src目录 + // create an axios instance const service = axios.create({ @@ -14,12 +16,11 @@ const service = axios.create({ service.interceptors.request.use( config => { // 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() + config.headers['Authorization'] = getToken() } return config }, @@ -43,19 +44,27 @@ service.interceptors.response.use( * You can also judge the status by HTTP Status Code */ response => { - // return response => response - const res = response.data console.log(res) - // if the custom code is not 20000, it is judged as an error. + if (res.code == 401) { + removeToken(); + MessageBox.confirm('登录已过期', '退出登录', { + confirmButtonText: '重新登录', + cancelButtonText: '取消', + type: 'warning' + }).then(() => { + router.push({ path: '/login' }); + }) + return ; + } + if (res.code !== 200) { Message({ - message: res.message || 'Error', + message: res.msg || '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', { diff --git a/ah-jjzhgd-web/src/views/login/index.vue b/ah-jjzhgd-web/src/views/login/index.vue index f4d1572..6aec62f 100644 --- a/ah-jjzhgd-web/src/views/login/index.vue +++ b/ah-jjzhgd-web/src/views/login/index.vue @@ -3,7 +3,7 @@
-

登录

+

智慧工地后台管理系统

@@ -13,7 +13,7 @@ - Login + 登陆 -
-
- Username : admin - Password : any -
-
- Username : editor - Password : any -
+ + + + + + + + + -
+
@@ -83,26 +83,26 @@ export default { data() { const validateUsername = (rule, value, callback) => { if (!validUsername(value)) { - callback(new Error('Please enter the correct user name')) + callback(new Error('请输入正确的用户名')) } else { callback() } } const validatePassword = (rule, value, callback) => { if (value.length < 6) { - callback(new Error('The password can not be less than 6 digits')) + callback(new Error('密码不能小于6位')) } else { callback() } } return { loginForm: { - username: 'admin', - password: '111111' + username: 'guest', + password: 'admin@123' }, loginRules: { - username: [{ required: true, trigger: 'blur', validator: validateUsername }], - password: [{ required: true, trigger: 'blur', validator: validatePassword }] + username: [{ required: true, trigger: 'blur', }], + password: [{ required: true, trigger: 'blur',}] }, passwordType: 'password', capsTooltip: false,