You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
canteen/subpkg/search/search.vue

192 lines
4.1 KiB

<template>
<view>
<view class="search-box">
<uni-search-bar @input="input" :radius="100" cancelButton="none" :focus="true"></uni-search-bar>
<button class="search" @click="goToSearchList"></button>
</view>
<!---->
<view class="sugest-list" v-if="searchresult.length !== 0">
<view class="sugest-item" v-for="(item,i) in searchresult" :key="i" @click="gotodetail(item)">
<view class="dish-name">{{item.dish_name}}</view>
<uni-icons type="arrowright" size="16"></uni-icons>
</view>
</view>
<!--搜索历史-->
<view class="history-box" v-else>
<!--标题区域-->
<view class="history-title">
<text>搜索历史</text>
<uni-icons type="trash" size="17" @click="clean"></uni-icons>
</view>
<!--列表区域-->
<view class="history-list">
<uni-tag v-for="(item,i) in histories" :text="item" :key="i" @click="gotosearchdetail(item)"></uni-tag>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
timer: null,
kw: '',
searchresult: [],
//搜索结果
searchlist: [],
//搜索历史的数组
historylist: [],
};
},
onLoad() {
this.historylist = JSON.parse(uni.getStorageSync('kw') || '[]')
this.cnt = this.historylist.length
console.log(this.historylist)
},
methods: {
async goToSearchList() {
this.getsearchresult()
this.savesearchhistory()
let that = this
setTimeout(()=>{
uni.$emit('item',this.searchresult)
},500)
uni.navigateTo({
url:'/subpkg/searchList/searchList?searchlist=' + encodeURIComponent(JSON.stringify(this.searchresult))
})
},
//input输入事件的处理函数
input(e) {
//延时器
clearTimeout(this.timer)
this.timer = setTimeout(
() => {
this.kw = e
this.getsearchresult()
}, 500)
},
async getsearchresult() {
let that = this
//判断搜索关键词是否为空
if (this.kw.length === 0) {
this.searchresult = []
return
}
uniCloud.callFunction({
name:'getDishes',
data:{
api:'getByName',
dish_name:that.kw
},
success:function(res){
that.searchresult = res.result.data
that.searchlist = res.result.data
}
})
},
gotodetail(item) {
this.savesearchhistory()
console.log(item._id)
uni.navigateTo({
url: '/subpkg/dishDetail/dishDetail?_id=' +encodeURIComponent(JSON.stringify(item._id))
})
},
savesearchhistory() {
const set = new Set(this.historylist)
set.delete(this.kw)
set.add(this.kw)
this.historylist = Array.from(set)
uni.setStorageSync('kw', JSON.stringify(this.historylist))
},
//清空本地历史记录
clean() {
this.historylist = []
uni.setStorageSync('kw', '[]')
this.cnt = 0
},
gotosearchdetail(item) {
let historyresult = []
uniCloud.callFunction({
name:'getDishes',
data:{
api:'getByName',
dish_name:item
},
success:function(res){
historyresult = res.result.data
}
})
setTimeout(()=>{
uni.$emit('item',historyresult)
},500)
uni.navigateTo({
url:'/subpkg/searchList/searchList?searchlist=' + encodeURIComponent(JSON.stringify(historyresult))
})
}
},
computed: {
histories() {
return [...this.historylist].reverse()
}
}
}
</script>
<style lang="scss">
.search-box {
position: sticky;
top: 0;
z-index: 999;
}
.sugest-list {
padding: 0 5px;
.sugest-item {
display: flex;
align-items: center;
justify-content: space-between;
font-size: 12px;
padding: 13px 0;
border-bottom: 1 px solid #efefef;
.dish-name {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
}
}
.history-box {
padding: 0 5px;
.history-title {
display: flex;
justify-content: space-between;
height: 40px;
align-items: center;
font-size: 13px;
border-bottom: 1px solid #efefef;
}
.history-list {
display: flex;
flex-wrap: wrap;
.uni-tag {
margin-top: 5px;
margin-right: 5px;
}
}
}
.search{
height: 50rpx;
width: 200rpx;
font-size: 20rpx;
justify-content: center;
}
</style>