Files

325 lines
6.3 KiB
Vue
Raw Normal View History

2023-10-11 11:27:47 +08:00
<template>
<layout>
<uv-navbar
2023-11-14 17:21:03 +08:00
:fixed="false"
title="搜索"
left-arrow
@leftClick="goBack"
2023-10-11 11:27:47 +08:00
/>
<view class="search-bar">
<uv-search
2023-11-14 17:21:03 +08:00
v-model="keywordData"
shape="round"
:placeholder="placeholder || '搜索商品'"
2023-11-14 17:21:03 +08:00
actionText="搜索"
show-action
@clear="onCancel"
@custom="searchByKeyword"
@search="searchByKeyword"
@cancel="onCancel"
2023-10-11 11:27:47 +08:00
>
</uv-search>
</view>
2023-11-14 17:21:03 +08:00
<!-- 热门搜索 -->
<view class="hotSearchBox tipsBox" v-if="hotSearchList.length>0">
<view class="boxTitle mar-leftgetList-30">
<label>热门搜索</label>
<image
class="seeIcon hotSearchListSee-icon"
2023-11-22 18:55:55 +08:00
:src="!hideHotFlag ? seeIcon : notSeeIcon"
2023-11-14 17:21:03 +08:00
@click="hideHotFlag = !hideHotFlag"
/>
2023-10-11 11:27:47 +08:00
</view>
2023-11-17 20:55:32 +08:00
<view
2023-11-14 17:21:03 +08:00
class="hot-flex-list"
v-if="!hideHotFlag"
>
<view
2023-11-17 20:55:32 +08:00
v-for="(item,index) in hotSearchList"
2023-11-14 17:21:03 +08:00
:key="index"
2023-11-17 20:55:32 +08:00
class="historySearDel-box flex-items-plus"
2023-11-14 17:21:03 +08:00
>
<view
class="boxContent"
@click="handleClickHistoryOrHot(item)"
>{{ item }}
</view>
2023-10-11 11:27:47 +08:00
</view>
2023-11-17 20:55:32 +08:00
</view>
2023-11-14 17:21:03 +08:00
<view
v-else
class="notSeeContent"
>当前热门搜索已隐藏
2023-10-11 11:27:47 +08:00
</view>
2023-11-14 17:21:03 +08:00
</view>
<!-- 历史搜索 -->
<view class="historyBox tipsBox" v-if="historyList.length>0">
<view class="boxTitle">
<label class="title">历史搜索</label>
<image
class="historyDel-icon"
2023-11-17 20:55:32 +08:00
@click="showModal(0)"
2023-11-22 18:55:55 +08:00
:src="historyDeleteIcon"
2023-11-14 17:21:03 +08:00
/>
2023-10-11 11:27:47 +08:00
</view>
2023-11-14 17:21:03 +08:00
<view class="historySear-box hot-flex-list">
<view
2023-11-17 20:55:32 +08:00
v-for="(item,index) in historyList"
2023-11-14 17:21:03 +08:00
:key="index"
class="historySearDel-box flex-items-plus"
>
<view
class="boxContent historyText line1"
@click="handleClickHistoryOrHot(item)"
>{{ item }}
</view>
</view>
</view>
</view>
2023-10-11 11:27:47 +08:00
</layout>
2023-11-17 20:55:32 +08:00
<Modal ref="modalRef" :content="modalTitle" @confirm="confirmModal" />
2023-10-11 11:27:47 +08:00
</template>
<script setup>
2023-11-17 20:55:32 +08:00
import { computed, ref, unref } from 'vue';
2023-11-14 17:21:03 +08:00
import { useRouter } from "@/hooks/useRouter";
import {onLoad, onShow} from '@dcloudio/uni-app'
2023-11-14 17:21:03 +08:00
import { hotSearch, historySearch, clearHistorySearch } from "@/api/product";
2023-11-17 20:55:32 +08:00
import Modal from "@/components/Modal/index.vue";
2023-11-22 18:55:55 +08:00
import { seeIcon, notSeeIcon, historyDeleteIcon } from "@/utils/images";
const {push, goBack, getParams} = useRouter()
2023-10-11 11:27:47 +08:00
const searchRef = ref();
const keyword = ref('');
const placeholder = ref('')
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
const hotSearchList = ref([])
const getHotKeywordList = async () => {
hotSearchList.value = await hotSearch(0, 10)
}
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
const historyList = ref([])
const hideHotFlag = ref(false); // 是否隐藏热门
const getHistoryKeywordList = async () => {
let historData = await historySearch()
historyList.value = historData.slice(0, 20)
}
2023-10-11 11:27:47 +08:00
2023-11-17 20:55:32 +08:00
const modalRef = ref()
const modalType = ref(0) // 0删除记录 1撤销申请
const modalTitle = computed(()=>{
const tipsArr = ['您确定要清空搜索记录吗?','您确定要删除这一条记录吗?']
return tipsArr[modalType.value]
})
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
/**
2023-11-17 20:55:32 +08:00
* 打开弹窗
* @param {number} type 0删除记录 1撤销申请
2023-11-14 17:21:03 +08:00
*/
2023-11-17 20:55:32 +08:00
function showModal(type){
modalType.value = type
console.log(modalRef.value)
unref(modalRef).show()
2023-10-11 11:27:47 +08:00
}
2023-11-17 20:55:32 +08:00
/**
* 确认弹窗
*/
function confirmModal(){
const funcArr = [doClearAllRequest]
funcArr[modalType.value]()
}
/**
* 清空所有
*/
async function doClearAllRequest(){
await clearHistorySearch()
await getHistoryKeywordList()
}
2023-11-14 17:21:03 +08:00
const keywordData = ref('')
function handleClickHistoryOrHot(key) {
keywordData.value = key
searchByKeyword()
}
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
function searchByKeyword() {
if (!keywordData.value && !placeholder.value) {
2023-11-14 17:21:03 +08:00
return uni.showToast({
title: '请输入要搜索内容!',
duration: 2000,
icon: 'none'
});
}
push({url: '/pages/goodsList/goodsList'}, {
data: {
keyword: keywordData.value || placeholder.value
2023-11-14 17:21:03 +08:00
}
})
2023-10-11 11:27:47 +08:00
}
2023-11-14 17:21:03 +08:00
const onCancel = () => {
}
2023-10-11 11:27:47 +08:00
2023-11-14 17:21:03 +08:00
onShow(() => {
2023-11-17 20:55:32 +08:00
Promise.all([getHotKeywordList(), getHistoryKeywordList()])
2023-11-14 17:21:03 +08:00
})
2023-10-11 11:27:47 +08:00
onLoad((options) => {
const params = getParams(options)
placeholder.value = params.key
})
2023-10-11 11:27:47 +08:00
</script>
2023-11-14 17:21:03 +08:00
<style lang="scss">
2023-10-11 11:27:47 +08:00
.search-box {
&-header {
display: flex;
justify-content: space-between;
align-items: center;
}
&-title {
line-height: 40rpx;
font-size: 28rpx;
font-weight: 500;
color: #333333;
margin-bottom: 20rpx;
}
&-action {
width: 28rpx;
height: 28rpx;
.image {
display: block;
width: 28rpx;
height: 28rpx;
}
}
background: #fff;
padding: 40rpx 35rpx 30rpx;
}
2023-11-14 17:21:03 +08:00
// 热门搜索
.hotSearchBox {
padding: 0 36rpx;
.historyText {
flex: 1;
}
.hotSearchListSee-icon {
height: 24rpx;
}
.hotSearchListNotSee-icon {
height: 36rpx;
}
.seeIcon {
width: 40rpx;
position: absolute;
right: 30rpx;
}
.notSeeContent {
margin-top: 20rpx;
text-align: center;
font-size: 24rpx;
color: #CCCCCC;
}
}
.line {
margin: 70rpx 0;
height: 2rpx;
background: #F3F4F5;
}
// 历史搜索
.historyBox {
padding: 0 36rpx;
.historySear-box {
width: 100%;
}
.historyDel-icon {
width: 30rpx;
height: 30rpx;
position: absolute;
right: 30rpx;
}
.historyIconBox {
width: 50rpx;
.historySearDel-icon {
position: relative;
width: 16rpx;
height: 16rpx;
}
}
}
.tipsBox {
padding: 15rpx 24rpx;
background: #ffffff;
margin-bottom: 20rpx;
}
.boxTitle {
margin: 30rpx 0;
label {
color: #333333;
font-size: 28rpx;
}
}
2023-11-17 20:55:32 +08:00
.hot-flex-list{
gap:20rpx;
}
2023-11-14 17:21:03 +08:00
.historySearDel-box {
//height: 54rpx;
2023-11-14 17:21:03 +08:00
line-height: 54rpx;
background-color: #F1F1F1;
//text-align: center;
2023-11-14 17:21:03 +08:00
overflow: hidden;
display: flex;
justify-content: space-between;
2023-11-17 20:55:32 +08:00
2023-11-14 17:21:03 +08:00
.boxContent {
font-size: 24rpx;
padding: 0 30rpx;
color: #333333;
font-weight: 400;
}
}
.hot-flex-list {
display: flex;
flex-wrap: wrap;
flex-direction: row;
.ertical-lines {
color: #DDDDDD;
text-align: center;
font-size: 24rpx;
padding: 10rpx 0;
}
}
.flex-items-plus {
display: flex;
justify-content: center;
align-items: center;
}
2023-10-11 11:27:47 +08:00
</style>