cm-coupon.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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, mapGetters } 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. 6: '消费分享券'
  62. }
  63. return tags[val] || '未知券'
  64. },
  65. // 优惠券使用范围
  66. formatUseType(val) {
  67. const type = {
  68. 1: '全商城商品使用',
  69. 2: '部分商品使用'
  70. }
  71. return type[val] || '优惠券无法使用'
  72. }
  73. },
  74. components: {
  75. UButton
  76. },
  77. props: {
  78. couponData: {
  79. type: Object,
  80. default: () => {}
  81. },
  82. // 背景颜色类型
  83. bgType: {
  84. type: String,
  85. validator: type => {
  86. return ['on', 'off'].indexOf(type) > -1
  87. },
  88. default: 'on'
  89. },
  90. // 优惠券状态图标
  91. status: {
  92. type: String,
  93. validator: type => {
  94. return ['received', 'expired', 'used', ''].indexOf(type) > -1
  95. },
  96. default: ''
  97. },
  98. // 按钮类型
  99. controlType: {
  100. type: String,
  101. validator: type => {
  102. return ['receive', 'use', 'buy', 'search', 'choose', ''].indexOf(type) > -1
  103. },
  104. default: ''
  105. },
  106. // 是否选择
  107. checked: {
  108. type: Boolean,
  109. default: true
  110. }
  111. },
  112. data() {
  113. return {
  114. isChecked: false
  115. }
  116. },
  117. computed: {
  118. ...mapGetters(['userId']),
  119. checkedClass() {
  120. return this.checked ? 'icon-xuanze' : 'icon-weixuanze'
  121. },
  122. plain() {
  123. return this.controlType === 'use'
  124. },
  125. isChoose() {
  126. return this.controlType === 'choose'
  127. },
  128. statusIcon() {
  129. if (!this.status) return
  130. return `url(${this.staticUrl}icon-coupon-${this.status}.png)`
  131. },
  132. couponStyle() {
  133. if (this.bgType === 'on') {
  134. return {
  135. backgroundImage: `url(${this.staticUrl}icon-coupon-bg-${this.bgType}.png)`
  136. }
  137. }
  138. return {
  139. backgroundImage: `url(${this.staticUrl}icon-coupon-bg-${this.bgType}.png)`,
  140. filter: 'grayscale(1)',
  141. opacity: '0.7'
  142. }
  143. },
  144. couponPrice() {
  145. return this.couponData.couponAmount.toString().split('.') || ['0', '00']
  146. }
  147. },
  148. methods: {
  149. ...mapActions('coupon', ['receiveCoupon']),
  150. // 操作优惠券事件
  151. handleClick(event) {
  152. const { name } = event.target.dataset
  153. console.log(name)
  154. switch (name) {
  155. case 'receive':
  156. this.clickReceive()
  157. break
  158. case 'use':
  159. case 'buy':
  160. case 'search':
  161. this.clickToList()
  162. break
  163. case 'choose':
  164. this.clickChoose()
  165. break
  166. default:
  167. console.log('nothing click')
  168. break
  169. }
  170. },
  171. // 领取优惠券
  172. async clickReceive() {
  173. try {
  174. await this.receiveCoupon(this.couponData)
  175. this.$emit('click', this.couponData)
  176. } catch (e) {
  177. console.log(e)
  178. }
  179. },
  180. // 跳转商品列表
  181. clickToList() {
  182. this.useCoupon()
  183. this.$emit('click', this.couponData)
  184. },
  185. // 选择/勾选
  186. clickChoose() {
  187. this.$emit('click', this.couponData)
  188. },
  189. // 使用优惠券
  190. useCoupon() {
  191. const productType = this.couponData.productType
  192. if (productType === 1) {
  193. console.log('全部商品可用')
  194. this.$router.switchTab('home')
  195. } else {
  196. console.log('部分商品可用')
  197. this.$router.navigateTo(`goods/goods-coupon-list?couponId=${this.couponData.couponId}`)
  198. }
  199. }
  200. }
  201. }
  202. </script>
  203. <style lang="scss" scoped>
  204. // 优惠券样式属性变量
  205. $color: #ff457b;
  206. $coupon-width: 702rpx;
  207. $coupon-height: 200rpx;
  208. $coupon-bg-on: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-on.png);
  209. $coupon-bg-off: url(https://static.caimei365.com/app/mini-hehe/icon/icon-coupon-bg-off.png);
  210. .coupon {
  211. @extend .cm-flex-between;
  212. justify-content: flex-start;
  213. width: $coupon-width;
  214. height: $coupon-height;
  215. box-sizing: border-box;
  216. background: #ffffff no-repeat center;
  217. background-size: $coupon-width $coupon-height;
  218. &.bg-on {
  219. background-image: $coupon-bg-on;
  220. }
  221. &.bg-off {
  222. background-image: $coupon-bg-off;
  223. }
  224. .amount,
  225. .use-scope,
  226. .control {
  227. height: 100%;
  228. }
  229. .amount {
  230. @extend .cm-flex-center;
  231. flex-direction: column;
  232. position: relative;
  233. width: 210rpx;
  234. .price {
  235. font-size: 56rpx;
  236. font-weight: 600;
  237. color: $color;
  238. .sm {
  239. font-size: 24rpx;
  240. }
  241. }
  242. .tip {
  243. margin-top: 6rpx;
  244. font-size: 24rpx;
  245. color: #404040;
  246. }
  247. }
  248. .coupon-info {
  249. flex: 1;
  250. max-width: 276rpx;
  251. margin: 0 28rpx;
  252. font-size: 26rpx;
  253. color: #333333;
  254. .name {
  255. @include ellipsis(1);
  256. font-weight: 600;
  257. }
  258. .use-scope {
  259. @include ellipsis(1);
  260. margin: 12rpx 0;
  261. }
  262. .time {
  263. @include ellipsis(1);
  264. font-size: 20rpx;
  265. color: #999999;
  266. }
  267. }
  268. .control {
  269. @extend .cm-flex-center;
  270. justify-content: flex-end;
  271. width: 160rpx;
  272. box-sizing: border-box;
  273. padding-right: 24rpx;
  274. background-position-x: 40rpx;
  275. background-position-y: 20rpx;
  276. background-repeat: no-repeat;
  277. background-size: 100rpx 100rpx;
  278. .icon {
  279. width: 100%;
  280. height: 100%;
  281. box-sizing: border-box;
  282. padding-top: 24rpx;
  283. color: $color;
  284. text-align: right;
  285. }
  286. }
  287. .coupon-tag {
  288. position: absolute;
  289. top: 6rpx;
  290. left: 6rpx;
  291. height: 32rpx;
  292. padding: 0 8rpx;
  293. line-height: 32rpx;
  294. font-size: 22rpx;
  295. color: #ffffff;
  296. background: linear-gradient(90deg, #fc32b4 0%, #f83c6c 100%);
  297. border-radius: 10rpx 0px 10rpx 0px;
  298. }
  299. }
  300. </style>