search.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <template>
  2. <view class="margin-sm">
  3. <use-tabbar :tabbar="false"></use-tabbar>
  4. <!-- 搜索框区域 -->
  5. <view class="search-area margin-bottom-sm pos-r w-full dflex-b border-radius-big margin-top-sm">
  6. <view class="dflex-c flex1 pos-r">
  7. <text class="iconfont iconsousuo-01 search-icon"></text>
  8. <input
  9. class="search-input"
  10. placeholder="请输入关键字"
  11. maxlength="20"
  12. :focus="true"
  13. v-model="keyword"
  14. type="text" />
  15. </view>
  16. <view class="bg-base padding-tb-16 padding-lr-lg" @click="doSearch">搜索</view>
  17. </view>
  18. <!-- 搜索历史 -->
  19. <view class="padding padding-bottom-xs pos-r w-full margin-bottom-sm bg-main border-radius"
  20. v-if="historyList.length > 0">
  21. <view class="padding-bottom-sm dflex-b">
  22. <view class="dflex margin-left-sm">
  23. <text class="fwb ft-dark">搜索历史</text>
  24. </view>
  25. <view class="iconfont iconlajitong-01 pos-a pos-right dflex-c ft-dark padding" @click="clearHistory"></view>
  26. </view>
  27. <view class="dflex dflex-wrap-w">
  28. <view
  29. class="item margin-right-sm margin-bottom-sm dflex bg-drak border-radius-lg padding-tb-xs padding-lr"
  30. v-for="(item, index) in historyList" :key="index" @click="selectHistory(item)">
  31. <text class="clamp" style="max-width: 25vw;">{{item}}</text>
  32. </view>
  33. </view>
  34. </view>
  35. <!-- 热门搜索 -->
  36. <view class="padding w-full bg-main padding-bottom-xs border-radius"
  37. v-if="fetch && fetch.hot && fetch.hot.length > 0">
  38. <view class="padding-bottom-sm dflex-b">
  39. <view class="dflex margin-left-sm">
  40. <text class="fwb ft-dark">热门搜索</text>
  41. </view>
  42. </view>
  43. <view class="dflex dflex-wrap-w">
  44. <view
  45. class="item margin-right-sm margin-bottom-sm dflex bg-drak border-radius-lg padding-tb-xs padding-lr"
  46. v-for="(item, index) in fetch.hot" :key="index" @click="selectHot(item)">
  47. <text>{{item.value}}</text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. data() {
  56. return {
  57. id: 0,
  58. source: 1,
  59. max: 20,
  60. fetch: {},
  61. // 搜索关键字
  62. keyword: '',
  63. // 历史搜索
  64. historyList: [],
  65. };
  66. },
  67. onLoad(options) {
  68. if (options) {
  69. if (!options.id) {
  70. return this.Dever.goHome();
  71. }
  72. if (options.source) {
  73. this.source = options.source
  74. }
  75. this.id = options.id;
  76. }
  77. this.keyword = '';
  78. this.loadData();
  79. this.loadHistory();
  80. },
  81. methods: {
  82. loadData() {
  83. this.DeverApi.get(this, 'source.search', {
  84. channel_id: this.id,
  85. source: this.source
  86. });
  87. },
  88. doSearch() {
  89. if (!this.keyword.trim()) return
  90. // 保存搜索词到历史
  91. this.saveHistory(this.keyword.trim())
  92. var url = 'source/list?search=' + this.keyword + '&id=' + this.id + '&type=2&source=' + this.source;
  93. this.keyword = '';
  94. this.Dever.location(url)
  95. },
  96. // 保存到本地缓存
  97. saveHistory(keyword) {
  98. let list = this.historyList
  99. // 去重:如果已存在,先删掉
  100. list = list.filter(item => item !== keyword)
  101. list.unshift(keyword) // 插入到最前
  102. if (list.length > this.max) {
  103. list = list.slice(0, this.max)
  104. }
  105. this.historyList = list
  106. uni.setStorageSync('searchHistory', list)
  107. },
  108. // 加载历史
  109. loadHistory() {
  110. const list = uni.getStorageSync('searchHistory') || []
  111. this.historyList = list
  112. },
  113. // 清空历史
  114. clearHistory() {
  115. this.Dever.confirm('确定清空搜索历史吗?', res => {
  116. this.historyList = []
  117. uni.removeStorageSync('searchHistory')
  118. })
  119. },
  120. // 点击历史记录
  121. selectHistory(item) {
  122. this.keyword = item
  123. this.doSearch()
  124. },
  125. // 点击热门搜索
  126. selectHot(item) {
  127. this.keyword = item.value
  128. this.doSearch()
  129. }
  130. }
  131. };
  132. </script>
  133. <style>
  134. .search-area {
  135. background: #fff;
  136. position: relative;
  137. }
  138. .search-icon {
  139. position: absolute;
  140. left: 14px;
  141. top: 50%;
  142. transform: translateY(-50%);
  143. font-size: 28rpx;
  144. color: #999;
  145. z-index: 2;
  146. }
  147. .search-input {
  148. padding-left: 60rpx; /* 给图标留空间 */
  149. height: 72rpx;
  150. line-height: 72rpx;
  151. font-size: 28rpx;
  152. color: #333;
  153. box-sizing: border-box;
  154. width: 100%;
  155. background-color: #fff;
  156. }
  157. .uni-input-placeholder {
  158. color: grey;
  159. }
  160. </style>