From 361dd1116c08ac24ab7e734c98c0771ca220f023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=95=E9=87=91=E6=B3=BD?= <1098696801@qq.com> Date: Sun, 7 Nov 2021 15:05:44 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=AD=E8=AF=81=E8=BF=87=E6=9C=9F=E9=87=8D?= =?UTF-8?q?=E6=96=B0=E7=99=BB=E5=BD=95=E5=90=8E=E6=89=A7=E8=A1=8C=E4=B8=8A?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- magic-boot-ui/src/scripts/request.js | 91 ++++++++++++++-------------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/magic-boot-ui/src/scripts/request.js b/magic-boot-ui/src/scripts/request.js index 9230246..6fedf1d 100644 --- a/magic-boot-ui/src/scripts/request.js +++ b/magic-boot-ui/src/scripts/request.js @@ -1,4 +1,3 @@ - import axios from 'axios' import { Message, MessageBox } from 'element-ui' import store from '@/store' @@ -45,51 +44,55 @@ service.interceptors.response.use( * You can also judge the status by HTTP Status Code */ response => { - const res = response.data - // if the custom code is not 1, it is judged as an error. - if (res.code !== 200) { - var duration = 5 - if (res.code === 402) { - duration = 1 - MessageBox.prompt('凭证已过期,请输入密码重新登录', '提示', { - confirmButtonText: '确定', - cancelButtonText: '退出', - inputType: 'password' - }).then(({ value }) => { - store.dispatch('user/login', { - username: store.getters.username, - password: value - }).then((res) => { - console.log(res) - }) - }).catch(() => { - store.dispatch('user/logout').then(() => { - location.reload() - }) - }) - return - } - if (isShowMsg === false) { - Message({ - message: res.message || 'Error', - type: 'error', - duration: duration * 1000, - showClose: true, - onClose: function() { - isShowMsg = false - if (res.code === 402) { - store.dispatch('user/logout').then(() => { - location.reload() - }) + return new Promise((reslove, reject) => { + const res = response.data + if (res.code !== 200) { + var duration = 5 + if (res.code === 402) { + duration = 1 + MessageBox.prompt(`当前账号:${store.getters.username}凭证已过期,请输入密码重新登录`, '提示', { + confirmButtonText: '确定', + cancelButtonText: '退出', + inputType: 'password', + closeOnClickModal: false, + beforeClose: (action, instance, done) => { + if (action === 'confirm') { + store.dispatch('user/login', { + username: store.getters.username, + password: instance.inputValue + }).then((res) => { + if (res) { + done() + service(response.config).then(ret => reslove(ret)) + } + }) + } else if (action === 'cancel') { + store.dispatch('user/logout').then(() => { + location.reload() + }) + } else { + done() + } } - } - }) - isShowMsg = true + }) + } + if (isShowMsg === false && res.code !== 402) { + Message({ + message: res.message || 'Error', + type: 'error', + duration: duration * 1000, + showClose: true, + onClose: function() { + isShowMsg = false + } + }) + isShowMsg = true + // reslove(res) + } + } else { + reslove(res) } - return Promise.reject(new Error(res.message || 'Error')) - } else { - return res - } + }) }, error => { // console.log('err' + error) // for debug