cm-coupon.vue 9.4 KB

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