search.vue 4.8 KB

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