feat: 九宫格基础结构

This commit is contained in:
2025-08-13 09:20:29 +08:00
parent 3c200051e6
commit c507d7397f
5 changed files with 501 additions and 475 deletions
@@ -0,0 +1,276 @@
<template>
<!-- 九宫格容器 -->
<view class="lottery-grid">
<!-- 高亮选中框 -->
<view class="highlight-box" :style="{
top: highlightTop,
left: highlightLeft,
transition: isRotating ? 'all 0.07s linear' : 'none'
}"></view>
<!-- 9个格子 -->
<view class="grid-item" v-for="(item, index) in 9" :key="index">
<!-- 中间格子作为抽奖按钮 (索引4) -->
<template v-if="index === 4">
<view class="lottery-btn" @click="handleStartClick">
<text>{{ isRotating ? '抽奖中...' : '开始抽奖' }}</text>
</view>
</template>
<!-- 其他8个奖品格子 -->
<template v-else>
<image :src="prizes[index].img" class="prize-img" mode="widthFix"></image>
<text class="prize-name">{{ prizes[index].name }}</text>
</template>
</view>
</view>
</template>
<script>
export default {
props: {
// 奖品数据数组
prizes: {
type: Array,
required: true,
default: () => []
},
// 是否禁用抽奖
isDisabled: {
type: Boolean,
default: false
}
},
data() {
return {
isRotating: false, // 是否正在旋转
highlightTop: '0rpx', // 高亮框顶部位置
highlightLeft: '0rpx', // 高亮框左侧位置
// 抽奖配置常量
LOTTERY_PATH: [0, 1, 2, 5, 8, 7, 6, 3], // 抽奖路径顺序(顺时针)
INITIAL_SPEED: 100, // 初始速度(ms) - 调慢以便观察
MAX_STEPS: 50, // 最大步数,防止无限循环
ACCELERATE_STEPS: 5, // 加速步数
SPEED_DECREMENT: 10, // 加速时的速度减少量
SPEED_INCREMENT: 15, // 减速时的速度增加量
MIN_SPEED: 50, // 最小速度
STOP_SPEED: 250, // 停止时的速度阈值
animationTimer: null // 动画计时器
}
},
methods: {
/**
* 移动高亮框到指定索引位置
* @param {number} index - 格子索引
*/
moveHighlight(index) {
const row = Math.floor(index / 3) // 计算行
const col = index % 3 // 计算列
this.highlightTop = `${row * 200}rpx`
this.highlightLeft = `${col * 200}rpx`
},
/**
* 执行抽奖动画
* @param {Object} options - 动画参数
*/
runAnimation(options) {
const { currentStep, speed, targetStep, cycleCount, totalSteps } = options
// 清除之前的计时器
if (this.animationTimer) {
clearTimeout(this.animationTimer)
}
// 计算下一步
const nextStep = (currentStep + 1) % this.LOTTERY_PATH.length
const nextTotalSteps = totalSteps + 1
let nextCycleCount = cycleCount
let nextSpeed = speed
// 计算循环次数
if (nextStep === 0) {
nextCycleCount++
}
// 速度控制逻辑
if (nextCycleCount < 1 && nextTotalSteps < this.ACCELERATE_STEPS) {
// 加速阶段
nextSpeed = Math.max(this.MIN_SPEED, nextSpeed - this.SPEED_DECREMENT)
} else {
// 减速阶段
nextSpeed += this.SPEED_INCREMENT
}
// 移动高亮框到当前步骤位置
this.moveHighlight(this.LOTTERY_PATH[nextStep])
// 检查是否应该停止
const shouldStop =
(nextSpeed > this.STOP_SPEED && nextStep === targetStep) || nextTotalSteps > this.MAX_STEPS
if (shouldStop) {
// 动画结束
this.isRotating = false
this.animationTimer = null
// 触发抽奖完成事件,返回中奖奖品
this.$emit('complete', this.prizes[this.LOTTERY_PATH[nextStep]])
return
}
// 继续下一帧动画
this.animationTimer = setTimeout(() => {
this.runAnimation({
currentStep: nextStep,
speed: nextSpeed,
targetStep,
cycleCount: nextCycleCount,
totalSteps: nextTotalSteps
})
}, nextSpeed)
},
/**
* 随机生成目标位置(带权重)
* @returns {number} 目标位置索引
*/
getRandomTarget() {
// 奖品权重配置,数值越高概率越大
const weights = [2, 2, 1, 0.5, 2, 1, 3, 2]
const total = weights.reduce((sum, w) => sum + w, 0) // 计算总权重
let random = Math.random() * total // 生成随机数
// 根据权重计算目标位置
for (let i = 0; i < weights.length; i++) {
random -= weights[i]
if (random <= 0) return i
}
// 兜底:随机返回一个位置
return Math.floor(Math.random() * this.LOTTERY_PATH.length)
},
/**
* 处理开始抽奖点击事件
*/
handleStartClick() {
// 如果正在抽奖或被禁用,则不执行
if (this.isDisabled || this.isRotating) {
console.log('抽奖被禁用或正在进行中')
return
}
// 通知父组件开始抽奖
this.$emit('start')
// 开始抽奖动画(无论父组件做了什么处理)
this.startLotteryAnimation()
},
/**
* 开始抽奖动画
*/
startLotteryAnimation() {
// 重置动画参数
clearTimeout(this.animationTimer)
// 标记为正在抽奖
this.isRotating = true
// 开始动画
this.runAnimation({
currentStep: 0,
speed: this.INITIAL_SPEED,
targetStep: this.getRandomTarget(),
cycleCount: 0,
totalSteps: 0
})
}
},
beforeDestroy() {
// 组件销毁时清理计时器
if (this.animationTimer) {
clearTimeout(this.animationTimer)
this.animationTimer = null
}
}
}
</script>
<style scoped>
.lottery-grid {
width: 600rpx;
height: 600rpx;
display: flex;
flex-wrap: wrap;
position: relative;
border: 8rpx solid #ffc107;
border-radius: 16rpx;
background-color: #fff;
box-sizing: content-box;
margin: 0 auto;
}
.grid-item {
width: 200rpx;
height: 200rpx;
box-sizing: border-box;
border: 2rpx solid #ffeeba;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
position: relative;
flex-shrink: 0;
}
.lottery-btn {
width: 180rpx;
height: 180rpx;
background: linear-gradient(135deg, #ff5252 0%, #e63946 100%);
border-radius: 50%;
display: flex;
justify-content: center;
align-items: center;
box-shadow: 0 6rpx 12rpx rgba(230, 57, 70, 0.3);
/* 增加点击反馈 */
transition: transform 0.2s;
}
.lottery-btn:active {
transform: scale(0.95);
}
.lottery-btn text {
color: #fff;
font-size: 32rpx;
font-weight: bold;
text-align: center;
padding: 0 10rpx;
}
.prize-img {
width: 100rpx;
height: 100rpx;
margin-bottom: 10rpx;
}
.prize-name {
font-size: 26rpx;
color: #333;
text-align: center;
padding: 0 10rpx;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.highlight-box {
position: absolute;
width: 200rpx;
height: 200rpx;
box-sizing: border-box;
border: 6rpx solid #ff4081;
background-color: rgba(255, 64, 129, 0.1);
z-index: 1;
pointer-events: none;
/* 增加高亮动画效果 */
box-shadow: 0 0 15rpx 2rpx rgba(255, 64, 129, 0.5);
}
</style>