# 智能搜索

一种高效查找内容的方式,让用户直达目的地。

# 示例

<template>
  <div :style="newBarStyle" class="wrapper">
    <!-- <dof-minibar title="智能搜索"> </dof-minibar>
    <SwitchTheme /> -->
    <div :class="['dof-search-bar']">
      <input
        autofocus="false"
        disabled="false"
        return-key-type="search"
        singleline="true"
        @return="rightBtnClicked"
        @focus="inputFocus"
        ref="search-input"
        v-model="inputContent"
        :placeholder="placeholder"
        :style="{ width: '592px' }"
        :class="['search-bar-input']"
      />
      <div class="search-bar-icon">
        <dof-iconfont size="64" :code="'\u4785'" color="rgba(255,255,255,0.40)"></dof-iconfont>
      </div>
      <!-- <image class="search-bar-icon" :aria-hidden="true" :src="searchIcon"></image> -->
      <!-- <image
        class="search-bar-clear"
        v-if="inputContent && inputContent != ''"
        :aria-hidden="true"
        @click="clearClicked"
        :src="clearIcon"
      ></image> -->
      <dof-iconfont
        class="search-bar-clear"
        v-if="inputContent && inputContent != ''"
        @dofIconFontClicked="clearClicked"
        size="48"
        :code="'\u4725'"
        color="#71717A"
      ></dof-iconfont>
      <text :class="['search-bar-button']" @click="rightBtnClicked">{{ rightLabel }}</text>
    </div>
    <scroller class="scroller" offset-accuracy="20" :loadmoreoffset="offsetValue" @loadmore="onloadmore">
      <auto-complete
        :keyword="inputContent"
        :autocompleteList="menuList"
        @dofAutoCompleteClicked="autoCompleteClicked"
      ></auto-complete>

      <capsule-list
        v-if="menuList && menuList.length == 0 && recipeData && recipeData.length == 0 && !noResult"
        :title="title1"
        :capsule-list="list1"
        @dofCapsuleClicked="dofCapsuleClick"
        @dofDeliconClicked="dofDeliconClick"
      >
      </capsule-list>
      <div class="h16"></div>
      <capsule-list
        v-if="menuList && menuList.length == 0 && recipeData && recipeData.length == 0 && !noResult"
        :title="title2"
        :capsule-list="list2"
        :show-del="false"
        @dofCapsuleClicked="dofCapsuleClick"
        @dofDeliconClicked="dofDeliconClick"
      >
      </capsule-list>
      <!-- 结果列表 -->
      <div v-if="noResult" class="empty-box" :style="{ 'margin-top': '300px' }">
        <!-- <image v-if="!isRecommend" class="empty-image" :src="noResultImg"></image> -->
        <div class="empty-text-box">
          <text class="empty-text">没有找到“</text><text class="empty-text">{{ inputContent }}</text
          ><text class="empty-text">”相关的内容</text>
        </div>
      </div>
      <!-- <flow-card
        v-if="recipeData && recipeData.length > 0 && !isRecommend"
        :recipe-data="recipeData"
        :keyword="inputContent"
      ></flow-card>

      <div v-if="isRecommend">
        <text class="title">配套食谱</text>
        <flow-card :recipe-data="recommendData" :keyword="inputContent"></flow-card>
      </div> -->
    </scroller>
  </div>
</template>

<script>
import { DofMinibar, DofSearchbar, Utils, DofButton, DofCatalog, DofIconfont } from 'dolphin-weex-ui'
import nativeService from 'src/service/nativeService.js'
import { SEARCH_ICON, CLEAR_ICON } from './icon'
import capsuleList from './capsule'
import autoComplete from './autocomplete'
import flowCard from './flowCard'
import SwitchTheme from 'src/components/SwitchTheme.vue'

export default {
  components: {
    DofMinibar,
    DofSearchbar,
    capsuleList,
    autoComplete,
    DofButton,
    DofCatalog,
    flowCard,
    SwitchTheme,
    DofIconfont
  },
  data: () => ({
    noResult: false,
    noResultImg: '../../assets/image/platform/platform-search/img_no_commodity@3x.png',
    searchIcon: SEARCH_ICON,
    clearIcon: CLEAR_ICON,
    placeholder: '搜索你感兴趣的内容',
    inputContent: '',
    rightLabel: '',
    isImmersion: true,
    // capsuleShow:true,
    title1: '历史搜索',
    list1: ['蒜茸炒娃娃菜', '炸薯条', '双皮奶'],
    title2: '热门搜索',
    list2: ['西红柿炒鸡蛋', '蒸鸡蛋'],
    menuList: [],
    offsetValue: 0,
    recipeData: [],
    isRecommend: false, //是否展示推荐
    isSetMenuList: true //解决点击autocomplete点击后,inputcontent 变化引起的menulist重新赋值
  }),

  mounted() {
    if (this.inputContent && this.inputContent != '') {
      this.rightLabel = '搜索'
    } else {
      this.rightLabel = '取消'
    }
  },

  methods: {
    dofCapsuleClick(item) {
      console.log('item', item)
      this.inputContent = item.text
      this.rightBtnClicked() //发起请求
    },

    dofDeliconClick(params) {
      console.log('dofDeliconClick', params)
      if (params.title == this.title1) {
        this.title1 = ''
        this.list1 = []
      } else {
        this.title2 = ''
        this.list2 = []
      }
    },

    clearClicked() {
      this.inputContent && this.inputContent != '' ? (this.inputContent = '') : ''
      this.recipeData = []
      this.menuList = []
      this.noResult = false
      this.isRecommend = false
    },

    autoCompleteClicked(params) {
      console.log('autoCompleteClicked params', params)
      this.inputContent = params.text
      // this.menuList = []  // 这里置空无效,因为inputContent变了,watch中会重新给menulist赋值
      this.isSetMenuList = false
      this.rightBtnClicked()
    },

    rightBtnClicked() {
      nativeService.killKeyboard()
      const { inputContent, rightLabel } = this
      if (rightLabel == '取消') return false
      if (inputContent == '西红柿炒鸡蛋') {
        this.isRecommend = true
      } else {
        this.isRecommend = false
      } //展示无结果和推荐菜单的效果
      this.noResult = false
      if (inputContent == '红烧' || inputContent == '红' || inputContent == '烧') {
        new Promise((resolve, reject) => {
          let data = [],
            food1 = {
              recipeThumb: '../../assets/image/platform/platform-search/food.jpg',
              applianceCate: '电饼铛',
              recipeName: '红烧煎饼果子',
              costTime: '5',
              difficulty: 2,
              kcal: '200',
              likeFlag: true,
              progress: 1,
              autoPlay: false
            },
            food2 = {
              recipeThumb: '../../assets/image/platform/platform-search/food2.jpg',
              applianceCate: '电饭煲',
              recipeName: '香菇红烧鸡肉',
              costTime: '40',
              difficulty: 3,
              kcal: '350',
              likeFlag: false,
              progress: 1,
              autoPlay: false
            }
          for (var index = 0; index < 10; index++) {
            let randomNum = Math.floor(Math.random() * 11)
            randomNum > 5 ? data.push(food1) : data.push(food2)
          }
          let response = { data }
          setTimeout(function() {
            resolve(response)
          }, 250)
        })
          .then(res => {
            console.log('responseList', res)
            this.menuList = []
            this.recipeData = res.data //模拟异步数据赋值
          })
          .catch(err => {
            console.log(err)
          })
      } else {
        this.recipeData = []
        this.menuList = []
        this.noResult = true
      }
    },

    inputFocus() {
      const { inputContent } = this
      // this.noResult = false
      // this.isRecommend = false
    },
    onloadmore() {},
    swipe(e) {
      //监听手势, 刷新loadmoreoffset为0 ,不触发加载更多bug
      if (!e || !e.direction) return
      if (e.direction == 'down') {
        this.offsetValue = 0
      } else {
        this.offsetValue = 1
      }
    }
  },

  computed: {
    statusBarHeight: function() {
      let result = '20'
      if (weex.config.env.statusBarHeight) {
        if (weex.config.env.platform === 'iOS') {
          // iOS使用pt为单位
          result = weex.config.env.statusBarHeight
        } else {
          // 安卓使用px为单位
          result = weex.config.env.statusBarHeight / weex.config.env.scale
        }
      }
      return result
    },

    newBarStyle() {
      let result
      if (this.isImmersion) {
        // 全屏显示,weex自行处理状态栏高度
        result = {
          paddingTop: this.statusBarHeight + 'wx'
          //   height: +this.statusBarHeight + 44 + 'wx',
        }
      } else {
        // 非全屏显示,app已处理状态栏高度
        result = {
          height: '44wx'
        }
      }
      return result
    },

    // 推荐菜谱
    recommendData() {
      let recommendList = [],
        food = {
          recipeThumb: '../../assets/image/platform/platform-search/food.jpg',
          applianceCate: '电饼铛',
          recipeName: '红烧煎饼果子',
          costTime: '5',
          difficulty: 2,
          kcal: '200',
          likeFlag: true,
          progress: 1,
          autoPlay: false
        }
      for (let index = 0; index < 6; index++) {
        recommendList.push(food)
      }
      return recommendList
    }
  },

  watch: {
    inputContent(newVal, oldVal) {
      const { recipeData } = this
      recipeData == [] ? (this.noResult = true) : (this.noResult = false)
      this.isRecommend = false
      if (newVal && newVal != '') {
        this.rightLabel = '搜索'
        this.recipeData = []
        if (newVal == '红烧' || newVal == '红' || newVal == '烧') {
          // setTimeout模拟服务端异步数据
          new Promise((resolve, reject) => {
            let response = {
              data: [
                '红烧',
                '红烧鱼块',
                '红土豆红烧鸡土豆红烧鸡土豆红烧鸡土豆红烧鸡土豆红烧鸡土豆红烧鸡土豆红烧鸡',
                '红烧肥牛'
              ]
            }
            setTimeout(function() {
              resolve(response)
            }, 250)
          })
            .then(res => {
              console.log('responseList', res)
              this.isSetMenuList ? (this.menuList = res.data) : '' //模拟异步数据赋值, 解决点击autocomplete点击后,inputcontent 变化引起的menulist重新赋值
              this.isSetMenuList = true
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          this.menuList = []
        }
      } else {
        this.rightLabel = '取消'
        this.noResult = false
        this.isRecommend = false
        this.menuList = []
        this.recipeData = []
      }
    }
  }
}
</script>

<style scoped>
.wrapper {
  background-color: #151617;
  font-family: PingFangSC-Regular;
}
.dof-search-bar {
  padding-left: 32px;
  padding-right: 32px;
  background-color: #151617;
  width: 750px;
  height: 88px;
  flex-direction: row;
  align-items: center;
}

.search-bar-input {
  position: absolute;
  top: 8px;
  padding-top: 0;
  padding-bottom: 0;
  padding-right: 64px;
  padding-left: 80px;
  font-size: 28px;
  width: 512px;
  height: 72px;
  line-height: 72px;
  background-color: rgba(177, 193, 234, 0.05);
  /* color: #8A8A8F; */
  color: rgba(255, 255, 255, 0.8);
  placeholder-color: rgba(255, 255, 255, 0.4);
}

::-webkit-input-placeholder {
  /* WebKit, Blink, Edge */
  color: #267aff;
  font-weight: bold;
}
.h16 {
  height: 36px;
}

.search-bar-icon {
  position: absolute;
  width: 64px;
  height: 64px;
  left: 48px;
  top: 12px;
}
.search-bar-clear {
  position: absolute;
  width: 48px;
  height: 48px;
  right: 142px;
  top: 20px;
}
.search-bar-button {
  width: 72px;
  font-size: 32px;
  text-align: center;
  line-height: 88px;
  height: 88px;
  margin-right: 0;
  color: #666666;
  position: absolute;
  right: 32px;
}

.scroller {
  flex: 1;
}

.noResultScroller {
  flex-direction: row;
  justify-content: center;
  align-items: center;
}

.empty-box {
  margin-top: 300px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}
.empty-image {
  width: 480px;
  height: 400px;
}
.empty-text-box {
  flex-direction: row;
  justify-content: center;
}
.empty-text {
  margin-top: 32px;
  font-size: 28px;
  line-height: 36px;
  color: rgba(255, 255, 255, 0.8);
}

.title {
  font-size: 36px;
  line-height: 32px;
  margin-top: 128px;
  margin-left: 32px;
  font-weight: bold;
}
</style>


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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453

# capsule.vue

<template>
  <div v-if="show" class="capsule-wrap">
    <div class="head">
      <text class="title">{{ title }}</text>
      <dof-iconfont
        v-if="showDel && capsuleList.length > 0"
        size="56"
        :code="'\u4733'"
        color="rgba(255,255,255,0.40)"
        @dofIconFontClicked="delIconClicked"
      ></dof-iconfont>

      <!-- <image v-if="showDel && capsuleList.length > 0" :src="delIcon" class="img" @click="delIconClicked"></image> -->
    </div>
    <div class="capsule-bar">
      <text class="capsule" @click="selectItem(item, index)" v-for="(item, index) in capsuleList" :key="index">{{
        item
      }}</text>
    </div>
  </div>
</template>

<script>
import { DofIconfont } from 'dolphin-weex-ui'

import { DEL_ICON } from './icon'
export default {
  components: {
    DofIconfont
  },
  data: () => ({
    delIcon: DEL_ICON,
    show: true
  }),

  props: {
    title: {
      type: String,
      default: ''
    },
    capsuleList: {
      type: Array,
      default: () => []
    },
    showDel: {
      type: Boolean,
      default: true
    }
  },

  methods: {
    selectItem(item, index) {
      const text = item
      if (item) {
        this.$emit('dofCapsuleClicked', { text, index })
      }
    },

    delIconClicked() {
      console.info('delIconClicked')
      const { title, capsuleList } = this
      this.show = false
      this.$emit('dofDeliconClicked', { title, capsuleList })
    }
  },

  computed: {}
}
</script>

<style scoped>
.capsule-wrap {
  margin-top: 56px;
}

.head {
  flex-direction: row;
  margin-left: 32px;
  margin-right: 28px;
  /* margin-top: 48px; */
  margin-bottom: 28px;
  justify-content: space-between;
  align-items: center;
}
.title {
  /* margin-left: 32px; */
  font-family: PingFangSC-Regular;
  font-size: 32px;
  color: rgba(255, 255, 255, 0.8);
}

.img {
  width: 56px;
  height: 56px;
}

.capsule-bar {
  flex-direction: row;
  justify-content: flex-start;
  margin-left: 16px;
}

.capsule {
  height: 64px; /* padding-top, padding-bottom  18 */
  line-height: 64px;
  padding-left: 24px;
  padding-right: 24px;
  font-family: PingFangSC-Regular;
  font-size: 28px;
  color: rgba(255, 255, 255, 0.4);
  margin-left: 16px;
  border-width: 2px;
  border-style: solid;
  border-color: rgba(255, 255, 255, 0.2);
}
</style>


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
点我扫二维码 查看demo
Last Updated: 3/15/2024, 3:36:46 PM