mall.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. import storage from "@/utils/storage.js";
  87. const searchKeyword = ref('')
  88. const activeCategory = ref(-1)
  89. const scrollLeft = ref(0)
  90. const goodsList = ref([])
  91. const pageNum = ref(1)
  92. const pageSize = ref(10)
  93. const noMoreData = ref(false)
  94. const isLoading = ref(false)
  95. const isRefreshing = ref(false)
  96. const filteredGoods = computed(() => {
  97. let goods = goodsList.value
  98. if (activeCategory.value != -1) {
  99. goods = goods.filter(item => item.productCategory == activeCategory.value)
  100. } else {
  101. goods = goods.filter(item => item.isRecommended == 1)
  102. }
  103. return goods
  104. })
  105. const getImageUrl = (item) => {
  106. if(item == null) return
  107. const imageUrls = item.split(',')
  108. return imageUrls[0]
  109. }
  110. const loadMallData = (keyword) => {
  111. isLoading.value = true
  112. uni.showLoading({
  113. title: '加载中'
  114. })
  115. const params = {
  116. pageNum: pageNum.value,
  117. pageSize: pageSize.value,
  118. status: 1
  119. }
  120. if(keyword != null && keyword != ''){
  121. params.searchKeyword = keyword
  122. }
  123. if (activeCategory.value == -1) {
  124. params.isRecommended = 1
  125. } else {
  126. params.productCategory = activeCategory.value
  127. }
  128. getMallList(params).then(res => {
  129. if (res.data.code === 200) {
  130. const { rows, total } = res.data
  131. if (pageNum.value === 1) {
  132. goodsList.value = rows
  133. } else {
  134. goodsList.value = [...goodsList.value, ...rows]
  135. }
  136. noMoreData.value = goodsList.value.length >= total
  137. uni.hideLoading()
  138. } else {
  139. uni.showToast({
  140. title: res.data.msg || '获取商品列表失败',
  141. icon: 'none'
  142. })
  143. }
  144. }).catch(err => {
  145. console.error('获取商品列表失败:', err)
  146. uni.showToast({
  147. title: '获取商品列表失败',
  148. icon: 'none'
  149. })
  150. }).finally(() => {
  151. isLoading.value = false
  152. isRefreshing.value = false
  153. })
  154. }
  155. const loadMore = () => {
  156. if (!isLoading.value && !noMoreData.value) {
  157. pageNum.value++
  158. loadMallData()
  159. }
  160. }
  161. const switchCategory = (categoryId) => {
  162. activeCategory.value = categoryId
  163. goodsList.value = []
  164. pageNum.value = 1
  165. loadMallData(searchKeyword.value.trim())
  166. nextTick(() => {
  167. scrollToActiveCategory(categoryId)
  168. })
  169. }
  170. const scrollToActiveCategory = (categoryId) => {
  171. const index = dictData.mall_product_category.findIndex(item => item.id == categoryId)
  172. if (index > -1) {
  173. const itemWidth = 120
  174. const containerWidth = 750
  175. const scrollLeftValue = Math.max(0, index * itemWidth - containerWidth / 2 + itemWidth / 2)
  176. scrollLeft.value = scrollLeftValue
  177. }
  178. }
  179. const handleSearch = () => {
  180. if (!storage.getHasLogin()) {
  181. uni.showModal({
  182. title: '提示',
  183. content: '您还未登录,请先登录',
  184. confirmText: '去登录',
  185. cancelText: '取消',
  186. success: function(res) {
  187. if (res.confirm) {
  188. uni.navigateTo({
  189. url: '/pages/login/index'
  190. });
  191. }
  192. },
  193. });
  194. return;
  195. }
  196. const keyword = searchKeyword.value.trim().toLowerCase()
  197. loadMallData(keyword)
  198. }
  199. const navigateToDetail = (goods) => {
  200. if (!storage.getHasLogin()) {
  201. uni.showModal({
  202. title: '提示',
  203. content: '您还未登录,请先登录',
  204. confirmText: '去登录',
  205. cancelText: '取消',
  206. success: function(res) {
  207. if (res.confirm) {
  208. uni.navigateTo({
  209. url: '/pages/login/index'
  210. });
  211. }
  212. },
  213. });
  214. return;
  215. }
  216. uni.navigateTo({
  217. url: `/pages/service/mall-detail?id=${goods.id}&title=${encodeURIComponent(goods.title)}`
  218. })
  219. }
  220. onLoad(() => {
  221. loadMallData()
  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;
  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. .container {
  312. height: 100vh;
  313. display: flex;
  314. flex-direction: column;
  315. }
  316. .goods-grid {
  317. display: grid;
  318. grid-template-columns: 1fr 1fr;
  319. gap: 20rpx;
  320. }
  321. .goods-card {
  322. background-color: #fff;
  323. border-radius: 16rpx;
  324. overflow: hidden;
  325. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
  326. transition: transform 0.2s ease;
  327. &:active {
  328. transform: scale(0.98);
  329. }
  330. }
  331. .goods-image {
  332. width: 100%;
  333. height: 240rpx;
  334. image {
  335. width: 100%;
  336. height: 100%;
  337. }
  338. }
  339. .goods-info {
  340. padding: 24rpx;
  341. }
  342. .goods-title {
  343. font-size: 28rpx;
  344. font-weight: bold;
  345. color: #333;
  346. line-height: 1.4;
  347. display: block;
  348. margin-bottom: 8rpx;
  349. }
  350. .goods-desc {
  351. font-size: 24rpx;
  352. color: #666;
  353. line-height: 1.4;
  354. display: block;
  355. margin-bottom: 16rpx;
  356. overflow: hidden;
  357. text-overflow: ellipsis;
  358. white-space: nowrap;
  359. }
  360. .goods-price {
  361. display: flex;
  362. align-items: baseline;
  363. margin-bottom: 20rpx;
  364. .price {
  365. font-size: 32rpx;
  366. font-weight: bold;
  367. color: #4CAF50;
  368. }
  369. .unit {
  370. font-size: 24rpx;
  371. color: #999;
  372. margin-left: 4rpx;
  373. }
  374. }
  375. .action-btn {
  376. background-color: #4CAF50;
  377. color: #fff;
  378. text-align: center;
  379. padding: 16rpx 0;
  380. border-radius: 8rpx;
  381. font-size: 28rpx;
  382. font-weight: 500;
  383. }
  384. .no-more-data{
  385. text-align: center;
  386. padding: 30rpx 0;
  387. }
  388. .no-more-data text {
  389. font-size: 24rpx;
  390. color: #999;
  391. margin-left: 10rpx;
  392. }
  393. </style>