gaoluyang
14 小时以前 045da5de062c1b43f53bc7b6a4cf125fbd97a3e5
src/layout/components/Sidebar/Logo.vue
@@ -1,129 +1,142 @@
<template>
  <div class="sidebar-logo-container" :class="{ 'collapse': collapse }">
    <transition name="sidebarLogoFade">
      <router-link v-if="collapse" key="collapse" class="sidebar-logo-link" to="/">
        <img v-if="logoUrl" :src="logoUrl" class="sidebar-logo" @error="handleImageError" alt="公司Logo" />
        <h1 v-else class="sidebar-title">{{ title }}</h1>
      </router-link>
      <router-link v-else key="expand" class="sidebar-logo-link" to="/">
        <img v-if="logoUrl" :src="logoUrl" class="sidebar-logo" @error="handleImageError" alt="公司Logo" />
        <h1 v-else class="sidebar-title">{{ title }}</h1>
      </router-link>
    </transition>
  </div>
</template>
<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import useUserStore from '@/store/modules/user'
import defaultLogo from '@/assets/logo/logo.png' // 导入默认logo
defineProps({
  collapse: {
    type: Boolean,
    required: true
  }
})
const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore()
// 处理工厂名称,生成合法的文件名
const cleanFactoryName = computed(() => {
  if (!userStore.currentFactoryName) return ''
  return userStore.currentFactoryName.trim()
})
// 动态logo路径
const logoUrl = ref('')
// 检查logo是否存在并设置url
const updateLogoUrl = () => {
  if (!cleanFactoryName.value) {
    logoUrl.value = defaultLogo
    return
  }
  // 使用Vite的动态导入
  try {
    const dynamicLogo = import.meta.glob('/src/assets/logo/*.png', { eager: true })
    const logoPath = `/src/assets/logo/${cleanFactoryName.value}.png`
    if (dynamicLogo[logoPath]) {
      logoUrl.value = dynamicLogo[logoPath].default
    } else {
      logoUrl.value = defaultLogo
    }
  } catch (error) {
    console.error('加载工厂Logo失败:', error)
    logoUrl.value = defaultLogo
  }
}
// 初始化和监听变化
onMounted(() => {
  updateLogoUrl()
  // 监听工厂名称变化
  watch(() => userStore.currentFactoryName, updateLogoUrl)
})
// 图片加载错误处理
const handleImageError = (event) => {
  console.warn('Logo加载失败,使用默认Logo')
  logoUrl.value = defaultLogo
}
</script>
<style lang="scss" scoped>
@import '@/assets/styles/variables.module.scss';
.sidebarLogoFade-enter-active {
  transition: opacity 1.5s;
}
.sidebarLogoFade-enter,
.sidebarLogoFade-leave-to {
  opacity: 0;
}
.sidebar-logo-container {
  position: relative;
  width: 100% !important;
  height: 50px !important;
  line-height: 50px;
  background: #fff;
  text-align: center;
  overflow: hidden;
  & .sidebar-logo-link {
    height: 100%;
    width: 100%;
    & .sidebar-logo {
      width: 100%;
      height: 100%;
      // height: 32px;
      vertical-align: middle;
      margin-right: 12px;
    }
    & .sidebar-title {
      display: inline-block;
      margin: 0;
      color: v-bind(getLogoTextColor);
      font-weight: 600;
      line-height: 50px;
      font-size: 14px;
      font-family: Avenir, Helvetica Neue, Arial, Helvetica, sans-serif;
      vertical-align: middle;
    }
  }
  &.collapse {
    .sidebar-logo {
      margin-right: 0px;
    }
  }
}
</style>
<template>
  <div class="sidebar-logo-container" :class="{ collapse }">
    <transition name="sidebarLogoFade">
      <router-link v-if="collapse" key="collapse" class="sidebar-logo-link" to="/">
        <img :src="faviconUrl" class="sidebar-logo sidebar-favicon" alt="站点图标" />
      </router-link>
      <router-link v-else key="expand" class="sidebar-logo-link" to="/">
        <img v-if="logoUrl" :src="logoUrl" class="sidebar-logo" @error="handleImageError" alt="公司Logo" />
        <h1 v-if="!logoUrl" class="sidebar-title">{{ title }}</h1>
      </router-link>
    </transition>
  </div>
</template>
<script setup>
import { ref, computed, onMounted, watch } from 'vue'
import useUserStore from '@/store/modules/user'
import defaultLogo from '@/assets/logo/logo.png'
defineProps({
  collapse: {
    type: Boolean,
    required: true
  }
})
const title = import.meta.env.VITE_APP_TITLE
const userStore = useUserStore()
const baseUrl = import.meta.env.BASE_URL || '/'
const faviconUrl = `${baseUrl.replace(/\/?$/, '/') }favicon.ico`.replace(/([^:]\/)\/+/g, '$1')
const cleanFactoryName = computed(() => {
  if (!userStore.currentFactoryName) return ''
  return userStore.currentFactoryName.trim()
})
const logoUrl = ref('')
const updateLogoUrl = () => {
  if (!cleanFactoryName.value) {
    logoUrl.value = defaultLogo
    return
  }
  try {
    const dynamicLogo = import.meta.glob('/src/assets/logo/*.png', { eager: true })
    const logoPath = `/src/assets/logo/${cleanFactoryName.value}.png`
    if (dynamicLogo[logoPath]) {
      logoUrl.value = dynamicLogo[logoPath].default
    } else {
      logoUrl.value = defaultLogo
    }
  } catch (error) {
    console.error('加载工厂 Logo 失败:', error)
    logoUrl.value = defaultLogo
  }
}
onMounted(() => {
  updateLogoUrl()
  watch(() => userStore.currentFactoryName, updateLogoUrl)
})
const handleImageError = () => {
  logoUrl.value = defaultLogo
}
</script>
<style lang="scss" scoped>
@import '@/assets/styles/variables.module.scss';
.sidebarLogoFade-enter-active {
  transition: opacity 1.5s;
}
.sidebarLogoFade-enter,
.sidebarLogoFade-leave-to {
  opacity: 0;
}
.sidebar-logo-container {
  position: relative;
  width: 100% !important;
  height: 56px !important;
  line-height: 56px;
  background: rgba(255, 255, 255, 0.78);
  border: 1px solid var(--surface-border);
  border-radius: 22px;
  text-align: center;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  .sidebar-logo-link {
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 18px 0 14px;
  }
  .sidebar-logo {
    width: auto;
    max-width: 250px;
    max-height: 50px;
    height: auto;
    vertical-align: middle;
    object-fit: contain;
    object-position: center;
  }
  .sidebar-title {
    display: inline-block;
    margin: 0;
    color: var(--text-primary);
    font-weight: 600;
    line-height: 1.2;
    font-size: 14px;
    font-family: "Segoe UI", "PingFang SC", sans-serif;
    vertical-align: middle;
  }
  &.collapse {
    .sidebar-logo-link {
      padding: 0;
    }
    .sidebar-logo {
      max-width: 30px;
      max-height: 30px;
    }
    .sidebar-favicon {
      width: 24px;
      height: 24px;
      max-width: 24px;
      max-height: 24px;
    }
  }
}
</style>