update 进入登录页时验证缓存token是否有效

This commit is contained in:
zk
2024-05-21 16:06:02 +08:00
parent 510f34953f
commit f3f100cd18
3 changed files with 57 additions and 4 deletions
+29 -3
View File
@@ -91,7 +91,7 @@
</div>
</template>
<script lang="ts" name="login-mini" setup>
import { getCodeInfo } from '/@/api/user';
import { getCodeInfo, getUserInfo } from '/@/api/user';
import { onMounted, reactive, ref, toRaw, watch } from 'vue';
import codeImg from '/@/assets/images/checkcode.png';
import { useUserStore } from '/@/store/modules/user';
@@ -101,7 +101,7 @@
import { useRoute, useRouter } from 'vue-router';
import { getAuthCache } from '/@/utils/auth.ts';
import { TOKEN_KEY } from '/@/enum/cacheEnum.ts';
import { PageEnum, setBaseLogin } from '/@/enum/pageEnum.ts';
import { getCurrentPath, PageEnum, setBaseLogin, toHomeByLogoutPath, toLoginByNowPath } from '/@/enum/pageEnum.ts';
const router = useRouter();
const route = useRoute();
@@ -157,6 +157,10 @@
}
);
function toHomeByNowPath() {
return route.path == PageEnum.BASE_LOGIN_YINCHUAN ? PageEnum.YINCHUAN_HOME : PageEnum.BASE_HOME;
}
async function accountLogin() {
if (!formData.username) {
createMessage.warn(t('login.accountPlaceholder'));
@@ -190,7 +194,7 @@
duration: 3,
});
let path = route.path == PageEnum.BASE_LOGIN_YINCHUAN ? PageEnum.YINCHUAN_HOME : PageEnum.BASE_HOME;
let path = toHomeByNowPath();
setBaseLogin(route.path);
router.replace({ path: path });
}
@@ -207,9 +211,31 @@
}
onMounted(() => {
checkToken();
//加载验证码
handleChangeCheckCode();
});
//校验缓存token是否有效
function checkToken() {
const token = getAuthCache(TOKEN_KEY);
if (!token) {
return;
}
const logoutPath = getCurrentPath(); //退出时路径
let path = toHomeByNowPath();
getUserInfo().then(({ code }) => {
if (code == 200) {
if (logoutPath) {
const hasLogoutPath = toHomeByLogoutPath(logoutPath);
router.push({ path: hasLogoutPath });
} else {
router.push({ path: path });
}
}
});
}
</script>
<style lang="less" scoped>