Files
kangyuan-mobile/pages/public/search.vue
T

284 lines
7.1 KiB
Vue

<!-- 搜索界面 -->
<template>
<s-pages class="set-wrap" title="搜索" color="#ffffff" :pink_bg="true">
<view class="">
<card_search class="search" style="margin-bottom: 32rpx" @search="onSearch"></card_search>
<view class="search_top">
<view>
<view class="flex-center ju-between search_title" v-if="state.historyList.length">
<view class="size32 c0">历史搜索</view>
<uni-icons @tap="onDelete" type="trash" color="" size="24" />
</view>
<view class="ss-flex ss-col-center ss-row-left ss-flex-wrap">
<button
class="history-btn ss-reset-button"
@tap="onSearch(item)"
v-for="(item, index) in state.historyList"
:key="index"
>
{{ item }}
</button>
</view>
</view>
<view class="m-t-10 m-b-50">
<view class="flex-center ju-between search_title" v-if="state.historyList.length">
<view class="size32 c0">热门搜索</view>
</view>
<view class="ss-flex ss-col-center ss-row-left ss-flex-wrap">
<button
class="history-btn ss-reset-button"
@tap="onSearch(item.searchKeyword)"
v-for="(item, index) in state.hotSearchList"
:key="index"
>
{{ item.searchKeyword }}
</button>
</view>
</view>
</view>
<view class="search_bottom w100">
<scroll-view class="scroll-view" scroll-x="true" scroll-left="0">
<view class="scroll flex-row nowrap">
<view
:style="{
width: width,
}"
v-for="(v, i) in shoops"
:key="i"
class="scroll_item shrink"
:class="{
scroll_item1: i == 0,
scroll_item2: i == 1,
}"
>
<view class="flex-center ju-between mb20">
<view class="flex-center">
<view class="icon1">
<image :src="$url + v.icon" mode="scaleToFill" />
</view>
<view class="bot_name size28">{{ v.name }}</view>
</view>
<view class="flex-center" @click="onSearch()">
<view class="size24 c34">更多</view>
<uni-icons type="right" color="#333333" />
</view>
</view>
<view class="goodsList w100">
<view class="w100" v-for="(item, index) in v.list" :key="index">
<card_goods :data="item" :type="3"></card_goods>
</view>
</view>
</view>
</view>
</scroll-view>
</view>
</view>
</s-pages>
</template>
<script setup>
import { ref, reactive } from 'vue';
import sheep from '@/sheep';
import { onLoad } from '@dcloudio/uni-app';
import * as api from '@/sheep/api/index/index';
import SpuApi from '@/sheep/api/product/spu';
import card_goods from '@/components/goodsItem/card_goods.vue';
let width = ref('516rpx');
const state = reactive({
historyList: [],
hotSearchList: [],
});
// 搜索
function onSearch(keyword) {
if (!keyword) {
sheep.$router.go('/pages/goods/list');
return;
}
saveSearchHistory(keyword);
// 前往商品列表(带搜索条件)
sheep.$router.go('/pages/goods/list', { keyword });
}
// 保存搜索历史
function saveSearchHistory(keyword) {
// 如果关键词在搜索历史中,则把此关键词先移除
if (state.historyList.includes(keyword)) {
state.historyList.splice(state.historyList.indexOf(keyword), 1);
}
// 置顶关键词
state.historyList.unshift(keyword);
// 最多保留 10 条记录
if (state.historyList.length >= 10) {
state.historyList.length = 10;
}
uni.setStorageSync('searchHistory', state.historyList);
}
function onDelete() {
uni.showModal({
title: '提示',
content: '确认清除搜索历史吗?',
confirmColor: '#FA7E31',
success: function (res) {
if (res.confirm) {
state.historyList = [];
uni.removeStorageSync('searchHistory');
}
},
});
}
const gethotsearchlist = () => {
api.gethotsearchlist().then((res) => {
if (res.code == 0) {
state.hotSearchList = res.data || [];
}
});
};
let shoops = reactive([
{
name: '推荐商品',
icon: 'index/search3.png',
list: [],
},
{
name: '销量',
icon: 'index/search2.png',
list: [],
},
]);
const getShoops = async (obj) => {
let res = await SpuApi.getSpuPage({
pageNo: 1,
pageSize: 5,
...obj,
});
if (res.code == 0) {
return res.data.list;
} else {
return [];
}
};
onLoad(async () => {
state.historyList = uni.getStorageSync('searchHistory') || [];
// 热门搜索
gethotsearchlist();
shoops[1].list = await getShoops({
sortField: 'salesCount',
sortAsc: true,
});
shoops[0].list = await getShoops({
recommend: true,
});
});
</script>
<style lang="scss" scoped>
.serach-title {
font-size: 30rpx;
font-weight: 500;
color: #ffffff;
}
.uni-searchbar {
padding-left: 0;
}
.serach-history {
font-weight: bold;
color: #ffffff;
font-size: 30rpx;
}
.clean-history {
font-weight: 500;
color: #ffffff;
font-size: 28rpx;
}
.history-btn {
padding: 0 24rpx;
// height: 48rpx;
line-height: 48rpx;
background: #f2f3f5;
border-radius: 30rpx;
font-size: 24rpx;
color: #333333;
max-width: 690rpx;
margin: 0 12rpx 24rpx 0;
}
.search {
padding: 0 30rpx;
box-sizing: border-box;
}
.search_top {
padding: 0 30rpx;
box-sizing: border-box;
.search_title {
margin-bottom: 16rpx;
font-weight: 500;
}
}
.search_bottom {
width: 100%;
overflow: hidden;
margin-bottom: 20rpx;
}
.scroll-view {
width: 100%;
white-space: nowrap;
overflow-x: auto; // 添加横向滚动
-webkit-overflow-scrolling: touch; // 优化移动端滚动体验
}
.scroll-view {
box-sizing: border-box;
}
.scroll {
display: flex;
flex-direction: row;
width: max-content;
padding: 0 32rpx;
.scroll_item {
padding: 32rpx 28rpx;
box-sizing: border-box;
border-radius: 16rpx;
}
.bot_name {
margin-left: 8rpx;
}
.scroll_item1 {
background: linear-gradient(180deg, #ffe4e6 0%, #ffebe7 100%);
margin-right: 32rpx;
.bot_name {
color: #fa7e31;
}
}
.scroll_item2 {
background: linear-gradient(180deg, #fff1cc 0%, rgba(255, 248, 222, 0.34) 100%);
.bot_name {
color: #ffbd15;
}
}
}
.icon1 {
width: 32rpx;
height: 32rpx;
image {
width: 100%;
height: 100%;
}
}
.goodsList {
padding: 40rpx 20rpx;
padding-bottom: 2rpx;
box-sizing: border-box;
background-color: #fff;
border-radius: 16rpx;
}
:deep(.uni-icons) {
font-size: 24rpx;
}
</style>