index.vue 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!-- -->
  2. <template>
  3. <div>
  4. <Navbar style="min-width: 1184px;" @search="handleSearch" />
  5. <div v-if="goodList.length" class="default-width white-bgc my-3 pa-5" :loading="loading" style="border-radius: 5px;">
  6. <div class="goods-box">
  7. <v-card v-for="val in goodList" :key="val.id" class="goods-box-item" hover elevation="2" @click="handleClickGood(val)">
  8. <v-img :src="val.picUrl" width="100%" height="68%" cover></v-img>
  9. <div class="px-3 pt-3">
  10. <p class="ellipsis color-333">{{ val.name }}</p>
  11. <p class="color-999 ellipsis font-size-14 mt-1">{{ val.introduction }}</p>
  12. <div class="mt-1 d-flex justify-space-between">
  13. <div class="goods-box-item-price">¥{{ isArray(val.price) ? fen2yuan(val.price[0]) : fen2yuan(val.price) }}</div>
  14. <div class="font-size-15 mt-1" style="color: #c4c4c4">{{ salesAndStock(val) }}</div>
  15. </div>
  16. </div>
  17. </v-card>
  18. </div>
  19. <CtPagination :total="total" :page="page.pageNo" :limit="page.pageSize" @handleChange="handleChangePage"></CtPagination>
  20. </div>
  21. <Empty v-else class="default-width my-3" :elevation="false" :message=message ></Empty>
  22. </div>
  23. </template>
  24. <script setup>
  25. defineOptions({name: 'mall-goodsList-index'})
  26. import Navbar from '@/views/mall/components/navbar.vue'
  27. import { useRoute } from 'vue-router'; const route = useRoute()
  28. import { getGoodsList } from '@/api/mall/index'
  29. import { isArray } from 'lodash-es'
  30. import { formatSales, fen2yuan } from '@/hooks/web/useGoods'
  31. import { ref, computed } from 'vue'
  32. const page = ref({
  33. pageNo: 1,
  34. pageSize: 10
  35. })
  36. // 根据id获取商品列表
  37. const loading = ref(false)
  38. const total = ref(0)
  39. const goodList = ref([])
  40. const getData = async () => {
  41. const params = {
  42. ...page.value,
  43. keyword: keyword.value,
  44. }
  45. const { list, total: number } = await getGoodsList(params)
  46. goodList.value = list
  47. total.value = number || 0
  48. }
  49. getData()
  50. // 分页
  51. const handleChangePage = (e) => {
  52. page.value.pageNo = e
  53. getData()
  54. }
  55. const message = ref('')
  56. const keyword = ref('')
  57. // 关键字检索
  58. const handleSearch = (val) => {
  59. keyword.value = val
  60. message.value = keyword.value ? `没有找到“${keyword.value}”相关商品` : '没有找到相关商品'
  61. getData()
  62. }
  63. // 格式化销量、库存信息
  64. const salesAndStock = computed(() => (data) => {
  65. let text = []
  66. text.push(formatSales(undefined, data.salesCount))
  67. return text.join(' | ')
  68. })
  69. </script>
  70. <style lang="scss" scoped>
  71. .goods-box {
  72. width: 100%;
  73. display: flex;
  74. flex-wrap: wrap;
  75. &-item {
  76. height: 330px;
  77. width: calc((100% - 48px) / 5);
  78. margin: 0 12px 12px 0;
  79. &:nth-child(5n) {
  80. margin-right: 0;
  81. }
  82. &-price {
  83. color: #ff3000;
  84. font-size: 20px;
  85. }
  86. }
  87. }
  88. </style>