index.vue 5.1 KB

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