mall.vue 9.2 KB

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