处理偶尔循环登录问题

This commit is contained in:
2025-08-23 17:51:36 +08:00
parent 9b1947f7c0
commit 81dc87c388
8 changed files with 884 additions and 579 deletions
+278 -153
View File
@@ -1,10 +1,10 @@
<template>
<div v-if="true" class='box'>
<header>
<h3>Hello~{{userName}}</h3>
<h3>Hello~{{ userName }}</h3>
<h4>呀 你又来审批预约了啊ㄟ( ▔, ▔ )ㄏ</h4>
<div class="plate-box">
<div @click="onClickGo('/Invitation')" class="plate-button">
<img src="../assets/ren.png" alt="">
<div class="right">
@@ -43,275 +43,400 @@
<div class="status" :class="stateEnum[item.status].className">{{ stateEnum[item.status].statusName }}</div>
</div>
</div>
<div v-if="isLoading && recordArr.length == 0" class="no-have">
<div v-if="isLoading && isDataLoaded && recordArr.length === 0" class="no-have">
<img class="" src="../assets/nodata.png" alt="">
<p>暂无待审批数据</p>
</div>
<div v-else-if="isLoading && !isDataLoaded" class="no-have">
<img class="" src="../assets/nodata.png" alt="">
<p>待审批数据加载中...</p>
</div>
</div>
</div>
</template>
<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { Toast, ImagePreview } from 'vant';
import {reactive, ref, onMounted} from 'vue'
import {useRouter} from 'vue-router'
import {useStore} from 'vuex'
import {Toast, ImagePreview} from 'vant';
import dd from 'dingtalk-jsapi'
import { rulesForm, resetForm, rulesPhone } from '@/com/index'
import { getToken, getVisitorRecord, getWhiteCode } from '@/api/admin.js'
import {rulesForm, resetForm, rulesPhone} from '@/com/index'
import {getToken, getVisitorRecord, getWhiteCode} from '@/api/admin.js'
/* @data */
const router = useRouter()
const store = useStore()
const isLoading = ref(false)
const token = ref('')
const userName = ref('')
const query = reactive({
status: 0,
size: 2,
current: 1,
})
const recordArr = ref([])
const stateEnum = store.state.stateEnum
token.value = localStorage.getItem('token')
localStorage.setItem('redirect', '/adminHome')
const router = useRouter()
const store = useStore()
const isLoading = ref(false)
const isDataLoaded = ref(false)
// 已经在登录处理中
const isLoginInProgress = ref(false);
const token = ref('')
const userName = ref('')
const query = reactive({
status: 0,
size: 2,
current: 1,
})
const recordArr = ref([])
const stateEnum = store.state.stateEnum
token.value = localStorage.getItem('token')
localStorage.setItem('redirect', '/adminHome')
/* @setup */
const login = () =>{
Toast.loading({ duration: 0, forbidClick: true, message: '登录中', });
dd.getAuthCode({
corpId: 'ding03f8e88dde9b426835c2f4657eb6378f',
success: (res) => {
const { code } = res;
localStorage.removeItem('token');
localStorage.removeItem('user');
getToken(code).then(data =>{
localStorage.setItem('user', JSON.stringify(data.data) )
localStorage.setItem('token', data.data.authorization)
userName.value = data.data.name
Toast.clear()
Toast.success('登录成功')
visitorList()
})
},
fail: (err) => {
},
complete: () => {},
});
const login = () => {
// 防止重复登录
if (isLoginInProgress.value) {
return;
}
// login()
const visitorList = () =>{
Toast.loading({ duration: 0, forbidClick: true, message: '加载中', });
getVisitorRecord(query).then(res =>{
isLoading.value = true
recordArr.value = res.data
}).catch(err=>{
isLoading.value = true
})
}
// visitorList()
if(token.value){
visitorList()
console.log((JSON.parse(localStorage.getItem('user'))).name )
userName.value = JSON.parse(localStorage.getItem('user')).name
}else{
login()
}
isLoginInProgress.value = true;
Toast.loading({duration: 0, forbidClick: true, message: '登录中',});
dd.getAuthCode({
corpId: 'ding03f8e88dde9b426835c2f4657eb6378f',
success: (res) => {
const {code} = res;
localStorage.removeItem('token');
localStorage.removeItem('user');
getToken(code).then(data => {
// 验证返回的数据
if (data && data.data && data.data.authorization) {
localStorage.setItem('user', JSON.stringify(data.data));
localStorage.setItem('token', data.data.authorization);
userName.value = data.data.name;
Toast.clear();
Toast.success('登录成功');
visitorList();
} else {
throw new Error('登录数据异常');
}
}).catch(err => {
Toast.clear();
Toast.fail('登录失败');
console.error('登录失败:', err);
}).finally(() => {
isLoginInProgress.value = false;
});
},
fail: (err) => {
},
complete: () => {
},
});
}
// login()
const visitorList = () => {
isDataLoaded.value = false
Toast.loading({duration: 0, forbidClick: true, message: '加载中',});
getVisitorRecord(query).then(res => {
isLoading.value = true
isDataLoaded.value = true
recordArr.value = res.data
}).catch(err => {
isLoading.value = true
isDataLoaded.value = true
recordArr.value = []
})
}
/* @method */
const onClickGo = (url = '/adminHome') =>{
router.push(url)
const onClickGo = (url = '/adminHome') => {
router.push(url)
}
// 改进页面加载时的初始化逻辑
const initializeApp = () => {
const token = localStorage.getItem('token');
const userStr = localStorage.getItem('user');
if (token && userStr) {
try {
const user = JSON.parse(userStr);
if (user && user.name) {
userName.value = user.name;
visitorList();
} else {
// 用户数据不完整,重新登录
login();
}
} catch (e) {
// 用户数据解析失败,重新登录
console.error('用户数据解析失败:', e);
login();
}
} else {
// 没有token或用户数据,需要登录
login();
}
};
// 在组件挂载时调用
onMounted(() => {
initializeApp();
});
</script>
<style lang='scss' scoped>
.box{
.box {
background-color: #F9FBFC;
display: flex;
flex-direction: column;
header{
header {
position: relative;
width: 100%;
height: 10.4rem /* 780/75 */;
height: 10.4rem /* 780/75 */
;
background: url('../assets/guanlibeijing.png') left top/100% 100% no-repeat;
box-sizing: border-box;
h3{
padding: 1.0933rem /* 82/75 */ .6933rem /* 52/75 */ .1067rem /* 8/75 */;
h3 {
padding: 1.0933rem /* 82/75 */
.6933rem /* 52/75 */
.1067rem /* 8/75 */
;
box-sizing: border-box;
line-height: .72rem /* 54/75 */;
font-size: .48rem /* 36/75 */;
font-family: Source Han Sans CN, Source Han Sans CN;
line-height: .72rem /* 54/75 */
;
font-size: .48rem /* 36/75 */
;
font-family: Source Han Sans CN, serif;
font-weight: 500;
color: #222222;
}
h4{
line-height: .48rem /* 36/75 */;
h4 {
line-height: .48rem /* 36/75 */
;
font-size: $sm-size;
font-family: Source Han Sans CN, Source Han Sans CN;
font-family: Source Han Sans CN, serif;
font-weight: 400;
color: #FFFFFF;
padding: 0 .6933rem /* 52/75 */ 1.12rem /* 84/75 */;
padding: 0 .6933rem /* 52/75 */
1.12rem /* 84/75 */
;
box-sizing: border-box;
}
.position-img{
.position-img {
position: absolute;
right: .4267rem /* 32/75 */;
top: .2933rem /* 22/75 */;
width: 3.6267rem /* 272/75 */;
height: 2.96rem /* 222/75 */;
img{
right: .4267rem /* 32/75 */
;
top: .2933rem /* 22/75 */
;
width: 3.6267rem /* 272/75 */
;
height: 2.96rem /* 222/75 */
;
img {
width: 100%;
height: 100%;
}
}
.plate-box{
.plate-box {
width: 100%;
padding: 0 .4rem /* 30/75 */;
padding: 0 .4rem /* 30/75 */
;
box-sizing: border-box;
display: flex;
justify-content: space-between;
.plate-button{
width: 4.4rem /* 330/75 */;
height: 1.92rem /* 144/75 */;
background: linear-gradient(180deg, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0.6) 48%);
border-radius: .2667rem /* 20/75 */;
border: .0267rem /* 2/75 */ solid #FFFFFF;
.plate-button {
width: 4.4rem /* 330/75 */
;
height: 1.92rem /* 144/75 */
;
background: linear-gradient(180deg, rgba(255, 255, 255, 0.3) 0%, rgba(255, 255, 255, 0.6) 48%);
border-radius: .2667rem /* 20/75 */
;
border: .0267rem /* 2/75 */
solid #FFFFFF;
display: flex;
align-items: center;
img{
width: .9867rem /* 74/75 */;
height: .9867rem /* 74/75 */;
margin-left: .32rem /* 24/75 */;
img {
width: .9867rem /* 74/75 */
;
height: .9867rem /* 74/75 */
;
margin-left: .32rem /* 24/75 */
;
}
.right{
margin-left: .3467rem /* 26/75 */;
div{
line-height: .64rem /* 48/75 */;
.right {
margin-left: .3467rem /* 26/75 */
;
div {
line-height: .64rem /* 48/75 */
;
font-size: $lg-size;
font-family: Source Han Sans CN, Source Han Sans CN;
font-family: Source Han Sans CN, serif;
font-weight: 500;
color: #222222;
}
P{
line-height: .4rem /* 30/75 */;
font-size: .2667rem /* 20/75 */;
font-family: Source Han Sans CN, Source Han Sans CN;
P {
line-height: .4rem /* 30/75 */
;
font-size: .2667rem /* 20/75 */
;
font-family: Source Han Sans CN, serif;
font-weight: 400;
color: #5F6583;
margin-top: .1067rem /* 8/75 */;
margin-top: .1067rem /* 8/75 */
;
}
}
}
}
}
.content{
.content {
flex: 1;
overflow: auto;
position: relative;
// top: 5.8667rem /* 440/75 */;
margin-top: -4.5333rem /* 340/75 */;
margin-top: -4.5333rem /* 340/75 */
;
width: 100%;
padding: 0 .4rem /* 30/75 */;
padding: 0 .4rem /* 30/75 */
;
box-sizing: border-box;
h2{
h2 {
width: 100%;
padding: .2667rem /* 20/75 */ .32rem /* 24/75 */;
padding: .2667rem /* 20/75 */
.32rem /* 24/75 */
;
box-sizing: border-box;
display: flex;
justify-content: space-between;
line-height: .72rem /* 54/75 */;
font-size: .48rem /* 36/75 */;
font-family: Source Han Sans CN, Source Han Sans CN;
line-height: .72rem /* 54/75 */
;
font-size: .48rem /* 36/75 */
;
font-family: Source Han Sans CN, serif;
font-weight: 500;
color: #222222;
span{
span {
// line-height: 36px;
font-size: $sm-size;
font-family: Source Han Sans CN, Source Han Sans CN;
font-family: Source Han Sans CN, serif;
font-weight: 400;
color: #999999;
}
}
.content-ul{
.wait-list{
.content-ul {
.wait-list {
position: relative;
width: 9.2rem /* 690/75 */;
width: 9.2rem /* 690/75 */
;
// height: 470px;
background: #FFFFFF;
border-radius: .2667rem /* 20/75 */;
padding: .48rem /* 36/75 */;
border-radius: .2667rem /* 20/75 */
;
padding: .48rem /* 36/75 */
;
box-sizing: border-box;
margin-bottom: .2667rem /* 20/75 */;
h5{
height: .6667rem /* 50/75 */;
margin-bottom: .2667rem /* 20/75 */
;
h5 {
height: .6667rem /* 50/75 */
;
font-size: $md-size;
font-family: Source Han Sans CN, Source Han Sans CN;
font-family: Source Han Sans CN, serif;
font-weight: 400;
color: #222222;
}
.apply-box{
.apply-box {
width: 100%;
padding: .4rem /* 30/75 */ .3733rem /* 28/75 */ .5067rem /* 38/75 */;
padding: .4rem /* 30/75 */
.3733rem /* 28/75 */
.5067rem /* 38/75 */
;
box-sizing: border-box;
border-radius: .1333rem /* 10/75 */;
border-radius: .1333rem /* 10/75 */
;
font-size: $md-size;
font-family: Source Han Sans CN, Source Han Sans CN;
font-family: Source Han Sans CN, serif;
font-weight: 400;
color: #999999;
line-height: .72rem /* 54/75 */;
line-height: .72rem /* 54/75 */
;
background: #F5F7F9;
span:last-child{
span:last-child {
color: #5F6583;
}
}
.status{
.status {
position: absolute;
top: 0;
right: 0;
width: 1.52rem /* 114/75 */;
height: .6667rem /* 50/75 */;
line-height: .6667rem /* 50/75 */;
width: 1.52rem /* 114/75 */
;
height: .6667rem /* 50/75 */
;
line-height: .6667rem /* 50/75 */
;
background: #FFE9E4;
border-radius: 0px .2667rem /* 20/75 */ 0px .1333rem /* 10/75 */;
border-radius: 0 .2667rem /* 20/75 */
0 .1333rem /* 10/75 */
;
color: #FF6643;
font-size: $sm-size;
font-family: Source Han Sans CN, Source Han Sans CN;
font-family: Source Han Sans CN, serif;
font-weight: 400;
color: #FF6643;
text-align: center;
}
.refuse{
.refuse {
color: #5F6583;
background: #EAEEF6;
}
.pass{
.pass {
color: #30BF78;
background: #E7F8F5;
}
}
}
.no-have{
.no-have {
// display: block;
width: 7.4667rem /* 560/75 */;
height: 5.3867rem /* 404/75 */;
margin: .8rem /* 60/75 */ auto;
img{
width: 7.4667rem /* 560/75 */
;
height: 5.3867rem /* 404/75 */
;
margin: .8rem /* 60/75 */
auto;
img {
width: 100%;
height: 100%;
}
// font-size: 15px;
font-family: Source Han Sans CN, Source Han Sans CN;
font-family: Source Han Sans CN, serif;
font-weight: 400;
color: #999999;
text-align: center;