refactor: App.vue统一路由守卫 — onLaunch总是重定向(已登录→扫码/未登录→登录)

This commit is contained in:
2026-08-07 17:01:55 +08:00
parent 71e55ed476
commit 0fcc607f17
2 changed files with 9 additions and 25 deletions

View File

@ -5,14 +5,17 @@ export default {
onLaunch() {
console.log("生产流转 T1.0.2 启动");
// 无登录态跳转登录页 — 双重检测防止部分手机划掉App后storage丢失
// 🚦 统一路由守卫:根据登录态重定向到正确页面
const token = uni.getStorageSync("access_token") || uni.getStorageSync("refresh_token");
const user = uni.getStorageSync("user");
if (!token && !user) {
if (token && user) {
// 已登录 → 直接进扫码干活页
uni.reLaunch({ url: "/pages/scan/index" });
} else {
// 未登录 → 去登录页
uni.reLaunch({ url: "/pages/login/login" });
return;
}
// token可能已过期但存在 → 正常启动让401拦截器自动续期
// 🚀 OTA 热更新检测(仅 App 端生效)
// #ifdef APP-PLUS

View File

@ -6,12 +6,7 @@
<text class="version">{{ appVersion }}</text>
</view>
<!-- 检查中防止登录页闪烁 -->
<view v-if="checking" class="checking">
<text class="checking-icon"></text>
</view>
<view v-else class="form">
<view class="form">
<input v-model="username" class="input" placeholder="用户名" />
<input v-model="password" class="input" type="password" placeholder="密码" />
<button class="login-btn" @tap="handleLogin" :disabled="loading">
@ -26,21 +21,9 @@
import { ref, onMounted } from "vue";
import { post } from "../../utils/request";
// 🚀 动态版本号 + 自动跳过登录(已登录用户
// 🚀 动态版本号 — 热更新后自动变化(路由由 App.vue onLaunch 统一接管
const appVersion = ref("T1.0.2");
const checking = ref(true);
onMounted(() => {
// 1. 自动跳过:如果已有 token + user直接进首页
const token = uni.getStorageSync("access_token") || uni.getStorageSync("refresh_token");
const user = uni.getStorageSync("user");
if (token && user) {
uni.switchTab({ url: "/pages/scan/index" });
return; // 不展示登录页
}
checking.value = false;
// 2. 动态版本号
try {
const v = uni.getSystemInfoSync().appWgtVersion || uni.getSystemInfoSync().appVersion;
if (v) appVersion.value = v.startsWith("T") ? v : "T" + v;
@ -91,6 +74,4 @@ async function handleLogin() {
.login-btn { width: 100%; height: 48px; background: #2563EB; color: #fff; border: none; border-radius: 10px; font-size: 16px; font-weight: 700; line-height: 48px; }
.login-btn[disabled] { opacity: 0.6; }
.error { display: block; text-align: center; color: #dc2626; font-size: 13px; margin-top: 12px; }
.checking { display: flex; align-items: center; justify-content: center; }
.checking-icon { font-size: 40px; }
</style>