wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<template>
    <cl-page status-bar-background="transparent">
        <cl-topbar
            background-color="transparent"
            :border="false"
            :show-back="false"
            :ref="setRefs('topbar')"
            with-mp
        >
            <cl-search v-model="keyWord" placeholder="搜索商品名称" @search="toSearch(keyWord)">
                <template #prepend>
                    <cl-icon name="arrow-left" size="22px" @tap="refs.topbar?.back" />
                </template>
            </cl-search>
        </cl-topbar>
 
        <view class="page">
            <view class="history">
                <view class="head">
                    <text class="label">搜索历史</text>
                    <cl-icon class-name="shop-icon-delete" :size="36" @tap="history.clear()" />
                </view>
 
                <view class="list">
                    <text
                        class="item"
                        v-for="(item, index) in history.list"
                        :key="index"
                        @tap="toSearch(item)"
                    >
                        {{ item }}
                    </text>
 
                    <text class="item" v-if="isEmpty(history.list)">无</text>
                </view>
            </view>
 
            <view class="search">
                <view class="head">
                    <text class="label">搜索发现</text>
                </view>
 
                <view class="list">
                    <text
                        class="item"
                        v-for="(item, index) in recommend.list"
                        :key="index"
                        @tap="toSearch(item.name!)"
                    >
                        {{ item.name }}
                    </text>
                </view>
            </view>
        </view>
    </cl-page>
</template>
 
<script lang="ts" setup>
import { reactive, ref } from "vue";
import { storage, useCool } from "/@/cool";
import { isEmpty } from "lodash-es";
import { onReady } from "@dcloudio/uni-app";
import { useUi } from "/$/cool-ui";
 
const { router, service, refs, setRefs } = useCool();
const ui = useUi();
 
// 搜索关键字
const keyWord = ref("");
 
// 搜索历史
const history = reactive({
    list: (storage.get("search.history") || []) as string[],
 
    add() {
        if (!keyWord.value || history.list.includes(keyWord.value)) {
            return false;
        }
 
        history.list.push(keyWord.value);
        history.update();
    },
 
    clear() {
        ui.showConfirm({
            title: "提示",
            message: "确定要清空搜索历史吗?",
            callback(action) {
                if (action == "confirm") {
                    history.list = [];
                    history.update();
                }
            },
        });
    },
 
    update() {
        storage.set("search.history", history.list);
    },
});
 
// 推荐
const recommend = reactive({
    list: [] as Eps.GoodsSearchKeywordEntity[],
 
    get() {
        service.goods.searchKeyword.list().then((res) => {
            recommend.list = res;
        });
    },
});
 
// 搜索
function toSearch(val: string) {
    history.add();
 
    router.push({
        path: "/pages/goods/list",
        query: {
            keyWord: val,
        },
    });
}
 
onReady(() => {
    recommend.get();
});
</script>
 
<style lang="scss" scoped>
.page {
    .history,
    .search {
        padding: 0 24rpx;
        margin-bottom: 24rpx;
 
        .head {
            display: flex;
            align-items: center;
            justify-content: space-between;
            height: 80rpx;
 
            .label {
                display: inline-block;
                font-size: 28rpx;
                font-weight: bold;
            }
        }
 
        .list {
            .item {
                display: inline-flex;
                align-items: center;
                height: 46rpx;
                padding: 0 24rpx;
                margin: 0 24rpx 24rpx 0;
                font-size: 24rpx;
                border-radius: 40rpx;
                background-color: #fff;
            }
        }
    }
}
</style>