fuliqi
2024-08-15 02b9931c5e3a54d69d4d20a9dc6aad141fc35141
src/views/home/components/user-panel/index.vue
@@ -1,77 +1,134 @@
<template>
  <div class="user-panel sticky left-10 top-5 max-w-sm">
  <div class="user-panel max-w-sm min-w-96 h-fit">
    <el-card class="card">
      <div class="panel-content flex flex-col items-center">
        <div class="avatar-container w-40 h-40 rounded-full overflow-hidden">
          <div class="avatar-content">
            <img src="@/assets/image/avatar/avatar.png" class="avatar-img" alt="">
        <!-- 学生信息 -->
        <el-dropdown>
          <div
            class="avatar-container w-40 h-40 rounded-full overflow-hidden el-dropdown-link"
            @click="stateClick()"
          >
            <div class="avatar-content" v-if="userInfo.imagePath">
              <img :src="userInfo.imagePath?'api/files/' + userInfo.imagePath:'/static/icons/touxiang.png'" class="avatar-img" alt="" />
            </div>
            <div
              class="avatar-content"
              :style="{ backgroundColor: getColor }"
              v-else
            >
              <div class="name text-5xl font-bold text-white">
                {{ getFirstName }}
              </div>
            </div>
          </div>
          <!-- <div class="avatar-content" :style="{backgroundColor: getColor}">
            <div class="name text-5xl font-bold text-white">张</div>
          </div> -->
        </div>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>修改密码</el-dropdown-item>
              <el-dropdown-item @click="goPersonalCenter()"
                >个人中心</el-dropdown-item
              >
              <el-dropdown-item @click="quit()">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <div class="name-container text-lg font-bold mt-5 mb-2">
          测试测试
          {{ userInfo.realName }}
        </div>
        <div class="department-container text-base mb-10">
          测试测试
          {{ userInfo.userName }}
        </div>
        <!-- 学生选项 -->
        <div class="tool-container grid grid-cols-3 gap-10">
          <div class="tool-item text-center cursor-pointer" v-for="item in toolList">
          <div
            class="tool-item text-center cursor-pointer"
            v-for="item in toolList"
            @click="toolClick(item)"
          >
            <div class="tool-icon mb-1">
              <img :src="item.iconPath" class="width-img" alt="">
              <img :src="item.iconPath" class="width-img" alt="" />
            </div>
            <div class="tool-title">
              {{ item.title }}
            </div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script setup>
import { ref, computed } from 'vue';
import randomColor from '@/utils/randomColor.js';
import { ref, computed } from "vue";
import randomColor from "@/utils/randomColor.js";
import { useRouter } from "vue-router";
import { storeToRefs } from "pinia";
import { useUserStore } from "@/store/index.js";
import { logout } from "@/api/modules/user.js";
const userStore = useUserStore();
const { userInfo } = storeToRefs(userStore);
const router = useRouter();
// 获取列表
const toolList = ref([
  {
    id: 1,
    title: '资源共享',
    iconPath: new URL('@/assets/icons/icon1.png', import.meta.url).href
    title: "在线培训",
    iconPath: new URL("@/assets/icons/icon1.png", import.meta.url).href,
    path: "/train",
  },
  {
    id: 2,
    title: '我的课程',
    iconPath: new URL('@/assets/icons/icon2.png', import.meta.url).href
    title: "我的考试",
    iconPath: new URL("@/assets/icons/icon2.png", import.meta.url).href,
    path: "/exam-list",
  },
  {
    id: 3,
    title: '我的课程',
    iconPath: new URL('@/assets/icons/icon2.png', import.meta.url).href
  },
  {
    id: 4,
    title: '我的课程',
    iconPath: new URL('@/assets/icons/icon2.png', import.meta.url).href
    title: "我的成绩",
    iconPath: new URL("@/assets/icons/icon2.png", import.meta.url).href,
    path: "/grade-list",
  },
]);
// 获取颜色
const getColor = computed(() => {
  return randomColor();
})
});
// 获取用户信息
const getFirstName = computed(() => {
  return userInfo.value.realName && userInfo.value.realName[0];
});
// 点击事件
const toolClick = (item) => {
  if (item.path) {
    router.push(item.path);
  }
};
// 用户选项
const dropdownRef = ref();
// 退出登录
const quit = () => {
  logout()
    .then(() => {
      router.push("/login").then(() => {
        userStore.setUserInfo(null);
        localStorage.clear("user");
      });
    })
    .catch((err) => {
      console.log("退出登录失败,失败原因;", err);
    });
};
const goPersonalCenter = () => {
  router.push("/personal-center");
};
</script>
<style lang="scss" scoped>
.card {
  border-radius: 30px;
}
@@ -89,4 +146,16 @@
  height: 100%;
  object-fit: cover;
}
</style>
.avatar-container {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
.example-showcase .el-dropdown-link {
  cursor: pointer;
  color: var(--el-color-primary);
  display: flex;
  align-items: center;
}
</style>