App.vue 2.35 KB
<template>
  <div id="app">
    <el-container style="height: 100vh;">
      <!-- 侧边栏 -->
      <el-aside width="200px" v-if="$route.name !== 'login'">
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          @select="handleSelect"
          router
        >
          <el-menu-item index="/upload">
            <el-icon><Upload /></el-icon>
            <span>上传工单</span>
          </el-menu-item>
          <el-menu-item index="/quality-list">
            <el-icon><List /></el-icon>
            <span>质检信息列表</span>
          </el-menu-item>
          <el-menu-item index="/staff-list">
            <el-icon><User /></el-icon>
            <span>工维人员管理</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 顶部导航栏 -->
        <el-header v-if="$route.name !== 'login'" style="background-color: #fff; border-bottom: 1px solid #dcdfe6; display: flex; justify-content: space-between; align-items: center;">
          <div></div>
          <el-button type="danger" @click="logout">退出</el-button>
        </el-header>

        <!-- 主要内容区域 -->
        <el-main>
          <router-view />
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { useRouter } from 'vue-router'
import { Upload, List, User } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
import { onMounted } from 'vue'
import {
  quitLogin
} from './utils/api'

const router = useRouter()

// 路由守卫
onMounted(() => {
  // 检查是否已登录
  const token = localStorage.getItem('token')
  if (!token && router.currentRoute.value.name !== 'login') {
    router.push('/login')
  }
})

const handleSelect = (key) => {
  console.log(key)
}

const logout = async () => {
  const res = await quitLogin()

  if (res.code == 200) {
      // 登出逻辑
      localStorage.removeItem('token')
      router.push('/login')
  }else{
    ElMessage.error(res.msg || '退出登录失败')
  }
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  min-width: 1400px;
}

.el-header {
  line-height: 60px;
}

.el-aside {
  background-color: #fff;
  border-right: 1px solid #dcdfe6;
}
</style>