index.vue 5.4 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. v-lazy:background-image="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">¥{{ formatMoney(item.price) }}</span>
  42. <del
  43. v-if="item.price !== item.original_price"
  44. class="original-price"
  45. >
  46. {{ formatMoney(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 { URL_PARAMS } from '@/constant';
  66. import { GoodsTypeEnum, IGoods } from '@/interface';
  67. import router from '@/router';
  68. import { formatMoney } from '@/utils';
  69. const route = useRoute();
  70. const list = ref<IGoods[]>([]);
  71. const loading = ref(false);
  72. const showQrPay = ref(false);
  73. const goodsInfo = reactive({
  74. money: 0,
  75. goodsId: -1,
  76. liveRoomId: -1,
  77. });
  78. const tabList = ref([
  79. { label: '礼物', key: GoodsTypeEnum.gift },
  80. { label: '赞助', key: GoodsTypeEnum.sponsors },
  81. { label: '服务', key: GoodsTypeEnum.support },
  82. ]);
  83. const currTab = ref(tabList.value[0].key);
  84. async function getGoodsList(type: GoodsTypeEnum) {
  85. try {
  86. loading.value = true;
  87. const res = await fetchGoodsList({
  88. type,
  89. orderName: 'price,created_at',
  90. orderBy: 'asc,desc',
  91. });
  92. if (res.code === 200) {
  93. list.value = res.data.rows;
  94. }
  95. } catch (error) {
  96. console.log(error);
  97. } finally {
  98. loading.value = false;
  99. }
  100. }
  101. onMounted(() => {
  102. const key = route.query[URL_PARAMS.goodsType] as GoodsTypeEnum;
  103. if (GoodsTypeEnum[key]) {
  104. currTab.value = key;
  105. } else {
  106. router.push({ query: {} });
  107. }
  108. getGoodsList(currTab.value);
  109. });
  110. function changeTab(type: GoodsTypeEnum) {
  111. showQrPay.value = false;
  112. currTab.value = type;
  113. getGoodsList(currTab.value);
  114. }
  115. function startPay(item: IGoods) {
  116. if (item.price! <= 0) {
  117. window.$message.info('该商品是免费的,不需要购买!');
  118. return;
  119. }
  120. showQrPay.value = false;
  121. nextTick(() => {
  122. goodsInfo.money = item.price!;
  123. goodsInfo.goodsId = item.id!;
  124. showQrPay.value = true;
  125. });
  126. }
  127. </script>
  128. <style lang="scss" scoped>
  129. .shop-wrap {
  130. padding: 0 30px;
  131. .tab-list {
  132. display: flex;
  133. align-items: center;
  134. margin-bottom: 20px;
  135. height: 40px;
  136. font-size: 14px;
  137. user-select: none;
  138. .tab {
  139. position: relative;
  140. margin-right: 20px;
  141. cursor: pointer;
  142. &.active {
  143. color: $theme-color-gold;
  144. font-size: 16px;
  145. &::after {
  146. position: absolute;
  147. bottom: -6px;
  148. left: 50%;
  149. width: 20px;
  150. height: 2px;
  151. border-radius: 10px;
  152. background-color: $theme-color-gold;
  153. content: '';
  154. transform: translateX(-50%);
  155. }
  156. }
  157. }
  158. }
  159. .goods-list {
  160. display: flex;
  161. flex-wrap: wrap;
  162. .goods {
  163. display: flex;
  164. box-sizing: border-box;
  165. margin-right: 20px;
  166. margin-bottom: 20px;
  167. padding: 18px 10px 10px;
  168. width: 400px;
  169. border-radius: 6px;
  170. box-shadow: 0 4px 30px 0 rgba(238, 242, 245, 0.8);
  171. cursor: pointer;
  172. transition: box-shadow 0.2s linear;
  173. &:hover {
  174. box-shadow: 4px 4px 20px 0 rgba(205, 216, 228, 0.6);
  175. }
  176. .left {
  177. position: relative;
  178. margin-right: 20px;
  179. width: 100px;
  180. height: 100px;
  181. @extend %containBg;
  182. .badge {
  183. position: absolute;
  184. top: -10px;
  185. right: -10px;
  186. display: flex;
  187. align-items: center;
  188. justify-content: center;
  189. padding: 2px;
  190. border-radius: 2px;
  191. color: white;
  192. .txt {
  193. display: inline-block;
  194. line-height: 1;
  195. transform-origin: center !important;
  196. @include minFont(10);
  197. }
  198. }
  199. }
  200. .right {
  201. .title {
  202. font-size: 22px;
  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>