增加登出逻辑

This commit is contained in:
liuxiangfei 2023-08-29 14:04:12 +08:00
parent 178ee07438
commit 36844a9229
6 changed files with 16 additions and 117 deletions

View File

@ -1,21 +0,0 @@
import request from './request'
import Qs from "qs";
import constant from './constant'
export function userLogin(param) {
console.log(constant.userUrl+'/auth/login')
return request({
url: constant.userUrl+'/auth/login',
method: 'post',
data: Qs.stringify(param)
})
}
export function userGetByUsername() {
return request({
url: constant.userUrl+'/user/getByUsername',
method: 'get',
})
}

View File

@ -1,8 +0,0 @@
export default {
ProjectName: "meter-server", //项目名称
userUrl: "/gateway/common-user/",
hesUrl: "/gateway/meter-server/",
fdfsUrl: "/gateway/common-fdfs/",
PageSize: 15,//默认每页数量
fileAccessPrex:"http://fcat.liuxiangfei.fun:6008/fdfs/"
};

View File

@ -1,79 +0,0 @@
import axios from 'axios'
import {Message} from 'element-ui'
import {getToken} from '@/utils/auth'
/*import {getCompanyId, getUsername} from "../utils/auth";*/
// create an axios instance
const service = axios.create({
timeout: 0 // request timeout
})
// 请求前拦截
service.interceptors.request.use(config => {
if (getToken()) {
config.headers['Authorization'] = getToken() // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改
}
/* if(getToken()){
config.headers['username']=getUsername() //先测试使用
config.headers['companyId']=getCompanyId() //先测试使用
}*/
return config
}, error => {
console.log(error) // for debug
Promise.reject(error)
})
let toLoginTimeOut = undefined;
// 去请求后拦截
service.interceptors.response.use(
response => {
const res = response.data
// console.log("service.interceptors.response.use");
//超时等问题
if (res.code === "-401" ||
res.code === 401 ||
res.code === "100" ||
res.code === -401 ||
res.code === -402 ||
res.code === -403
) {
if (!toLoginTimeOut && window.location.pathname !== "/login") {//跳转到登录页面时不需要提示信息
// element.Message({
// showClose: true,
// message: res.msg,
// type: 'error'
// });
toLoginTimeOut = window.setTimeout(() => {
window.location.href = "/login";
}, 3 * 1000);
}
return res.msg
}
//文件下载直接返回
if(response.config.responseType==='blob'){
console.log('直接返回了')
return response
}
//后台进行了 code封装
if(res.code==0){
return response.data.data
}else{
Message({
message: res.msg,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(res.msg)
}
},
error => {
console.log('err' + error) // for debug
Message({
message: error.message,
type: 'error',
duration: 5 * 1000
})
return Promise.reject(error)
})
export default service

View File

@ -35,8 +35,13 @@ const actions = {
return new Promise(resolve => {
ApiService.post("/gateway/common-user/auth/login", Qs.stringify(credentials))
.then(({ data }) => {
context.commit(SET_AUTH, data);
resolve(data);
if(data.code==0){
let user = {token:data.data,username:credentials.username};
context.commit(SET_AUTH, user);
resolve(data);
}else{
alert(data.msg);
}
})
.catch(({ response }) => {
context.commit(SET_ERROR, response.data.errors);
@ -44,6 +49,11 @@ const actions = {
});
},
[LOGOUT](context) {
ApiService.post("/gateway/common-user/auth/logout")
.then(() => {
})
.catch(() => {
});
context.commit(PURGE_AUTH);
},
[REGISTER](context, credentials) {

View File

@ -441,22 +441,19 @@ export default {
let _param = {
mac: "11",
mac: "fcat-vue",
username: this.form.username,
password: this.form.password
}
this.$store
.dispatch(LOGIN, _param)
// go to which page after successfully login
.then((e) => {
this.$store.dispatch(LOGIN, _param).then((e) => {
if(e.code==0){
this.$router.push({ name: "dashboard" })
}else{
alert(e.msg);
}
})
.catch(() => {
.catch((e) => {
alert(e);
});
submitButton.classList.remove(

BIN
settings.zip Normal file

Binary file not shown.