slotMachine.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!-- 老虎机 -->
  2. <template>
  3. <view class="box" v-if="prizes?.length">
  4. <SlotMachine
  5. ref="myLucky"
  6. width="300px"
  7. :height="props.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. <image
  19. v-if="!disabled"
  20. src="https://img1.baidu.com/it/u=3949019928,2138080900&fm=253&fmt=auto&app=138&f=PNG?w=300&h=300"
  21. style="margin: 0 auto; width: 180rpx; height: 180rpx;"
  22. @click="startCallback"
  23. />
  24. </view>
  25. </template>
  26. <script setup>
  27. import PrizeApi from '@/sheep/api/prizeDraw'
  28. import SlotMachine from '@lucky-canvas/uni/slot-machine'
  29. // import { onLoad } from '@dcloudio/uni-app'
  30. import { ref } from 'vue'
  31. const emit = defineEmits(['start', 'end'])
  32. const props = defineProps({
  33. height: {
  34. type: String,
  35. default: '300px'
  36. },
  37. disabled: Boolean,
  38. lotteryId: String,
  39. orderId: String,
  40. })
  41. const myLucky = ref()
  42. // 背景样式
  43. const blocks = [
  44. { padding: '10px', background: '#00B760', borderRadius: 6 },
  45. ]
  46. // 转速与方向
  47. const slots = [
  48. { speed: 15, direction: -1 },
  49. { speed: 15, direction: 1 },
  50. { speed: 15, direction: -1 },
  51. ]
  52. const prizes = ref([])
  53. const defaultConfig = {
  54. rowSpacing: '10px',
  55. colSpacing: '10px',
  56. accelerationTime: 1000, // 开始旋转时间
  57. decelerationTime: 5000, // 缓慢停止时间
  58. }
  59. // 根据活动id获取奖品列表
  60. const getPrizeData = async () => {
  61. const res = await PrizeApi.getPrizeByLotteryId(props.lotteryId)
  62. prizes.value = []
  63. const data = res?.data || []
  64. data.forEach(item => {
  65. prizes.value.push({
  66. prize: item,
  67. background: '#fe920200',
  68. borderRadius: '10px',
  69. imgs: [{
  70. width: '100%',
  71. top: '0%',
  72. src: item.image
  73. }]
  74. })
  75. })
  76. }
  77. getPrizeData()
  78. const startCallback = () => {
  79. // if (props.disabled) return sheep.$helper.toast('抽奖次数已用完')
  80. emit('start')
  81. // 调用抽奖组件的play方法开始游戏
  82. myLucky.value.play()
  83. // 模拟调用接口异步抽奖
  84. setTimeout(() => {
  85. // 中奖索引
  86. const index = 3
  87. // 调用stop停止旋转并传递中奖索引
  88. myLucky.value.stop(index)
  89. }, 1500)
  90. }
  91. const endCallback = (value) => {
  92. emit('end', value)
  93. }
  94. </script>
  95. <style lang="scss" scoped>
  96. .box {
  97. display: flex;
  98. flex-direction: column;
  99. align-items: center;
  100. }
  101. </style>