grid.vue 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <!-- 九宫格 -->
  2. <template>
  3. <div>
  4. <LuckyGrid
  5. ref="myLucky"
  6. width="300px"
  7. height="300px"
  8. :prizes="prizes"
  9. :blocks="blocks"
  10. :buttons="buttons"
  11. :default-config="defaultConfig"
  12. class="prizeDraw"
  13. @start="startCallback"
  14. @end="endCallback"
  15. />
  16. </div>
  17. </template>
  18. <script setup>
  19. defineOptions({name: 'prizeDraw-LuckyGrid'})
  20. import { getPrizeByLotteryId } from '@/api/mall/prize'
  21. import Snackbar from '@/plugins/snackbar'
  22. import { ref } from 'vue'
  23. const emit = defineEmits(['start', 'end'])
  24. const props = defineProps({ lotteryId: [Number, String] })
  25. const myLucky = ref()
  26. // 背景样式
  27. const blocks = [
  28. { padding: '10px', background: '#00897B', borderRadius: 6 }
  29. ]
  30. const prizes = ref([
  31. { x: 0, y: 0, imgs: [] },
  32. { x: 1, y: 0, imgs: [] },
  33. { x: 2, y: 0, imgs: [] },
  34. { x: 2, y: 1, imgs: [] },
  35. { x: 2, y: 2, imgs: [] },
  36. { x: 1, y: 2, imgs: [] },
  37. { x: 0, y: 2, imgs: [] },
  38. { x: 0, y: 1, imgs: [] }
  39. ])
  40. const buttons = [
  41. {
  42. x: 1, y: 1,
  43. background: '#fe920200',
  44. borderRadius: '10px',
  45. imgs: [{
  46. src: 'https://img1.baidu.com/it/u=3949019928,2138080900&fm=253&fmt=auto&app=138&f=PNG?w=300&h=300',
  47. width: '100%',
  48. height: '100%'
  49. }]
  50. }
  51. ]
  52. const defaultConfig = {
  53. gutter: 10, // 格子之间的间隙
  54. speed: 10 // 旋转速度的峰值
  55. }
  56. // 根据活动id获取奖品列表
  57. const getPrizeData = async (lotteryId) => {
  58. let id = lotteryId || props.lotteryId
  59. const data = await getPrizeByLotteryId(id)
  60. data.forEach((item, index) => {
  61. prizes.value[index].prize = item
  62. prizes.value[index].imgs = [{
  63. src: item.image,
  64. activeSrc: 'https://bpic.588ku.com/element_origin_min_pic/19/11/15/a6f8bd3b208aca40e3de49209ab309ca.jpg',
  65. width: '100%',
  66. top: '0%'
  67. }]
  68. })
  69. }
  70. if (props.lotteryId) getPrizeData()
  71. const startCallback = () => {
  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>