cwg-submenu.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. emit('submenu-click', item)
  52. }
  53. function handleMenuClick(a: string) {
  54. if (!a) return
  55. locale.value = a
  56. lang.value = a
  57. const localeValue = LANG_MAP[a] || 'zh-Hans'
  58. uni.setLocale(localeValue)
  59. }
  60. </script>
  61. <style scoped lang="scss">
  62. @import "@/uni.scss";
  63. .cwg-submenu {
  64. width: 100%;
  65. .submenu {
  66. width: 100%;
  67. display: flex;
  68. flex-direction: column;
  69. align-items: center;
  70. gap: px2rpx(8);
  71. padding: px2rpx(8) px2rpx(8) px2rpx(8) px2rpx(32);
  72. box-sizing: border-box;
  73. }
  74. .cwg-submenu-item {
  75. width: 100%;
  76. height: px2rpx(40);
  77. cursor: pointer;
  78. display: flex;
  79. align-items: center;
  80. justify-content: space-between;
  81. gap: 12px;
  82. padding: px2rpx(10);
  83. box-sizing: border-box;
  84. font-size: 14px;
  85. .tabler-icon-external-link {
  86. width: px2rpx(20);
  87. height: px2rpx(20);
  88. }
  89. &:hover {
  90. background: rgba(108, 133, 149, 0.12) !important;
  91. border: 1px solid rgb(145, 163, 176) !important;
  92. border-radius: px2rpx(4);
  93. }
  94. &.active {
  95. background: rgba(108, 133, 149, 0.12) !important;
  96. border-radius: px2rpx(4);
  97. }
  98. }
  99. }
  100. </style>