修改登录失败不提示

This commit is contained in:
不做码农 2022-04-18 09:59:57 +08:00
parent bc633bf62d
commit 4f25ffb185
3 changed files with 94 additions and 90 deletions

View File

@ -1,6 +1,6 @@
import { login, logout, getInfo } from '@/api/system/login' import { login, logout, getInfo } from '@/api/system/login'
import { getToken, setToken, removeToken } from '@/utils/auth' import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg' // import defAva from '@/assets/images/profile.jpg'
const user = { const user = {
state: { state: {

View File

@ -1,18 +1,13 @@
import axios from 'axios' import axios from 'axios'
import { ElNotification, ElMessageBox, ElMessage, ElLoading } from 'element-plus' import { ElNotification, ElMessageBox, ElMessage } from 'element-plus'
import store from '@/store' import store from '@/store'
import { getToken } from '@/utils/auth' import { getToken } from '@/utils/auth'
// import { blobValidate } from "@/utils/ruoyi";
// import errorCode from '@/utils/errorCode' // import errorCode from '@/utils/errorCode'
// import {
// tansParams,
// blobValidate
// } from '@/utils/ruoyi'
import cache from '@/plugins/cache'
// import { saveAs } from 'file-saver' // import { saveAs } from 'file-saver'
let downloadLoadingInstance; // 解决后端跨域获取不到cookie问题
// 是否显示重新登录 // axios.defaults.withCredentials = true
let isReloginShow;
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8' axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例 // 创建axios实例
const service = axios.create({ const service = axios.create({
@ -32,37 +27,7 @@ service.interceptors.request.use(config => {
} else { } else {
// console.log(config) // console.log(config)
} }
// get请求映射params参数 return config;
// if (config.method === 'get' && config.params) {
// let url = config.url + '?' + tansParams(config.params);
// url = url.slice(0, -1);
// config.params = {};
// config.url = url;
// }
// if (!isRepeatSubmit && (config.method === 'post' || config.method === 'put')) {
// const requestObj = {
// url: config.url,
// data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
// time: new Date().getTime()
// }
// const sessionObj = cache.session.getJSON('sessionObj')
// if (sessionObj === undefined || sessionObj === null || sessionObj === '') {
// cache.session.setJSON('sessionObj', requestObj)
// } else {
// const s_url = sessionObj.url; // 请求地址
// const s_data = sessionObj.data; // 请求数据
// const s_time = sessionObj.time; // 请求时间
// const interval = 1000; // 间隔时间(ms),小于此时间视为重复提交
// if (s_data === requestObj.data && requestObj.time - s_time < interval && s_url === requestObj.url) {
// const message = '数据正在处理,请勿重复提交';
// console.warn(`[${s_url}]: ` + message)
// return Promise.reject(new Error(message))
// } else {
// cache.session.setJSON('sessionObj', requestObj)
// }
// }
// }
return config
}, error => { }, error => {
console.log(error) console.log(error)
Promise.reject(error) Promise.reject(error)
@ -76,28 +41,21 @@ service.interceptors.response.use(res => {
} }
// 未设置状态码则默认成功状态 // 未设置状态码则默认成功状态
const { code, msg } = res.data; const { code, msg } = res.data;
// 获取错误信息
// 二进制数据则直接返回 // 二进制数据则直接返回
if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') { if (res.request.responseType === 'blob' || res.request.responseType === 'arraybuffer') {
return res.data return res.data
} }
if (code === 401) { if (code == 401) {
ElMessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', { ElMessageBox.confirm('登录状态已过期,重新登录', '系统提示', {
confirmButtonText: '重新登录', confirmButtonText: '重新登录',
cancelButtonText: '取消', cancelButtonText: '取消',
type: 'warning' type: 'warning'
}).then(() => { }).then(() => {
isReloginShow = false;
store.dispatch('LogOut').then(() => { store.dispatch('LogOut').then(() => {
// 如果是登录页面不需要重新加载 location.href = process.env.VUE_APP_ROUTER_PREFIX + 'index';
if (window.location.hash.indexOf("#/login") != 0) {
location.href = '/index';
}
}) })
}).catch(() => { })
isReloginShow = false;
});
return Promise.reject('无效的会话,或者会话已过期,请重新登录。') return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
} else if (code == 0 || code == 1 || code == 110 || code == 101 || code == 103 || code == 403 || code == 500 || code == 429) { } else if (code == 0 || code == 1 || code == 110 || code == 101 || code == 103 || code == 403 || code == 500 || code == 429) {
ElMessage({ ElMessage({
@ -112,13 +70,13 @@ service.interceptors.response.use(res => {
}, },
error => { error => {
console.log('err' + error) console.log('err' + error)
let { let { message } = error;
message
} = error;
if (message == "Network Error") { if (message == "Network Error") {
message = "后端接口连接异常"; message = "后端接口连接异常";
} else if (message.includes("timeout")) { } else if (message.includes("timeout")) {
message = "系统接口请求超时"; message = "系统接口请求超时";
} else if (message.includes("Request failed with status code 429")) {
message = "请求过于频繁,请稍后再试";
} else if (message.includes("Request failed with status code")) { } else if (message.includes("Request failed with status code")) {
message = "系统接口" + message.substr(message.length - 3) + "异常"; message = "系统接口" + message.substr(message.length - 3) + "异常";
} }
@ -131,37 +89,81 @@ service.interceptors.response.use(res => {
} }
) )
// 通用下载方法 /**
export function download(url, params, filename) { * get方法对应get请求
downloadLoadingInstance = ElLoading.service({ * @param {String} url [请求的url地址]
text: "正在下载数据,请稍候", * @param {Object} params [请求时携带的参数]
background: "rgba(0, 0, 0, 0.7)", */
export function get(url, params) {
return new Promise((resolve, reject) => {
axios
.get(url, {
params: params
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err)
}) })
return service.post(url, params, {
transformRequest: [(params) => {
return tansParams(params)
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
responseType: 'blob'
}).then(async (data) => {
const isLogin = await blobValidate(data);
if (isLogin) {
const blob = new Blob([data])
saveAs(blob, filename)
} else {
const resText = await data.text();
const rspObj = JSON.parse(resText);
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default']
ElMessage.error(errMsg);
}
downloadLoadingInstance.close();
}).catch((r) => {
console.error(r)
ElMessage.error('下载文件出现错误,请联系管理员!')
downloadLoadingInstance.close();
}) })
} }
export function post(url, params) {
return new Promise((resolve, reject) => {
axios
.post(url, {
params: params
})
.then(res => {
resolve(res.data)
})
.catch(err => {
reject(err)
})
})
}
/**
* 提交表单
* @param {*} url
* @param {*} data
*/
export function postForm(url, data, config) {
return new Promise((resolve, reject) => {
axios.post(url, data, config).then(res => {
resolve(res.data)
}).catch(err => {
reject(err)
})
})
}
// 通用下载方法
// export function download(url, params, filename) {
// //downloadLoadingInstance = Loading.service({ text: "正在下载数据,请稍候", spinner: "el-icon-loading", background: "rgba(0, 0, 0, 0.7)", })
// return service.post(url, params, {
// //transformRequest: [(params) => { return tansParams(params) }],
// headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
// responseType: 'blob'
// }).then(async (data) => {
// const isLogin = await blobValidate(data);
// if (isLogin) {
// const blob = new Blob([data])
// saveAs(blob, filename)
// } else {
// const resText = await data.text();
// const rspObj = JSON.parse(resText);
// const errMsg = "出錯了";// errorCode[rspObj.code] || rspObj.msg || errorCode['default']
// Message.error(errMsg);
// }
// // downloadLoadingInstance.close();
// }).catch((r) => {
// console.error(r)
// Message.error('下载文件出现错误,请联系管理员!')
// // downloadLoadingInstance.close();
// })
// }
export default service export default service

View File

@ -105,7 +105,8 @@ function handleLogin() {
router.push({ path: redirect.value || "/" }); router.push({ path: redirect.value || "/" });
}) })
.catch((error) => { .catch((error) => {
// proxy.$modal.msgError(error.msg); console.log(error)
proxy.$modal.msgError(error.msg);
loading.value = false; loading.value = false;
// //
if (captchaOnOff.value) { if (captchaOnOff.value) {
@ -155,13 +156,14 @@ getCookie();
.title { .title {
margin: 0px auto 30px auto; margin: 0px auto 30px auto;
text-align: center; text-align: center;
color: #707070; color: #fff;
} }
.login-form { .login-form {
border-radius: 6px; border-radius: 6px;
background: #ffffff; // background: #ffffff;
width: 400px; background-color: hsla(0, 0%, 100%, 0.3);
width: 310px;
padding: 25px 25px 5px 25px; padding: 25px 25px 5px 25px;
.el-input { .el-input {
height: 40px; height: 40px;