mall.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <template>
  2. <view class="mall-container">
  3. <!-- 顶部搜索框 -->
  4. <view class="search-header">
  5. <view class="search-box">
  6. <view class="search-input">
  7. <image class="search-icon" src="/static/icons/search.png" mode="aspectFit"></image>
  8. <input
  9. class="input"
  10. placeholder="搜索农资商品(如:化肥、除草剂)"
  11. placeholder-style="color: #999;"
  12. v-model="searchKeyword"
  13. @confirm="handleSearch"
  14. />
  15. </view>
  16. </view>
  17. </view>
  18. <!-- 分类导航 -->
  19. <view class="category-nav">
  20. <scroll-view
  21. class="nav-scroll"
  22. scroll-x="true"
  23. :show-scrollbar="false"
  24. :enhanced="true"
  25. :bounces="false"
  26. :scroll-with-animation="true"
  27. :scroll-left="scrollLeft"
  28. >
  29. <view class="nav-list">
  30. <view
  31. class="nav-item"
  32. :class="{ active: activeCategory == item.dictValue }"
  33. v-for="item in dictData.mall_product_category"
  34. :key="item.dictValue"
  35. @click="switchCategory(item.dictValue)"
  36. >
  37. {{ item.dictLabel }}
  38. </view>
  39. </view>
  40. </scroll-view>
  41. </view>
  42. <!-- 商品展示区 -->
  43. <!-- <view class="goods-container">
  44. <scroll-view
  45. scroll-y
  46. v-if="goodsList.length > 0"
  47. style="height: 100vh;"
  48. @scrolltolower="loadMore">
  49. <view class="goods-grid">
  50. <view
  51. class="goods-card"
  52. v-for="item in filteredGoods"
  53. :key="item.id"
  54. @click="navigateToDetail(item)"
  55. >
  56. <view class="goods-image">
  57. <image :src="getImageUrl(item.swiperImages)" mode="aspectFill"></image>
  58. </view>
  59. <view class="goods-info">
  60. <text class="goods-title">{{ item.title }}</text>
  61. <text class="goods-desc">{{ item.description }}</text>
  62. <view class="goods-price">
  63. <text class="price">¥{{ item.price }}</text>
  64. <!-- <text class="unit">/{{ item.unit }}</text>
  65. </view>
  66. <view class="action-btn">了解更多</view>
  67. </view>
  68. </view>
  69. </view>
  70. </scroll-view>
  71. <view class="no-more-data" v-if="noMoreData && goodsList.length > 0">
  72. <text>没有更多数据了</text>
  73. </view>
  74. </view> -->
  75. <view class="container">
  76. <!-- 有数据 -->
  77. <scroll-view
  78. v-if="goodsList.length > 0"
  79. scroll-y
  80. style="height: 100vh;"
  81. @scrolltolower="loadMore"
  82. >
  83. <view class="goods-grid">
  84. <view
  85. class="goods-card"
  86. v-for="item in filteredGoods"
  87. :key="item.id"
  88. @click="navigateToDetail(item)"
  89. >
  90. <view class="goods-image">
  91. <image :src="getImageUrl(item.swiperImages)" mode="aspectFill"></image>
  92. </view>
  93. <view class="goods-info">
  94. <text class="goods-title">{{ item.title }}</text>
  95. <text class="goods-desc">{{ item.description }}</text>
  96. <view class="goods-price">
  97. <text class="price">¥{{ item.price }}</text>
  98. <text class="unit">{{ item.unit }}</text>
  99. </view>
  100. <view class="action-btn">了解更多</view>
  101. </view>
  102. </view>
  103. </view>
  104. </scroll-view>
  105. <!-- 没有更多数据提示 -->
  106. <view class="no-more-data" v-if="noMoreData">
  107. <text>没有更多数据了</text>
  108. </view>
  109. <!-- 无数据 -->
  110. <!-- <view v-else class="empty">
  111. 暂无数据
  112. </view> -->
  113. </view>
  114. </view>
  115. </template>
  116. <script>
  117. import { getMallList } from '@/api/services/mall.js';
  118. import dictMixin from '@/utils/mixins/dictMixin';
  119. import api from "@/config/api.js";
  120. export default {
  121. mixins: [dictMixin],
  122. data() {
  123. return {
  124. dictTypeList: ['mall_product_category'],
  125. searchKeyword: '',
  126. activeCategory: -1,
  127. scrollLeft: 0,
  128. // 商品列表
  129. goodsList: [],
  130. pageNum: 1,
  131. pageSize: 10,
  132. noMoreData: false,
  133. isLoading: false,
  134. isRefreshing: false,
  135. }
  136. },
  137. computed: {
  138. filteredGoods() {
  139. let goods = this.goodsList;
  140. // 按分类筛选
  141. if (this.activeCategory != -1) {
  142. goods = goods.filter(item => item.productCategory == this.activeCategory);
  143. }else{
  144. goods = goods.filter(item => item.isRecommended == 1);
  145. }
  146. // // 按搜索关键词筛选
  147. // if (this.searchKeyword.trim()) {
  148. // const keyword = this.searchKeyword.trim().toLowerCase();
  149. // goods = goods.filter(item =>
  150. // item.title.toLowerCase().includes(keyword) ||
  151. // item.description.toLowerCase().includes(keyword)
  152. // );
  153. // }
  154. return goods;
  155. }
  156. },
  157. methods: {
  158. getImageUrl(item) {
  159. // 默认返回url或path
  160. if(item == null) return
  161. const imageUrls = item.split(',');
  162. console.log("imageUrls",imageUrls);
  163. return imageUrls[0];
  164. },
  165. loadMallData(keyword){
  166. this.isLoading = true;
  167. uni.showLoading({
  168. title: '加载中'
  169. });
  170. // 构建查询参数
  171. const params = {
  172. pageNum: this.pageNum,
  173. pageSize: this.pageSize,
  174. // productCategory: this.activeCategory,
  175. // recommend:1 // 查询推荐商品
  176. };
  177. if(keyword != null && keyword != ''){
  178. params.searchKeyword = keyword
  179. }
  180. // 分类逻辑
  181. if (this.activeCategory == -1) {
  182. // 默认场景:推荐查询
  183. params.isRecommended = 1;
  184. } else {
  185. // 分类查询:只带 productCategory
  186. params.productCategory = this.activeCategory;
  187. }
  188. getMallList(params).then(res => {
  189. if (res.data.code === 200) {
  190. const { rows, total } = res.data;
  191. if (this.pageNum === 1) {
  192. this.goodsList = rows;
  193. } else {
  194. this.goodsList = [...this.goodsList, ...rows];
  195. }
  196. console.log("this.goodsList.length >= total",this.goodsList.length);
  197. console.log("this.goodsList.length >= total",this.goodsList.length >= total);
  198. // 判断是否还有更多数据
  199. this.noMoreData = this.goodsList.length >= total;
  200. uni.hideLoading();
  201. // 统计待完成商品数量
  202. // this.countPendingTasks();
  203. } else {
  204. uni.showToast({
  205. title: res.data.msg || '获取商品列表失败',
  206. icon: 'none'
  207. });
  208. }
  209. }).catch(err => {
  210. console.error('获取商品列表失败:', err);
  211. uni.showToast({
  212. title: '获取商品列表失败',
  213. icon: 'none'
  214. });
  215. }).finally(() => {
  216. this.isLoading = false;
  217. this.isRefreshing = false;
  218. });
  219. },
  220. // 上拉加载更多
  221. loadMore() {
  222. if (!this.isLoading && !this.noMoreData) {
  223. this.pageNum++;
  224. this.loadMallData();
  225. }
  226. },
  227. // 切换分类
  228. switchCategory(categoryId) {
  229. console.log("dfdssdf",categoryId);
  230. this.activeCategory = categoryId;
  231. this.goodsList = [] // 切换时置空数据数组
  232. // 滚动到当前分类位置
  233. this.loadMallData(this.searchKeyword.trim())
  234. this.$nextTick(() => {
  235. this.scrollToActiveCategory(categoryId);
  236. });
  237. },
  238. // 滚动到激活的分类
  239. scrollToActiveCategory(categoryId) {
  240. const index = this.dictData.mall_product_category.findIndex(item => item.id == categoryId);
  241. console.log("index",index);
  242. if (index > -1) {
  243. // 计算滚动位置,让当前分类居中显示
  244. const itemWidth = 120; // 每个分类项大约120rpx宽度
  245. const containerWidth = 750; // 屏幕宽度
  246. const scrollLeft = Math.max(0, index * itemWidth - containerWidth / 2 + itemWidth / 2);
  247. // 设置scroll-view的scrollLeft
  248. this.scrollLeft = scrollLeft;
  249. }
  250. },
  251. // 搜索处理
  252. handleSearch() {
  253. // if (!this.searchKeyword.trim()) {
  254. // uni.showToast({
  255. // title: '请输入搜索关键词',
  256. // icon: 'none'
  257. // });
  258. // return;
  259. // }
  260. const keyword = this.searchKeyword.trim().toLowerCase();
  261. this.loadMallData(keyword)
  262. },
  263. // 导航到商品详情
  264. navigateToDetail(goods) {
  265. uni.navigateTo({
  266. url: `/pages/service/mall-detail?id=${goods.id}&title=${encodeURIComponent(goods.title)}`
  267. });
  268. }
  269. },
  270. // 页面加载时获取数据
  271. onLoad() {
  272. this.loadMallData();
  273. },
  274. onNavigationBarTitleText() {
  275. return '农资商城';
  276. }
  277. }
  278. </script>
  279. <style lang="scss">
  280. .mall-container {
  281. min-height: 100vh;
  282. background-color: #f5f5f5;
  283. }
  284. .search-header {
  285. background-color: #fff;
  286. padding: 20rpx;
  287. border-bottom: 1rpx solid #f0f0f0;
  288. }
  289. .search-box {
  290. position: relative;
  291. }
  292. .search-input {
  293. display: flex;
  294. align-items: center;
  295. background-color: #f8f8f8;
  296. border-radius: 32rpx;
  297. padding: 16rpx 24rpx;
  298. .search-icon {
  299. width: 32rpx;
  300. height: 32rpx;
  301. margin-right: 16rpx;
  302. }
  303. .input {
  304. flex: 1;
  305. font-size: 28rpx;
  306. line-height: 1.5;
  307. }
  308. }
  309. .category-nav {
  310. background-color: #fff;
  311. border-bottom: 1rpx solid #f0f0f0;
  312. position: sticky;
  313. top: 0;
  314. z-index: 100;
  315. }
  316. .nav-scroll {
  317. white-space: nowrap;
  318. height: 88rpx;
  319. width: 100%;
  320. overflow: hidden;
  321. -webkit-overflow-scrolling: touch; /* iOS滑动优化 */
  322. scroll-behavior: smooth; /* 平滑滚动 */
  323. }
  324. .nav-list {
  325. display: inline-flex;
  326. padding: 0 20rpx;
  327. height: 100%;
  328. align-items: center;
  329. min-width: 100%;
  330. }
  331. .nav-item {
  332. flex-shrink: 0;
  333. padding: 24rpx 32rpx;
  334. margin-right: 8rpx;
  335. font-size: 28rpx;
  336. color: #666;
  337. font-weight: 500;
  338. position: relative;
  339. white-space: nowrap;
  340. transition: all 0.2s ease;
  341. border-radius: 8rpx;
  342. &:active {
  343. background-color: #f0f0f0;
  344. transform: scale(0.95);
  345. }
  346. &.active {
  347. color: #4CAF50;
  348. font-weight: bold;
  349. background-color: #f8fdf8;
  350. }
  351. &.active::after {
  352. content: '';
  353. position: absolute;
  354. bottom: 8rpx;
  355. left: 50%;
  356. transform: translateX(-50%);
  357. width: 40rpx;
  358. height: 4rpx;
  359. background-color: #4CAF50;
  360. border-radius: 2rpx;
  361. }
  362. }
  363. .goods-container {
  364. padding: 20rpx;
  365. }
  366. .container {
  367. height: 100vh;
  368. display: flex;
  369. flex-direction: column;
  370. }
  371. .goods-grid {
  372. display: grid;
  373. grid-template-columns: 1fr 1fr;
  374. gap: 20rpx;
  375. }
  376. .goods-card {
  377. background-color: #fff;
  378. border-radius: 16rpx;
  379. overflow: hidden;
  380. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  381. transition: transform 0.2s ease;
  382. &:active {
  383. transform: scale(0.98);
  384. }
  385. }
  386. .goods-image {
  387. width: 100%;
  388. height: 240rpx;
  389. image {
  390. width: 100%;
  391. height: 100%;
  392. }
  393. }
  394. .goods-info {
  395. padding: 24rpx;
  396. }
  397. .goods-title {
  398. font-size: 28rpx;
  399. font-weight: bold;
  400. color: #333;
  401. line-height: 1.4;
  402. display: block;
  403. margin-bottom: 8rpx;
  404. }
  405. .goods-desc {
  406. font-size: 24rpx;
  407. color: #666;
  408. line-height: 1.4;
  409. display: block;
  410. margin-bottom: 16rpx;
  411. overflow: hidden;
  412. text-overflow: ellipsis;
  413. white-space: nowrap;
  414. }
  415. .goods-price {
  416. display: flex;
  417. align-items: baseline;
  418. margin-bottom: 20rpx;
  419. .price {
  420. font-size: 32rpx;
  421. font-weight: bold;
  422. color: #4CAF50;
  423. }
  424. .unit {
  425. font-size: 24rpx;
  426. color: #999;
  427. margin-left: 4rpx;
  428. }
  429. }
  430. .action-btn {
  431. background-color: #4CAF50;
  432. color: #fff;
  433. text-align: center;
  434. padding: 16rpx 0;
  435. border-radius: 8rpx;
  436. font-size: 28rpx;
  437. font-weight: 500;
  438. }
  439. .no-more-data{
  440. text-align: center;
  441. padding: 30rpx 0;
  442. }
  443. .no-more-data text {
  444. font-size: 24rpx;
  445. color: #999;
  446. margin-left: 10rpx;
  447. }
  448. </style>