123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <template name="zy-search">
- <view>
- <view class="search">
- <!-- #ifdef APP-PLUS -->
- <image src="../../static/zy-search/voice.svg" mode="aspectFit" @click="startRecognize()" class="voice-icon"></image>
- <!-- #endif -->
- <template v-if="isFocus">
- <input maxlength="20" focus type="text" value="" confirm-type="search" @confirm="searchStart()" placeholder="请输入关键词搜索" v-model.trim="searchText"/>
- </template>
- <template v-else>
- <input maxlength="20" type="text" value="" confirm-type="search" @confirm="searchStart()" placeholder="请输入关键词搜索" v-model.trim="searchText"/>
- </template>
- <image src="../../static/zy-search/search.svg" mode="aspectFit" @click="searchStart()" class="search-icon"></image>
- </view>
- <view :class="'s-' + theme" v-if="hList.length > 0">
- <view class="header">
- 历史记录
- <image src="../../static/zy-search/delete.svg" mode="aspectFit" @click="delhistory"></image>
- </view>
- <view class="list">
- <view v-for="(item,index) in hList" :key="index" @click="keywordsClick(item)">{{item}}</view>
- </view>
- </view>
- <view :class="'wanted-' + theme" v-if="showWant">
- <view class="header">猜你想搜的</view>
- <view class="list">
- <view v-for="(item,index) in hotList" :key="index" @click="keywordsClick(item)">{{item}}</view>
- </view>
- </view>
- </view>
- </template>
- <script>
- export default{
- name:"zy-search",
- props:{
- isFocus:{ //是否自动获取焦点
- type: Boolean,
- default: false
- },
- theme:{ //选择块级显示还是圆形显示
- type: String,
- default: 'block'
- },
- showWant:{ //是否展示推荐菜单
- type: Boolean,
- default: false
- },
- hotList: { //推荐列表数据
- type: Array,
- default () {
- return []
- }
- },
- speechEngine: { //语音引擎=>讯飞:iFly,百度:'baidu'
- type: String,
- default: 'baidu'
- }
- },
- data() {
- return {
- searchText:'', //搜索关键词
- hList:uni.getStorageSync('search_cache') //历史记录
- };
- },
- methods: {
- searchStart: function() { //触发搜索
- let _this = this;
- if (_this.searchText == '') {
- uni.showToast({
- title: '请输入关键字',
- icon: 'none',
- duration: 1000
- });
- }else{
- _this.$emit('getSearchText', _this.searchText);
- uni.getStorage({
- key:'search_cache',
- success(res){
- let list = res.data;
- if(list.length > 5){
- for(let item of list){
- if(item == _this.searchText){
- return;
- }
- }
- list.pop();
- list.unshift(_this.searchText);
- }else{
- for(let item of list){
- if(item == _this.searchText){
- return;
- }
- }
- list.unshift(_this.searchText);
- }
- _this.hList = list;
- uni.setStorage({
- key: 'search_cache',
- data: _this.hList
- });
- },
- fail() {
- _this.hList = [];
- _this.hList.push(_this.searchText);
- uni.setStorage({
- key: 'search_cache',
- data: _this.hList
- });
- _this.$emit('getSearchText', _this.searchText);
- }
- })
- }
- },
- keywordsClick (item) { //关键词搜索与历史搜索
- this.searchText = item;
- this.searchStart();
- },
- delhistory () { //清空历史记录
- this.hList = [];
- uni.setStorage({
- key: 'search_cache',
- data: []
- });
- },
- startRecognize: function() { //语音输入
- let _this = this;
- let options = {};
- options.engine = _this.speechEngine;
- options.punctuation = false; // 是否需要标点符号
- options.timeout = 10 * 1000;
- plus.speech.startRecognize(options, function(s) {
- _this.searchText = _this.searchText + s;
- });
- }
- }
- }
- </script>
- <style scoped>
- .search{
- width: 640upx;
- margin: 30upx auto 0;
- position: relative;
- input{
- background-color: #F7F7F7;
- padding: 10upx 74upx;
- font-size: 28upx;
- border-radius: 50upx;
- }
- .voice-icon{
- width: 36upx;
- height: 36upx;
- padding: 16upx 20upx 16upx 0;
- position: absolute;
- left: 16upx;
- top: 4upx;
- z-index: 10;
- }
- .search-icon{
- width: 36upx;
- height: 36upx;
- padding: 16upx 20upx 16upx 0;
- position: absolute;
- right: 0;
- top: -2upx;
- z-index: 10;
- }
- }
- .s-block{
- margin-top: 30upx;
- .header{
- font-size: 32upx;
- padding: 30upx;
- position: relative;
- image{
- width: 36upx;
- height: 36upx;
- padding: 10upx;
- position: absolute;
- right: 40upx;
- top: 24upx;
- }
- }
- .list{
- display: flex;
- flex-wrap: wrap;
- view{
- width: 50%;
- color: #8A8A8A;
- font-size: 28upx;
- box-sizing: border-box;
- text-align: center;
- padding: 20upx 0;
- border-top: 2upx solid #FFF;
- border-left: 2upx solid #FFF;
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- background-color: #F7F7F7;
- }
- }
- }
- .s-circle{
- margin-top: 30upx;
- .header{
- font-size: 32upx;
- padding: 30upx;
- border-bottom: 2upx solid #F9F9F9;
- position: relative;
- image{
- width: 36upx;
- height: 36upx;
- padding: 10upx;
- position: absolute;
- right: 40upx;
- top: 24upx;
- }
- }
- .list{
- display: flex;
- flex-wrap: wrap;
- padding: 0 30upx 20upx;
- view{
- padding: 8upx 30upx;
- margin: 20upx 30upx 0 0;
- font-size: 28upx;
- color: #8A8A8A;
- background-color: #F7F7F7;
- box-sizing: border-box;
- text-align: center;
- border-radius: 20upx;
- }
- }
- }
- .wanted-block{
- margin-top: 30upx;
- .header{
- font-size: 32upx;
- padding: 30upx;
- }
- .list{
- display: flex;
- flex-wrap: wrap;
- view{
- width: 50%;
- color: #8A8A8A;
- font-size: 28upx;
- box-sizing: border-box;
- text-align: center;
- padding: 20upx 0;
- border-top: 2upx solid #FFF;
- border-left: 2upx solid #FFF;
- background-color: #F7F7F7;
- overflow: hidden;
- white-space: nowrap;
- text-overflow: ellipsis;
- }
- }
- }
- .wanted-circle{
- margin-top: 30upx;
- .header{
- font-size: 32upx;
- padding: 30upx;
- }
- .list{
- display: flex;
- flex-wrap: wrap;
- padding: 0 30upx 20upx;
- view{
- padding: 8upx 30upx;
- margin: 20upx 30upx 0 0;
- font-size: 28upx;
- color: #8A8A8A;
- background-color: #F7F7F7;
- box-sizing: border-box;
- text-align: center;
- border-radius: 20upx;
- }
- }
- }
- </style>
|