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.
shop-app/pages/goods/search.vue

188 lines
3.6 KiB

3 years ago
<!--
* @Author: ch
* @Date: 2022-03-21 11:29:38
* @LastEditors: ch
3 years ago
* @LastEditTime: 2022-05-05 10:51:53
3 years ago
* @Description: file content
-->
<template>
<view class="container">
3 years ago
<UiPageHeader class="header">
3 years ago
<view slot="custom" class="search">
3 years ago
<u--input class="search--input" focus placeholder="请输入您想搜索的商品名称"
3 years ago
clearable v-model="searchValue" @confirm="onSearch"
placeholderClass="search--input__placeholder">
</u--input>
3 years ago
<u-icon class="search--icon" size="48rpx" name="search" @click="onSearch" color="#ccc"/>
<!-- <image class="search--icon" src="@/static/search/search.png"/> -->
3 years ago
</view>
</UiPageHeader>
3 years ago
<view class="history" v-if="historyList.length">
<view class="history--head">
<text>最近搜索</text>
<image class="history--del" src="@/static/search/del.png" @click="clearHistory" />
</view>
<view class="history--list">
<view class="history--item" v-for="(val, index) in historyList"
:key="index" @click="handleQuick(val)">{{ val }}
</view>
</view>
</view>
</view>
</template>
<script>
3 years ago
import UiPageHeader from '../../components/UiPageHeader.vue';
3 years ago
const HISTORY_SEARCH_KEY = "historySearch";
export default {
3 years ago
components: { UiPageHeader },
3 years ago
data() {
return {
historyList : [],
searchValue: '',
};
},
onShow(){
this.searchValue = '';
this.historyList = uni.getStorageSync(HISTORY_SEARCH_KEY) || [];
},
3 years ago
onHide(){
},
3 years ago
mounted(){
},
3 years ago
methods: {
/**
* 搜索提交
*/
onSearch() {
const { searchValue } = this;
if (searchValue) {
this.setHistory(searchValue);
this.$Router.replace({path:'/goodsList', query :{
3 years ago
search: searchValue
}
});
}
},
/**
* 清空最近搜索记录
*/
clearHistory() {
this.updateHistoryStorage([]);
},
/**
* 记录历史搜索
*/
setHistory(searchValue) {
const data = this.historyList;
const index = data.indexOf(searchValue);
index > -1 && data.splice(index, 1);
data.unshift(searchValue);
if(data.length === 11){
data.pop();
}
this.updateHistoryStorage(data);
},
/**
* 更新历史搜索缓存
* @param {Object} data
*/
updateHistoryStorage(data) {
this.historyList = data;
uni.setStorageSync(HISTORY_SEARCH_KEY, data);
},
/**
* 跳转到最近搜索
*/
handleQuick(searchValue) {
this.setHistory(searchValue);
this.$Router.replace({path:'/goodsList', query :{
3 years ago
search: searchValue
}
});
},
},
};
</script>
3 years ago
<style lang="scss">
page {
background: $color-grey0;
3 years ago
}
3 years ago
</style>
<style lang="scss" scoped>
3 years ago
.header{
background: $color-grey0;
}
3 years ago
.search{
height: 88rpx;
3 years ago
flex: 1;
3 years ago
display: flex;
justify-content: space-between;
align-items: center;
&--input{
display: block;
height: 70rpx;
border:none;
box-sizing: border-box;
background: $color-grey1;
3 years ago
padding-right: 70rpx !important;
3 years ago
&__placeholder{
font-size: $font-size-base;
color: $color-grey5;
3 years ago
}
}
3 years ago
&--icon{
position: absolute;
right: 60rpx;
}
3 years ago
&--btn{
font-size: 26rpx;
color: $color-grey5;
3 years ago
}
}
// 最近搜索
.history {
3 years ago
padding: 0 40rpx;
3 years ago
&--head {
text{
font-size: 26rpx;
color: $color-grey4;
}
3 years ago
display: flex;
justify-content: space-between;
align-items: center;
margin: 40rpx 0 20rpx;
3 years ago
}
&--del{
width: 27rpx;
height: 28rpx;
}
&--list{
display: flex;
flex-wrap: wrap;
}
&--item{
max-width: 460rpx;
3 years ago
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
3 years ago
height: 70rpx;
line-height: 70rpx;
padding: 0 20rpx;
font-size: 26rpx;
color: $color-grey5;
background: $color-grey1;
margin: 10rpx;
3 years ago
}
}
</style>