search.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <view class="search-container">
  3. <!-- 搜索输入框 -->
  4. <view class="search-header">
  5. <view class="search-input-wrap">
  6. <text class="search-icon">🔍</text>
  7. <input
  8. class="search-input"
  9. v-model="keyword"
  10. :focus="true"
  11. placeholder="输入商品名称"
  12. @confirm="onSearch"
  13. @input="onInput"
  14. />
  15. <text v-if="keyword" class="clear-btn" @click="clearKeyword">✕</text>
  16. </view>
  17. <text class="search-cancel" @click="goBack">取消</text>
  18. </view>
  19. <!-- 搜索建议 / 历史记录 -->
  20. <view v-if="!searched" class="suggest-wrap">
  21. <view class="suggest-section" v-if="history.length">
  22. <view class="suggest-header">
  23. <text class="suggest-title">搜索历史</text>
  24. <text class="clear-history" @click="clearHistory">清除</text>
  25. </view>
  26. <view class="history-tags">
  27. <text
  28. v-for="(item, idx) in history"
  29. :key="idx"
  30. class="history-tag"
  31. @click="searchKeyword(item)"
  32. >{{ item }}</text>
  33. </view>
  34. </view>
  35. </view>
  36. <!-- 搜索结果 -->
  37. <view v-else class="result-wrap">
  38. <view v-if="results.length === 0" class="empty-wrap">
  39. <text class="empty-icon">📦</text>
  40. <text class="empty-text">没有找到相关商品</text>
  41. </view>
  42. <view v-else class="product-grid">
  43. <view
  44. v-for="item in results"
  45. :key="item.id"
  46. class="product-card"
  47. @click="goDetail(item.id)"
  48. >
  49. <image
  50. class="cover"
  51. :src="item.image || item.coverImage || '/static/default-product.png'"
  52. mode="aspectFill"
  53. />
  54. <view class="card-body">
  55. <text class="product-name">{{ item.name }}</text>
  56. <view class="price-row">
  57. <text class="price">{{ formatPriceWithSymbol(item.price) }}</text>
  58. <text v-if="item.memberPrice" class="member-price">会员{{ formatPriceWithSymbol(item.memberPrice) }}</text>
  59. </view>
  60. </view>
  61. </view>
  62. </view>
  63. <view v-if="loadingMore" class="loading-more">
  64. <text class="loading-text">加载中...</text>
  65. </view>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. import { productList } from '@/utils/api.js'
  71. export default {
  72. data() {
  73. return {
  74. keyword: '',
  75. searched: false,
  76. results: [],
  77. page: 1,
  78. size: 20,
  79. loadingMore: false,
  80. noMore: false,
  81. history: []
  82. }
  83. },
  84. onLoad() {
  85. var historyStr = uni.getStorageSync('searchHistory')
  86. this.history = historyStr ? JSON.parse(historyStr) : []
  87. },
  88. methods: {
  89. onInput(e) {
  90. this.keyword = e.detail.value
  91. },
  92. onSearch() {
  93. if (!this.keyword.trim()) return
  94. this.searched = true
  95. this.page = 1
  96. this.results = []
  97. this.noMore = false
  98. this.saveHistory()
  99. this.loadResults()
  100. },
  101. searchKeyword(keyword) {
  102. this.keyword = keyword
  103. this.onSearch()
  104. },
  105. loadResults() {
  106. if (this.loadingMore) return
  107. this.loadingMore = true
  108. var params = { page: this.page, size: this.size, keyword: this.keyword.trim() }
  109. productList(params).then(res => {
  110. this.loadingMore = false
  111. if (res.code === 200 && res.data) {
  112. var records = res.data.records || []
  113. if (this.page === 1) {
  114. this.results = records
  115. } else {
  116. this.results = this.results.concat(records)
  117. }
  118. var total = res.data.total || 0
  119. this.noMore = this.results.length >= total
  120. }
  121. }).catch(() => {
  122. this.loadingMore = false
  123. })
  124. },
  125. saveHistory() {
  126. var kw = this.keyword.trim()
  127. if (!kw) return
  128. var list = this.history.filter(function(h) { return h !== kw })
  129. list.unshift(kw)
  130. if (list.length > 10) list = list.slice(0, 10)
  131. this.history = list
  132. uni.setStorageSync('searchHistory', JSON.stringify(list))
  133. },
  134. clearHistory() {
  135. this.history = []
  136. uni.removeStorageSync('searchHistory')
  137. },
  138. clearKeyword() {
  139. this.keyword = ''
  140. this.searched = false
  141. this.results = []
  142. },
  143. goBack() {
  144. uni.navigateBack()
  145. },
  146. goDetail(id) {
  147. uni.navigateTo({ url: '/pages/shop/detail/detail?id=' + id })
  148. },
  149. formatPriceWithSymbol(price) {
  150. if (price === null || price === undefined) return ''
  151. return '¥' + (Number(price) / 100).toFixed(2)
  152. }
  153. }
  154. }
  155. </script>
  156. <style scoped>
  157. .search-container {
  158. min-height: 100vh;
  159. background: #f5f5f5;
  160. }
  161. .search-header {
  162. display: flex;
  163. align-items: center;
  164. padding: 16rpx 20rpx;
  165. background: #fff;
  166. }
  167. .search-input-wrap {
  168. flex: 1;
  169. display: flex;
  170. align-items: center;
  171. background: #f5f5f5;
  172. border-radius: 50rpx;
  173. padding: 0 24rpx;
  174. height: 72rpx;
  175. }
  176. .search-icon {
  177. font-size: 32rpx;
  178. margin-right: 12rpx;
  179. flex-shrink: 0;
  180. }
  181. .search-input {
  182. flex: 1;
  183. font-size: 28rpx;
  184. color: #333;
  185. height: 72rpx;
  186. }
  187. .clear-btn {
  188. font-size: 28rpx;
  189. color: #999;
  190. padding: 8rpx;
  191. flex-shrink: 0;
  192. }
  193. .search-cancel {
  194. font-size: 28rpx;
  195. color: #666;
  196. margin-left: 20rpx;
  197. flex-shrink: 0;
  198. }
  199. .suggest-wrap {
  200. padding: 20rpx;
  201. }
  202. .suggest-section {
  203. background: #fff;
  204. border-radius: 16rpx;
  205. padding: 24rpx;
  206. margin-bottom: 20rpx;
  207. }
  208. .suggest-header {
  209. display: flex;
  210. justify-content: space-between;
  211. align-items: center;
  212. margin-bottom: 20rpx;
  213. }
  214. .suggest-title {
  215. font-size: 28rpx;
  216. font-weight: bold;
  217. color: #333;
  218. }
  219. .clear-history {
  220. font-size: 24rpx;
  221. color: #999;
  222. }
  223. .history-tags {
  224. display: flex;
  225. flex-wrap: wrap;
  226. gap: 16rpx;
  227. }
  228. .history-tag {
  229. padding: 12rpx 24rpx;
  230. background: #f5f5f5;
  231. border-radius: 30rpx;
  232. font-size: 24rpx;
  233. color: #666;
  234. }
  235. .result-wrap {
  236. padding: 0 20rpx;
  237. }
  238. .product-grid {
  239. display: flex;
  240. flex-wrap: wrap;
  241. justify-content: space-between;
  242. padding: 20rpx 0;
  243. }
  244. .product-card {
  245. width: 49%;
  246. background: #fff;
  247. border-radius: 16rpx;
  248. overflow: hidden;
  249. margin-bottom: 20rpx;
  250. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  251. }
  252. .cover {
  253. width: 100%;
  254. height: 320rpx;
  255. background: #f0f0f0;
  256. }
  257. .card-body {
  258. padding: 16rpx;
  259. }
  260. .product-name {
  261. display: block;
  262. font-size: 28rpx;
  263. color: #333;
  264. lines: 2;
  265. overflow: hidden;
  266. text-overflow: ellipsis;
  267. display: -webkit-box;
  268. -webkit-line-clamp: 2;
  269. -webkit-box-orient: vertical;
  270. margin-bottom: 12rpx;
  271. }
  272. .price-row {
  273. display: flex;
  274. align-items: baseline;
  275. margin-bottom: 8rpx;
  276. }
  277. .price {
  278. font-size: 32rpx;
  279. color: #F97316;
  280. font-weight: bold;
  281. margin-right: 12rpx;
  282. }
  283. .member-price {
  284. font-size: 22rpx;
  285. color: #999;
  286. }
  287. .empty-wrap {
  288. display: flex;
  289. flex-direction: column;
  290. align-items: center;
  291. padding-top: 200rpx;
  292. }
  293. .empty-icon {
  294. font-size: 100rpx;
  295. margin-bottom: 30rpx;
  296. }
  297. .empty-text {
  298. font-size: 28rpx;
  299. color: #999;
  300. }
  301. .loading-more {
  302. text-align: center;
  303. padding: 30rpx;
  304. }
  305. .loading-text {
  306. font-size: 24rpx;
  307. color: #999;
  308. }
  309. </style>