Right.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <div class="right-box">
  3. <a-table
  4. v-if="isList"
  5. bordered
  6. :dataSource="data"
  7. :columns="mainData.columns"
  8. :showHeader="false"
  9. :pagination="false"
  10. >
  11. <template #emptyText>暂无商品信息</template>
  12. <template #bodyCell="{ column, record }">
  13. <template v-if="column.key === 'goodsName'">
  14. <div class="goods-name" @click="viewDetail(record)">
  15. <a-flex align="center">
  16. <img :src="record.imgUrl" width="60" height="60" style="margin-right: 10px;" />
  17. <span>{{ record.goodsName }}</span>
  18. </a-flex>
  19. <a-flex v-if="foramtTagData(record).length > 0" gutter="small" style="margin-top: 10px; margin-left: 70px;">
  20. <a-tag
  21. v-for="(tag, tIndex) in foramtTagData(record)"
  22. :key="tIndex"
  23. color="red"
  24. >{{ tag }}</a-tag>
  25. </a-flex>
  26. </div>
  27. </template>
  28. <template v-if="column.key === 'soldNum'">
  29. <div v-if="isLogin">销量: {{ record.soldNum }}</div>
  30. <div v-else>
  31. <a-button type="link" @click="toLogin">登录</a-button>
  32. <span>查看销量</span>
  33. </div>
  34. </template>
  35. <template v-if="column.key === 'goodsPrice'">
  36. <div v-if="isLogin">价格: ¥ {{ record.goodsPrice }}</div>
  37. <div v-else>
  38. <a-button type="link" @click="toLogin">登录</a-button>
  39. <span>查看价格</span>
  40. </div>
  41. </template>
  42. <template v-if="column.key === 'opetation'">
  43. <a-flex align="center" justify="space-between">
  44. <a-button :disabled="!isLogin || disabled" type="primary" @click="handleAddToCart(record)">添加到购物车</a-button>
  45. <a-button :disabled="!isLogin || disabled" type="primary" @click="handleAddToLike(record)">添加到列表</a-button>
  46. </a-flex>
  47. </template>
  48. </template>
  49. </a-table>
  50. <div v-else class="grid-list__box">
  51. <GridItem
  52. v-for="item in data"
  53. :key="item.goodsId"
  54. :isLogin="isLogin"
  55. :item="item"
  56. :disabled="disabled"
  57. @goods-detail="viewDetail"
  58. @to-login="toLogin"
  59. @add-to-cart="handleAddToCart"
  60. @add-to-like="handleAddToLike"
  61. />
  62. </div>
  63. <div style="margin-top: 10px;">
  64. <a-pagination
  65. show-size-changer
  66. :current="pagination.pageNum"
  67. :pageSize="pagination.pageSize"
  68. :total="pagination.total"
  69. :page-size-options="['6', '12', '24', '26', '48']"
  70. @change="changePagination"
  71. />
  72. </div>
  73. </div>
  74. </template>
  75. <script setup lang="js">
  76. import { reactive, computed } from 'vue';
  77. import GridItem from './GridItem.vue';
  78. import { useUserStore } from '@/store/user';
  79. const props = defineProps({
  80. data: {
  81. type: Array,
  82. default: () => []
  83. },
  84. pagination: {
  85. type: Object,
  86. default: () => ({
  87. pageNum: 1,
  88. pageSize: 10,
  89. total: 0
  90. })
  91. },
  92. isList: {
  93. type: Boolean,
  94. default: true
  95. },
  96. disabled: {
  97. type: Boolean,
  98. default: false
  99. }
  100. })
  101. const emits = defineEmits([
  102. 'change-pagination',
  103. 'goods-detail',
  104. 'add-to-cart',
  105. 'add-to-like',
  106. 'to-login'
  107. ]);
  108. const userStore = useUserStore();
  109. const isLogin = computed(() => userStore.isLogin);
  110. const mainData = reactive({
  111. columns: [
  112. {
  113. title: '商品名称',
  114. dataIndex: 'goodsName',
  115. key: 'goodsName'
  116. },
  117. {
  118. title: '商品库存',
  119. dataIndex: 'soldNum',
  120. key: 'soldNum',
  121. width: 160
  122. },
  123. {
  124. title: '商品价格',
  125. dataIndex: 'goodsPrice',
  126. key: 'goodsPrice',
  127. width: 160
  128. },
  129. {
  130. title: '操作',
  131. dataIndex: 'opetation',
  132. key: 'opetation',
  133. width: 260
  134. }
  135. ]
  136. })
  137. const changePagination = (pageNum, pageSize) => {
  138. emits('change-pagination', {pageNum, pageSize});
  139. }
  140. const viewDetail = (record) => {
  141. emits('goods-detail', record);
  142. }
  143. const handleAddToCart = record => {
  144. emits('add-to-cart', record)
  145. }
  146. const handleAddToLike = record => {
  147. emits('add-to-like', record)
  148. }
  149. const toLogin = () => {
  150. emits('to-login')
  151. }
  152. const foramtTagData = record => {
  153. const tags1 = Array.isArray(record.tags1) ? record.tags1 : [];
  154. const tags2 = Array.isArray(record.tags2) ? record.tags2 : [];
  155. return [].concat(tags1, tags2);
  156. }
  157. </script>
  158. <style lang="less" scoped>
  159. .goods-name {
  160. color: @theme-color;
  161. cursor: pointer;
  162. }
  163. .grid-list__box {
  164. width: 100%;
  165. display: grid;
  166. gap: 16px;
  167. grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  168. }
  169. </style>