cm-coupon.vue 8.9 KB

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