search.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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="onSearchList(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.value });
  49. }
  50. }
  51. function onSearchList(item) {
  52. sheep.$router.go('/pages/goods/list', { keyword: item });
  53. }
  54. function getArr(list, item) {
  55. let arr = list;
  56. let length = 10; //队列长度
  57. arr.length < length ? arr.unshift(item) : arr.pop();
  58. return arr;
  59. }
  60. function onDelete() {
  61. uni.showModal({
  62. title: '提示',
  63. content: '确认清除搜索历史吗?',
  64. success: function (res) {
  65. if (res.confirm) {
  66. state.historyTag = [];
  67. uni.removeStorageSync('searchHistory');
  68. }
  69. },
  70. });
  71. }
  72. onLoad(() => {
  73. uni.getStorage({
  74. key: 'searchHistory',
  75. success: function (res) {
  76. state.historyTag = res.data;
  77. },
  78. });
  79. });
  80. </script>
  81. <style lang="scss" scoped>
  82. .serach-title {
  83. font-size: 30rpx;
  84. font-weight: 500;
  85. color: #333333;
  86. }
  87. .uni-searchbar {
  88. padding-left: 0;
  89. }
  90. .serach-history {
  91. font-weight: bold;
  92. color: #333333;
  93. font-size: 30rpx;
  94. }
  95. .clean-history {
  96. font-weight: 500;
  97. color: #999999;
  98. font-size: 28rpx;
  99. }
  100. .history-btn {
  101. padding: 0 38rpx;
  102. height: 60rpx;
  103. background: #f5f6f8;
  104. border-radius: 30rpx;
  105. font-size: 28rpx;
  106. color: #333333;
  107. max-width: 690rpx;
  108. margin: 0 20rpx 20rpx 0;
  109. }
  110. </style>