feat: 实现商品详情页、用户信息页面和搜索功能

refactor: 重构商品列表和主页布局

style: 优化UI组件样式和交互效果

docs: 更新类型定义和路由配置

fix: 修复购物车商品重复问题

perf: 提升页面加载性能和响应速度

test: 添加商品详情页和搜索功能的测试用例

build: 更新依赖项以支持新功能

chore: 清理无用代码和文件
This commit is contained in:
qingfeng1121
2026-01-08 16:13:40 +08:00
parent 0f89705f94
commit 0c07d33bf9
17 changed files with 3964 additions and 127 deletions

View File

@@ -2,26 +2,88 @@
<div id="header">
<div id="header-profile">
<div id="header-profile-left">
<!-- 登录 注册 -->
<Button type="primary" @click="router.push('/login')">登录</Button>
<Button type="primary" @click="router.push('/register')">注册</Button>
<!-- 用户信息悬浮菜单 -->
<Dropdown :menu="{ items: userMenu }" trigger="hover">
<div class="user-info-container">
<Avatar size="large" src="https://api.dicebear.com/7.x/avataaars/svg?seed=user123" />
<span class="user-name">用户名</span>
<span class="user-arrow"></span>
</div>
</Dropdown>
</div>
<div id="header-profile-right">
<!-- 购物车 个人中心 -->
<!-- 右侧功能菜单 -->
<Dropdown :menu="{ items: rightMenu }" trigger="hover">
<Button type="text">更多</Button>
</Dropdown>
<a href="/">首页</a>
<Button type="primary" @click="router.push('/cart')">购物车</Button>
<Button type="primary" @click="router.push('/user')">个人中心</Button>
<Button type="primary" @click="router.push('/order')">订单</Button>
</div>
</div>
<Row id="header-nav-row">
<Col :span="4">
<Row id="header-nav-row" v-if="booleanSearch">
<Col class="header-nav-Logo" :span="4">
<a href="/">TaoTaoWang</a>
</Col>
<Col :span="16">
<a-input-search v-model:value="value" placeholder="请输入" style="width: 200px"
@search="onSearch" />
<Col class="header-nav-search" :span="16">
<div class="search-input-container" :class="{ 'search-focused': showHistory }">
<div class="search-wrapper">
<!-- 搜索类型选择 -->
<div class="search-type-selector">
<ul>
<li class="search-type-item active" @click="setSearchType('宝贝')">宝贝</li>
<li class="search-type-item" @click="setSearchType('店铺')">店铺</li>
</ul>
</div>
<!-- 搜索输入区域 -->
<div class="search-input-wrapper">
<div class="search-input-prefix">
<i class="iconfont icon-sousuo prefix-icon"></i>
</div>
<Input
v-model:value="searchValue"
placeholder="请输入搜索关键词"
style="width: 100%"
@search="onSearch"
@focus="onInputFocus"
@blur="onInputBlur"
class="custom-search-input"
/>
<div class="search-input-suffix" v-if="searchValue">
<i class="iconfont icon-guanbi" @click="clearSearch"></i>
</div>
</div>
<!-- 搜索按钮 -->
<div class="search-button-container" @onclick="onSearch">
<span class="search-button-text">搜索</span>
<div class="search-button-icon">
<i class="iconfont icon-sousuo"></i>
</div>
</div>
</div>
</div>
<div class="search-history-container" v-if="showHistory">
<div class="search-history-header">
<span class="history-title">搜索历史</span>
<a class="clear-history" @click="clearHistory" href="javascript:void(0)">
清空历史
</a>
</div>
<div class="search-history-tags" v-if="historyList.length > 0">
<Tag v-for="(item, index) in historyList" :key="index" class="history-tag" @click="onClickHistory(item)">
{{ item }}
</Tag>
</div>
<div class="no-history" v-else>
暂无搜索历史
</div>
</div>
</Col>
<Col :span="4">
<a href="/product">商品列表</a>
<Col class="header-nav-product" :span="4">
<a href="#">随便放点东西显得对称</a>
<!-- 其他页面的时候修改布局 -->
</Col>
</Row>
</div>
@@ -30,31 +92,157 @@
<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Button, Col, Row ,Space } from 'ant-design-vue';
import { Button, Col, Row, Space, Dropdown, Avatar, Menu, Divider, Tag } from 'ant-design-vue';
const router = useRouter()
// 定义搜索框绑定的变量
const value = ref('')
const searchValue = ref('')
// 控制历史记录显示状态
const showHistory = ref(false)
// 标志:是否正在清空历史记录
const isClearingHistory = ref(false)
// 标志:是否在搜索界面
const booleanSearch = ref(true)
// 路由事件
router.beforeEach((to, from, next) => {
if (to.name === 'search' && to.query.keyword) {
searchValue.value = to.query.keyword as string
}
console.log(to.name)
// 如果在商品页面 隐藏搜索框
if (to.name === 'productDetail') {
booleanSearch.value = false
} else {
booleanSearch.value = true
}
next()
})
// 定义搜索框的搜索事件
const onSearch = (value: string) => {
console.log('搜索关键词:', value)
// 跳转到搜索结果页面
if (value.trim()) {
// 添加到历史记录
if (!historyList.value.includes(value)) {
historyList.value.unshift(value)
// 限制历史记录数量
if (historyList.value.length > 10) {
historyList.value.pop()
}
}
// 判断是否在搜索界面 如果不在 则跳转到搜索界面 如果在搜索界面 则更新查询参数
if (router.currentRoute.value.name !== 'search') {
router.push({ name: 'search', query: { keyword: value } })
} else {
router.push({ name: 'search', query: { keyword: value } })
}
}
}
// 搜索历史记录
const historyList = ref(['手机', '电脑', '耳机', '键盘', '鼠标'])
// 清空历史记录
const clearHistory = () => {
isClearingHistory.value = true
historyList.value = []
// 300ms后重置标志确保onInputBlur的200ms延迟执行完毕
setTimeout(() => {
isClearingHistory.value = false
}, 300)
}
// 点击历史记录
const onClickHistory = (item: string) => {
searchValue.value = item
onSearch(item)
}
// 搜索框获得焦点
const onInputFocus = () => {
showHistory.value = true
}
// 搜索框失去焦点
const onInputBlur = () => {
// 使用setTimeout确保点击历史记录的事件能先执行
setTimeout(() => {
// 如果正在清空历史记录,则不隐藏历史记录容器
if (!isClearingHistory.value) {
showHistory.value = false
}
}, 200)
}
// 设置搜索类型
const setSearchType = (type: string) => {
const typeItems = document.querySelectorAll('.search-type-item')
typeItems.forEach(item => {
item.classList.remove('active')
})
// 添加active类到点击的元素
// event?.currentTarget?.classList.add('active')
console.log('搜索类型:', type)
}
// 清除搜索输入
const clearSearch = () => {
searchValue.value = ''
console.log('已清除搜索输入')
}
// 左侧用户菜单
const userMenu = [
{
key: '1',
label: '个人中心'
},
{
key: '2',
label: '设置'
},
{
key: '3',
label: '退出登录',
danger: true
}
]
// 右侧功能菜单
const rightMenu = [
{
key: '1',
label: '浏览记录'
},
{
key: '2',
label: '收藏夹'
},
{
key: '3',
label: '帮助中心'
}
]
</script>
<style scoped>
#header {
width: 100%;
background-color: #f5f5f5;
padding: 0 20px;
background-color: #ffffff;
padding: 10px 20px;
/* box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1); */
position: relative;
z-index: 1000;
}
h1 {
color: #42b983;
#header-nav-row {
height: 60px;
align-items: center;
}
#header-profile {
display: flex;
justify-content: space-between;
align-items: center;
height: 60px;
padding: 10px 0;
}
#header-profile-left {
@@ -63,17 +251,512 @@ h1 {
align-items: center;
}
#header-profile-left>button {
margin-right: 10px;
}
#header-profile-right {
display: flex;
justify-content: flex-end;
align-items: center;
gap: 10px;
}
#header-profile-right>button {
margin-left: 10px;
/* 用户信息容器样式 */
.user-info-container {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 12px;
border-radius: 20px;
cursor: pointer;
transition: all 0.3s ease;
}
.user-info-container:hover {
background-color: #f0f2f5;
}
.user-name {
font-size: 14px;
font-weight: 500;
color: #333;
}
.user-arrow {
font-size: 12px;
color: #999;
transition: transform 0.3s ease;
}
.user-info-container:hover .user-arrow {
transform: rotate(180deg);
}
/* Logo样式 */
.header-nav-Logo a {
font-size: 24px;
font-weight: bold;
color: #1890ff;
text-decoration: none;
transition: color 0.3s ease;
}
.header-nav-Logo a:hover {
color: #40a9ff;
}
/* 搜索框样式 */
.header-nav-search {
padding: 0 20px;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-items: flex-start;
position: relative;
width: 100%;
max-width: 700px;
margin: 0 auto;
}
.search-input-container {
width: 100%;
display: block;
justify-content: center;
align-items: center;
position: relative;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
.search-input-container.search-focused {
/* box-shadow: 0 4px 16px rgba(255, 80, 0, 0.2); */
}
/* 搜索框整体布局 */
.search-wrapper {
display: flex;
align-items: center;
background-color: #ffffff;
border: 2px solid #e8e8e8;
border-radius: 28px;
overflow: hidden;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
backdrop-filter: blur(10px);
}
.search-input-container.search-focused .search-wrapper {
border-color: #ff5000;
box-shadow: 0 4px 20px rgba(255, 80, 0, 0.2);
transform: translateY(-1px);
}
/* 搜索类型选择器 */
.search-type-selector {
padding: 3.5px 16px;
border-right: 1px solid #e8e8e8;
background-color: #fafafa;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
border-radius: 28px 0 0 28px;
}
.search-type-selector:hover {
background-color: #f5f5f5;
}
.search-type-selector ul {
list-style: none;
padding: 0;
margin: 0;
display: flex;
gap: 20px;
}
.search-type-item {
padding: 14px 0;
font-size: 14px;
font-weight: 500;
color: #666;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
position: relative;
user-select: none;
letter-spacing: 0.5px;
}
.search-type-item:hover {
color: #ff5000;
transform: translateY(-1px);
}
.search-type-item.active {
color: #ff5000;
font-weight: 600;
}
.search-type-item.active::after {
content: '';
position: absolute;
bottom: 8px;
left: 0;
width: 100%;
height: 2px;
background-color: #ff5000;
border-radius: 1px;
animation: slideIn 0.3s ease-out;
}
@keyframes slideIn {
from {
width: 0;
left: 50%;
}
to {
width: 100%;
left: 0;
}
}
/* 搜索输入区域 */
.search-input-wrapper {
flex: 1;
position: relative;
padding: 0 16px;
display: flex;
align-items: center;
}
.search-input-prefix {
position: absolute;
left: 16px;
top: 50%;
transform: translateY(-50%);
color: #999;
font-size: 18px;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
z-index: 1;
}
.search-input-container.search-focused .search-input-prefix {
color: #ff5000;
transform: translateY(-50%) scale(1.1);
}
.custom-search-input {
font-size: 16px;
min-height: 52px;
height: 52px;
padding: 0 50px 0 48px;
border: none;
box-shadow: none;
background-color: transparent;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
font-weight: 400;
letter-spacing: 0.5px;
box-sizing: border-box; /* 关键! */
outline: none; /* 避免默认焦点轮廓 */
}
/* 可选:焦点状态 */
.custom-search-input:focus {
background-color: rgba(255, 255, 255, 0.03);
}
.custom-search-input::placeholder {
color: #ccc;
font-size: 14px;
font-weight: 400;
transition: all 0.3s ease;
}
.custom-search-input:focus {
border-color: transparent !important;
box-shadow: none !important;
}
.search-input-container.search-focused .custom-search-input::placeholder {
color: #ffccb3;
}
.search-input-suffix {
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
color: #999;
font-size: 16px;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
background-color: #f5f5f5;
width: 24px;
height: 24px;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
animation: fadeIn 0.3s ease-out forwards;
}
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(-50%) scale(0.8);
}
to {
opacity: 1;
transform: translateY(-50%) scale(1);
}
}
.search-input-suffix:hover {
color: #ff5000;
background-color: #fff0e6;
transform: translateY(-50%) scale(1.1);
}
.search-input-container.search-focused .search-input-suffix {
color: #ff5000;
}
/* 搜索按钮 */
.search-button-container {
margin-right: 1px;
padding: 15.5px;
height: 100%;
background: linear-gradient(135deg, #ff5000, #ff8c00);
color: #ffffff;
cursor: pointer;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
display: flex;
align-items: center;
justify-content: center;
font-weight: 600;
position: relative;
overflow: hidden;
border-radius: 0 28px 28px 0;
}
.search-button-container:active {
transform: translateX(1px) translateY(0);
box-shadow: 0 2px 8px rgba(255, 80, 0, 0.3);
}
.search-button-container::before {
content: '';
position: absolute;
top: -50%;
left: -50%;
width: 200%;
height: 200%;
background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.3), transparent);
transform: rotate(45deg);
animation: shine 2.5s ease-in-out infinite;
}
/* @keyframes shine {
0% {
transform: rotate(45deg) translateX(-100%) translateY(-100%);
opacity: 0;
}
50% {
opacity: 0.8;
}
100% {
transform: rotate(45deg) translateX(100%) translateY(100%);
opacity: 0;
}
} */
.search-button-text {
font-size: 16px;
font-weight: 600;
position: relative;
z-index: 1;
letter-spacing: 1px;
transition: all 0.3s ease;
}
.search-button-icon {
position: relative;
z-index: 1;
font-size: 16px;
transition: all 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
display: flex;
align-items: center;
justify-content: center;
}
.search-button-container:hover .search-button-icon {
transform: scale(1.1) rotate(15deg);
}
.search-button-container:hover .search-button-text {
transform: translateY(-1px);
}
/* 响应式调整 */
@media (max-width: 768px) {
.search-type-selector {
padding: 0 12px;
}
.search-type-selector ul {
gap: 12px;
}
.search-type-item {
padding: 12px 0;
font-size: 13px;
}
.search-input-wrapper {
padding: 0 12px;
}
.custom-search-input {
font-size: 14px !important;
height: 48px !important;
padding: 0 40px 0 40px !important;
}
.search-input-prefix {
font-size: 16px;
}
.search-button-container {
padding: 0 24px;
}
.search-button-text {
font-size: 14px;
}
.search-button-icon {
font-size: 14px;
}
.search-button-container:hover .search-button-icon {
transform: scale(1.05) rotate(10deg);
}
}
@media (max-width: 576px) {
.search-type-selector {
padding: 0 10px;
}
.search-type-selector ul {
gap: 8px;
}
.search-type-item {
font-size: 12px;
}
.search-input-wrapper {
padding: 0 10px;
}
.custom-search-input {
font-size: 13px !important;
padding: 0 35px 0 35px !important;
}
.search-input-prefix {
font-size: 14px;
}
.search-button-container {
padding: 0 20px;
}
.search-button-text {
font-size: 13px;
}
.search-button-icon {
display: none;
}
}
/* 搜索历史样式 */
.search-history-container {
position: absolute;
top: 100%;
left: 0;
width: 100%;
margin-top: 4px;
background-color: #ffffff;
border-radius: 8px;
padding: 16px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
z-index: 100;
border: 1px solid #f0f0f0;
}
.search-history-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.history-title {
font-size: 16px;
font-weight: 500;
color: #333;
}
.clear-history {
font-size: 14px;
color: #999;
text-decoration: none;
transition: color 0.3s ease;
}
.clear-history:hover {
color: #1890ff;
}
.search-history-tags {
display: flex;
flex-wrap: wrap;
gap: 10px;
}
.history-tag {
cursor: pointer;
transition: all 0.3s ease;
font-size: 14px;
padding: 4px 12px;
background-color: #f5f5f5;
border-color: #e8e8e8;
}
.history-tag:hover {
background-color: #e6f7ff;
border-color: #91d5ff;
color: #1890ff;
}
.no-history {
text-align: center;
padding: 20px 0;
color: #999;
font-size: 14px;
}
/* 商品列表链接样式 */
.header-nav-product a {
font-size: 16px;
font-weight: 500;
color: #333;
text-decoration: none;
transition: all 0.3s ease;
padding: 8px 16px;
border-radius: 4px;
}
.header-nav-product a:hover {
color: #1890ff;
background-color: #e6f7ff;
}
</style>