slotMachine.vue 2.2 KB

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