You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

298 lines
6.2 KiB

  1. <template>
  2. <view style="padding:20rpx;">
  3. <view>
  4. <view class="card">
  5. <view class="card-left">
  6. <view class="">
  7. {{switchType(couponData.stockType)}}
  8. </view>
  9. </view>
  10. <view class="card-center">
  11. <view class="card-center-top"></view>
  12. <view class="card-center-bottom"></view>
  13. </view>
  14. <view class="card-right">
  15. <view class="card-content">
  16. <view class="card-info">{{couponData.stockName}}</view>
  17. <view class="card-type">可用于
  18. <text class="card-type-text">专业喂养</text>
  19. <text class="card-type-text">专业遛狗</text>
  20. <!-- <text class="card-type-text">{{ couponData.goodsName }}</text> -->
  21. </view>
  22. <view class="card-time">有效期至: {{couponData.availableEndTime ? couponData.availableEndTime.slice(0, 16) : ''}}</view>
  23. </view>
  24. <view :class="['coupon-btn', { 'coupon-btn-disabled': !canReceiveCoupon }]" @click="handleReceiveCoupon">
  25. <text class="coupon-btn-text">{{ getCouponButtonText }}</text>
  26. </view>
  27. </view>
  28. </view>
  29. <view class="card-bottom">
  30. <view class="card-bottom-text">
  31. 优惠券不可兑换现金
  32. </view>
  33. <view class="card-bottom-text" @click="showRulePopup">
  34. 查看详细规则>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { receiveCoupon } from "@/api/system/user"
  42. export default {
  43. name: 'CouponItem',
  44. props: {
  45. // 优惠券数据
  46. couponData: {
  47. type: Object,
  48. default: () => ({})
  49. }
  50. },
  51. computed: {
  52. // 判断优惠券是否可以领取
  53. canReceiveCoupon() {
  54. // 如果已经领取过,则不能再领取
  55. if (this.couponData.alreadyReceived && this.couponData.alreadyReceived >= this.couponData.maxCouponsPerUser) {
  56. return false;
  57. }
  58. return true;
  59. },
  60. // 获取按钮文本
  61. getCouponButtonText() {
  62. if (!this.canReceiveCoupon) {
  63. return '已领取';
  64. }
  65. return '立即领取';
  66. }
  67. },
  68. methods: {
  69. // 切换优惠券类型显示
  70. switchType(type) {
  71. if (type == 'PNORMAL') {
  72. return '满减券'
  73. }
  74. if (type == 'PDISCOUNT') {
  75. return '折扣券'
  76. }
  77. if (type == 'PTRAIL') {
  78. return '体验券'
  79. }
  80. return '优惠券'
  81. },
  82. // 处理优惠券领取点击事件
  83. handleReceiveCoupon() {
  84. if (!this.canReceiveCoupon) {
  85. return; // 已领取的优惠券不能再次领取
  86. }
  87. // 直接调用API领取优惠券
  88. this.receiveCouponApi(this.couponData.id);
  89. },
  90. // 调用优惠券领取API
  91. receiveCouponApi(id) {
  92. let data = {
  93. stockId: id
  94. }
  95. receiveCoupon(data).then(res => {
  96. console.log("receiveCoupon response:", res)
  97. if (res.code == 200) {
  98. // 显示成功提示
  99. if (this.$modal && this.$modal.showToast) {
  100. this.$modal.showToast('优惠券领取成功')
  101. } else {
  102. uni.showToast({
  103. title: '优惠券领取成功',
  104. icon: 'success'
  105. })
  106. }
  107. // 更新本地优惠券状态
  108. this.updateLocalCouponStatus();
  109. // 通知父组件优惠券已领取
  110. this.$emit('coupon-received', this.couponData);
  111. } else {
  112. // 显示失败提示
  113. if (this.$modal && this.$modal.showToast) {
  114. this.$modal.showToast('领取优惠券失败')
  115. } else {
  116. uni.showToast({
  117. title: '领取优惠券失败',
  118. icon: 'none'
  119. })
  120. }
  121. }
  122. }).catch(err => {
  123. console.error('领取优惠券失败:', err)
  124. // 显示错误提示
  125. if (this.$modal && this.$modal.showToast) {
  126. this.$modal.showToast('领取优惠券失败')
  127. } else {
  128. uni.showToast({
  129. title: '领取优惠券失败',
  130. icon: 'none'
  131. })
  132. }
  133. })
  134. },
  135. // 更新本地优惠券状态
  136. updateLocalCouponStatus() {
  137. // 创建更新后的优惠券数据副本
  138. const updatedCoupon = { ...this.couponData };
  139. // 如果alreadyReceived字段不存在,初始化为0
  140. if (!updatedCoupon.alreadyReceived) {
  141. updatedCoupon.alreadyReceived = 0;
  142. }
  143. // 累加已领取次数
  144. updatedCoupon.alreadyReceived += 1;
  145. // 通知父组件更新数据
  146. this.$emit('update-coupon', updatedCoupon);
  147. },
  148. // 显示优惠券规则弹窗
  149. showRulePopup() {
  150. // 触发父组件的显示规则事件
  151. this.$emit('show-rule', this.couponData);
  152. }
  153. }
  154. }
  155. </script>
  156. <style lang="scss" scoped>
  157. .card {
  158. display: flex;
  159. align-items: center;
  160. width: 100%;
  161. padding: 10px 0;
  162. background: #fff;
  163. border-radius: 8px 8px 0 0;
  164. }
  165. .card-bottom {
  166. display: flex;
  167. background-color: #FFF1E0;
  168. height: 50rpx;
  169. align-items: center;
  170. justify-content: space-between;
  171. padding: 0 20rpx 0 20rpx;
  172. border-radius: 0 0 8px 8px;
  173. .card-bottom-text {
  174. color: #AAAAAA;
  175. font-size: 24rpx;
  176. font-weight: 400;
  177. }
  178. }
  179. .card-left {
  180. width: 88px;
  181. text-align: center;
  182. color: #FF530A;
  183. font-size: 28rpx;
  184. font-weight: 900;
  185. }
  186. .card-center {
  187. display: flex;
  188. flex-direction: column;
  189. .card-center-top {
  190. width: 40rpx;
  191. height: 20rpx;
  192. border-radius: 0 0 20rpx 20rpx;
  193. background-color: #F5F5F7;
  194. line-height: 20rpx;
  195. margin-top: -22rpx;
  196. margin-bottom: 20rpx;
  197. margin-left: -19rpx;
  198. }
  199. .card-center-bottom {
  200. border-right: 1px dashed #AAAAAA;
  201. width: 1px;
  202. height: 120rpx;
  203. }
  204. }
  205. .card-right {
  206. padding: 0px 12px;
  207. display: flex;
  208. flex: 1;
  209. justify-content: space-between;
  210. align-items: center;
  211. height: 60px;
  212. .card-content {
  213. width: 77%;
  214. }
  215. .card-icon {
  216. position: relative;
  217. right: -10px;
  218. top: -10px;
  219. }
  220. }
  221. .card-info {
  222. margin: 0;
  223. font-size: 28rpx;
  224. line-height: 28rpx;
  225. color: #333333;
  226. font-weight: 500;
  227. }
  228. .card-type {
  229. font-size: 24rpx;
  230. font-weight: 400;
  231. line-height: 24rpx;
  232. font-weight: 400;
  233. color: #AAAAAA;
  234. margin-top: 10rpx;
  235. .card-type-text {
  236. color: #FFAA48;
  237. font-size: 24rpx;
  238. font-weight: 400;
  239. line-height: 24rpx;
  240. border: #FFAA48 1px solid;
  241. border-radius: 7rpx;
  242. margin-left: 8rpx;
  243. }
  244. }
  245. .card-time {
  246. font-size: 24rpx;
  247. font-weight: 400;
  248. line-height: 24rpx;
  249. font-weight: 400;
  250. color: #AAAAAA;
  251. margin-top: 10rpx;
  252. }
  253. // 优惠券按钮样式
  254. .coupon-btn {
  255. width: 132rpx;
  256. height: 52rpx;
  257. background-color: #FFAA48;
  258. display: flex;
  259. align-items: center;
  260. justify-content: center;
  261. border-radius: 56rpx;
  262. cursor: pointer;
  263. transition: all 0.3s ease;
  264. &.coupon-btn-disabled {
  265. background-color: #CCCCCC;
  266. cursor: not-allowed;
  267. opacity: 0.6;
  268. }
  269. }
  270. .coupon-btn-text {
  271. font-size: 24rpx;
  272. font-weight: 500;
  273. color: #FFFFFF;
  274. }
  275. </style>