grid.vue 2.4 KB

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