AppHeader.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <template>
  2. <header class="app-header">
  3. <h1 class="app-header-title">前期研究前置报告计划管理系统</h1>
  4. <div class="app-header-right">
  5. <div class="app-header-datetime">
  6. <div class="app-header-time">{{ currentTime }}</div>
  7. <div class="app-header-date">{{ currentDate }}</div>
  8. </div>
  9. <div class="app-header-divider"></div>
  10. <div class="app-header-user">
  11. <span class="app-header-avatar">{{ avatarText }}</span>
  12. <span class="app-header-username">{{ displayName }}</span>
  13. </div>
  14. <button type="button" class="app-header-logout" @click="handleLogout">
  15. 退出
  16. </button>
  17. </div>
  18. </header>
  19. </template>
  20. <script setup>
  21. import { computed, onBeforeUnmount, onMounted, ref } from "vue";
  22. import { useRouter } from "vue-router";
  23. import { ElMessage } from "element-plus";
  24. import { loginout } from "@/api/login";
  25. import globalStore from "@/store/modules/global";
  26. defineOptions({
  27. name: "AppHeader",
  28. });
  29. const router = useRouter();
  30. const global = globalStore();
  31. const currentTime = ref("");
  32. const currentDate = ref("");
  33. let timer = null;
  34. /** 暂写死,后续接登录用户信息 */
  35. const displayName = "苏小苏";
  36. const avatarText = computed(() => displayName.slice(0, 1) || "");
  37. const WEEKDAYS = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
  38. const padZero = (num) => String(num).padStart(2, "0");
  39. const updateDateTime = () => {
  40. const now = new Date();
  41. currentTime.value = `${padZero(now.getHours())}:${padZero(now.getMinutes())}:${padZero(now.getSeconds())}`;
  42. currentDate.value = `${now.getFullYear()}年${now.getMonth() + 1}月${now.getDate()}日 ${WEEKDAYS[now.getDay()]}`;
  43. };
  44. const handleLogout = async () => {
  45. try {
  46. await loginout();
  47. } catch (error) {
  48. console.error(error);
  49. } finally {
  50. global.logout();
  51. ElMessage.success("已退出登录");
  52. router.replace({ name: "login" });
  53. }
  54. };
  55. onMounted(() => {
  56. updateDateTime();
  57. timer = window.setInterval(updateDateTime, 1000);
  58. });
  59. onBeforeUnmount(() => {
  60. if (timer) {
  61. clearInterval(timer);
  62. timer = null;
  63. }
  64. });
  65. </script>
  66. <style scoped lang="scss">
  67. .app-header {
  68. box-sizing: border-box;
  69. flex-shrink: 0;
  70. display: flex;
  71. align-items: center;
  72. justify-content: space-between;
  73. padding: 17px 40px;
  74. background: linear-gradient(135deg, #238be5 60%);
  75. box-shadow: 0px 4px 12px 0px rgba(34, 125, 205, 0.1);
  76. border-radius: 0;
  77. }
  78. .app-header-title {
  79. margin: 0;
  80. font-family: PingFang SC, PingFang SC;
  81. font-weight: 600;
  82. font-size: 28px;
  83. color: #ffffff;
  84. line-height: 39px;
  85. letter-spacing: 2px;
  86. text-align: left;
  87. }
  88. .app-header-right {
  89. display: flex;
  90. align-items: center;
  91. flex-shrink: 0;
  92. gap: 40px;
  93. }
  94. .app-header-datetime {
  95. display: flex;
  96. flex-direction: column;
  97. }
  98. .app-header-time {
  99. font-family: PingFang SC, PingFang SC;
  100. font-weight: 500;
  101. font-size: 15px;
  102. color: #ffffff;
  103. line-height: 21px;
  104. text-align: right;
  105. }
  106. .app-header-date {
  107. font-family: PingFang SC, PingFang SC;
  108. font-weight: 400;
  109. font-size: 14px;
  110. color: rgba(255, 255, 255, 0.8);
  111. line-height: 20px;
  112. text-align: right;
  113. font-style: normal;
  114. }
  115. .app-header-divider {
  116. width: 1px;
  117. height: 32px;
  118. background: rgba(255, 255, 255, 0.6);
  119. }
  120. .app-header-user {
  121. display: flex;
  122. align-items: center;
  123. gap: 8px;
  124. }
  125. .app-header-avatar {
  126. box-sizing: border-box;
  127. display: inline-flex;
  128. align-items: center;
  129. justify-content: center;
  130. width: 36px;
  131. height: 36px;
  132. background: #e6f8ff;
  133. border-radius: 18px;
  134. font-family: PingFang SC, PingFang SC;
  135. font-weight: 600;
  136. font-size: 16px;
  137. line-height: 22px;
  138. text-align: center;
  139. font-style: normal;
  140. text-transform: none;
  141. color: #238be5;
  142. }
  143. .app-header-username {
  144. font-family: PingFang SC, PingFang SC;
  145. font-weight: 500;
  146. font-size: 15px;
  147. color: rgba(255, 255, 255, 0.9);
  148. line-height: 21px;
  149. text-align: left;
  150. font-style: normal;
  151. }
  152. .app-header-logout {
  153. margin: 0;
  154. padding: 6px 16px;
  155. border-radius: 2px;
  156. border: 1px solid #ffffff;
  157. background: transparent;
  158. font-family: PingFang SC, PingFang SC;
  159. font-weight: 400;
  160. font-size: 14px;
  161. color: #ffffff;
  162. line-height: 20px;
  163. text-align: left;
  164. cursor: pointer;
  165. }
  166. </style>