search.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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"
  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.historyTag"
  23. :key="index"
  24. >
  25. {{ item }}
  26. </button>
  27. </view>
  28. </view>
  29. </s-layout>
  30. </template>
  31. <script setup>
  32. import { computed, reactive } from 'vue';
  33. import sheep from '@/sheep';
  34. import { onLoad } from '@dcloudio/uni-app';
  35. const state = reactive({
  36. historyTag: [],
  37. });
  38. function onSearch(res) {
  39. if (res.value) {
  40. if (!state.historyTag.includes(res.value)) {
  41. let search = getArr(state.historyTag, res.value);
  42. uni.setStorageSync('searchHistory', search);
  43. sheep.$router.go('/pages/goods/list', { keyword: res.value });
  44. } else {
  45. sheep.$router.go('/pages/goods/list', { keyword: res.value });
  46. }
  47. } else {
  48. sheep.$router.go('/pages/goods/list', { keyword: res });
  49. }
  50. }
  51. function getArr(list, item) {
  52. let arr = list;
  53. let length = 10; //队列长度
  54. arr.length < length ? arr.unshift(item) : arr.pop();
  55. return arr;
  56. }
  57. function onDelete() {
  58. uni.showModal({
  59. title: '提示',
  60. content: '确认清除搜索历史吗?',
  61. success: function (res) {
  62. if (res.confirm) {
  63. state.historyTag = [];
  64. uni.removeStorageSync('search');
  65. }
  66. },
  67. });
  68. }
  69. onLoad(() => {
  70. uni.getStorage({
  71. key: 'searchHistory',
  72. success: function (res) {
  73. state.historyTag = res.data;
  74. },
  75. });
  76. });
  77. </script>
  78. <style lang="scss" scoped>
  79. .serach-title {
  80. font-size: 30rpx;
  81. font-weight: 500;
  82. color: #333333;
  83. }
  84. .uni-searchbar {
  85. padding-left: 0;
  86. }
  87. .serach-history {
  88. font-weight: bold;
  89. color: #333333;
  90. font-size: 30rpx;
  91. }
  92. .clean-history {
  93. font-weight: 500;
  94. color: #999999;
  95. font-size: 28rpx;
  96. }
  97. .history-btn {
  98. padding: 0 38rpx;
  99. height: 60rpx;
  100. background: #f5f6f8;
  101. border-radius: 30rpx;
  102. font-size: 28rpx;
  103. color: #333333;
  104. max-width: 690rpx;
  105. margin: 0 20rpx 20rpx 0;
  106. }
  107. </style>