cm-coupon.vue 9.0 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. }
  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. if (!this.userId) return this.$router.navigateTo('authorize/login-custom')
  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>