index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <template>
  2. <view class="search-page">
  3. <!-- 状态栏占位 -->
  4. <view class="status-bar"></view>
  5. <!-- 搜索框 -->
  6. <view class="search-header">
  7. <view class="search-input-wrap">
  8. <input class="search-input" type="text" v-model="keyword" placeholder="搜索商品" confirm-type="search" @confirm="handleSearch" />
  9. <text class="clear-btn" v-if="keyword" @tap="clearKeyword">×</text>
  10. </view>
  11. <text class="cancel-btn" @tap="goBack">取消</text>
  12. </view>
  13. <!-- 搜索历史和热门搜索 -->
  14. <view v-if="!showProductList" class="search-content">
  15. <!-- 搜索历史 -->
  16. <view class="search-history" v-if="historyList.length > 0">
  17. <view class="section-header">
  18. <text class="section-title">历史记录</text>
  19. <text class="clear-history" @tap="clearHistory">清空历史</text>
  20. </view>
  21. <view class="tag-list">
  22. <view class="tag-item" v-for="(item, index) in historyList" :key="index" @tap="handleTagClick(item)">
  23. {{item}}
  24. </view>
  25. </view>
  26. </view>
  27. <!-- 热门搜索 -->
  28. <view class="hot-search">
  29. <view class="section-header">
  30. <text class="section-title">大家都在搜</text>
  31. </view>
  32. <view class="tag-list">
  33. <view class="tag-item" v-for="(item, index) in hotList" :key="index" @tap="handleTagClick(item)">
  34. {{item}}
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. <!-- 商品列表 -->
  40. <view v-else class="product-section">
  41. <!-- 排序栏 -->
  42. <view class="sort-bar">
  43. <view class="sort-item" :class="{ active: !params.sort }" @tap="handleSort(0)">
  44. 综合
  45. </view>
  46. <view class="sort-item" :class="{ active: params.sort === 1 }" @tap="handleSort(1)">
  47. 价格
  48. <text class="sort-arrow" :class="{ up: params.sort === 1 && params.arrow === 1, down: params.sort === 1 && params.arrow === 2 }">
  49. </text>
  50. </view>
  51. <view class="sort-item" :class="{ active: params.sort === 2 }" @tap="handleSort(2)">
  52. 销量
  53. <text class="sort-arrow" :class="{ up: params.sort === 2 && params.arrow === 1, down: params.sort === 2 && params.arrow === 2 }">
  54. </text>
  55. </view>
  56. </view>
  57. <!-- 商品列表 -->
  58. <scroll-view
  59. class="product-list"
  60. scroll-y
  61. @scrolltolower="loadMore"
  62. :refresher-enabled="true"
  63. :refresher-triggered="isRefreshing"
  64. @refresherrefresh="onRefresh"
  65. >
  66. <view class="product-item" v-for="item in productList" :key="item.id" @tap="goDetail(item.id)">
  67. <image class="product-image" :src="item.images" mode="aspectFill"></image>
  68. <view class="product-info">
  69. <view class="product-name">{{item.name}}</view>
  70. <view class="product-price">
  71. <text class="current-price">¥{{item.price}}</text>
  72. <text class="original-price" v-if="item.originalPrice">¥{{item.originalPrice}}</text>
  73. </view>
  74. <view class="product-meta">
  75. <text class="sales">已售{{item.salesTotal}}件</text>
  76. <text class="rating" v-if="item.productAvg">评分:{{item.productAvg}}</text>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- 加载状态 -->
  81. <view class="loading-more" v-if="productList.length > 0">
  82. {{ hasMore ? '加载中...' : '没有更多了' }}
  83. </view>
  84. <view class="empty-state" v-if="productList.length === 0 && !isLoading">
  85. 暂无相关商品
  86. </view>
  87. </scroll-view>
  88. </view>
  89. </view>
  90. </template>
  91. <script>
  92. import { productSearch } from '@/config/api.js';
  93. export default {
  94. data() {
  95. return {
  96. keyword: '',
  97. historyList: [],
  98. hotList: ['鸡蛋', '大蒜', '蔬菜', '王守义十三香', '西瓜', '包菜'],
  99. showProductList: false,
  100. productList: [],
  101. params: {
  102. current: 1,
  103. size: 10,
  104. name: '',
  105. sort: 0,
  106. arrow: 1
  107. },
  108. isLoading: false,
  109. hasMore: true,
  110. isRefreshing: false
  111. }
  112. },
  113. onLoad(options) {
  114. // 从本地存储获取搜索历史
  115. const history = uni.getStorageSync('searchHistory');
  116. if (history) {
  117. this.historyList = JSON.parse(history);
  118. }
  119. // 如果有关键词参数,直接搜索
  120. if (options.keyword) {
  121. this.keyword = decodeURIComponent(options.keyword);
  122. this.handleSearch();
  123. }
  124. },
  125. methods: {
  126. // 处理搜索
  127. handleSearch() {
  128. if (!this.keyword.trim()) return;
  129. // 保存到历史记录
  130. this.saveHistory(this.keyword);
  131. // 重置参数并搜索
  132. this.params.current = 1;
  133. this.params.name = this.keyword;
  134. this.productList = [];
  135. this.hasMore = true;
  136. this.showProductList = true;
  137. this.searchProducts();
  138. },
  139. // 搜索商品
  140. async searchProducts() {
  141. if (this.isLoading || !this.hasMore) return;
  142. this.isLoading = true;
  143. uni.showLoading({
  144. title: '加载中...'
  145. });
  146. try {
  147. const res = await productSearch(this.params);
  148. if (res.code === 200) {
  149. const { records, total } = res.data;
  150. // 过滤上架商品
  151. const validProducts = records.filter(item => item.shelfLife === 1);
  152. if (this.params.current === 1) {
  153. this.productList = validProducts;
  154. } else {
  155. this.productList = [...this.productList, ...validProducts];
  156. }
  157. this.hasMore = this.productList.length < total;
  158. if (this.hasMore) {
  159. this.params.current++;
  160. }
  161. } else {
  162. uni.showToast({
  163. title: res.msg || '获取商品列表失败',
  164. icon: 'none'
  165. });
  166. }
  167. } catch (err) {
  168. console.error('搜索商品失败:', err);
  169. uni.showToast({
  170. title: '搜索失败,请重试',
  171. icon: 'none'
  172. });
  173. } finally {
  174. uni.hideLoading();
  175. this.isLoading = false;
  176. this.isRefreshing = false;
  177. }
  178. },
  179. // 处理排序
  180. handleSort(sortType) {
  181. if (sortType === 0) {
  182. this.params.sort = 0;
  183. this.params.arrow = 1;
  184. } else {
  185. if (this.params.sort === sortType) {
  186. this.params.arrow = this.params.arrow === 1 ? 2 : 1;
  187. } else {
  188. this.params.sort = sortType;
  189. this.params.arrow = 1;
  190. }
  191. }
  192. this.params.current = 1;
  193. this.productList = [];
  194. this.hasMore = true;
  195. this.searchProducts();
  196. },
  197. // 加载更多
  198. loadMore() {
  199. if (!this.isLoading && this.hasMore) {
  200. this.searchProducts();
  201. }
  202. },
  203. // 下拉刷新
  204. async onRefresh() {
  205. this.isRefreshing = true;
  206. this.params.current = 1;
  207. this.hasMore = true;
  208. await this.searchProducts();
  209. },
  210. // 保存搜索历史
  211. saveHistory(keyword) {
  212. let history = this.historyList;
  213. // 去重
  214. const index = history.indexOf(keyword);
  215. if (index !== -1) {
  216. history.splice(index, 1);
  217. }
  218. // 最多保存10条
  219. if (history.length >= 10) {
  220. history.pop();
  221. }
  222. history.unshift(keyword);
  223. this.historyList = history;
  224. uni.setStorageSync('searchHistory', JSON.stringify(history));
  225. },
  226. // 清空历史记录
  227. clearHistory() {
  228. uni.showModal({
  229. title: '提示',
  230. content: '确定要清空搜索历史吗?',
  231. success: (res) => {
  232. if (res.confirm) {
  233. this.historyList = [];
  234. uni.removeStorageSync('searchHistory');
  235. }
  236. }
  237. });
  238. },
  239. // 点击标签
  240. handleTagClick(keyword) {
  241. this.keyword = keyword;
  242. this.handleSearch();
  243. },
  244. // 清除输入
  245. clearKeyword() {
  246. this.keyword = '';
  247. if (this.showProductList) {
  248. this.showProductList = false;
  249. this.productList = [];
  250. }
  251. },
  252. // 返回
  253. goBack() {
  254. if (this.showProductList) {
  255. this.showProductList = false;
  256. this.productList = [];
  257. this.keyword = '';
  258. } else {
  259. uni.navigateBack();
  260. }
  261. },
  262. // 跳转到商品详情
  263. goDetail(id) {
  264. uni.navigateTo({
  265. url: '/packageShop/pages/detail/index?id=' + id
  266. });
  267. }
  268. }
  269. }
  270. </script>
  271. <style lang="scss" scoped>
  272. .search-page {
  273. min-height: 100vh;
  274. background: #fff;
  275. padding-bottom: env(safe-area-inset-bottom);
  276. }
  277. .status-bar {
  278. height: var(--status-bar-height);
  279. width: 100%;
  280. background: #fff;
  281. }
  282. .search-header {
  283. display: flex;
  284. align-items: center;
  285. padding: 20rpx 30rpx;
  286. background: #fff;
  287. position: sticky;
  288. top: var(--status-bar-height);
  289. z-index: 100;
  290. border-bottom: 1rpx solid #f5f5f5;
  291. }
  292. .search-content {
  293. padding-top: 20rpx;
  294. }
  295. .search-input-wrap {
  296. flex: 1;
  297. height: 72rpx;
  298. background: #F5F5F5;
  299. border-radius: 36rpx;
  300. display: flex;
  301. align-items: center;
  302. padding: 0 30rpx;
  303. margin-right: 20rpx;
  304. position: relative;
  305. }
  306. .search-input {
  307. flex: 1;
  308. height: 100%;
  309. font-size: 28rpx;
  310. }
  311. .clear-btn {
  312. position: absolute;
  313. right: 20rpx;
  314. font-size: 40rpx;
  315. color: #999;
  316. padding: 10rpx;
  317. }
  318. .cancel-btn {
  319. font-size: 28rpx;
  320. color: #333;
  321. }
  322. .section-header {
  323. display: flex;
  324. justify-content: space-between;
  325. align-items: center;
  326. margin: 40rpx 0 20rpx;
  327. padding: 0 30rpx;
  328. }
  329. .section-title {
  330. font-size: 28rpx;
  331. color: #333;
  332. font-weight: bold;
  333. }
  334. .clear-history {
  335. font-size: 24rpx;
  336. color: #999;
  337. }
  338. .tag-list {
  339. display: flex;
  340. flex-wrap: wrap;
  341. padding: 0 30rpx;
  342. }
  343. .tag-item {
  344. padding: 12rpx 30rpx;
  345. background: #F5F5F5;
  346. border-radius: 30rpx;
  347. font-size: 24rpx;
  348. color: #666;
  349. margin: 0 20rpx 20rpx 0;
  350. }
  351. .product-section {
  352. display: flex;
  353. flex-direction: column;
  354. height: calc(100vh - var(--status-bar-height) - 112rpx);
  355. }
  356. .sort-bar {
  357. display: flex;
  358. align-items: center;
  359. height: 88rpx;
  360. background: #fff;
  361. border-bottom: 1rpx solid #f5f5f5;
  362. }
  363. .sort-item {
  364. flex: 1;
  365. display: flex;
  366. align-items: center;
  367. justify-content: center;
  368. font-size: 28rpx;
  369. color: #666;
  370. position: relative;
  371. &.active {
  372. color: #D93025;
  373. }
  374. .sort-arrow {
  375. margin-left: 4rpx;
  376. font-size: 24rpx;
  377. &.up {
  378. transform: scaleY(-1);
  379. }
  380. &.down {
  381. transform: scaleY(1);
  382. }
  383. }
  384. }
  385. .product-list {
  386. flex: 1;
  387. background: #f5f5f5;
  388. }
  389. .product-item {
  390. margin: 20rpx;
  391. background: #fff;
  392. border-radius: 12rpx;
  393. overflow: hidden;
  394. }
  395. .product-image {
  396. width: 100%;
  397. height: 360rpx;
  398. }
  399. .product-info {
  400. padding: 20rpx;
  401. }
  402. .product-name {
  403. font-size: 28rpx;
  404. color: #333;
  405. margin-bottom: 16rpx;
  406. }
  407. .product-price {
  408. margin-bottom: 12rpx;
  409. .current-price {
  410. font-size: 32rpx;
  411. color: #D93025;
  412. font-weight: bold;
  413. }
  414. .original-price {
  415. font-size: 24rpx;
  416. color: #999;
  417. text-decoration: line-through;
  418. margin-left: 12rpx;
  419. }
  420. }
  421. .product-meta {
  422. display: flex;
  423. align-items: center;
  424. font-size: 24rpx;
  425. color: #999;
  426. .rating {
  427. margin-left: 20rpx;
  428. }
  429. }
  430. .loading-more {
  431. text-align: center;
  432. font-size: 24rpx;
  433. color: #999;
  434. padding: 20rpx 0;
  435. }
  436. .empty-state {
  437. text-align: center;
  438. font-size: 28rpx;
  439. color: #999;
  440. padding: 100rpx 0;
  441. }
  442. </style>