cm-coupon.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <template>
  2. <view class="coupon" :class="canUse ? 'on' : 'off'" v-if="couponData">
  3. <view class="content" :class="[statusIcon, { 'cover-bg': showStatus }]">
  4. <view class="header">
  5. <!-- 优惠券类别 -->
  6. <view class="tag">{{ couponData.couponType | formatTag }}</view>
  7. <view class="price"><text>¥</text>{{ couponData.couponAmount }}</view>
  8. <!-- 使用条件 -->
  9. <view class="tip">
  10. <template v-if="couponData.noThresholdFlag === 1">
  11. 无门槛
  12. </template>
  13. <template v-else>
  14. 满{{ couponData.touchPrice }}可用
  15. </template>
  16. </view>
  17. </view>
  18. <view class="center desc">
  19. <!-- 优惠券名称 -->
  20. <view class="row bold">{{ couponData.couponName }}</view>
  21. <!-- 适用范围 -->
  22. <view class="row">{{ couponData.productType | formatUseType }}</view>
  23. <!-- 截止日期 -->
  24. <view class="end-time">截止日期:{{ couponData.receivePeriod | formatDate }}</view>
  25. </view>
  26. <view class="footer">
  27. <view class="btn" @click="handleBtnClick" v-if="couponData.useStatus === 0">领取</view>
  28. <view class="btn plain" @click="handleBtnClick" v-if="couponData.useStatus === 1">去使用</view>
  29. </view>
  30. <!-- <text class="radio-flag iconfont icon-weixuanze"></text> -->
  31. <text class="radio-flag iconfont icon-xuanze" v-if="chooseAble" @click="choose"></text>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import { dateFormat } from '@/common/util.js'
  37. import { mapGetters, mapActions } from 'vuex'
  38. export default {
  39. props: {
  40. // 优惠券数据
  41. couponData: {
  42. type: Object,
  43. default: () => {}
  44. },
  45. // 设置优惠券是否可选
  46. chooseAble: {
  47. type: Boolean,
  48. default: false
  49. },
  50. // 是否显示优惠券状态
  51. showStatus: {
  52. type: Boolean,
  53. default: false
  54. }
  55. },
  56. filters: {
  57. // 优惠券标签格式化
  58. formatTag(val) {
  59. const tags = {
  60. 1: '活动券',
  61. 2: '用户专享券',
  62. 3: '新用户券',
  63. 4: '好友分享券',
  64. 5: '好友消费券'
  65. }
  66. return tags[val] || '未知券'
  67. },
  68. // 格式化日期
  69. formatDate(val) {
  70. return dateFormat(new Date(val), 'yyyy-MM-dd')
  71. },
  72. // 优惠券使用范围
  73. formatUseType(val) {
  74. const type = {
  75. 1: '全商城商品使用',
  76. 2: '部分商品使用'
  77. }
  78. return type[val] || '优惠券无法使用'
  79. }
  80. },
  81. computed: {
  82. ...mapGetters(['hasLogin']),
  83. // 优惠券状态图标
  84. statusIcon() {
  85. if (!this.couponData) return
  86. let name = ''
  87. const type = {
  88. 0: '', // 未领取
  89. 1: 'received', //已领取
  90. 2: 'used', // 已使用
  91. 3: 'expired' //已失效
  92. }
  93. return type[this.couponData.useStatus + 1]
  94. },
  95. // 优惠券是能否领取和使用
  96. canUse() {
  97. return ![2, 3].includes(this.couponData.useStatus)
  98. }
  99. },
  100. methods: {
  101. ...mapActions('coupon', ['receiveCoupon']),
  102. // 点击勾选按钮
  103. choose() {
  104. this.$emit('choose', this.couponData)
  105. },
  106. // 按钮点击
  107. handleBtnClick() {
  108. if (!this.hasLogin) return this.$api.navigateTo('/pages/login/login')
  109. const clickFns = {
  110. 0: this.receiveCoupon, // 领取优惠券
  111. 1: this.useCoupon // 使用优惠券
  112. }
  113. // 将优惠券id作为参数传递进去
  114. clickFns[this.couponData.useStatus](this.couponData.couponId).then(res=>{
  115. // 向父组件发送领取优惠券事件
  116. this.couponData.useStatus === 0 && this.$emit('btnClick', this.couponData)
  117. })
  118. },
  119. // 使用优惠券
  120. useCoupon() {
  121. return Promise.resolve()
  122. console.log('使用优惠券')
  123. }
  124. }
  125. }
  126. </script>
  127. <style lang="scss" scoped>
  128. $grid: 24rpx;
  129. $color: #ff457b;
  130. $coupon-width: 702rpx;
  131. $coupon-height: 200rpx;
  132. $coupon-bg-on: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-on.png);
  133. $coupon-bg-off: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-off.png);
  134. $coupon-bg-received: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-received.png); // 已领取
  135. $coupon-bg-expired: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-expired.png); // 已失效
  136. $coupon-bg-used: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-used.png); // 已使用
  137. .coupon {
  138. width: $coupon-width;
  139. height: $coupon-height;
  140. padding: 6rpx;
  141. margin: $grid;
  142. box-sizing: border-box;
  143. background: #ffffff no-repeat center;
  144. background-size: $coupon-width $coupon-height;
  145. .content {
  146. position: relative;
  147. display: flex;
  148. justify-content: space-between;
  149. align-items: center;
  150. }
  151. .cover-bg {
  152. background-position: 580rpx 16rpx;
  153. background-size: 100rpx 100rpx;
  154. background-repeat: no-repeat;
  155. &.received {
  156. background-image: $coupon-bg-received;
  157. }
  158. &.expired {
  159. background-image: $coupon-bg-expired;
  160. }
  161. &.used {
  162. background-image: $coupon-bg-used;
  163. }
  164. }
  165. &.on {
  166. background-image: $coupon-bg-on;
  167. }
  168. &.off {
  169. filter: grayscale(1);
  170. opacity: 0.7;
  171. background-image: $coupon-bg-off;
  172. }
  173. .header,
  174. .center,
  175. .footer {
  176. display: flex;
  177. flex-direction: column;
  178. justify-content: center;
  179. }
  180. .header {
  181. align-items: center;
  182. position: relative;
  183. width: 204rpx;
  184. height: 188rpx;
  185. .tag {
  186. position: absolute;
  187. top: 0;
  188. left: 0;
  189. height: 32rpx;
  190. padding: 0 6rpx;
  191. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  192. border-radius: 10rpx 0px 10rpx 0px;
  193. font-size: 22rpx;
  194. color: #ffffff;
  195. text-align: center;
  196. line-height: 32rpx;
  197. }
  198. .price {
  199. font-size: 56rpx;
  200. font-weight: 600;
  201. color: $color;
  202. text {
  203. font-size: 24rpx;
  204. }
  205. }
  206. .tip {
  207. margin-top: 6rpx;
  208. font-size: 24rpx;
  209. color: #404040;
  210. }
  211. }
  212. .center {
  213. flex: 1;
  214. margin-left: 36rpx;
  215. .row {
  216. width: 250rpx;
  217. white-space: nowrap;
  218. overflow: hidden;
  219. text-overflow: ellipsis;
  220. font-size: 26rpx;
  221. color: #333333;
  222. margin-bottom: 16rpx;
  223. &.bold {
  224. font-weight: 600;
  225. }
  226. }
  227. .end-time {
  228. font-size: 20rpx;
  229. color: #999999;
  230. }
  231. }
  232. .footer {
  233. align-items: center;
  234. width: 128rpx;
  235. margin-right: $grid;
  236. .btn {
  237. width: 128rpx;
  238. height: 48rpx;
  239. background: linear-gradient(270deg, #f83c6c 0%, #fc32b4 100%);
  240. border-radius: 28rpx;
  241. box-sizing: border-box;
  242. border: 1rpx solid transparent;
  243. text-align: center;
  244. line-height: 46rpx;
  245. font-size: 26rpx;
  246. color: #ffffff;
  247. &.plain {
  248. border: 1rpx solid $color;
  249. background: transparent;
  250. color: $color;
  251. }
  252. }
  253. }
  254. .radio-flag {
  255. position: absolute;
  256. top: $grid;
  257. right: $grid;
  258. color: $color;
  259. font-size: 32rpx;
  260. }
  261. }
  262. </style>