slotMachine.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <!-- 老虎机 -->
  2. <template>
  3. <div v-if="prizes?.length">
  4. <SlotMachine
  5. ref="myLucky"
  6. width="300px"
  7. :height="height + 'px'"
  8. :prizes="prizes"
  9. :blocks="blocks"
  10. :slots="slots"
  11. accelerationTime="1000"
  12. decelerationTime="10000"
  13. :default-config="defaultConfig"
  14. class="prizeDraw"
  15. @start="startCallback"
  16. @end="endCallback"
  17. />
  18. <v-img
  19. v-if="!disabled"
  20. class="cursor-pointer"
  21. src="https://img1.baidu.com/it/u=3949019928,2138080900&fm=253&fmt=auto&app=138&f=PNG?w=300&h=300"
  22. :width="100"
  23. style="margin: 0 auto;"
  24. @click="startCallback"
  25. />
  26. </div>
  27. </template>
  28. <script setup>
  29. defineOptions({name: 'prizeDraw-LuckyGrid'})
  30. import { getPrizeByLotteryId } from '@/api/mall/prize'
  31. // import Snackbar from '@/plugins/snackbar'
  32. import { ref } from 'vue'
  33. const emit = defineEmits(['start', 'end'])
  34. const props = defineProps({ lotteryId: [Number, String], height: [Number, String], disabled: Boolean })
  35. const myLucky = ref()
  36. // 背景样式
  37. const blocks = [
  38. { padding: '10px', background: '#00B760', borderRadius: 6 },
  39. ]
  40. // 转速与方向
  41. const slots = [
  42. { speed: 15, direction: -1 },
  43. { speed: 15, direction: 1 },
  44. { speed: 15, direction: -1 },
  45. ]
  46. const prizes = ref([])
  47. const defaultConfig = {
  48. rowSpacing: '10px',
  49. colSpacing: '10px',
  50. accelerationTime: 1000, // 开始旋转时间
  51. decelerationTime: 3000, // 缓慢停止时间
  52. }
  53. // 根据活动id获取奖品列表
  54. const getPrizeData = async (lotteryId) => {
  55. let id = lotteryId || props.lotteryId
  56. const data = await getPrizeByLotteryId(id)
  57. prizes.value = []
  58. data.forEach(item => {
  59. prizes.value.push({
  60. prize: item,
  61. background: '#fe920200',
  62. borderRadius: '10px',
  63. imgs: [{
  64. width: '100%',
  65. top: '0%',
  66. src: item.image
  67. }]
  68. })
  69. })
  70. }
  71. if (props.lotteryId) getPrizeData()
  72. const startCallback = () => {
  73. // if (props.disabled) return Snackbar.warning('您已经参与过该抽奖活动了哦')
  74. emit('start')
  75. // 调用抽奖组件的play方法开始游戏
  76. myLucky.value.play()
  77. // 模拟调用接口异步抽奖
  78. setTimeout(() => {
  79. const index = 3
  80. // 调用stop停止旋转并传递中奖索引
  81. myLucky.value.stop(index)
  82. }, 1500)
  83. }
  84. const endCallback = (value) => {
  85. emit('end', value)
  86. }
  87. </script>
  88. <style lang="scss" scoped>
  89. </style>