<template>
|
<view class="container">
|
<view class="search-container">
|
<!-- 搜索框 -->
|
<view class="search-container-bar">
|
<!-- :cancelText="keyBoardPopup ? '取消' : '搜索'" -->
|
<uni-search-bar ref="searchBar" style="flex:1;" radius="100" v-model="searchText" :focus="focus"
|
placeholder="请输入关键字" clearButton="auto" cancelButton="always" @clear="clear" @confirm="confirm"
|
@cancel="cancel" />
|
</view>
|
</view>
|
<view class="search-body">
|
<!-- 搜索历史 -->
|
<view class="word-container" v-if="localSearchList.length">
|
<view class="word-container_header">
|
<text class="word-container_header-text">搜索历史</text>
|
<uni-icons v-if="!localSearchListDel" @click="localSearchListDel = true" class="search-icons"
|
style="padding-right: 0;" :color="iconColor" size="18" type="trash"></uni-icons>
|
<view v-else class="flex-center flex-row" style="font-weight: 500;justify-content: space-between;">
|
<text
|
style="font-size: 22rpx;color: #666;padding-top:4rpx;padding-bottom:4rpx;padding-right:20rpx;"
|
@click="show = true">全部删除</text>
|
<text
|
style="font-size: 22rpx;color: #c0402b;padding-top:4rpx;padding-bottom:4rpx;padding-left:20rpx;"
|
@click="localSearchListDel = false">完成</text>
|
</view>
|
</view>
|
|
<view class="word-container_body">
|
<view class="flex-center flex-row word-container_body-text" v-for="(word,index) in localSearchList"
|
:key="index" @click="LocalSearchlistItemClick(word,index)">
|
<text class="word-display" :key="word">{{word}}</text>
|
<uni-icons v-if="localSearchListDel" size="12" type="closeempty" />
|
</view>
|
</view>
|
</view>
|
</view>
|
<u-modal v-model="show" content="是否删除所有搜索历史?" show-cancel-button @confirm="LocalSearchListClear"></u-modal>
|
</view>
|
</template>
|
|
<script>
|
/**
|
* 云端一体搜索模板
|
* @description uniCloud云端一体搜索模板,自带下拉候选、历史搜索、热搜。无需再开发服务器代码
|
*/
|
const associativeSearchField = 'title'; // 联想时,搜索框值检索数据库字段名
|
const associativeField = 'title,thumb,charge,charge_original,short_description'; // 联想列表每一项携带的字段
|
const localSearchListKey = '__local_search_history'; // 本地历史存储字段名
|
|
// 数组去重
|
const arrUnique = arr => {
|
for (let i = arr.length - 1; i >= 0; i--) {
|
const curIndex = arr.indexOf(arr[i]);
|
const lastIndex = arr.lastIndexOf(arr[i])
|
curIndex != lastIndex && arr.splice(lastIndex, 1)
|
}
|
return arr
|
} // 节流
|
// 防抖
|
function debounce(fn, interval, isFirstAutoRun) {
|
/**
|
*
|
* @param {要执行的函数} fn
|
* @param {在操作多长时间后可再执行,第一次立即执行} interval
|
*/
|
var _self = fn;
|
var timer = null;
|
var first = true;
|
|
if (isFirstAutoRun) {
|
_self();
|
}
|
|
return function() {
|
var args = arguments;
|
var _me = this;
|
if (first) {
|
first = false;
|
_self.apply(_me, args);
|
}
|
|
if (timer) {
|
clearTimeout(timer)
|
// return false;
|
}
|
|
timer = setTimeout(function() {
|
clearTimeout(timer);
|
timer = null;
|
_self.apply(_me, args);
|
}, interval || 200);
|
}
|
}
|
|
export default {
|
data() {
|
return {
|
localSearchList: uni.getStorageSync(localSearchListKey),
|
localSearchListDel: false,
|
netHotListIsHide: false,
|
searchText: '',
|
iconColor: '#999999',
|
associativeList: [],
|
keyBoardPopup: false,
|
|
focus: true, // 是否自动聚焦
|
speechEngine: 'iFly', // 语音识别引擎 iFly 讯飞 baidu 百度
|
show: false
|
}
|
},
|
created() {
|
this.db = uniCloud.database();
|
|
uni.onKeyboardHeightChange((res) => {
|
this.keyBoardPopup = res.height !== 0;
|
})
|
|
this.searchText = getApp().globalData.searchText;
|
},
|
methods: {
|
clear(res) {
|
console.log("res: ", res);
|
},
|
confirm(res) {
|
// 键盘确认
|
this.search(res.value);
|
},
|
cancel(res) {
|
uni.hideKeyboard();
|
this.searchText = '';
|
},
|
search(value) {
|
if (!value) return
|
if (this.searchText !== value) this.searchText = value
|
this.localSearchListManage(value)
|
uni.hideKeyboard();
|
this.$nextTick(() => {
|
uni.vk.navigateTo(`./search-result-list?text=${value}`)
|
})
|
},
|
localSearchListManage(word) {
|
let list = uni.getStorageSync(localSearchListKey);
|
if (list.length) {
|
this.localSearchList.unshift(word);
|
arrUnique(this.localSearchList);
|
if (this.localSearchList.length > 10) {
|
this.localSearchList.pop();
|
}
|
} else {
|
this.localSearchList = [word];
|
}
|
uni.setStorageSync(localSearchListKey, this.localSearchList);
|
},
|
LocalSearchListClear() {
|
this.localSearchListDel = false;
|
this.localSearchList = [];
|
uni.removeStorageSync(localSearchListKey)
|
},
|
LocalSearchlistItemClick(word, index) {
|
if (this.localSearchListDel) {
|
this.localSearchList.splice(index, 1);
|
uni.setStorageSync(localSearchListKey, this.localSearchList);
|
if (!this.localSearchList.length) {
|
this.localSearchListDel = false;
|
}
|
return;
|
}
|
this.search(word);
|
},
|
getDeviceId() {
|
return new Promise((resolve, reject) => {
|
const uniId = uni.getStorageSync('uni_id');
|
if (!uniId) {
|
// #ifdef APP-PLUS
|
plus.device.getInfo({
|
success: (deviceInfo) => {
|
resolve(deviceInfo.uuid)
|
},
|
fail: () => {
|
resolve(uni.getSystemInfoSync().system + '_' + Math.random().toString(
|
36).substr(2))
|
}
|
});
|
// #endif
|
// #ifndef APP-PLUS
|
resolve(uni.getSystemInfoSync().system + '_' + Math.random().toString(36).substr(2))
|
// #endif
|
} else {
|
resolve(uniId)
|
}
|
})
|
}
|
},
|
computed: {
|
associativeShow() {
|
return this.searchText && this.associativeList.length;
|
}
|
}
|
}
|
</script>
|
|
<style>
|
page {
|
/* #ifndef APP-NVUE */
|
height: 100%;
|
/* #endif */
|
flex: 1;
|
}
|
</style>
|
|
<style lang="scss" scoped>
|
$search-bar-height:52px;
|
$word-container_header-height:72rpx;
|
|
.container {
|
/* #ifndef APP-NVUE */
|
height: 100%;
|
/* #endif */
|
flex: 1;
|
background-color: #f7f7f7;
|
}
|
|
.search-body {
|
background-color: #fff;
|
border-bottom-right-radius: 10px;
|
border-bottom-left-radius: 10px;
|
}
|
|
@mixin uni-flex {
|
/* #ifndef APP-NVUE */
|
display: flex;
|
/* #endif */
|
}
|
|
@mixin words-display {
|
font-size: 26rpx;
|
color: #666;
|
}
|
|
.flex-center {
|
@include uni-flex;
|
justify-content: center;
|
align-items: center;
|
}
|
|
.flex-row {
|
@include uni-flex;
|
flex-direction: row;
|
}
|
|
/* #ifdef APP-PLUS */
|
/* #ifndef APP-NVUE */
|
/deep/
|
/* #endif */
|
.uni-searchbar {
|
padding-left: 0;
|
}
|
|
/* #endif */
|
|
/* #ifndef APP-NVUE */
|
/deep/
|
/* #endif */
|
.uni-searchbar__box {
|
border-width: 0;
|
}
|
|
/* #ifndef APP-NVUE */
|
/deep/
|
/* #endif */
|
.uni-input-placeholder {
|
font-size: 28rpx;
|
}
|
|
.search-container {
|
height: $search-bar-height;
|
@include uni-flex;
|
flex-direction: column;
|
justify-content: center;
|
align-items: center;
|
position: relative;
|
background-color: #fff;
|
|
@at-root {
|
#{&}-bar {
|
@include uni-flex;
|
flex-direction: row;
|
justify-content: center;
|
align-items: center;
|
position: absolute;
|
top: 0;
|
left: 0;
|
right: 0;
|
}
|
}
|
}
|
|
.search-container-bar {
|
padding: 0 20rpx;
|
}
|
|
.search-associative {
|
/* #ifndef APP-NVUE */
|
overflow-y: auto;
|
/* #endif */
|
position: absolute;
|
top: $search-bar-height;
|
left: 0;
|
right: 0;
|
bottom: 0;
|
background-color: #fff;
|
margin-top: 10rpx;
|
padding-left: 10rpx;
|
padding-right: 10rpx;
|
}
|
|
.search-icons {
|
padding: 16rpx;
|
}
|
|
.word-display {
|
@include words-display;
|
}
|
|
.word-container {
|
padding: 20rpx;
|
|
@at-root {
|
#{&}_header {
|
@include uni-flex;
|
height: $word-container_header-height;
|
line-height: $word-container_header-height;
|
flex-direction: row;
|
justify-content: space-between;
|
align-items: center;
|
|
@at-root {
|
#{&}-text {
|
color: #3e3e3e;
|
font-size: 30rpx;
|
font-weight: bold;
|
}
|
}
|
}
|
|
#{&}_body {
|
@include uni-flex;
|
flex-wrap: wrap;
|
flex-direction: row;
|
|
@at-root {
|
#{&}-text {
|
@include uni-flex;
|
@include words-display;
|
justify-content: center;
|
align-items: center;
|
background-color: #f6f6f6;
|
padding: 10rpx 20rpx;
|
margin: 20rpx 30rpx 0 0;
|
border-radius: 30rpx;
|
/* #ifndef APP-NVUE */
|
box-sizing: border-box;
|
/* #endif */
|
text-align: center;
|
}
|
|
#{&}-info {
|
/* #ifndef APP-NVUE */
|
display: block;
|
/* #endif */
|
flex: 1;
|
text-align: center;
|
font-size: 26rpx;
|
color: #808080;
|
margin-top: 20rpx;
|
}
|
}
|
}
|
}
|
}
|
</style>
|