cm-coupon.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="coupon" :style="[couponStyle]">
  3. <!-- 抵扣金额 -->
  4. <view class="amount">
  5. <view class="coupon-tag">{{ couponData.couponType | formatTag }}</view>
  6. <view class="price">
  7. <text class="sm">¥</text>
  8. <text>{{ couponPrice[0] }}</text>
  9. <text class="sm" v-if="couponPrice[1]">.{{ couponPrice[1] }}</text>
  10. </view>
  11. <view class="tip">
  12. <template v-if="couponData.noThresholdFlag === 1">
  13. 无门槛
  14. </template>
  15. <template v-else>
  16. 满{{ couponData.touchPrice }}可用
  17. </template>
  18. </view>
  19. </view>
  20. <!-- 使用范围 -->
  21. <view class="coupon-info">
  22. <!-- 优惠券名称 -->
  23. <view class="name">{{ couponData.couponName }}</view>
  24. <!-- 适用范围 -->
  25. <view class="use-scope">{{ couponData.productType | formatUseType }}</view>
  26. <!-- 有效期 -->
  27. <view class="time">
  28. <!-- 领取截至期限 -->
  29. <view class="receive-time" v-if="couponData.useStatus === 0">
  30. <text v-if="couponData.receivePeriod">截止日期:{{ couponData.receivePeriod | dateFormat }}</text>
  31. <text v-else>永久</text>
  32. </view>
  33. <!-- 使用期限 -->
  34. <view class="use-time" v-else>有效期至:{{ couponData.usePeriod | dateFormat }}</view>
  35. </view>
  36. </view>
  37. <!-- 操作区域 -->
  38. <view class="control" @click="handleClick" :style="{ backgroundImage: statusIcon }">
  39. <!-- 领取 去使用 去凑单 可用商品 选择 -->
  40. <template v-if="controlType">
  41. <view class="iconfont icon" :class="checkedClass" :data-name="controlType" v-if="isChoose"></view>
  42. <u-button :plain="plain" :data-name="controlType" :controlType="controlType" v-else></u-button>
  43. </template>
  44. </view>
  45. </view>
  46. </template>
  47. <script>
  48. import UButton from './u-button.vue'
  49. import { mapActions } from 'vuex'
  50. export default {
  51. name: 'cm-coupon',
  52. filters: {
  53. // 优惠券标签格式化
  54. formatTag(val) {
  55. const tags = {
  56. 1: '活动券',
  57. 2: '用户专享券',
  58. 3: '新用户券',
  59. 4: '好友邀请券',
  60. 5: '好友消费券'
  61. }
  62. return tags[val] || '未知券'
  63. },
  64. // 优惠券使用范围
  65. formatUseType(val) {
  66. const type = {
  67. 1: '全商城商品使用',
  68. 2: '部分商品使用'
  69. }
  70. return type[val] || '优惠券无法使用'
  71. }
  72. },
  73. components: {
  74. UButton
  75. },
  76. props: {
  77. couponData: {
  78. type: Object,
  79. default: () => {}
  80. },
  81. // 背景颜色类型
  82. bgType: {
  83. type: String,
  84. validator: type => {
  85. return ['on', 'off'].indexOf(type) > -1
  86. },
  87. default: 'on'
  88. },
  89. // 优惠券状态图标
  90. status: {
  91. type: String,
  92. validator: type => {
  93. return ['received', 'expired', 'used', ''].indexOf(type) > -1
  94. },
  95. default: ''
  96. },
  97. // 按钮类型
  98. controlType: {
  99. type: String,
  100. validator: type => {
  101. return ['receive', 'use', 'buy', 'search', 'choose', ''].indexOf(type) > -1
  102. },
  103. default: ''
  104. },
  105. // 是否选择
  106. checked: {
  107. type: Boolean,
  108. default: true
  109. }
  110. },
  111. data() {
  112. return {
  113. isChecked: false
  114. }
  115. },
  116. computed: {
  117. checkedClass() {
  118. return this.checked ? 'icon-xuanze' : 'icon-weixuanze'
  119. },
  120. plain() {
  121. return this.controlType === 'use'
  122. },
  123. isChoose() {
  124. return this.controlType === 'choose'
  125. },
  126. statusIcon() {
  127. if (!this.status) return
  128. return `url(${this.staticUrl}icon-coupon-${this.status}.png)`
  129. },
  130. couponStyle() {
  131. if (this.bgType === 'on') {
  132. return {
  133. backgroundImage: `url(${this.staticUrl}icon-coupon-bg-${this.bgType}.png)`
  134. }
  135. }
  136. return {
  137. backgroundImage: `url(${this.staticUrl}icon-coupon-bg-${this.bgType}.png)`,
  138. filter: 'grayscale(1)',
  139. opacity: '0.7'
  140. }
  141. },
  142. couponPrice() {
  143. return this.couponData.couponAmount.toString().split('.') || ['0', '00']
  144. }
  145. },
  146. methods: {
  147. ...mapActions('coupon', ['receiveCoupon']),
  148. // 操作优惠券事件
  149. handleClick(event) {
  150. const { name } = event.target.dataset
  151. console.log(name)
  152. switch (name) {
  153. case 'receive':
  154. this.clickReceive()
  155. break
  156. case 'use':
  157. case 'buy':
  158. case 'search':
  159. this.clickToList()
  160. break
  161. case 'choose':
  162. this.clickChoose()
  163. break
  164. default:
  165. console.log('nothing click')
  166. break
  167. }
  168. },
  169. // 领取优惠券
  170. async clickReceive() {
  171. try {
  172. await this.receiveCoupon(this.couponData)
  173. this.$emit('click', this.couponData)
  174. } catch (e) {
  175. console.log(e)
  176. }
  177. },
  178. // 跳转商品列表
  179. clickToList() {
  180. this.$emit('click', this.couponData)
  181. },
  182. // 选择/勾选
  183. clickChoose() {
  184. this.$emit('click', this.couponData)
  185. }
  186. }
  187. }
  188. </script>
  189. <style lang="scss" scoped>
  190. // 优惠券样式属性变量
  191. $color: #ff457b;
  192. $coupon-width: 702rpx;
  193. $coupon-height: 200rpx;
  194. $coupon-bg-on: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-on.png);
  195. $coupon-bg-off: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-off.png);
  196. .coupon {
  197. @extend .cm-flex-between;
  198. justify-content: flex-start;
  199. width: $coupon-width;
  200. height: $coupon-height;
  201. box-sizing: border-box;
  202. background: #ffffff no-repeat center;
  203. background-size: $coupon-width $coupon-height;
  204. &.bg-on {
  205. background-image: $coupon-bg-on;
  206. }
  207. &.bg-off {
  208. background-image: $coupon-bg-off;
  209. }
  210. .amount,
  211. .use-scope,
  212. .control {
  213. height: 100%;
  214. }
  215. .amount {
  216. @extend .cm-flex-center;
  217. flex-direction: column;
  218. position: relative;
  219. width: 210rpx;
  220. .price {
  221. font-size: 56rpx;
  222. font-weight: 600;
  223. color: $color;
  224. .sm {
  225. font-size: 24rpx;
  226. }
  227. }
  228. .tip {
  229. margin-top: 6rpx;
  230. font-size: 24rpx;
  231. color: #404040;
  232. }
  233. }
  234. .coupon-info {
  235. flex: 1;
  236. max-width: 276rpx;
  237. margin: 0 28rpx;
  238. font-size: 26rpx;
  239. color: #333333;
  240. .name {
  241. @include ellipsis(1);
  242. font-weight: 600;
  243. }
  244. .use-scope {
  245. @include ellipsis(1);
  246. margin: 12rpx 0;
  247. }
  248. .time {
  249. @include ellipsis(1);
  250. font-size: 20rpx;
  251. color: #999999;
  252. }
  253. }
  254. .control {
  255. @extend .cm-flex-center;
  256. justify-content: flex-end;
  257. width: 160rpx;
  258. box-sizing: border-box;
  259. padding-right: 24rpx;
  260. background-position-x: 40rpx;
  261. background-position-y: 20rpx;
  262. background-repeat: no-repeat;
  263. background-size: 100rpx 100rpx;
  264. .icon {
  265. width: 100%;
  266. height: 100%;
  267. box-sizing: border-box;
  268. padding-top: 24rpx;
  269. color: $color;
  270. text-align: right;
  271. }
  272. }
  273. .coupon-tag {
  274. position: absolute;
  275. top: 6rpx;
  276. left: 6rpx;
  277. height: 32rpx;
  278. padding: 0 8rpx;
  279. line-height: 32rpx;
  280. font-size: 22rpx;
  281. color: #ffffff;
  282. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  283. border-radius: 10rpx 0px 10rpx 0px;
  284. }
  285. }
  286. </style>