coupons-selector.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <script setup lang="ts">
  2. import { getPointsCoupons, getProductCoupons } from '../../../core/libs/requests'
  3. import { Coupon } from '../../../core/libs/models'
  4. import { useUserStore } from '../../../store'
  5. import { storeToRefs } from 'pinia'
  6. import CouponCard from './coupon-card.vue'
  7. import { useMessage } from 'wot-design-uni'
  8. const props = withDefaults(
  9. defineProps<{
  10. type: 'points' | 'product'
  11. products?: any[]
  12. businessId?: number
  13. show?: boolean
  14. }>(),
  15. { show: false },
  16. )
  17. const modelValue = defineModel({ default: () => [], type: Array as PropType<Coupon[]> })
  18. const emits = defineEmits<{ close: []; clickInstruction: [coupon: Coupon] }>()
  19. const userStore = useUserStore()
  20. const { userInfo } = storeToRefs(userStore)
  21. const { alert } = useMessage()
  22. // const show = ref(false)
  23. const tab = ref(0)
  24. const query = ref<{ userId: number; available?: boolean }>({
  25. userId: userInfo.value.userId,
  26. available: true,
  27. })
  28. const request = computed(() =>
  29. props.type === 'points'
  30. ? () =>
  31. getPointsCoupons({
  32. userId: userInfo.value.userId,
  33. // productIds: props.products.map((it) => it.productId).join(','),
  34. businessId: props.businessId,
  35. isUse: 0,
  36. ...query.value,
  37. })
  38. : () =>
  39. getProductCoupons({
  40. userId: userInfo.value.userId,
  41. productIds: props.products.map((it) => it.productId).join(','),
  42. isUse: 0,
  43. ...query.value,
  44. }),
  45. )
  46. const { data: coupons, run: setCoupons } = useRequest(() => request.value(), { initialData: [] })
  47. const handleSelect = (coupon: Coupon) => {
  48. // if (modelValue.value.map(({ id }) => id).includes(coupon.id)) {
  49. // modelValue.value = modelValue.value.filter(({ id }) => id !== coupon.id)
  50. // } else {
  51. // modelValue.value = [...modelValue.value, coupon]
  52. // }
  53. modelValue.value = [coupon]
  54. emits('close')
  55. }
  56. const handleTabsChange = async ({ index, name }) => {
  57. console.log(index)
  58. query.value = { ...query.value, available: index === 0 }
  59. await setCoupons()
  60. }
  61. watch(
  62. () => props.show,
  63. async (val) => {
  64. console.log(val)
  65. if (val) {
  66. console.log(111111)
  67. await setCoupons()
  68. }
  69. },
  70. )
  71. onMounted(async () => {})
  72. </script>
  73. <template>
  74. <wd-action-sheet title="优惠券" :modelValue="show" @close="emits('close')">
  75. <view class="">
  76. <wd-tabs v-model="tab" @change="handleTabsChange">
  77. <wd-tab title="可用优惠券"></wd-tab>
  78. <wd-tab title="不可用优惠券"></wd-tab>
  79. </wd-tabs>
  80. <div class="h-100 overflow-y-scroll">
  81. <div class="bg-[#f6f6f6] py-3.5 flex flex-col gap-4 min-h-full">
  82. <template v-for="(it, i) in coupons" :key="i">
  83. <CouponCard
  84. :options="{ couponType: 2, ...it }"
  85. :canSelect="tab === 0"
  86. :selected="modelValue?.map(({ id }) => id).includes(it.id)"
  87. @select="handleSelect"
  88. @click-instruction="(e) => emits('clickInstruction', e)"
  89. ></CouponCard>
  90. </template>
  91. </div>
  92. </div>
  93. <!-- <wd-button block :round="false">确认</wd-button> -->
  94. </view>
  95. </wd-action-sheet>
  96. </template>