slotMachine.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!-- 老虎机 -->
  2. <template>
  3. <view class="mb-n5" v-if="prizes?.length">
  4. <SlotMachine
  5. ref="myLucky"
  6. width="300px"
  7. height="300px"
  8. :prizes="prizes"
  9. :blocks="blocks"
  10. :slots="slots"
  11. accelerationTime="1000"
  12. decelerationTime="10000"
  13. :default-config="defaultConfig"
  14. :disabled="true"
  15. class="prizeDraw"
  16. @start="startCallback"
  17. @end="endCallback"
  18. />
  19. <image
  20. src="https://img1.baidu.com/it/u=3949019928,2138080900&fm=253&fmt=auto&app=138&f=PNG?w=300&h=300"
  21. :width="100"
  22. style="margin: 0 auto;"
  23. @click="startCallback"
  24. />
  25. </view>
  26. </template>
  27. <script setup>
  28. import PrizeApi from '@/sheep/api/prizeDraw'
  29. import SlotMachine from '@lucky-canvas/uni/slot-machine'
  30. import { ref } from 'vue'
  31. const emit = defineEmits(['start', 'end'])
  32. const props = defineProps({ lotteryId: [Number, String], disabled: [Number, Boolean] })
  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. debugger
  52. // 根据活动id获取奖品列表
  53. const getPrizeData = async (lotteryId) => {
  54. let id = lotteryId || props.lotteryId
  55. const data = await PrizeApi.getPrizeByLotteryId(id)
  56. prizes.value = []
  57. debugger
  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 sheep.$helper.toast('抽奖次数已用完')
  74. emit('start')
  75. // 调用抽奖组件的play方法开始游戏
  76. myLucky.value.play()
  77. // 模拟调用接口异步抽奖
  78. setTimeout(() => {
  79. // 假设后端返回的中奖索引是0
  80. const index = 3
  81. // 调用stop停止旋转并传递中奖索引
  82. myLucky.value.stop(index)
  83. }, 1500)
  84. }
  85. const endCallback = (value) => {
  86. emit('end', value)
  87. }
  88. </script>
  89. <style lang="scss" scoped>
  90. </style>