search.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <s-layout class="set-wrap" title="搜索" :bgStyle="{ color: '#FFF' }">
  3. <view class="ss-p-x-24">
  4. <view class="ss-flex ss-col-center">
  5. <uni-search-bar
  6. class="ss-flex-1"
  7. radius="33"
  8. placeholder="请输入关键字"
  9. cancelButton="none"
  10. :focus="true"
  11. @confirm="onSearch($event.value)"
  12. />
  13. </view>
  14. <view class="ss-flex ss-row-between ss-col-center">
  15. <view class="serach-history">搜索历史</view>
  16. <button class="clean-history ss-reset-button" @tap="onDelete"> 清除搜索历史 </button>
  17. </view>
  18. <view class="ss-flex ss-col-center ss-row-left ss-flex-wrap">
  19. <button
  20. class="history-btn ss-reset-button"
  21. @tap="onSearch(item)"
  22. v-for="(item, index) in state.historyList"
  23. :key="index"
  24. >
  25. {{ item }}
  26. </button>
  27. </view>
  28. </view>
  29. </s-layout>
  30. </template>
  31. <script setup>
  32. import { reactive } from 'vue';
  33. import sheep from '@/sheep';
  34. import { onLoad } from '@dcloudio/uni-app';
  35. const state = reactive({
  36. historyList: [],
  37. });
  38. // 搜索
  39. function onSearch(keyword) {
  40. if (!keyword) return;
  41. saveSearchHistory(keyword);
  42. sheep.$router.go('/pages/goods/list', { keyword });
  43. }
  44. // 保存搜索历史
  45. function saveSearchHistory(keyword) {
  46. // 如果关键词在搜索历史中,则把此关键词先移除
  47. if (state.historyList.includes(keyword)) {
  48. state.historyList.splice(state.historyList.indexOf(keyword), 1);
  49. }
  50. // 置顶关键词
  51. state.historyList.unshift(keyword);
  52. // 最多保留10条记录
  53. if (state.historyList.length >= 10) {
  54. state.historyList.length = 10;
  55. }
  56. uni.setStorageSync('searchHistory', state.historyList);
  57. }
  58. function onDelete() {
  59. uni.showModal({
  60. title: '提示',
  61. content: '确认清除搜索历史吗?',
  62. success: function (res) {
  63. if (res.confirm) {
  64. state.historyTag = [];
  65. uni.removeStorageSync('searchHistory');
  66. }
  67. },
  68. });
  69. }
  70. onLoad(() => {
  71. state.historyList = uni.getStorageSync('searchHistory') || [];
  72. });
  73. </script>
  74. <style lang="scss" scoped>
  75. .serach-title {
  76. font-size: 30rpx;
  77. font-weight: 500;
  78. color: #333333;
  79. }
  80. .uni-searchbar {
  81. padding-left: 0;
  82. }
  83. .serach-history {
  84. font-weight: bold;
  85. color: #333333;
  86. font-size: 30rpx;
  87. }
  88. .clean-history {
  89. font-weight: 500;
  90. color: #999999;
  91. font-size: 28rpx;
  92. }
  93. .history-btn {
  94. padding: 0 38rpx;
  95. height: 60rpx;
  96. background: #f5f6f8;
  97. border-radius: 30rpx;
  98. font-size: 28rpx;
  99. color: #333333;
  100. max-width: 690rpx;
  101. margin: 0 20rpx 20rpx 0;
  102. }
  103. </style>