index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <template>
  2. <div class="header-view">
  3. <div class="page-header-view">
  4. <div class="header">
  5. <div class="left">
  6. <div class="logo" @click="goHome">
  7. <img src="@/assets/images/common/logo.svg" alt="Vitiens" class="logo-icon" />
  8. <img src="@/assets/images/common/Vitiens.svg" alt="Vitiens" class="logo-text" />
  9. </div>
  10. </div>
  11. <div class="header-right">
  12. <lang-popover ref="langPopoverRef" />
  13. <!-- 主题切换按钮 -->
  14. <div class="theme-toggle-container" @click.stop="onToggleThemePopover">
  15. <div class="theme-toggle-btn">
  16. <!-- 暗黑主题:月牙 -->
  17. <svg v-if="currentTheme === 'dark'" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="theme-icon">
  18. <path d="M12 3a9 9 0 1 0 9 9c0-.46-.04-.92-.1-1.36a5.389 5.389 0 0 1-4.4 2.26 5.403 5.403 0 0 1-3.14-9.8c-.44-.06-.9-.1-1.36-.1z" fill="currentColor"/>
  19. </svg>
  20. <!-- 蓝白/浅色主题:太阳 -->
  21. <svg v-else viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" class="theme-icon">
  22. <circle cx="12" cy="12" r="5" fill="currentColor"/>
  23. <line x1="12" y1="1" x2="12" y2="3" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  24. <line x1="12" y1="21" x2="12" y2="23" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  25. <line x1="4.22" y1="4.22" x2="5.64" y2="5.64" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  26. <line x1="18.36" y1="18.36" x2="19.78" y2="19.78" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  27. <line x1="1" y1="12" x2="3" y2="12" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  28. <line x1="21" y1="12" x2="23" y2="12" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  29. <line x1="4.22" y1="19.78" x2="5.64" y2="18.36" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  30. <line x1="18.36" y1="5.64" x2="19.78" y2="4.22" stroke="currentColor" stroke-width="2" stroke-linecap="round"/>
  31. </svg>
  32. </div>
  33. <!-- 主题选择下拉 -->
  34. <div v-if="isThemePopoverOpen" class="theme-popover">
  35. <div class="theme-popover-title">{{ $t('theme.title') || '外观设置' }}</div>
  36. <div
  37. v-for="item in themeList"
  38. :key="item.key"
  39. class="theme-option"
  40. :class="{ selected: currentTheme === item.key }"
  41. @click.stop="onSelectTheme(item.key)"
  42. >
  43. <div class="theme-dot" :class="'dot-' + item.key"></div>
  44. <span>{{ $t(item.labelKey) }}</span>
  45. <span v-if="currentTheme === item.key" class="check-mark">&#10003;</span>
  46. </div>
  47. </div>
  48. </div>
  49. <div class="avatar-dropdown-container" @click="onToggleAvatarDropdown">
  50. <img :src="avatarSrc" class="icon icon-avatar" />
  51. <img src="@/assets/images/common/icon_down.svg" class="icon icon-down" />
  52. <div v-if="isAvatarDropdownOpen" class="dropdown-container">
  53. <!-- 用户信息区 -->
  54. <div class="user-profile-section">
  55. <img :src="avatarSrc" class="profile-avatar" />
  56. <div class="profile-info">
  57. <div class="profile-name">{{ userInfo?.nickname || userInfo?.username || 'User' }}</div>
  58. <div class="profile-uid" @click="onCopy">
  59. <span>ID: {{ userInfo?.uid }}</span>
  60. <img src="@/assets/images/common/copy-icon.svg" class="copy-icon" />
  61. </div>
  62. </div>
  63. </div>
  64. <!-- 余额显示 -->
  65. <div class="balance-section">
  66. <div class="balance-label">{{ $t('finance.balance') }}</div>
  67. <div class="balance-value">{{ userInfo?.balance ?? '0.00' }}</div>
  68. </div>
  69. <!-- 基本信息 -->
  70. <div class="info-section">
  71. <div class="info-item">
  72. <span class="info-label">{{ $t('user.region') }}</span>
  73. <span class="info-value">{{ userInfo?.region || '--' }}</span>
  74. </div>
  75. <div class="info-item">
  76. <span class="info-label">{{ $t('user.age') }}</span>
  77. <span class="info-value">{{ userInfo?.age || '--' }}</span>
  78. </div>
  79. <div class="info-item">
  80. <span class="info-label">{{ $t('user.gender') }}</span>
  81. <span class="info-value">{{ genderText }}</span>
  82. </div>
  83. </div>
  84. <!-- 退出登录 -->
  85. <div class="logout-wrapper" @click="onLogout">
  86. <img src="@/assets/images/common/exit.svg" />
  87. <span>{{ $t("auth.logout") }}</span>
  88. </div>
  89. </div>
  90. </div>
  91. </div>
  92. </div>
  93. </div>
  94. </div>
  95. </template>
  96. <script setup lang="ts">
  97. import LangPopover from "@/components/LangPopover/index.vue";
  98. import { computed, onMounted, onUnmounted, ref, watch } from "vue";
  99. import { useRouter } from "vue-router";
  100. import { useI18n } from "vue-i18n";
  101. import { copyText } from "@/utils/utils";
  102. import { useGlobalStore } from "@/store/modules/globalStore";
  103. import { useThemeStore } from "@/store/modules/themeStore";
  104. import type { ThemeType } from "@/store/modules/themeStore";
  105. import { requestGetUserInfo } from "@/api";
  106. import defaultAvatar from "@/assets/images/common/logo.svg";
  107. const { t } = useI18n();
  108. const router = useRouter();
  109. const userInfo = ref<UserInfo>();
  110. const avatarSrc = computed(() => userInfo.value?.avatar || defaultAvatar);
  111. // 主题相关
  112. const themeStore = useThemeStore();
  113. const currentTheme = computed(() => themeStore.currentTheme);
  114. const themeList = themeStore.themeList;
  115. const isThemePopoverOpen = ref(false);
  116. const onToggleThemePopover = () => {
  117. isThemePopoverOpen.value = !isThemePopoverOpen.value;
  118. // 关闭其他弹出
  119. isAvatarDropdownOpen.value = false;
  120. langPopoverRef.value?.close();
  121. };
  122. const onSelectTheme = (theme: ThemeType) => {
  123. themeStore.setTheme(theme);
  124. isThemePopoverOpen.value = false;
  125. };
  126. const genderText = computed(() => {
  127. const g = userInfo.value?.gender;
  128. if (g === 1) return t('user.male');
  129. if (g === 2) return t('user.female');
  130. return '--';
  131. });
  132. const isAvatarDropdownOpen = ref(false);
  133. const langPopoverRef = ref<typeof LangPopover>(null);
  134. // 头像下拉框相关方法
  135. const onToggleAvatarDropdown = () => {
  136. isAvatarDropdownOpen.value = !isAvatarDropdownOpen.value;
  137. isThemePopoverOpen.value = false;
  138. langPopoverRef.value?.close();
  139. };
  140. const onCopy = async () => copyText(userInfo.value?.uid);
  141. const onLogout = () => {
  142. isAvatarDropdownOpen.value = false;
  143. localStorage.removeItem("token");
  144. router.replace("/home");
  145. window.location.reload();
  146. };
  147. const goHome = () => {
  148. router.push('/');
  149. };
  150. // 点击外部区域关闭下拉框
  151. const handleClickOutside = (event: Event) => {
  152. const target = event.target as HTMLElement;
  153. if (!target.closest(".avatar-dropdown-container")) {
  154. isAvatarDropdownOpen.value = false;
  155. }
  156. if (!target.closest(".theme-toggle-container")) {
  157. isThemePopoverOpen.value = false;
  158. }
  159. };
  160. const getUserInfo = async () => {
  161. const res = await requestGetUserInfo();
  162. userInfo.value = res.data.user || res.data;
  163. };
  164. const globalStore = useGlobalStore();
  165. const isLoggedIn = computed(() => globalStore.isLoggedIn);
  166. watch(
  167. () => isLoggedIn.value,
  168. newVal => {
  169. if (newVal) {
  170. getUserInfo();
  171. }
  172. }
  173. );
  174. onMounted(() => {
  175. if (isLoggedIn.value) {
  176. getUserInfo();
  177. }
  178. });
  179. onMounted(() => {
  180. document.addEventListener("click", handleClickOutside);
  181. });
  182. onUnmounted(() => {
  183. document.removeEventListener("click", handleClickOutside);
  184. });
  185. </script>
  186. <style lang="scss" scoped>
  187. .header-view {
  188. max-width: 500px;
  189. margin: 0 auto;
  190. position: fixed;
  191. top: 0;
  192. left: 0;
  193. right: 0;
  194. z-index: 50;
  195. background: var(--bg-header, #161617);
  196. box-shadow: var(--shadow-header, none);
  197. height: min(11.2vw, 53.76px);
  198. }
  199. .page-header-view {
  200. height: 100%;
  201. .header {
  202. height: 100%;
  203. padding: 0 min(4.267vw, 20.48px);
  204. display: flex;
  205. align-items: center;
  206. justify-content: space-between;
  207. .left {
  208. display: flex;
  209. align-items: center;
  210. height: 100%;
  211. .logo {
  212. display: flex;
  213. align-items: center;
  214. font-weight: 700;
  215. font-size: min(6.24vw, 29.9px);
  216. letter-spacing: 0.5px;
  217. cursor: pointer;
  218. .logo-icon {
  219. width: min(7vw, 32px);
  220. height: min(7vw, 32px);
  221. margin-right: min(1.5vw, 6px);
  222. }
  223. .logo-text {
  224. height: min(5vw, 24px);
  225. }
  226. }
  227. }
  228. .header-right {
  229. display: flex;
  230. align-items: center;
  231. height: 100%;
  232. gap: 4px;
  233. }
  234. .icon-avatar {
  235. margin-left: 7px;
  236. width: min(8vw, 38.4px);
  237. height: min(8vw, 38.4px);
  238. border-radius: 50%;
  239. object-fit: cover;
  240. }
  241. .icon-down {
  242. margin-left: 7px;
  243. }
  244. }
  245. // ======== 主题切换按钮 ========
  246. .theme-toggle-container {
  247. position: relative;
  248. display: flex;
  249. align-items: center;
  250. .theme-toggle-btn {
  251. width: min(8vw, 34px);
  252. height: min(8vw, 34px);
  253. border-radius: 50%;
  254. display: flex;
  255. align-items: center;
  256. justify-content: center;
  257. cursor: pointer;
  258. background: var(--bg-theme-toggle, #2a2a2c);
  259. transition: transform 0.2s ease, background 0.3s ease;
  260. &:hover {
  261. transform: scale(1.1);
  262. }
  263. &:active {
  264. transform: scale(0.95);
  265. }
  266. .theme-icon {
  267. width: min(4.5vw, 18px);
  268. height: min(4.5vw, 18px);
  269. color: var(--color-theme-toggle-icon, #ffc300);
  270. }
  271. }
  272. .theme-popover {
  273. position: absolute;
  274. top: calc(100% + 8px);
  275. right: 0;
  276. width: 160px;
  277. border-radius: 12px;
  278. padding: 8px;
  279. z-index: 200;
  280. background: var(--bg-popover, #1e1e20);
  281. border: 1px solid var(--border-color-light, #3c3c3c);
  282. box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
  283. animation: popoverSlideIn 0.2s ease;
  284. .theme-popover-title {
  285. font-size: 11px;
  286. font-weight: 600;
  287. padding: 4px 8px 8px;
  288. color: var(--text-secondary, #999);
  289. opacity: 0.7;
  290. }
  291. .theme-option {
  292. display: flex;
  293. align-items: center;
  294. gap: 10px;
  295. padding: 10px;
  296. border-radius: 8px;
  297. cursor: pointer;
  298. font-size: 13px;
  299. font-weight: 500;
  300. color: var(--text-primary, #ccc);
  301. transition: background 0.2s;
  302. &:hover {
  303. background: var(--bg-hover, #2a2a2c);
  304. }
  305. &.selected {
  306. background: var(--bg-hover, #2a2a2c);
  307. color: var(--color-accent, #ffc300);
  308. .check-mark {
  309. display: inline;
  310. color: var(--color-accent, #ffc300);
  311. }
  312. }
  313. .theme-dot {
  314. width: 22px;
  315. height: 22px;
  316. border-radius: 50%;
  317. flex-shrink: 0;
  318. &.dot-dark {
  319. background: linear-gradient(135deg, #161617, #2a2a2c);
  320. border: 2px solid #ffc300;
  321. }
  322. &.dot-blue {
  323. background: linear-gradient(135deg, #1a56db, #1a73e8);
  324. border: 2px solid #1a73e8;
  325. }
  326. &.dot-light {
  327. background: linear-gradient(135deg, #f7f8fa, #ffffff);
  328. border: 2px solid #ddd;
  329. }
  330. }
  331. .check-mark {
  332. margin-left: auto;
  333. font-size: 14px;
  334. display: none;
  335. }
  336. }
  337. }
  338. }
  339. // ======== 头像下拉 ========
  340. .avatar-dropdown-container {
  341. display: flex;
  342. align-items: center;
  343. position: relative;
  344. .dropdown-container {
  345. position: absolute;
  346. top: 150%;
  347. right: 0;
  348. width: 220px;
  349. background: var(--bg-popover, #1e1e20);
  350. border: 1px solid var(--border-color-light, #3c3c3c);
  351. border-radius: 12px;
  352. display: flex;
  353. flex-direction: column;
  354. padding: 12px;
  355. gap: 8px;
  356. z-index: 50;
  357. box-shadow: var(--shadow-card, 0px 2px 12px 0px rgba(0, 0, 0, 0.4));
  358. // 用户信息区
  359. .user-profile-section {
  360. display: flex;
  361. align-items: center;
  362. gap: 10px;
  363. padding-bottom: 10px;
  364. border-bottom: 1px solid var(--border-color, #2a2a2c);
  365. .profile-avatar {
  366. width: 42px;
  367. height: 42px;
  368. border-radius: 50%;
  369. object-fit: cover;
  370. border: 2px solid var(--color-accent, #e8a820);
  371. flex-shrink: 0;
  372. }
  373. .profile-info {
  374. flex: 1;
  375. min-width: 0;
  376. .profile-name {
  377. font-size: 14px;
  378. font-weight: 600;
  379. color: var(--text-primary, #fff);
  380. white-space: nowrap;
  381. overflow: hidden;
  382. text-overflow: ellipsis;
  383. }
  384. .profile-uid {
  385. display: flex;
  386. align-items: center;
  387. gap: 4px;
  388. margin-top: 2px;
  389. cursor: pointer;
  390. span {
  391. font-size: 11px;
  392. color: var(--text-tertiary, #828694);
  393. }
  394. .copy-icon {
  395. width: 12px;
  396. height: 12px;
  397. opacity: 0.6;
  398. }
  399. }
  400. }
  401. }
  402. // 余额显示
  403. .balance-section {
  404. background: var(--bg-balance, linear-gradient(135deg, #2a2520 0%, #1e1e20 100%));
  405. border: 1px solid var(--border-balance, #3a3020);
  406. border-radius: 8px;
  407. padding: 10px 12px;
  408. display: flex;
  409. justify-content: space-between;
  410. align-items: center;
  411. .balance-label {
  412. font-size: 12px;
  413. color: var(--text-tertiary, #828694);
  414. }
  415. .balance-value {
  416. font-size: 18px;
  417. font-weight: 700;
  418. color: var(--color-accent, #e8a820);
  419. }
  420. }
  421. // 基本信息
  422. .info-section {
  423. display: flex;
  424. flex-direction: column;
  425. gap: 6px;
  426. padding: 6px 0;
  427. border-bottom: 1px solid var(--border-color, #2a2a2c);
  428. .info-item {
  429. display: flex;
  430. justify-content: space-between;
  431. align-items: center;
  432. padding: 2px 4px;
  433. .info-label {
  434. font-size: 12px;
  435. color: var(--text-tertiary, #828694);
  436. }
  437. .info-value {
  438. font-size: 12px;
  439. color: var(--text-primary, #d0d0d0);
  440. }
  441. }
  442. }
  443. // 退出登录
  444. .logout-wrapper {
  445. display: flex;
  446. align-items: center;
  447. padding: 8px 4px;
  448. gap: 6px;
  449. cursor: pointer;
  450. color: var(--text-tertiary, #828694);
  451. font-size: 13px;
  452. border-radius: 6px;
  453. transition: background 0.2s;
  454. &:hover {
  455. background: var(--bg-hover, #232325);
  456. }
  457. img {
  458. width: 16px;
  459. height: 16px;
  460. }
  461. }
  462. }
  463. }
  464. }
  465. @keyframes popoverSlideIn {
  466. from {
  467. opacity: 0;
  468. transform: translateY(-6px) scale(0.96);
  469. }
  470. to {
  471. opacity: 1;
  472. transform: translateY(0) scale(1);
  473. }
  474. }
  475. </style>