grid.vue 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!-- -->
  2. <template>
  3. <LuckyGrid
  4. ref="myLucky"
  5. width="300px"
  6. height="300px"
  7. :prizes="prizes"
  8. :blocks="blocks"
  9. :buttons="buttons"
  10. :default-config="defaultConfig"
  11. :disabled="true"
  12. class="prizeDraw"
  13. @start="startCallback"
  14. @end="endCallback"
  15. />
  16. </template>
  17. <script setup>
  18. defineOptions({name: 'prizeDraw-LuckyGrid'})
  19. import { getPrizeByLotteryId } from '@/api/mall/prize'
  20. import Snackbar from '@/plugins/snackbar'
  21. import { ref } from 'vue'
  22. const emit = defineEmits(['start', 'end'])
  23. const props = defineProps({ lotteryId: [Number, String], disabled: [Number, Boolean] })
  24. const myLucky = ref()
  25. // 背景样式
  26. const blocks = [
  27. { padding: '10px', background: '#00897B', borderRadius: 6 }
  28. ]
  29. const prizes = ref([
  30. { x: 0, y: 0, imgs: [] },
  31. { x: 1, y: 0, imgs: [] },
  32. { x: 2, y: 0, imgs: [] },
  33. { x: 2, y: 1, imgs: [] },
  34. { x: 2, y: 2, imgs: [] },
  35. { x: 1, y: 2, imgs: [] },
  36. { x: 0, y: 2, imgs: [] },
  37. { x: 0, y: 1, imgs: [] }
  38. ])
  39. const buttons = [
  40. {
  41. x: 1, y: 1,
  42. background: '#fe920200',
  43. borderRadius: '10px',
  44. imgs: [{
  45. src: 'https://img1.baidu.com/it/u=3949019928,2138080900&fm=253&fmt=auto&app=138&f=PNG?w=300&h=300',
  46. width: '100%',
  47. height: '100%'
  48. }]
  49. }
  50. ]
  51. const defaultConfig = {
  52. gutter: 10, // 格子之间的间隙
  53. speed: 10 // 旋转速度的峰值
  54. }
  55. // 根据活动id获取奖品列表
  56. const getPrizeData = async (lotteryId) => {
  57. let id = lotteryId || props.lotteryId
  58. const data = await getPrizeByLotteryId(id)
  59. data.forEach((item, index) => {
  60. prizes.value[index].prize = item
  61. prizes.value[index].imgs = [{
  62. src: item.image,
  63. activeSrc: 'https://bpic.588ku.com/element_origin_min_pic/19/11/15/a6f8bd3b208aca40e3de49209ab309ca.jpg',
  64. width: '100%',
  65. top: '0%'
  66. }]
  67. })
  68. }
  69. if (props.lotteryId) getPrizeData()
  70. const startCallback = () => {
  71. if (props.disabled) return Snackbar.warning('抽奖次数已用完!')
  72. emit('start')
  73. // 调用抽奖组件的play方法开始游戏
  74. myLucky.value.play()
  75. // 模拟调用接口异步抽奖
  76. setTimeout(() => {
  77. // 假设后端返回的中奖索引是0
  78. const index = 3
  79. // 调用stop停止旋转并传递中奖索引
  80. myLucky.value.stop(index)
  81. }, 1500)
  82. }
  83. const endCallback = (value) => {
  84. emit('end', value)
  85. }
  86. </script>
  87. <style lang="scss" scoped>
  88. </style>