index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <template>
  2. <div class="shop-wrap">
  3. <div class="tab-list">
  4. <div
  5. v-for="(item, index) in tabList"
  6. :key="index"
  7. class="tab"
  8. :class="{ active: item.key === currTab }"
  9. @click="changeTab(item.key)"
  10. >
  11. {{ item.label }}
  12. </div>
  13. </div>
  14. <div
  15. v-loading="loading"
  16. class="goods-list"
  17. >
  18. <div
  19. v-for="(item, index) in list"
  20. :key="index"
  21. class="goods"
  22. @click="startPay(item)"
  23. >
  24. <div
  25. class="left"
  26. :style="{ backgroundImage: `url(${item.cover})` }"
  27. >
  28. <div
  29. v-if="item.badge"
  30. class="badge"
  31. :style="{ backgroundColor: item.badge_bg }"
  32. >
  33. <div class="txt">{{ item.badge }}</div>
  34. </div>
  35. </div>
  36. <div class="right">
  37. <div class="title">{{ item.name }}</div>
  38. <div class="info">100%好评</div>
  39. <div class="desc">{{ item.desc }}</div>
  40. <div class="price-wrap">
  41. <span class="price">¥{{ item.price }}</span>
  42. <del
  43. v-if="item.price !== item.original_price"
  44. class="original-price"
  45. >
  46. {{ item.original_price }}
  47. </del>
  48. </div>
  49. </div>
  50. </div>
  51. </div>
  52. <QrPayCpt
  53. v-if="showQrPay"
  54. :money="goodsInfo.money"
  55. :goods-id="goodsInfo.goodsId"
  56. :live-room-id="goodsInfo.liveRoomId"
  57. ></QrPayCpt>
  58. </div>
  59. </template>
  60. <script lang="ts" setup>
  61. import { nextTick, onMounted, reactive, ref } from 'vue';
  62. import { useRoute } from 'vue-router';
  63. import { fetchGoodsList } from '@/api/goods';
  64. import QrPayCpt from '@/components/QrPay/index.vue';
  65. import { GoodsTypeEnum, IGoods } from '@/interface';
  66. import router from '@/router';
  67. const route = useRoute();
  68. const list = ref<IGoods[]>([]);
  69. const loading = ref(false);
  70. const showQrPay = ref(false);
  71. const goodsInfo = reactive({
  72. money: '0.00',
  73. goodsId: -1,
  74. liveRoomId: -1,
  75. });
  76. const tabList = ref([
  77. { label: '礼物', key: GoodsTypeEnum.gift },
  78. { label: '赞助', key: GoodsTypeEnum.sponsors },
  79. { label: '服务', key: GoodsTypeEnum.support },
  80. ]);
  81. const currTab = ref(tabList.value[0].key);
  82. async function getGoodsList(type: GoodsTypeEnum) {
  83. try {
  84. loading.value = true;
  85. const res = await fetchGoodsList({
  86. type,
  87. orderName: 'created_at',
  88. orderBy: 'desc',
  89. });
  90. if (res.code === 200) {
  91. list.value = res.data.rows;
  92. }
  93. } catch (error) {
  94. console.log(error);
  95. } finally {
  96. loading.value = false;
  97. }
  98. }
  99. onMounted(() => {
  100. const key = route.query.goodsType as GoodsTypeEnum;
  101. if (GoodsTypeEnum[key]) {
  102. currTab.value = key;
  103. } else {
  104. router.push({ query: {} });
  105. }
  106. getGoodsList(currTab.value);
  107. });
  108. function changeTab(type: GoodsTypeEnum) {
  109. showQrPay.value = false;
  110. currTab.value = type;
  111. getGoodsList(currTab.value);
  112. }
  113. function startPay(item: IGoods) {
  114. if (Number(item.price) === 0) {
  115. window.$message.info('该商品是免费的,不需要购买!');
  116. return;
  117. }
  118. showQrPay.value = false;
  119. nextTick(() => {
  120. goodsInfo.money = item.price!;
  121. goodsInfo.goodsId = item.id!;
  122. showQrPay.value = true;
  123. });
  124. }
  125. </script>
  126. <style lang="scss" scoped>
  127. .shop-wrap {
  128. padding: 0 30px;
  129. .tab-list {
  130. display: flex;
  131. align-items: center;
  132. margin-bottom: 20px;
  133. height: 40px;
  134. font-size: 14px;
  135. user-select: none;
  136. .tab {
  137. position: relative;
  138. margin-right: 20px;
  139. cursor: pointer;
  140. &.active {
  141. color: $theme-color-gold;
  142. font-size: 16px;
  143. &::after {
  144. position: absolute;
  145. bottom: -6px;
  146. left: 50%;
  147. width: 20px;
  148. height: 2px;
  149. border-radius: 10px;
  150. background-color: $theme-color-gold;
  151. content: '';
  152. transform: translateX(-50%);
  153. }
  154. }
  155. }
  156. }
  157. .goods-list {
  158. display: flex;
  159. flex-wrap: wrap;
  160. .goods {
  161. display: flex;
  162. box-sizing: border-box;
  163. margin-right: 20px;
  164. margin-bottom: 20px;
  165. padding: 18px 10px 10px;
  166. width: 400px;
  167. border-radius: 6px;
  168. box-shadow: 0 4px 30px 0 rgba(238, 242, 245, 0.8);
  169. cursor: pointer;
  170. transition: box-shadow 0.2s linear;
  171. &:hover {
  172. box-shadow: 4px 4px 20px 0 rgba(205, 216, 228, 0.6);
  173. }
  174. .left {
  175. position: relative;
  176. margin-right: 20px;
  177. width: 100px;
  178. height: 100px;
  179. @extend %containBg;
  180. .badge {
  181. position: absolute;
  182. top: -10px;
  183. right: -10px;
  184. display: flex;
  185. align-items: center;
  186. justify-content: center;
  187. padding: 2px;
  188. border-radius: 2px;
  189. color: white;
  190. .txt {
  191. display: inline-block;
  192. line-height: 1;
  193. transform-origin: center !important;
  194. @include minFont(10);
  195. }
  196. }
  197. }
  198. .right {
  199. .title {
  200. font-size: 22px;
  201. }
  202. .info {
  203. }
  204. .price-wrap {
  205. display: flex;
  206. align-items: center;
  207. .price {
  208. color: $theme-color-gold;
  209. font-size: 22px;
  210. }
  211. .original-price {
  212. margin-left: 5px;
  213. color: #666;
  214. font-size: 14px;
  215. }
  216. }
  217. }
  218. }
  219. }
  220. }
  221. </style>