search.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <template>
  2. <view :style="themeColor()">
  3. <view class="flex items-center px-[20rpx] h-[120rpx]">
  4. <view class="h-[68rpx] bg-[var(--temp-bg)] px-[30rpx] flex items-center rounded-[100rpx] flex-1">
  5. <text class="nc-iconfont nc-icon-sousuo-duanV6xx1 text-[var(--text-color-light9)] text-[26rpx] mr-[18rpx]"></text>
  6. <input class="text-[28rpx] flex-1" maxlength="50" type="text" v-model="inputValue" placeholder="请搜索您想要的商品" confirm-type="search" placeholderClass="text-[var(--text-color-light9)] text-[28rpx]" @confirm="search">
  7. <text v-if="inputValue" class="nc-iconfont nc-icon-cuohaoV6xx text-[24rpx] text-[var(--text-color-light9)]" @click="inputValue=''"></text>
  8. </view>
  9. <text @click.stop="search()" class="text-[28rpx] ml-[32rpx] -mb-[2rpx]">搜索</text>
  10. </view>
  11. <view class="search-content">
  12. <!-- 历史搜索 -->
  13. <view class="history" v-if="historyList.length">
  14. <view class="history-box">
  15. <view class="history-top">
  16. <view class="title font-500">历史搜索</view>
  17. <view class="icon nc-iconfont nc-icon-shanchu-yuangaizhiV6xx !text-[24rpx] text-[var(--text-color-light9)]" @click="deleteHistoryList"></view>
  18. </view>
  19. <view class="history-bottom " id="history-list" :style="{ maxHeight: !isAllHistory ? '100%' : '168rpx' }">
  20. <view class="history-li" v-for="(item, index) in historyList" :key="index" @click="otherSearch(item)">
  21. <view>{{ item }}</view>
  22. </view>
  23. <view class="history-li history_more" v-if="isAllHistory" @click="isAllHistory = false">
  24. <view>
  25. <text class="text-[30rpx] nc-iconfont nc-icon-xiaV6xx"></text>
  26. </view>
  27. </view>
  28. </view>
  29. </view>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script setup lang="ts">
  35. import { onLoad, onShow } from '@dcloudio/uni-app'
  36. import { ref, nextTick } from 'vue';
  37. import { redirect } from '@/utils/common';
  38. import useConfigStore from "@/stores/config";
  39. const inputValue = ref('') //搜索框的值
  40. const historyList = ref([]) //历史搜索记录
  41. const isAllHistory = ref(false)
  42. onLoad((options:any) => {
  43. uni.getStorageSync('goodsSearchHistory') ? '' : uni.setStorageSync('goodsSearchHistory', []);
  44. });
  45. onShow(() => {
  46. findHistoryList()
  47. nextTick(()=> {
  48. getHistoryHeight();
  49. });
  50. })
  51. //获取历史搜索记录
  52. const findHistoryList =()=> {
  53. historyList.value = uni.getStorageSync('goodsSearchHistory').reverse();
  54. }
  55. // 删除所有历史记录
  56. const deleteHistoryList =()=> {
  57. uni.showModal({
  58. title: '提示',
  59. content: '确认删除全部历史记录?',
  60. confirmColor: useConfigStore().themeColor['--primary-color'],
  61. success: res => {
  62. if (res.confirm) {
  63. uni.setStorageSync('goodsSearchHistory', []);
  64. findHistoryList();
  65. }
  66. }
  67. });
  68. }
  69. //搜索
  70. const search = ()=> {
  71. if (inputValue.value.trim() != '') {
  72. // 对历史搜索处理,判断有无,最近搜索显示在最前
  73. let historyList = uni.getStorageSync('goodsSearchHistory');
  74. let array = [];
  75. if (historyList.length) {
  76. array = historyList.filter(v => {
  77. return v != inputValue.value.trim();
  78. });
  79. array.push(inputValue.value.trim());
  80. } else {
  81. array.push(inputValue.value.trim());
  82. }
  83. uni.setStorageSync('goodsSearchHistory', array);
  84. redirect({ url: '/addon/mall/pages/goods/list', param: { goods_name: inputValue.value.trim() }, mode: 'navigateTo' })
  85. }
  86. }
  87. // 点击历史搜索
  88. const otherSearch = (e:any)=> {
  89. inputValue.value = e;
  90. search();
  91. }
  92. // 获取元素高度
  93. const getHistoryHeight =()=> {
  94. const query = uni.createSelectorQuery().in(this);
  95. query.select('#history-list').boundingClientRect(data => {
  96. if (data && data.height > uni.upx2px(70) * 2 + uni.upx2px(35) * 2) {
  97. isAllHistory.value = true;
  98. }
  99. }).exec();
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. .content {
  104. width: 100vw;
  105. /* #ifdef MP */
  106. height: 100vh;
  107. /* #endif */
  108. /* #ifdef H5 */
  109. height: calc(100vh - env(safe-area-inset-bottom) - var(--status-bar-height));
  110. height: calc(100vh - constant(safe-area-inset-bottom) - var(--status-bar-height));
  111. /* #endif */
  112. /* #ifdef APP-PLUS */
  113. height: calc(100vh - 44px - env(safe-area-inset-bottom));
  114. height: calc(100vh - 44px - constant(safe-area-inset-bottom));
  115. /* #endif */
  116. background: #ffffff;
  117. }
  118. .search-content {
  119. box-sizing: border-box;
  120. background: #ffffff;
  121. }
  122. .history {
  123. width: 100%;
  124. box-sizing: border-box;
  125. .history-box {
  126. width: 100%;
  127. height: 100%;
  128. background: #ffffff;
  129. padding: 16rpx 20rpx 0rpx 20rpx;
  130. box-sizing: border-box;
  131. overflow: hidden;
  132. .history-top {
  133. width: 100%;
  134. display: flex;
  135. justify-content: space-between;
  136. align-items: center;
  137. .title {
  138. font-size: 28rpx;
  139. }
  140. .iconfont {
  141. color: var(--text-color-light9);
  142. font-size: 28rpx;
  143. }
  144. }
  145. .history-bottom {
  146. width: 100%;
  147. padding-top: 20rpx;
  148. position: relative;
  149. .history-li {
  150. display: inline-block;
  151. margin-right: 20rpx;
  152. margin-bottom: 15rpx;
  153. max-width: 100%;
  154. view {
  155. line-height: 56rpx;
  156. background: var(--temp-bg) !important;
  157. height: 56rpx;
  158. color: #333 !important;
  159. margin: 0 0rpx 4rpx 0 !important;
  160. padding: 0 24rpx;
  161. overflow: hidden;
  162. white-space: nowrap;
  163. text-overflow: ellipsis;
  164. border-radius: 100rpx;
  165. font-size: 24rpx;
  166. }
  167. &.history_more {
  168. margin-right: 0;
  169. position: absolute;
  170. bottom: 0;
  171. right: 0;
  172. }
  173. }
  174. }
  175. }
  176. }
  177. </style>