cm-coupon.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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.useCoupon()
  181. this.$emit('click', this.couponData)
  182. },
  183. // 选择/勾选
  184. clickChoose() {
  185. this.$emit('click', this.couponData)
  186. },
  187. // 使用优惠券
  188. useCoupon() {
  189. const productType = this.couponData.productType
  190. if (productType === 1) {
  191. console.log('全部商品可用')
  192. this.$router.switchTab('home')
  193. } else {
  194. console.log('部分商品可用')
  195. this.$router.navigateTo(`goods/goods-coupon-list?couponId=${this.couponData.couponId}`)
  196. }
  197. }
  198. }
  199. }
  200. </script>
  201. <style lang="scss" scoped>
  202. // 优惠券样式属性变量
  203. $color: #ff457b;
  204. $coupon-width: 702rpx;
  205. $coupon-height: 200rpx;
  206. $coupon-bg-on: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-on.png);
  207. $coupon-bg-off: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-off.png);
  208. .coupon {
  209. @extend .cm-flex-between;
  210. justify-content: flex-start;
  211. width: $coupon-width;
  212. height: $coupon-height;
  213. box-sizing: border-box;
  214. background: #ffffff no-repeat center;
  215. background-size: $coupon-width $coupon-height;
  216. &.bg-on {
  217. background-image: $coupon-bg-on;
  218. }
  219. &.bg-off {
  220. background-image: $coupon-bg-off;
  221. }
  222. .amount,
  223. .use-scope,
  224. .control {
  225. height: 100%;
  226. }
  227. .amount {
  228. @extend .cm-flex-center;
  229. flex-direction: column;
  230. position: relative;
  231. width: 210rpx;
  232. .price {
  233. font-size: 56rpx;
  234. font-weight: 600;
  235. color: $color;
  236. .sm {
  237. font-size: 24rpx;
  238. }
  239. }
  240. .tip {
  241. margin-top: 6rpx;
  242. font-size: 24rpx;
  243. color: #404040;
  244. }
  245. }
  246. .coupon-info {
  247. flex: 1;
  248. max-width: 276rpx;
  249. margin: 0 28rpx;
  250. font-size: 26rpx;
  251. color: #333333;
  252. .name {
  253. @include ellipsis(1);
  254. font-weight: 600;
  255. }
  256. .use-scope {
  257. @include ellipsis(1);
  258. margin: 12rpx 0;
  259. }
  260. .time {
  261. @include ellipsis(1);
  262. font-size: 20rpx;
  263. color: #999999;
  264. }
  265. }
  266. .control {
  267. @extend .cm-flex-center;
  268. justify-content: flex-end;
  269. width: 160rpx;
  270. box-sizing: border-box;
  271. padding-right: 24rpx;
  272. background-position-x: 40rpx;
  273. background-position-y: 20rpx;
  274. background-repeat: no-repeat;
  275. background-size: 100rpx 100rpx;
  276. .icon {
  277. width: 100%;
  278. height: 100%;
  279. box-sizing: border-box;
  280. padding-top: 24rpx;
  281. color: $color;
  282. text-align: right;
  283. }
  284. }
  285. .coupon-tag {
  286. position: absolute;
  287. top: 6rpx;
  288. left: 6rpx;
  289. height: 32rpx;
  290. padding: 0 8rpx;
  291. line-height: 32rpx;
  292. font-size: 22rpx;
  293. color: #ffffff;
  294. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  295. border-radius: 10rpx 0px 10rpx 0px;
  296. }
  297. }
  298. </style>