mall.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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="container">
  44. <!-- 有数据 -->
  45. <scroll-view
  46. v-if="goodsList.length > 0"
  47. scroll-y
  48. style="height: 100vh;"
  49. @scrolltolower="loadMore"
  50. >
  51. <view class="goods-grid">
  52. <view
  53. class="goods-card"
  54. v-for="item in filteredGoods"
  55. :key="item.id"
  56. @click="navigateToDetail(item)"
  57. >
  58. <view class="goods-image">
  59. <image :src="getImageUrl(item.swiperImages)" mode="aspectFill"></image>
  60. </view>
  61. <view class="goods-info">
  62. <text class="goods-title">{{ item.title }}</text>
  63. <text class="goods-desc">{{ item.description }}</text>
  64. <view class="goods-price">
  65. <text class="price">¥{{ item.price }}</text>
  66. <text class="unit">{{ item.unit }}</text>
  67. </view>
  68. <view class="action-btn">了解更多</view>
  69. </view>
  70. </view>
  71. </view>
  72. </scroll-view>
  73. <!-- 没有更多数据提示 -->
  74. <view class="no-more-data" v-if="noMoreData">
  75. <text>没有更多数据了</text>
  76. </view>
  77. </view>
  78. </view>
  79. </template>
  80. <script setup>
  81. import { ref, computed, nextTick } from 'vue'
  82. import { onLoad } from '@dcloudio/uni-app'
  83. import { getMallList } from '@/api/services/mall.js'
  84. import { useDict } from '@/utils/composables/useDict'
  85. const { dictData } = useDict(['mall_product_category'])
  86. const searchKeyword = ref('')
  87. const activeCategory = ref(-1)
  88. const scrollLeft = ref(0)
  89. const goodsList = ref([])
  90. const pageNum = ref(1)
  91. const pageSize = ref(10)
  92. const noMoreData = ref(false)
  93. const isLoading = ref(false)
  94. const isRefreshing = ref(false)
  95. const filteredGoods = computed(() => {
  96. let goods = goodsList.value
  97. if (activeCategory.value != -1) {
  98. goods = goods.filter(item => item.productCategory == activeCategory.value)
  99. } else {
  100. goods = goods.filter(item => item.isRecommended == 1)
  101. }
  102. return goods
  103. })
  104. const getImageUrl = (item) => {
  105. if(item == null) return
  106. const imageUrls = item.split(',')
  107. return imageUrls[0]
  108. }
  109. const loadMallData = (keyword) => {
  110. isLoading.value = true
  111. uni.showLoading({
  112. title: '加载中'
  113. })
  114. const params = {
  115. pageNum: pageNum.value,
  116. pageSize: pageSize.value,
  117. status: 1
  118. }
  119. if(keyword != null && keyword != ''){
  120. params.searchKeyword = keyword
  121. }
  122. if (activeCategory.value == -1) {
  123. params.isRecommended = 1
  124. } else {
  125. params.productCategory = activeCategory.value
  126. }
  127. getMallList(params).then(res => {
  128. if (res.data.code === 200) {
  129. const { rows, total } = res.data
  130. if (pageNum.value === 1) {
  131. goodsList.value = rows
  132. } else {
  133. goodsList.value = [...goodsList.value, ...rows]
  134. }
  135. noMoreData.value = goodsList.value.length >= total
  136. uni.hideLoading()
  137. } else {
  138. uni.showToast({
  139. title: res.data.msg || '获取商品列表失败',
  140. icon: 'none'
  141. })
  142. }
  143. }).catch(err => {
  144. console.error('获取商品列表失败:', err)
  145. uni.showToast({
  146. title: '获取商品列表失败',
  147. icon: 'none'
  148. })
  149. }).finally(() => {
  150. isLoading.value = false
  151. isRefreshing.value = false
  152. })
  153. }
  154. const loadMore = () => {
  155. if (!isLoading.value && !noMoreData.value) {
  156. pageNum.value++
  157. loadMallData()
  158. }
  159. }
  160. const switchCategory = (categoryId) => {
  161. activeCategory.value = categoryId
  162. goodsList.value = []
  163. pageNum.value = 1
  164. loadMallData(searchKeyword.value.trim())
  165. nextTick(() => {
  166. scrollToActiveCategory(categoryId)
  167. })
  168. }
  169. const scrollToActiveCategory = (categoryId) => {
  170. const index = dictData.value.mall_product_category.findIndex(item => item.id == categoryId)
  171. if (index > -1) {
  172. const itemWidth = 120
  173. const containerWidth = 750
  174. const scrollLeftValue = Math.max(0, index * itemWidth - containerWidth / 2 + itemWidth / 2)
  175. scrollLeft.value = scrollLeftValue
  176. }
  177. }
  178. const handleSearch = () => {
  179. const keyword = searchKeyword.value.trim().toLowerCase()
  180. loadMallData(keyword)
  181. }
  182. const navigateToDetail = (goods) => {
  183. uni.navigateTo({
  184. url: `/pages/service/mall-detail?id=${goods.id}&title=${encodeURIComponent(goods.title)}`
  185. })
  186. }
  187. onLoad(() => {
  188. loadMallData()
  189. })
  190. </script>
  191. <style lang="scss">
  192. .mall-container {
  193. min-height: 100vh;
  194. background-color: #f5f5f5;
  195. }
  196. .search-header {
  197. background-color: #fff;
  198. padding: 20rpx;
  199. border-bottom: 1rpx solid #f0f0f0;
  200. }
  201. .search-box {
  202. position: relative;
  203. }
  204. .search-input {
  205. display: flex;
  206. align-items: center;
  207. background-color: #f8f8f8;
  208. border-radius: 32rpx;
  209. padding: 16rpx 24rpx;
  210. .search-icon {
  211. width: 32rpx;
  212. height: 32rpx;
  213. margin-right: 16rpx;
  214. }
  215. .input {
  216. flex: 1;
  217. font-size: 28rpx;
  218. line-height: 1.5;
  219. }
  220. }
  221. .category-nav {
  222. background-color: #fff;
  223. border-bottom: 1rpx solid #f0f0f0;
  224. position: sticky;
  225. top: 0;
  226. z-index: 100;
  227. }
  228. .nav-scroll {
  229. white-space: nowrap;
  230. height: 88rpx;
  231. width: 100%;
  232. overflow: hidden;
  233. -webkit-overflow-scrolling: touch;
  234. scroll-behavior: smooth;
  235. }
  236. .nav-list {
  237. display: inline-flex;
  238. padding: 0 20rpx;
  239. height: 100%;
  240. align-items: center;
  241. min-width: 100%;
  242. }
  243. .nav-item {
  244. flex-shrink: 0;
  245. padding: 24rpx 32rpx;
  246. margin-right: 8rpx;
  247. font-size: 28rpx;
  248. color: #666;
  249. font-weight: 500;
  250. position: relative;
  251. white-space: nowrap;
  252. transition: all 0.2s ease;
  253. border-radius: 8rpx;
  254. &:active {
  255. background-color: #f0f0f0;
  256. transform: scale(0.95);
  257. }
  258. &.active {
  259. color: #4CAF50;
  260. font-weight: bold;
  261. background-color: #f8fdf8;
  262. }
  263. &.active::after {
  264. content: '';
  265. position: absolute;
  266. bottom: 8rpx;
  267. left: 50%;
  268. transform: translateX(-50%);
  269. width: 40rpx;
  270. height: 4rpx;
  271. background-color: #4CAF50;
  272. border-radius: 2rpx;
  273. }
  274. }
  275. .goods-container {
  276. padding: 20rpx;
  277. }
  278. .container {
  279. height: 100vh;
  280. display: flex;
  281. flex-direction: column;
  282. }
  283. .goods-grid {
  284. display: grid;
  285. grid-template-columns: 1fr 1fr;
  286. gap: 20rpx;
  287. }
  288. .goods-card {
  289. background-color: #fff;
  290. border-radius: 16rpx;
  291. overflow: hidden;
  292. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  293. transition: transform 0.2s ease;
  294. &:active {
  295. transform: scale(0.98);
  296. }
  297. }
  298. .goods-image {
  299. width: 100%;
  300. height: 240rpx;
  301. image {
  302. width: 100%;
  303. height: 100%;
  304. }
  305. }
  306. .goods-info {
  307. padding: 24rpx;
  308. }
  309. .goods-title {
  310. font-size: 28rpx;
  311. font-weight: bold;
  312. color: #333;
  313. line-height: 1.4;
  314. display: block;
  315. margin-bottom: 8rpx;
  316. }
  317. .goods-desc {
  318. font-size: 24rpx;
  319. color: #666;
  320. line-height: 1.4;
  321. display: block;
  322. margin-bottom: 16rpx;
  323. overflow: hidden;
  324. text-overflow: ellipsis;
  325. white-space: nowrap;
  326. }
  327. .goods-price {
  328. display: flex;
  329. align-items: baseline;
  330. margin-bottom: 20rpx;
  331. .price {
  332. font-size: 32rpx;
  333. font-weight: bold;
  334. color: #4CAF50;
  335. }
  336. .unit {
  337. font-size: 24rpx;
  338. color: #999;
  339. margin-left: 4rpx;
  340. }
  341. }
  342. .action-btn {
  343. background-color: #4CAF50;
  344. color: #fff;
  345. text-align: center;
  346. padding: 16rpx 0;
  347. border-radius: 8rpx;
  348. font-size: 28rpx;
  349. font-weight: 500;
  350. }
  351. .no-more-data{
  352. text-align: center;
  353. padding: 30rpx 0;
  354. }
  355. .no-more-data text {
  356. font-size: 24rpx;
  357. color: #999;
  358. margin-left: 10rpx;
  359. }
  360. </style>