search.vue 7.1 KB

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