mall-uniapp/pages/index/search.vue

120 lines
2.8 KiB
Vue
Raw Normal View History

2023-12-16 02:40:04 +00:00
<!-- 搜索界面 -->
2022-11-22 07:45:36 +00:00
<template>
<s-layout :bgStyle="{ color: '#FFF' }" class="set-wrap" title="搜索">
2022-11-22 07:45:36 +00:00
<view class="ss-p-x-24">
<view class="ss-flex ss-col-center">
<uni-search-bar
class="ss-flex-1"
radius="33"
placeholder="请输入关键字"
cancelButton="none"
:focus="true"
2023-03-13 10:34:27 +00:00
@confirm="onSearch($event.value)"
2022-11-22 07:45:36 +00:00
/>
</view>
<view class="ss-flex ss-row-between ss-col-center">
<view class="serach-history">搜索历史</view>
<button class="clean-history ss-reset-button" @tap="onDelete"> </button>
</view>
<view class="ss-flex ss-col-center ss-row-left ss-flex-wrap">
<button
class="history-btn ss-reset-button"
2023-03-13 10:34:27 +00:00
@tap="onSearch(item)"
2023-03-16 09:56:35 +00:00
v-for="(item, index) in state.historyList"
2022-11-22 07:45:36 +00:00
:key="index"
>
{{ item }}
</button>
</view>
</view>
</s-layout>
</template>
<script setup>
2023-03-13 10:34:27 +00:00
import { reactive } from 'vue';
2022-11-22 07:45:36 +00:00
import sheep from '@/sheep';
import { onLoad } from '@dcloudio/uni-app';
2023-12-16 02:40:04 +00:00
2022-11-22 07:45:36 +00:00
const state = reactive({
2023-03-16 09:56:35 +00:00
historyList: [],
2022-11-22 07:45:36 +00:00
});
2023-03-13 10:34:27 +00:00
// 搜索
function onSearch(keyword) {
2023-12-16 02:40:04 +00:00
if (!keyword) {
return;
}
2023-03-13 10:34:27 +00:00
saveSearchHistory(keyword);
2023-12-16 02:40:04 +00:00
// 前往商品列表(带搜索条件)
2023-03-13 10:34:27 +00:00
sheep.$router.go('/pages/goods/list', { keyword });
2022-11-22 07:45:36 +00:00
}
2023-03-13 10:34:27 +00:00
// 保存搜索历史
function saveSearchHistory(keyword) {
// 如果关键词在搜索历史中,则把此关键词先移除
if (state.historyList.includes(keyword)) {
state.historyList.splice(state.historyList.indexOf(keyword), 1);
}
// 置顶关键词
state.historyList.unshift(keyword);
2023-12-16 02:40:04 +00:00
// 最多保留 10 条记录
2023-03-13 10:34:27 +00:00
if (state.historyList.length >= 10) {
state.historyList.length = 10;
}
uni.setStorageSync('searchHistory', state.historyList);
2022-11-22 07:45:36 +00:00
}
function onDelete() {
uni.showModal({
title: '提示',
content: '确认清除搜索历史吗?',
success: function (res) {
if (res.confirm) {
state.historyTag = [];
uni.removeStorageSync('searchHistory');
2022-11-22 07:45:36 +00:00
}
},
});
}
2023-12-16 02:40:04 +00:00
2022-11-22 07:45:36 +00:00
onLoad(() => {
2023-03-13 10:34:27 +00:00
state.historyList = uni.getStorageSync('searchHistory') || [];
2022-11-22 07:45:36 +00:00
});
</script>
<style lang="scss" scoped>
.serach-title {
font-size: 30rpx;
font-weight: 500;
color: #333333;
}
.uni-searchbar {
padding-left: 0;
}
.serach-history {
font-weight: bold;
color: #333333;
font-size: 30rpx;
}
.clean-history {
font-weight: 500;
color: #999999;
font-size: 28rpx;
}
.history-btn {
padding: 0 38rpx;
height: 60rpx;
background: #f5f6f8;
border-radius: 30rpx;
font-size: 28rpx;
color: #333333;
max-width: 690rpx;
margin: 0 20rpx 20rpx 0;
}
</style>