search.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <!-- 搜索界面 -->
  2. <template>
  3. <s-layout class="set-wrap" title="搜索" :bgStyle="{ color: '#FFF' }">
  4. <view class="ss-p-x-24">
  5. <view class="ss-flex ss-col-center">
  6. <uni-search-bar
  7. class="ss-flex-1"
  8. radius="33"
  9. placeholder="请输入关键字"
  10. cancelButton="none"
  11. :focus="true"
  12. @confirm="onSearch($event.value)"
  13. />
  14. </view>
  15. <view class="ss-flex ss-row-between ss-col-center">
  16. <view class="serach-history">搜索历史</view>
  17. <button class="clean-history ss-reset-button" @tap="onDelete"> 清除搜索历史 </button>
  18. </view>
  19. <view class="ss-flex ss-col-center ss-row-left ss-flex-wrap">
  20. <button
  21. class="history-btn ss-reset-button"
  22. @tap="onSearch(item)"
  23. v-for="(item, index) in state.historyList"
  24. :key="index"
  25. >
  26. {{ item }}
  27. </button>
  28. </view>
  29. </view>
  30. </s-layout>
  31. </template>
  32. <script setup>
  33. import { reactive } from 'vue';
  34. import sheep from '@/sheep';
  35. import { onLoad } from '@dcloudio/uni-app';
  36. const state = reactive({
  37. historyList: [],
  38. });
  39. // 搜索
  40. function onSearch(keyword) {
  41. if (!keyword) {
  42. return;
  43. }
  44. saveSearchHistory(keyword);
  45. // 前往商品列表(带搜索条件)
  46. sheep.$router.go('/pages/goods/list', { keyword });
  47. }
  48. // 保存搜索历史
  49. function saveSearchHistory(keyword) {
  50. // 如果关键词在搜索历史中,则把此关键词先移除
  51. if (state.historyList.includes(keyword)) {
  52. state.historyList.splice(state.historyList.indexOf(keyword), 1);
  53. }
  54. // 置顶关键词
  55. state.historyList.unshift(keyword);
  56. // 最多保留 10 条记录
  57. if (state.historyList.length >= 10) {
  58. state.historyList.length = 10;
  59. }
  60. uni.setStorageSync('searchHistory', state.historyList);
  61. }
  62. function onDelete() {
  63. uni.showModal({
  64. title: '提示',
  65. content: '确认清除搜索历史吗?',
  66. success: function (res) {
  67. if (res.confirm) {
  68. state.historyTag = [];
  69. uni.removeStorageSync('searchHistory');
  70. }
  71. },
  72. });
  73. }
  74. onLoad(() => {
  75. state.historyList = uni.getStorageSync('searchHistory') || [];
  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>