请求交互优化,登录失效清除用户数据
parent
86e88fda09
commit
37f61763ff
|
|
@ -9,6 +9,7 @@ import { localCache } from './cache';
|
||||||
import router from "@/router";
|
import router from "@/router";
|
||||||
import { showToast } from 'vant';
|
import { showToast } from 'vant';
|
||||||
import { useAuthModal } from '@/stores/authModal';
|
import { useAuthModal } from '@/stores/authModal';
|
||||||
|
import { useUserInfo } from '@/stores/userInfo';
|
||||||
import hostAPI from '@/config/host.config'
|
import hostAPI from '@/config/host.config'
|
||||||
|
|
||||||
export default function () {
|
export default function () {
|
||||||
|
|
@ -16,6 +17,7 @@ export default function () {
|
||||||
let requestCount = 0;//请求数量
|
let requestCount = 0;//请求数量
|
||||||
|
|
||||||
const authModal = useAuthModal();
|
const authModal = useAuthModal();
|
||||||
|
const userInfo = useUserInfo();
|
||||||
|
|
||||||
const showLoading = ()=>{
|
const showLoading = ()=>{
|
||||||
if (requestCount === 0) {
|
if (requestCount === 0) {
|
||||||
|
|
@ -77,6 +79,7 @@ export default function () {
|
||||||
showToast('账号过期或异地登录, 请重新登录');
|
showToast('账号过期或异地登录, 请重新登录');
|
||||||
localCache.remove('auth');
|
localCache.remove('auth');
|
||||||
localCache.remove('userInfo');
|
localCache.remove('userInfo');
|
||||||
|
userInfo.updateUserInfo({});
|
||||||
authModal.setAuthModalType('login');
|
authModal.setAuthModalType('login');
|
||||||
authModal.showAuthModal();
|
authModal.showAuthModal();
|
||||||
// router.replace({name: 'Home', params: {auth: 'over'}});
|
// router.replace({name: 'Home', params: {auth: 'over'}});
|
||||||
|
|
@ -97,6 +100,7 @@ export default function () {
|
||||||
showToast('账号过期或异地登录, 请重新登录');
|
showToast('账号过期或异地登录, 请重新登录');
|
||||||
localCache.remove('auth');
|
localCache.remove('auth');
|
||||||
localCache.remove('userInfo');
|
localCache.remove('userInfo');
|
||||||
|
userInfo.updateUserInfo({});
|
||||||
authModal.setAuthModalType('login');
|
authModal.setAuthModalType('login');
|
||||||
authModal.showAuthModal();
|
authModal.showAuthModal();
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ import { localCache } from '@/io/cache'
|
||||||
import { showToast } from 'vant'
|
import { showToast } from 'vant'
|
||||||
|
|
||||||
import { useAuthModal } from '@/stores/authModal';
|
import { useAuthModal } from '@/stores/authModal';
|
||||||
|
import { useUserInfo } from '@/stores/userInfo';
|
||||||
|
|
||||||
|
|
||||||
const service = axios.create({
|
const service = axios.create({
|
||||||
|
|
@ -38,6 +38,7 @@ service.interceptors.response.use(
|
||||||
const { requestBaseUrl } = response.config
|
const { requestBaseUrl } = response.config
|
||||||
const res = response.data
|
const res = response.data
|
||||||
const authModal = useAuthModal();
|
const authModal = useAuthModal();
|
||||||
|
const userInfo = useUserInfo();
|
||||||
if (requestBaseUrl == 'chat') {
|
if (requestBaseUrl == 'chat') {
|
||||||
return res
|
return res
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -45,6 +46,7 @@ service.interceptors.response.use(
|
||||||
if (res.status == 401) {
|
if (res.status == 401) {
|
||||||
authModal.setAuthModalType('login');
|
authModal.setAuthModalType('login');
|
||||||
authModal.showAuthModal();
|
authModal.showAuthModal();
|
||||||
|
userInfo.updateUserInfo({});
|
||||||
localCache.remove('auth');
|
localCache.remove('auth');
|
||||||
localCache.remove('userInfo');
|
localCache.remove('userInfo');
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -58,6 +60,7 @@ service.interceptors.response.use(
|
||||||
},
|
},
|
||||||
error => {
|
error => {
|
||||||
const authModal = useAuthModal();
|
const authModal = useAuthModal();
|
||||||
|
const userInfo = useUserInfo();
|
||||||
|
|
||||||
if (error.message == 'canceled') return Promise.reject(error)
|
if (error.message == 'canceled') return Promise.reject(error)
|
||||||
const res = error.response?.data
|
const res = error.response?.data
|
||||||
|
|
@ -67,6 +70,7 @@ service.interceptors.response.use(
|
||||||
if (res.errcode == 401) {
|
if (res.errcode == 401) {
|
||||||
authModal.setAuthModalType('login');
|
authModal.setAuthModalType('login');
|
||||||
authModal.showAuthModal();
|
authModal.showAuthModal();
|
||||||
|
userInfo.updateUserInfo({});
|
||||||
localCache.remove('auth');
|
localCache.remove('auth');
|
||||||
localCache.remove('userInfo');
|
localCache.remove('userInfo');
|
||||||
} else {
|
} else {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue