search.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <template>
  2. <view class="page">
  3. <!-- 全局搜索框 -->
  4. <Search placeholder="请输入关键字" showCancelBtn padding="0 30rpx 20rpx" @change="searchChange" @search="goSearchResult" ></Search>
  5. <!-- 最近搜索 -->
  6. <TitleOperate v-if="historyList.length > 0" title="最近搜索" padding="10rpx 30rpx 24rpx 30rpx">
  7. <view class="remove" @click="showRemoveHistory = true"><u-icon name="trash"></u-icon></view>
  8. </TitleOperate>
  9. <view class="list" v-if="historyList.length > 0">
  10. <Tag v-for="(item, index) in historyList" :key="index" @click="goSearchResult(item)">{{ item }}</Tag>
  11. </view>
  12. <!-- 间隔 -->
  13. <view style="height: 30rpx; width: 100%;"></view>
  14. <!-- 热门搜索 -->
  15. <TitleOperate v-if="hotList.length > 0" title="热门搜索" padding="10rpx 30rpx 24rpx 30rpx"></TitleOperate>
  16. <view class="list" v-if="hotList.length > 0">
  17. <Tag v-for="(item, index) in hotList" :key="index" @click="goSearchResult(item)">{{ item }}</Tag>
  18. </view>
  19. <!-- 删除最近搜索的模态框 -->
  20. <u-modal v-model="showRemoveHistory" content="是否清空最近搜索记录?" @confirm="removeHistory" show-cancel-button :confirm-color="confirmColor"></u-modal>
  21. </view>
  22. </template>
  23. <script>
  24. import Search from '@/components/search/search.vue';
  25. import TitleOperate from '@/components/title-operate.vue';
  26. import Tag from '@/components/tag.vue';
  27. var _self;
  28. export default {
  29. components: { Search, TitleOperate, Tag },
  30. data() {
  31. return {
  32. // 直接写在html里不识别,需要在data里转一下
  33. confirmColor: this.$appTheme.appThemeColor.toString(),
  34. // 是否显示删除最近搜索的模态框
  35. showRemoveHistory: false,
  36. // 是否正在搜索,用于判断是否显示最近搜索和热门搜索
  37. isInput: false,
  38. // 最近搜索
  39. historyList: [],
  40. // 热门搜索
  41. hotList: []
  42. };
  43. },
  44. onLoad() {
  45. _self = this;
  46. const hist = uni.getStorageSync('historySearch');
  47. console.log('historySearch>>>',hist)
  48. if(hist){
  49. this.historyList = [].concat(hist);
  50. }
  51. },
  52. methods: {
  53. // 搜索框正在输入
  54. searchChange(e) {
  55. if (e) {
  56. this.isInput = true;
  57. } else {
  58. this.isInput = false;
  59. }
  60. },
  61. // 跳转搜索结果页
  62. goSearchResult(e) {
  63. this.saveSeaHistory(e);
  64. uni.navigateTo({
  65. url: '/pages/search-result?key='+e
  66. });
  67. },
  68. //保存搜索记录
  69. saveSeaHistory(e) {
  70. let _self = this;
  71. let index = _self.historyList.indexOf(e);
  72. if (index != -1) {
  73. //获取 e 在数组中的位置
  74. // let thisindex = 0;
  75. // for (var i = 0; i < _self.historyList.length; i++) {
  76. // if (_self.historyList[i] == e)
  77. // thisindex = i;
  78. // }
  79. //将 e 在数组中删除
  80. _self.historyList.splice(index, 1);
  81. }
  82. //将 e 重新追加到数组第一位
  83. _self.historyList.unshift(e);
  84. //超过10位移除
  85. _self.historyList.splice(20, 1);
  86. //存入缓存
  87. uni.setStorageSync('historySearch', _self.historyList);
  88. },
  89. // 清空最近搜索记录
  90. removeHistory() {
  91. this.historyList = [];
  92. uni.removeStorageSync('historySearch');
  93. },
  94. }
  95. };
  96. </script>
  97. <style lang="scss" scoped>
  98. .page {
  99. background-color: #fff;
  100. }
  101. .list {
  102. padding: 0 30rpx;
  103. }
  104. </style>