mall.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. status: 1 // 已上架
  175. // productCategory: this.activeCategory,
  176. // recommend:1 // 查询推荐商品
  177. };
  178. if(keyword != null && keyword != ''){
  179. params.searchKeyword = keyword
  180. }
  181. // 分类逻辑
  182. if (this.activeCategory == -1) {
  183. // 默认场景:推荐查询
  184. params.isRecommended = 1;
  185. } else {
  186. // 分类查询:只带 productCategory
  187. params.productCategory = this.activeCategory;
  188. }
  189. getMallList(params).then(res => {
  190. if (res.data.code === 200) {
  191. const { rows, total } = res.data;
  192. if (this.pageNum === 1) {
  193. this.goodsList = rows;
  194. } else {
  195. this.goodsList = [...this.goodsList, ...rows];
  196. }
  197. console.log("this.goodsList.length >= total",this.goodsList.length);
  198. console.log("this.goodsList.length >= total",this.goodsList.length >= total);
  199. // 判断是否还有更多数据
  200. this.noMoreData = this.goodsList.length >= total;
  201. uni.hideLoading();
  202. // 统计待完成商品数量
  203. // this.countPendingTasks();
  204. } else {
  205. uni.showToast({
  206. title: res.data.msg || '获取商品列表失败',
  207. icon: 'none'
  208. });
  209. }
  210. }).catch(err => {
  211. console.error('获取商品列表失败:', err);
  212. uni.showToast({
  213. title: '获取商品列表失败',
  214. icon: 'none'
  215. });
  216. }).finally(() => {
  217. this.isLoading = false;
  218. this.isRefreshing = false;
  219. });
  220. },
  221. // 上拉加载更多
  222. loadMore() {
  223. if (!this.isLoading && !this.noMoreData) {
  224. this.pageNum++;
  225. this.loadMallData();
  226. }
  227. },
  228. // 切换分类
  229. switchCategory(categoryId) {
  230. console.log("dfdssdf",categoryId);
  231. this.activeCategory = categoryId;
  232. this.goodsList = [] // 切换时置空数据数组
  233. // 滚动到当前分类位置
  234. this.loadMallData(this.searchKeyword.trim())
  235. this.$nextTick(() => {
  236. this.scrollToActiveCategory(categoryId);
  237. });
  238. },
  239. // 滚动到激活的分类
  240. scrollToActiveCategory(categoryId) {
  241. const index = this.dictData.mall_product_category.findIndex(item => item.id == categoryId);
  242. console.log("index",index);
  243. if (index > -1) {
  244. // 计算滚动位置,让当前分类居中显示
  245. const itemWidth = 120; // 每个分类项大约120rpx宽度
  246. const containerWidth = 750; // 屏幕宽度
  247. const scrollLeft = Math.max(0, index * itemWidth - containerWidth / 2 + itemWidth / 2);
  248. // 设置scroll-view的scrollLeft
  249. this.scrollLeft = scrollLeft;
  250. }
  251. },
  252. // 搜索处理
  253. handleSearch() {
  254. // if (!this.searchKeyword.trim()) {
  255. // uni.showToast({
  256. // title: '请输入搜索关键词',
  257. // icon: 'none'
  258. // });
  259. // return;
  260. // }
  261. const keyword = this.searchKeyword.trim().toLowerCase();
  262. this.loadMallData(keyword)
  263. },
  264. // 导航到商品详情
  265. navigateToDetail(goods) {
  266. uni.navigateTo({
  267. url: `/pages/service/mall-detail?id=${goods.id}&title=${encodeURIComponent(goods.title)}`
  268. });
  269. }
  270. },
  271. // 页面加载时获取数据
  272. onLoad() {
  273. this.loadMallData();
  274. },
  275. onNavigationBarTitleText() {
  276. return '农资商城';
  277. }
  278. }
  279. </script>
  280. <style lang="scss">
  281. .mall-container {
  282. min-height: 100vh;
  283. background-color: #f5f5f5;
  284. }
  285. .search-header {
  286. background-color: #fff;
  287. padding: 20rpx;
  288. border-bottom: 1rpx solid #f0f0f0;
  289. }
  290. .search-box {
  291. position: relative;
  292. }
  293. .search-input {
  294. display: flex;
  295. align-items: center;
  296. background-color: #f8f8f8;
  297. border-radius: 32rpx;
  298. padding: 16rpx 24rpx;
  299. .search-icon {
  300. width: 32rpx;
  301. height: 32rpx;
  302. margin-right: 16rpx;
  303. }
  304. .input {
  305. flex: 1;
  306. font-size: 28rpx;
  307. line-height: 1.5;
  308. }
  309. }
  310. .category-nav {
  311. background-color: #fff;
  312. border-bottom: 1rpx solid #f0f0f0;
  313. position: sticky;
  314. top: 0;
  315. z-index: 100;
  316. }
  317. .nav-scroll {
  318. white-space: nowrap;
  319. height: 88rpx;
  320. width: 100%;
  321. overflow: hidden;
  322. -webkit-overflow-scrolling: touch; /* iOS滑动优化 */
  323. scroll-behavior: smooth; /* 平滑滚动 */
  324. }
  325. .nav-list {
  326. display: inline-flex;
  327. padding: 0 20rpx;
  328. height: 100%;
  329. align-items: center;
  330. min-width: 100%;
  331. }
  332. .nav-item {
  333. flex-shrink: 0;
  334. padding: 24rpx 32rpx;
  335. margin-right: 8rpx;
  336. font-size: 28rpx;
  337. color: #666;
  338. font-weight: 500;
  339. position: relative;
  340. white-space: nowrap;
  341. transition: all 0.2s ease;
  342. border-radius: 8rpx;
  343. &:active {
  344. background-color: #f0f0f0;
  345. transform: scale(0.95);
  346. }
  347. &.active {
  348. color: #4CAF50;
  349. font-weight: bold;
  350. background-color: #f8fdf8;
  351. }
  352. &.active::after {
  353. content: '';
  354. position: absolute;
  355. bottom: 8rpx;
  356. left: 50%;
  357. transform: translateX(-50%);
  358. width: 40rpx;
  359. height: 4rpx;
  360. background-color: #4CAF50;
  361. border-radius: 2rpx;
  362. }
  363. }
  364. .goods-container {
  365. padding: 20rpx;
  366. }
  367. .container {
  368. height: 100vh;
  369. display: flex;
  370. flex-direction: column;
  371. }
  372. .goods-grid {
  373. display: grid;
  374. grid-template-columns: 1fr 1fr;
  375. gap: 20rpx;
  376. }
  377. .goods-card {
  378. background-color: #fff;
  379. border-radius: 16rpx;
  380. overflow: hidden;
  381. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  382. transition: transform 0.2s ease;
  383. &:active {
  384. transform: scale(0.98);
  385. }
  386. }
  387. .goods-image {
  388. width: 100%;
  389. height: 240rpx;
  390. image {
  391. width: 100%;
  392. height: 100%;
  393. }
  394. }
  395. .goods-info {
  396. padding: 24rpx;
  397. }
  398. .goods-title {
  399. font-size: 28rpx;
  400. font-weight: bold;
  401. color: #333;
  402. line-height: 1.4;
  403. display: block;
  404. margin-bottom: 8rpx;
  405. }
  406. .goods-desc {
  407. font-size: 24rpx;
  408. color: #666;
  409. line-height: 1.4;
  410. display: block;
  411. margin-bottom: 16rpx;
  412. overflow: hidden;
  413. text-overflow: ellipsis;
  414. white-space: nowrap;
  415. }
  416. .goods-price {
  417. display: flex;
  418. align-items: baseline;
  419. margin-bottom: 20rpx;
  420. .price {
  421. font-size: 32rpx;
  422. font-weight: bold;
  423. color: #4CAF50;
  424. }
  425. .unit {
  426. font-size: 24rpx;
  427. color: #999;
  428. margin-left: 4rpx;
  429. }
  430. }
  431. .action-btn {
  432. background-color: #4CAF50;
  433. color: #fff;
  434. text-align: center;
  435. padding: 16rpx 0;
  436. border-radius: 8rpx;
  437. font-size: 28rpx;
  438. font-weight: 500;
  439. }
  440. .no-more-data{
  441. text-align: center;
  442. padding: 30rpx 0;
  443. }
  444. .no-more-data text {
  445. font-size: 24rpx;
  446. color: #999;
  447. margin-left: 10rpx;
  448. }
  449. </style>