search.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <template>
  2. <view>
  3. <view class="search-box">
  4. <!-- 使用 uni-ui 提供的搜索组件 -->
  5. <uni-search-bar @input="input" :radius="100" cancelButton="none" :focus="true"></uni-search-bar>
  6. </view>
  7. <!-- 搜索建议列表 -->
  8. <view class="sugg-list" v-if="searchResults.length !== 0">
  9. <view class="sugg-item" v-for="(item, i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
  10. <view class="goods-name">{{item.goods_name}}</view>
  11. <uni-icons type="arrowright" size="16"></uni-icons>
  12. </view>
  13. </view>
  14. <!-- 搜索历史 -->
  15. <view class="history-box" v-else>
  16. <!-- 标题区域 -->
  17. <view class="history-title">
  18. <text>搜索历史</text>
  19. <uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
  20. </view>
  21. <!-- 列表区域 -->
  22. <view class="history-list">
  23. <uni-tag :text="item" v-for="(item, i) in historys" :key="i" @click="gotoGoodsList(item)">
  24. </uni-tag>
  25. </view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. computed: {
  32. historys() {
  33. // 注意:由于数组是引用类型,所以不要直接基于原数组调用 reverse 方法,以免修改原数
  34. 组中元素的顺序
  35. // 而是应该新建一个内存无关的数组,再进行 reverse 反转
  36. return [...this.historyList].reverse()
  37. }
  38. },
  39. onLoad() {
  40. this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
  41. },
  42. data() {
  43. return {
  44. // 延时器的 timerId
  45. timer: null,
  46. // 搜索关键词
  47. kw: '',
  48. // 搜索结果列表
  49. searchResults: [],
  50. // 搜索关键词的历史记录
  51. historyList: ['a', 'app', 'apple']
  52. };
  53. },
  54. methods: {
  55. input(e) {
  56. // e.value 是最新的搜索内容
  57. // console.log(e.value)
  58. // 清除 timer 对应的延时器
  59. clearTimeout(this.timer)
  60. // 重新启动一个延时器,并把 timerId 赋值给 this.timer
  61. this.timer = setTimeout(() => {
  62. // 如果 500 毫秒内,没有触发新的输入事件,则为搜索关键词赋值
  63. this.kw = e.value
  64. console.log(this.kw)
  65. // 根据关键词,查询搜索建议列表
  66. this.getSearchList()
  67. }, 500)
  68. },
  69. // 根据搜索关键词,搜索商品建议列表
  70. async getSearchList() {
  71. // 判断关键词是否为空
  72. if (this.kw === '') {
  73. this.searchResults = []
  74. return
  75. }
  76. // 发起请求,获取搜索建议列表
  77. const { data: res } = await uni.$http.get('/api/public/v1/goods/qsearch', { query: this.kw })
  78. if (res.meta.status !== 200) return uni.$showMsg()
  79. this.searchResults = res.message
  80. // 1. 查询到搜索建议之后,调用 saveSearchHistory() 方法保存搜索关键词
  81. this.saveSearchHistory()
  82. },
  83. // 2. 保存搜索关键词的方法
  84. saveSearchHistory() {
  85. // 2.1 直接把搜索关键词 push 到 historyList 数组中
  86. // this.historyList.push(this.kw)
  87. // 1. 将 Array 数组转化为 Set 对象
  88. const set = new Set(this.historyList)
  89. // 2. 调用 Set 对象的 delete 方法,移除对应的元素
  90. set.delete(this.kw)
  91. // 3. 调用 Set 对象的 add 方法,向 Set 中添加元素
  92. set.add(this.kw)
  93. // 4. 将 Set 对象转化为 Array 数组
  94. this.historyList = Array.from(set)
  95. // 调用 uni.setStorageSync(key, value) 将搜索历史记录持久化存储到本地
  96. uni.setStorageSync('kw', JSON.stringify(this.historyList))
  97. },
  98. gotoDetail(goods_id) {
  99. uni.navigateTo({
  100. // 指定详情页面的 URL 地址,并传递 goods_id 参数
  101. url: '/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
  102. })
  103. },
  104. // 清空搜索历史记录
  105. cleanHistory() {
  106. // 清空 data 中保存的搜索历史
  107. this.historyList = []
  108. // 清空本地存储中记录的搜索历史
  109. uni.setStorageSync('kw', '[]')
  110. },
  111. // 点击跳转到商品列表页面
  112. gotoGoodsList(kw) {
  113. uni.navigateTo({
  114. url: '/subpkg/goods_list/goods_list?query=' + kw
  115. })
  116. }
  117. }
  118. }
  119. </script>
  120. <style lang="scss">
  121. .uni-searchbar {
  122. /* 将默认的 #FFFFFF 改为 #C00000 */
  123. background-color: #c00000;
  124. }
  125. .search-box {
  126. position: sticky;
  127. top: 0;
  128. z-index: 999;
  129. }
  130. .sugg-list {
  131. padding: 0 5px;
  132. .sugg-item {
  133. font-size: 12px;
  134. padding: 13px 0;
  135. border-bottom: 1px solid #efefef;
  136. display: flex;
  137. align-items: center;
  138. justify-content: space-between;
  139. .goods-name {
  140. // 文字不允许换行(单行文本)
  141. white-space: nowrap;
  142. // 溢出部分隐藏
  143. overflow: hidden;
  144. // 文本溢出后,使用 ... 代替
  145. text-overflow: ellipsis;
  146. margin-right: 3px;
  147. }
  148. }
  149. }
  150. .history-box {
  151. padding: 0 5px;
  152. .history-title {
  153. display: flex;
  154. justify-content: space-between;
  155. align-items: center;
  156. height: 40px;
  157. font-size: 13px;
  158. border-bottom: 1px solid #efefef;
  159. }
  160. .history-list {
  161. display: flex;
  162. flex-wrap: wrap;
  163. .uni-tag {
  164. margin-top: 5px;
  165. margin-right: 5px;
  166. }
  167. }
  168. }
  169. </style>