巴青农资商城

index.vue 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <template>
  2. <view class="page-search">
  3. <view class="search-header">
  4. <view class="search-header__bar">
  5. <!-- <u-icon name="arrow-left" size="20" color="#333" @click="onBack" /> -->
  6. <view class="search-header__input-wrap">
  7. <u-icon name="search" color="#999" size="18" />
  8. <input
  9. class="search-header__input"
  10. v-model="keyword"
  11. :placeholder="placeholder"
  12. confirm-type="search"
  13. :focus="inputFocus"
  14. @confirm="onSubmit"
  15. />
  16. <u-icon
  17. v-if="keyword"
  18. name="close-circle-fill"
  19. color="#ccc"
  20. size="18"
  21. @click="keyword = ''"
  22. />
  23. </view>
  24. <text class="search-header__btn" @click="onSubmit">搜索</text>
  25. </view>
  26. </view>
  27. <view v-if="historyList.length" class="history">
  28. <view class="history__head">
  29. <text class="history__title">搜索历史</text>
  30. <text class="history__clear" @click="onClearAll">清空</text>
  31. </view>
  32. <view class="history__list">
  33. <view v-for="item in historyList" :key="item.keyword" class="history__item">
  34. <text class="history__text" @click="onHistoryTap(item.keyword)">{{ item.keyword }}</text>
  35. <u-icon
  36. name="close"
  37. color="#bbb"
  38. size="14"
  39. @click.stop="onRemoveOne(item.keyword)"
  40. />
  41. </view>
  42. </view>
  43. </view>
  44. <view v-else class="history-empty">
  45. <text>暂无搜索历史</text>
  46. </view>
  47. </view>
  48. </template>
  49. <script setup>
  50. import { ref } from 'vue'
  51. import { onLoad, onShow } from '@dcloudio/uni-app'
  52. import { SEARCH_PLACEHOLDER } from '@/constants/search'
  53. import {
  54. getSearchHistory,
  55. addSearchHistory,
  56. removeSearchHistoryItem,
  57. clearSearchHistory
  58. } from '@/utils/searchHistory'
  59. import { goSearchResult, navigateBackFromSearchIndex } from '@/utils/searchNav'
  60. const placeholder = SEARCH_PLACEHOLDER
  61. const keyword = ref('')
  62. const historyList = ref([])
  63. const inputFocus = ref(false)
  64. function refreshHistory() {
  65. historyList.value = getSearchHistory()
  66. }
  67. function onBack() {
  68. navigateBackFromSearchIndex()
  69. }
  70. function submitSearch() {
  71. const text = (keyword.value || '').trim()
  72. if (!text) {
  73. uni.showToast({ title: '请输入搜索内容', icon: 'none' })
  74. return
  75. }
  76. addSearchHistory(text)
  77. goSearchResult(text)
  78. }
  79. function onSubmit() {
  80. submitSearch()
  81. }
  82. function onHistoryTap(text) {
  83. keyword.value = text
  84. submitSearch()
  85. }
  86. function onRemoveOne(text) {
  87. removeSearchHistoryItem(text)
  88. refreshHistory()
  89. }
  90. function onClearAll() {
  91. uni.showModal({
  92. title: '提示',
  93. content: '确定清空全部搜索历史吗?',
  94. success: (res) => {
  95. if (res.confirm) {
  96. clearSearchHistory()
  97. refreshHistory()
  98. }
  99. }
  100. })
  101. }
  102. onLoad((options) => {
  103. if (options && options.keyword) {
  104. keyword.value = decodeURIComponent(options.keyword)
  105. }
  106. inputFocus.value = !keyword.value
  107. })
  108. onShow(() => {
  109. refreshHistory()
  110. })
  111. </script>
  112. <style lang="scss" scoped>
  113. .page-search {
  114. height: calc(100vh - 100rpx);
  115. background: #f5f6f8;
  116. }
  117. .search-header {
  118. background: #fff;
  119. padding: 16rpx 24rpx 24rpx;
  120. }
  121. .search-header__bar {
  122. display: flex;
  123. align-items: center;
  124. }
  125. .search-header__input-wrap {
  126. flex: 1;
  127. display: flex;
  128. align-items: center;
  129. height: 72rpx;
  130. margin: 0 16rpx;
  131. padding: 0 20rpx;
  132. background: #f5f6f8;
  133. border-radius: 36rpx;
  134. }
  135. .search-header__input {
  136. flex: 1;
  137. margin: 0 12rpx;
  138. font-size: 28rpx;
  139. color: #333;
  140. }
  141. .search-header__btn {
  142. font-size: 28rpx;
  143. color: #2e7d32;
  144. font-weight: 500;
  145. flex-shrink: 0;
  146. }
  147. .history {
  148. margin-top: 16rpx;
  149. padding: 24rpx;
  150. background: #fff;
  151. }
  152. .history__head {
  153. display: flex;
  154. justify-content: space-between;
  155. align-items: center;
  156. margin-bottom: 20rpx;
  157. }
  158. .history__title {
  159. font-size: 28rpx;
  160. color: #333;
  161. font-weight: 600;
  162. }
  163. .history__clear {
  164. font-size: 26rpx;
  165. color: #999;
  166. }
  167. .history__item {
  168. display: flex;
  169. align-items: center;
  170. justify-content: space-between;
  171. padding: 20rpx 0;
  172. border-bottom: 1rpx solid #f5f5f5;
  173. }
  174. .history__text {
  175. flex: 1;
  176. font-size: 28rpx;
  177. color: #666;
  178. }
  179. .history-empty {
  180. padding: 80rpx 24rpx;
  181. text-align: center;
  182. font-size: 26rpx;
  183. color: #bbb;
  184. }
  185. </style>