navbar.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <div>
  3. <div class="stickyBox py-5">
  4. <div class="default-width d-flex align-center justify-space-between">
  5. <div class="header-link d-flex">
  6. <span class="cursor-pointer d-flex align-center ml-8" :class="{'active-route' : isActive('/mall')}" @click="router.push('/mall')">
  7. <v-icon size="24" class="mr-2">mdi-shopping-outline</v-icon>
  8. 首页
  9. </span>
  10. <span class="cursor-pointer d-flex align-center ml-8" :class="{'active-route' : isActive('/mall/user', true)}" @click="handleTo('/mall/user')">
  11. <v-icon size="24" class="mr-2">mdi-account-circle-outline</v-icon>
  12. 我的
  13. </span>
  14. <!-- <span class="cursor-pointer d-flex align-center ml-8" @click="emit('pointExchange')">
  15. <v-icon>mdi-octagram-outline</v-icon>
  16. 积分兑换
  17. </span> -->
  18. <!-- <span class="cursor-pointer d-flex align-center ml-8" :class="{'active-route' : isActive('/mall/order')}" @click="handleTo('/mall/user/order')">
  19. <v-icon size="20" class="mr-2">mdi-order-bool-ascending</v-icon>
  20. 我的订单
  21. </span> -->
  22. <span class="cursor-pointer d-flex align-center ml-8" :class="{'active-route' : isActive('/mall/cart')}" @click="handleTo('/mall/cart')">
  23. <v-icon size="20" class="mr-2">mdi-cart-outline</v-icon>
  24. 购物车
  25. </span>
  26. </div>
  27. <div v-if="!props.hideSearch" class="search d-flex align-center">
  28. <v-text-field
  29. v-model="inputVal"
  30. placeholder="请输入商品关键词"
  31. color="primary"
  32. variant="plain"
  33. density="compact"
  34. clearable
  35. :hide-details="true"
  36. class="ml-3 px-2"
  37. style="height: 100%; line-height: 100%;"
  38. @keyup.enter="handleSearch"
  39. ></v-text-field>
  40. <v-btn class="searchBtn" prepend-icon="mdi-shopping-search-outline" @click="handleSearch">搜索</v-btn>
  41. </div>
  42. </div>
  43. </div>
  44. <!-- 快速登录 -->
  45. <loginPage v-if="showLogin" @loginSuccess="loginSuccess" @close="loginClose"></loginPage>
  46. </div>
  47. </template>
  48. <script setup>
  49. defineOptions({ name: 'mall-navbar'})
  50. import { computed, ref } from 'vue'
  51. import { useRoute } from 'vue-router'; const route = useRoute()
  52. import { useRouter } from 'vue-router'; const router = useRouter()
  53. import { getToken } from '@/utils/auth'
  54. import Snackbar from '@/plugins/snackbar'
  55. import loginPage from '@/views/common/loginDialog.vue'
  56. import { getPrizeByGoodsId } from '@/api/mall/prize'
  57. const emit = defineEmits(['login', 'pointExchange', 'search'])
  58. const props = defineProps({
  59. hideSearch: {
  60. type: Boolean,
  61. default: false,
  62. },
  63. })
  64. const isActive = computed(() => (path, hasChild) => {
  65. const currentPath = router.currentRoute.value.path
  66. return hasChild ? currentPath.includes(path) : currentPath === path
  67. })
  68. const showLogin = ref(false)
  69. const handleTo = (path) => {
  70. if (!getToken()) {
  71. Snackbar.warning('请先登录')
  72. showLogin.value = true // 打开快速登录弹窗
  73. return
  74. }
  75. router.push(path)
  76. }
  77. const open = false
  78. // 更新路由进行搜素
  79. const inputVal = ref(route?.query?.keyWord || '')
  80. const handleSearch = () => {
  81. if (!open) return
  82. const path = '/mall/goodsList'
  83. router.push({ path, query: { keyWord: inputVal.value } })
  84. emit('search', inputVal.value)
  85. }
  86. // 快速登录
  87. const loginSuccess = () => {
  88. showLogin.value = false
  89. Snackbar.success('登录成功')
  90. }
  91. const loginClose = () => {
  92. showLogin.value = false
  93. Snackbar.warning('您已取消登录')
  94. }
  95. </script>
  96. <style scoped lang="scss">
  97. .stickyBox {
  98. position: sticky;
  99. top: 48px;
  100. z-index: 999;
  101. background-color: #fff;
  102. }
  103. .active-route {
  104. position: relative;
  105. color: var(--v-primary-base);
  106. &::before {
  107. content: '';
  108. width: 100%;
  109. height: 3px;
  110. position: absolute;
  111. bottom: -8px;
  112. left: 0;
  113. background-color: var(--v-primary-base);
  114. }
  115. }
  116. .header-link span:hover {
  117. color: var(--v-primary-base);
  118. }
  119. .search {
  120. height: 50px;
  121. width: 350px;
  122. border: 2px solid var(--v-primary-base);
  123. border-radius: 5px;
  124. .searchBtn {
  125. width: 100px;
  126. height: 49px;
  127. line-height: 48px;
  128. text-align: center;
  129. font-size: 18px;
  130. color: #fff;
  131. background-color: var(--v-primary-base);
  132. cursor: pointer;
  133. }
  134. }
  135. </style>