search.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  48. }
  49. function onSearchList(item) {
  50. sheep.$router.go('/pages/goods/list', { keyword: item });
  51. }
  52. function getArr(list, item) {
  53. let arr = list;
  54. let length = 10; //队列长度
  55. arr.length < length ? arr.unshift(item) : arr.pop();
  56. return arr;
  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. uni.getStorage({
  72. key: 'searchHistory',
  73. success: function (res) {
  74. state.historyTag = res.data;
  75. },
  76. });
  77. });
  78. </script>
  79. <style lang="scss" scoped>
  80. .serach-title {
  81. font-size: 30rpx;
  82. font-weight: 500;
  83. color: #333333;
  84. }
  85. .uni-searchbar {
  86. padding-left: 0;
  87. }
  88. .serach-history {
  89. font-weight: bold;
  90. color: #333333;
  91. font-size: 30rpx;
  92. }
  93. .clean-history {
  94. font-weight: 500;
  95. color: #999999;
  96. font-size: 28rpx;
  97. }
  98. .history-btn {
  99. padding: 0 38rpx;
  100. height: 60rpx;
  101. background: #f5f6f8;
  102. border-radius: 30rpx;
  103. font-size: 28rpx;
  104. color: #333333;
  105. max-width: 690rpx;
  106. margin: 0 20rpx 20rpx 0;
  107. }
  108. </style>