mall.vue 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. loadMallData(searchKeyword.value.trim())
  164. nextTick(() => {
  165. scrollToActiveCategory(categoryId)
  166. })
  167. }
  168. const scrollToActiveCategory = (categoryId) => {
  169. const index = dictData.value.mall_product_category.findIndex(item => item.id == categoryId)
  170. if (index > -1) {
  171. const itemWidth = 120
  172. const containerWidth = 750
  173. const scrollLeftValue = Math.max(0, index * itemWidth - containerWidth / 2 + itemWidth / 2)
  174. scrollLeft.value = scrollLeftValue
  175. }
  176. }
  177. const handleSearch = () => {
  178. const keyword = searchKeyword.value.trim().toLowerCase()
  179. loadMallData(keyword)
  180. }
  181. const navigateToDetail = (goods) => {
  182. uni.navigateTo({
  183. url: `/pages/service/mall-detail?id=${goods.id}&title=${encodeURIComponent(goods.title)}`
  184. })
  185. }
  186. onLoad(() => {
  187. loadMallData()
  188. })
  189. </script>
  190. <style lang="scss">
  191. .mall-container {
  192. min-height: 100vh;
  193. background-color: #f5f5f5;
  194. }
  195. .search-header {
  196. background-color: #fff;
  197. padding: 20rpx;
  198. border-bottom: 1rpx solid #f0f0f0;
  199. }
  200. .search-box {
  201. position: relative;
  202. }
  203. .search-input {
  204. display: flex;
  205. align-items: center;
  206. background-color: #f8f8f8;
  207. border-radius: 32rpx;
  208. padding: 16rpx 24rpx;
  209. .search-icon {
  210. width: 32rpx;
  211. height: 32rpx;
  212. margin-right: 16rpx;
  213. }
  214. .input {
  215. flex: 1;
  216. font-size: 28rpx;
  217. line-height: 1.5;
  218. }
  219. }
  220. .category-nav {
  221. background-color: #fff;
  222. border-bottom: 1rpx solid #f0f0f0;
  223. position: sticky;
  224. top: 0;
  225. z-index: 100;
  226. }
  227. .nav-scroll {
  228. white-space: nowrap;
  229. height: 88rpx;
  230. width: 100%;
  231. overflow: hidden;
  232. -webkit-overflow-scrolling: touch;
  233. scroll-behavior: smooth;
  234. }
  235. .nav-list {
  236. display: inline-flex;
  237. padding: 0 20rpx;
  238. height: 100%;
  239. align-items: center;
  240. min-width: 100%;
  241. }
  242. .nav-item {
  243. flex-shrink: 0;
  244. padding: 24rpx 32rpx;
  245. margin-right: 8rpx;
  246. font-size: 28rpx;
  247. color: #666;
  248. font-weight: 500;
  249. position: relative;
  250. white-space: nowrap;
  251. transition: all 0.2s ease;
  252. border-radius: 8rpx;
  253. &:active {
  254. background-color: #f0f0f0;
  255. transform: scale(0.95);
  256. }
  257. &.active {
  258. color: #4CAF50;
  259. font-weight: bold;
  260. background-color: #f8fdf8;
  261. }
  262. &.active::after {
  263. content: '';
  264. position: absolute;
  265. bottom: 8rpx;
  266. left: 50%;
  267. transform: translateX(-50%);
  268. width: 40rpx;
  269. height: 4rpx;
  270. background-color: #4CAF50;
  271. border-radius: 2rpx;
  272. }
  273. }
  274. .goods-container {
  275. padding: 20rpx;
  276. }
  277. .container {
  278. height: 100vh;
  279. display: flex;
  280. flex-direction: column;
  281. }
  282. .goods-grid {
  283. display: grid;
  284. grid-template-columns: 1fr 1fr;
  285. gap: 20rpx;
  286. }
  287. .goods-card {
  288. background-color: #fff;
  289. border-radius: 16rpx;
  290. overflow: hidden;
  291. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  292. transition: transform 0.2s ease;
  293. &:active {
  294. transform: scale(0.98);
  295. }
  296. }
  297. .goods-image {
  298. width: 100%;
  299. height: 240rpx;
  300. image {
  301. width: 100%;
  302. height: 100%;
  303. }
  304. }
  305. .goods-info {
  306. padding: 24rpx;
  307. }
  308. .goods-title {
  309. font-size: 28rpx;
  310. font-weight: bold;
  311. color: #333;
  312. line-height: 1.4;
  313. display: block;
  314. margin-bottom: 8rpx;
  315. }
  316. .goods-desc {
  317. font-size: 24rpx;
  318. color: #666;
  319. line-height: 1.4;
  320. display: block;
  321. margin-bottom: 16rpx;
  322. overflow: hidden;
  323. text-overflow: ellipsis;
  324. white-space: nowrap;
  325. }
  326. .goods-price {
  327. display: flex;
  328. align-items: baseline;
  329. margin-bottom: 20rpx;
  330. .price {
  331. font-size: 32rpx;
  332. font-weight: bold;
  333. color: #4CAF50;
  334. }
  335. .unit {
  336. font-size: 24rpx;
  337. color: #999;
  338. margin-left: 4rpx;
  339. }
  340. }
  341. .action-btn {
  342. background-color: #4CAF50;
  343. color: #fff;
  344. text-align: center;
  345. padding: 16rpx 0;
  346. border-radius: 8rpx;
  347. font-size: 28rpx;
  348. font-weight: 500;
  349. }
  350. .no-more-data{
  351. text-align: center;
  352. padding: 30rpx 0;
  353. }
  354. .no-more-data text {
  355. font-size: 24rpx;
  356. color: #999;
  357. margin-left: 10rpx;
  358. }
  359. </style>