cwg-submenu.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view class="cwg-submenu">
  3. <view class="submenu">
  4. <view class="cwg-submenu-item" :class="{ 'active': activePath === item.path }" v-for="item in submenuItems"
  5. :key="item.path" @click="handleClick(item)">
  6. <text v-t="item.label"></text>
  7. <cwg-icon v-if="item.isExternal" name="crm-fx" :size="20" color="#6c8595" />
  8. </view>
  9. </view>
  10. </view>
  11. </template>
  12. <script lang="ts" setup>
  13. import { computed, watch, nextTick } from 'vue';
  14. import { openExternalUrl } from '@/utils/openExternalUrl'
  15. import { useI18n } from 'vue-i18n'
  16. import { lang } from '@/composables/config'
  17. import { LANG_MAP } from '@/locale/index'
  18. const { locale } = useI18n()
  19. import useRouter from "@/hooks/useRouter";
  20. const router = useRouter();
  21. import useRoute from '@/hooks/useRoute'
  22. const route = useRoute()
  23. interface MenuItem {
  24. key: string;
  25. label: string;
  26. icon?: string;
  27. type?: string;
  28. lang?: string;
  29. path?: string;
  30. isExternal?: boolean;
  31. children?: MenuItem[];
  32. }
  33. const props = defineProps({
  34. submenuItems: {
  35. type: Array as () => MenuItem[],
  36. default: () => []
  37. }
  38. })
  39. const activePath = computed(() => route.path + (route.query?.type ? `?type=${route.query.type}` : '') || '')
  40. const emit = defineEmits(['submenu-click']);
  41. function handleClick(item: MenuItem) {
  42. if (item.type == 'lang') {
  43. handleMenuClick(item.lang)
  44. } else {
  45. if (item.isExternal) {
  46. openExternalUrl(item.path);
  47. } else if (route.path !== item.path) {
  48. router.push(item.path);
  49. }
  50. }
  51. }
  52. function handleMenuClick(a: string) {
  53. if (!a) return
  54. locale.value = a
  55. lang.value = a
  56. const localeValue = LANG_MAP[a] || 'zh-Hans'
  57. uni.setLocale(localeValue)
  58. }
  59. </script>
  60. <style scoped lang="scss">
  61. @import "@/uni.scss";
  62. .cwg-submenu {
  63. width: 100%;
  64. .submenu {
  65. width: 100%;
  66. display: flex;
  67. flex-direction: column;
  68. align-items: center;
  69. gap: px2rpx(8);
  70. padding: px2rpx(8) px2rpx(8) px2rpx(8) px2rpx(32);
  71. box-sizing: border-box;
  72. }
  73. .cwg-submenu-item {
  74. width: 100%;
  75. height: px2rpx(40);
  76. cursor: pointer;
  77. display: flex;
  78. align-items: center;
  79. justify-content: space-between;
  80. gap: 12px;
  81. padding: px2rpx(10);
  82. box-sizing: border-box;
  83. font-size: 14px;
  84. .tabler-icon-external-link {
  85. width: px2rpx(20);
  86. height: px2rpx(20);
  87. }
  88. &:hover {
  89. background: rgba(108, 133, 149, 0.12) !important;
  90. border: 1px solid rgb(145, 163, 176) !important;
  91. border-radius: px2rpx(4);
  92. }
  93. &.active {
  94. background: rgba(108, 133, 149, 0.12) !important;
  95. border-radius: px2rpx(4);
  96. }
  97. }
  98. }
  99. </style>