index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <!-- 优惠券组 -->
  3. <view v-if="couponList.length" class="diy-coupon" :style="{ padding: `${itemStyle.paddingTop}px 0`, background: itemStyle.background }">
  4. <scroll-view :scroll-x="true">
  5. <view class="coupon-wrapper">
  6. <view class="coupon-item" :class="{ disable: !dataItem.state.value }" v-for="(dataItem, index) in couponList" :key="index"
  7. :style="{ marginRight: `${itemStyle.marginRight}px` }">
  8. <text class="before" :style="{ background: itemStyle.background }"></text>
  9. <view class="left-content" :style="{ background: itemStyle.couponBgColor }">
  10. <view class="content-top">
  11. <block v-if="dataItem.coupon_type == 10">
  12. <text class="unit">¥</text>
  13. <text class="price">{{ dataItem.reduce_price }}</text>
  14. </block>
  15. <text v-if="dataItem.coupon_type == 20" class="price">{{ dataItem.discount }}折</text>
  16. </view>
  17. <view class="content-bottom">
  18. <text class="f-22">满{{ dataItem.min_price }}元可用</text>
  19. </view>
  20. </view>
  21. <view class="right-receive" :style="{ background: itemStyle.receiveBgColor }" @click="handleReceive(index, dataItem)">
  22. <block v-if="dataItem.state.value">
  23. <text>立即</text>
  24. <text>领取</text>
  25. </block>
  26. <text v-else>{{ dataItem.state.text }}</text>
  27. </view>
  28. </view>
  29. </view>
  30. </scroll-view>
  31. </view>
  32. </template>
  33. <script>
  34. import mixin from '../mixin'
  35. import * as MyCouponApi from '@/api/myCoupon'
  36. import { cloneObj } from '@/utils/util'
  37. export default {
  38. /**
  39. * 组件的属性列表
  40. * 用于组件自定义设置
  41. */
  42. props: {
  43. itemIndex: String,
  44. itemStyle: Object,
  45. params: Object,
  46. dataList: Array
  47. },
  48. data() {
  49. return {
  50. // 优惠券列表
  51. couponList: [],
  52. // 防止重复提交
  53. disable: false
  54. }
  55. },
  56. watch: {
  57. // 这里监听dataList并写入到data中, 因为领取事件不能直接修改props中的属性
  58. dataList: {
  59. handler(data) {
  60. this.couponList = cloneObj(data)
  61. // console.log(this.couponList)
  62. },
  63. immediate: true,
  64. deep: true
  65. }
  66. },
  67. mixins: [mixin],
  68. /**
  69. * 组件的方法列表
  70. * 更新属性和数据的方法与更新页面数据的方法类似
  71. */
  72. methods: {
  73. // 立即领取事件
  74. handleReceive(index, item) {
  75. const app = this
  76. if (app.disable || !item.state.value) {
  77. return
  78. }
  79. app.disable = true
  80. MyCouponApi.receive(item.coupon_id, {}, { load: false })
  81. .then(result => {
  82. // 显示领取成功提示
  83. app.$success(result.message)
  84. // 将优惠券设置为已领取
  85. app.setReceived(index, item)
  86. })
  87. .finally(() => app.disable = false)
  88. },
  89. // 将优惠券设置为已领取
  90. setReceived(index, item) {
  91. const app = this
  92. // #ifdef VUE2
  93. app.$set(app.couponList, index, {
  94. ...item,
  95. state: { value: 0, text: '已领取' }
  96. })
  97. // #endif
  98. // #ifdef VUE3
  99. app.couponList[index] = {
  100. ...item,
  101. state: { value: 0, text: '已领取' }
  102. }
  103. // #endif
  104. }
  105. }
  106. }
  107. </script>
  108. <style lang="scss" scoped>
  109. .diy-coupon {
  110. .coupon-wrapper {
  111. display: flex;
  112. width: max-content;
  113. height: 130rpx;
  114. padding: 0 24rpx;
  115. &::-webkit-scrollbar {
  116. display: none;
  117. }
  118. }
  119. }
  120. .coupon-item {
  121. flex-shrink: 0;
  122. width: 300rpx;
  123. height: 130rpx;
  124. position: relative;
  125. color: #fff;
  126. overflow: hidden;
  127. box-sizing: border-box;
  128. margin-right: 40rpx;
  129. display: flex;
  130. &:last-child {
  131. margin-right: 0 !important;
  132. }
  133. &.disable {
  134. .left-content {
  135. background: linear-gradient(-113deg, #bdbdbd, #a2a1a2) !important;
  136. }
  137. .right-receive {
  138. background-color: #949494 !important;
  139. }
  140. }
  141. .before {
  142. content: "";
  143. position: absolute;
  144. z-index: 1;
  145. width: 32rpx;
  146. height: 32rpx;
  147. top: 45%;
  148. left: -16rpx;
  149. transform: translateY(-50%);
  150. border-radius: 80%;
  151. background-color: #fff;
  152. }
  153. .left-content {
  154. display: flex;
  155. flex-direction: column;
  156. justify-content: center;
  157. align-items: center;
  158. position: relative;
  159. width: 70%;
  160. height: 100%;
  161. background-color: #E5004F;
  162. font-size: 24rpx;
  163. .content-top {
  164. .unit {
  165. font-size: 15px;
  166. }
  167. .price {
  168. font-size: 44rpx;
  169. }
  170. }
  171. }
  172. .right-receive {
  173. display: flex;
  174. flex-direction: column;
  175. justify-content: center;
  176. align-items: center;
  177. width: 30%;
  178. height: 100%;
  179. background-color: #4e4e4e;
  180. text-align: center;
  181. font-size: 24rpx;
  182. }
  183. }
  184. </style>