cm-coupon.vue 9.6 KB

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