商品分页完善,首页高亮显示所在界面
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { ref, computed } from "vue";
|
import { ref, computed } from "vue";
|
||||||
import { useRouter } from "vue-router";
|
import { useRouter, useRoute } from "vue-router";
|
||||||
import { useUserStore } from "@/stores/UserStore";
|
import { useUserStore } from "@/stores/UserStore";
|
||||||
import { ElMessage } from "element-plus";
|
import { ElMessage } from "element-plus";
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
const route = useRoute(); // 添加路由实例
|
||||||
const userStore = useUserStore();
|
const userStore = useUserStore();
|
||||||
const isMobileMenuOpen = ref(false);
|
const isMobileMenuOpen = ref(false);
|
||||||
|
|
||||||
@@ -23,6 +24,20 @@ const userNickname = computed(() => {
|
|||||||
return userStore.userInfo?.nickname || userStore.userInfo?.username || "用户";
|
return userStore.userInfo?.nickname || userStore.userInfo?.username || "用户";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 计算属性:判断当前路由
|
||||||
|
const currentRoute = computed(() => {
|
||||||
|
return route.path;
|
||||||
|
});
|
||||||
|
|
||||||
|
// 检查当前路由是否匹配指定路径
|
||||||
|
const isActiveRoute = (path: string) => {
|
||||||
|
if (path === "/") {
|
||||||
|
// 对于首页,需要精确匹配或只匹配子路径
|
||||||
|
return currentRoute.value === "/" || currentRoute.value.startsWith("/home");
|
||||||
|
}
|
||||||
|
return currentRoute.value.startsWith(path);
|
||||||
|
};
|
||||||
|
|
||||||
// 跳转登录页
|
// 跳转登录页
|
||||||
const goToLogin = () => {
|
const goToLogin = () => {
|
||||||
router.push("/user/login");
|
router.push("/user/login");
|
||||||
@@ -83,27 +98,44 @@ const goToProductManage = () => {
|
|||||||
<nav class="hidden md:flex items-center space-x-8">
|
<nav class="hidden md:flex items-center space-x-8">
|
||||||
<a
|
<a
|
||||||
href="#"
|
href="#"
|
||||||
class="text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
>首页</a
|
'transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/') },
|
||||||
|
]"
|
||||||
|
@click.prevent="$router.push('/')"
|
||||||
>
|
>
|
||||||
|
首页
|
||||||
|
</a>
|
||||||
<a
|
<a
|
||||||
href="#"
|
href="#"
|
||||||
class="text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
|
'transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/product/') },
|
||||||
|
]"
|
||||||
@click.prevent="$router.push('/product/product')"
|
@click.prevent="$router.push('/product/product')"
|
||||||
>购买商品</a
|
|
||||||
>
|
>
|
||||||
|
购买商品
|
||||||
|
</a>
|
||||||
<a
|
<a
|
||||||
href="#"
|
href="#"
|
||||||
class="text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
|
'transition-colors',
|
||||||
|
{
|
||||||
|
'text-blue-600 font-medium': isActiveRoute('/shoppingcart/'),
|
||||||
|
},
|
||||||
|
]"
|
||||||
@click.prevent="$router.push('/shoppingcart/content')"
|
@click.prevent="$router.push('/shoppingcart/content')"
|
||||||
>购物车</a
|
|
||||||
>
|
>
|
||||||
<!-- 移除我的订单链接 -->
|
购物车
|
||||||
|
</a>
|
||||||
<!-- 管理员导航项 -->
|
<!-- 管理员导航项 -->
|
||||||
<a
|
<a
|
||||||
v-if="isAdmin"
|
v-if="isAdmin"
|
||||||
href="#"
|
href="#"
|
||||||
class="text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
|
'transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/admin') },
|
||||||
|
]"
|
||||||
@click.prevent="goToAdmin"
|
@click.prevent="goToAdmin"
|
||||||
>
|
>
|
||||||
管理界面
|
管理界面
|
||||||
@@ -178,30 +210,51 @@ const goToProductManage = () => {
|
|||||||
<div class="container mx-auto px-4 py-3 flex flex-col space-y-4">
|
<div class="container mx-auto px-4 py-3 flex flex-col space-y-4">
|
||||||
<a
|
<a
|
||||||
href="#"
|
href="#"
|
||||||
class="py-2 text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
>首页</a
|
'py-2 transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/') },
|
||||||
|
]"
|
||||||
|
@click.prevent="$router.push('/')"
|
||||||
>
|
>
|
||||||
|
首页
|
||||||
|
</a>
|
||||||
<a
|
<a
|
||||||
href="#"
|
href="#"
|
||||||
class="py-2 text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
|
'py-2 transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/product/') },
|
||||||
|
]"
|
||||||
@click.prevent="$router.push('/product/product')"
|
@click.prevent="$router.push('/product/product')"
|
||||||
>购买商品</a
|
|
||||||
>
|
>
|
||||||
|
购买商品
|
||||||
|
</a>
|
||||||
<a
|
<a
|
||||||
href="#"
|
href="#"
|
||||||
class="py-2 text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
|
'py-2 transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/shoppingcart/') },
|
||||||
|
]"
|
||||||
@click.prevent="$router.push('/shoppingcart/content')"
|
@click.prevent="$router.push('/shoppingcart/content')"
|
||||||
>购物车</a
|
|
||||||
>
|
>
|
||||||
|
购物车
|
||||||
|
</a>
|
||||||
<a
|
<a
|
||||||
href="#"
|
href="#"
|
||||||
class="py-2 text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
>我的订单</a
|
'py-2 transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/order/') },
|
||||||
|
]"
|
||||||
|
@click.prevent="$router.push('/order/content')"
|
||||||
>
|
>
|
||||||
|
我的订单
|
||||||
|
</a>
|
||||||
<a
|
<a
|
||||||
v-if="isAdmin"
|
v-if="isAdmin"
|
||||||
href="#"
|
href="#"
|
||||||
class="py-2 text-gray-600 hover:text-blue-600 transition-colors"
|
:class="[
|
||||||
|
'py-2 transition-colors',
|
||||||
|
{ 'text-blue-600 font-medium': isActiveRoute('/admin') },
|
||||||
|
]"
|
||||||
@click.prevent="goToAdmin"
|
@click.prevent="goToAdmin"
|
||||||
>
|
>
|
||||||
管理界面
|
管理界面
|
||||||
@@ -211,27 +264,45 @@ const goToProductManage = () => {
|
|||||||
<template v-if="isLoggedIn">
|
<template v-if="isLoggedIn">
|
||||||
<div class="py-2 text-gray-700">欢迎,{{ userNickname }}!</div>
|
<div class="py-2 text-gray-700">欢迎,{{ userNickname }}!</div>
|
||||||
<button
|
<button
|
||||||
|
:class="[
|
||||||
|
'w-full mb-2 px-4 py-2 text-left rounded-lg transition-colors',
|
||||||
|
{
|
||||||
|
'text-blue-600 bg-blue-50': isActiveRoute('/user/profile'),
|
||||||
|
},
|
||||||
|
]"
|
||||||
@click="goToProfile"
|
@click="goToProfile"
|
||||||
class="w-full mb-2 px-4 py-2 text-left text-blue-600 hover:bg-blue-50 rounded-lg transition-colors"
|
|
||||||
>
|
>
|
||||||
个人中心
|
个人中心
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
|
:class="[
|
||||||
|
'w-full mb-2 px-4 py-2 text-left rounded-lg transition-colors',
|
||||||
|
{
|
||||||
|
'text-blue-600 bg-blue-50': isActiveRoute('/user/product'),
|
||||||
|
},
|
||||||
|
]"
|
||||||
@click="goToProductManage"
|
@click="goToProductManage"
|
||||||
class="w-full mb-2 px-4 py-2 text-left text-blue-600 hover:bg-blue-50 rounded-lg transition-colors"
|
|
||||||
>
|
>
|
||||||
我的商品
|
我的商品
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
|
:class="[
|
||||||
|
'w-full mb-2 px-4 py-2 text-left rounded-lg transition-colors',
|
||||||
|
{
|
||||||
|
'text-blue-600 bg-blue-50': isActiveRoute('/user/address'),
|
||||||
|
},
|
||||||
|
]"
|
||||||
@click="goToAddress"
|
@click="goToAddress"
|
||||||
class="w-full mb-2 px-4 py-2 text-left text-blue-600 hover:bg-blue-50 rounded-lg transition-colors"
|
|
||||||
>
|
>
|
||||||
我的地址
|
我的地址
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
v-if="isAdmin"
|
v-if="isAdmin"
|
||||||
|
:class="[
|
||||||
|
'w-full mb-2 px-4 py-2 text-left rounded-lg transition-colors',
|
||||||
|
{ 'text-blue-600 bg-blue-50': isActiveRoute('/admin') },
|
||||||
|
]"
|
||||||
@click="goToAdmin"
|
@click="goToAdmin"
|
||||||
class="w-full mb-2 px-4 py-2 text-left text-blue-600 hover:bg-blue-50 rounded-lg transition-colors"
|
|
||||||
>
|
>
|
||||||
管理界面
|
管理界面
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
@@ -55,7 +55,8 @@ const fetchProductList = async () => {
|
|||||||
(product) => product.status === 1,
|
(product) => product.status === 1,
|
||||||
);
|
);
|
||||||
productList.value = activeProducts;
|
productList.value = activeProducts;
|
||||||
total.value = activeProducts.length;
|
// 使用API返回的总数量,而不是过滤后的数量
|
||||||
|
total.value = response.data.total || 0;
|
||||||
} else {
|
} else {
|
||||||
productList.value = [];
|
productList.value = [];
|
||||||
total.value = 0;
|
total.value = 0;
|
||||||
@@ -287,17 +288,32 @@ onMounted(() => {
|
|||||||
|
|
||||||
<!-- 搜索栏 -->
|
<!-- 搜索栏 -->
|
||||||
<div class="search-bar mb-8">
|
<div class="search-bar mb-8">
|
||||||
<div class="flex">
|
<div class="w-full">
|
||||||
<el-input
|
<div class="relative">
|
||||||
v-model="searchKeyword"
|
<el-input
|
||||||
placeholder="搜索商品..."
|
v-model="searchKeyword"
|
||||||
class="flex-1"
|
placeholder="搜索商品..."
|
||||||
@keyup.enter="handleSearch"
|
class="search-input"
|
||||||
>
|
size="large"
|
||||||
<template #append>
|
@keyup.enter="handleSearch"
|
||||||
<el-button :icon="Search" @click="handleSearch" />
|
>
|
||||||
</template>
|
<template #prefix>
|
||||||
</el-input>
|
<el-icon class="el-input__icon">
|
||||||
|
<search />
|
||||||
|
</el-icon>
|
||||||
|
</template>
|
||||||
|
<template #append>
|
||||||
|
<el-button
|
||||||
|
class="search-button"
|
||||||
|
type="primary"
|
||||||
|
size="large"
|
||||||
|
@click="handleSearch"
|
||||||
|
>
|
||||||
|
搜索
|
||||||
|
</el-button>
|
||||||
|
</template>
|
||||||
|
</el-input>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@@ -569,6 +585,36 @@ onMounted(() => {
|
|||||||
font-weight: normal;
|
font-weight: normal;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* 搜索框样式 */
|
||||||
|
.search-input {
|
||||||
|
border-radius: 50px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-input :deep(.el-input__wrapper) {
|
||||||
|
border-radius: 50px;
|
||||||
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
|
||||||
|
padding-left: 20px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-input :deep(.el-input__prefix) {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
margin-right: 8px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-input :deep(.el-input-group__append) {
|
||||||
|
border-radius: 50px;
|
||||||
|
margin-left: 8px; /* 增加间距 */
|
||||||
|
overflow: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.search-button {
|
||||||
|
border-radius: 50px;
|
||||||
|
padding: 0 30px;
|
||||||
|
font-weight: 500;
|
||||||
|
}
|
||||||
|
|
||||||
/* 滚动条样式 */
|
/* 滚动条样式 */
|
||||||
.category-container::-webkit-scrollbar {
|
.category-container::-webkit-scrollbar {
|
||||||
width: 6px;
|
width: 6px;
|
||||||
|
|||||||
Reference in New Issue
Block a user