You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

186 lines
6.9 KiB

import { ref, computed, watch } from 'vue';
import { useLotteryStore } from '../../../store/lottery' // 路径根据实际情况调整
import { drawLottery } from '../../../api/API'; // 导入新的抽奖接口
function getRandomInt(max) {
return Math.floor(Math.random() * max);
}
const lotteryStore = useLotteryStore();
const winners = computed({
get: () => lotteryStore.winners,
set: (val) => lotteryStore.setWinners(val),
});
// 用watch监听winners的变化
// watch(winners, (newVal) => {
// console.log('winners', newVal);
// winners.value = newVal;
// });
export function useLotteryEngine(dataManager, renderer3D) {
const isLotting = ref(false);
const lotteryStore = useLotteryStore(); // 只获取一次
async function executeLottery() {
if (isLotting.value) return;
isLotting.value = true;
dataManager.setLotteryStatus(true);
await dataManager.saveData();
changePrize();
// 重置卡片动画
await renderer3D.resetCard([]);
// 计算本次应该抽奖的人数
const currentPrizeIndex = dataManager.state.currentPrizeIndex;
const prize = dataManager.state.basicData.prizes[currentPrizeIndex];
const luckyUsers = dataManager.state.basicData.luckyUsers[currentPrizeIndex] || [];
const remainingPrizeCount = prize.count - luckyUsers.length; // 奖品剩余数量
const basePerCount = dataManager.state.config.EACH_COUNT[currentPrizeIndex] || 1;
const actualPerCount = Math.min(basePerCount, remainingPrizeCount); // 取最小值
console.log('executeLottery - currentPrizeIndex:', currentPrizeIndex, 'prize:', prize, 'basePerCount:', basePerCount, 'remainingPrizeCount:', remainingPrizeCount, 'actualPerCount:', actualPerCount);
// 请求后端进行抽奖
try {
const lotteryData = {
gradeId: prize.gradeId,
prizeId: prize.prizeId,
perWin: basePerCount,
remainNum: prize.remainNum
};
console.log('请求后端抽奖,参数:', lotteryData);
const response = await drawLottery(lotteryData);
console.log('response', response);
winners.value = response.data.data;
console.log('抽奖的winners', winners.value);
// winners.value = response.data;
console.log('后端抽奖返回结果:', response);
if (response && response.data.data && Array.isArray(response.data.data)) {
// 后端返回中奖用户数据
const currentLuckys = response.data.data.map(item => ({
jwcode: item.jwcode,
username: item.username
}));
console.log('后端返回的中奖用户:', currentLuckys);
// 生成随机卡片索引用于显示
const totalCards = dataManager.getTotalCards();
let selectedCardIndex = [];
for (let i = 0; i < currentLuckys.length; i++) {
let cardIndex = getRandomInt(totalCards);
while (selectedCardIndex.includes(cardIndex)) {
cardIndex = getRandomInt(totalCards);
}
selectedCardIndex.push(cardIndex);
}
console.log('executeLottery - selectedCardIndex:', selectedCardIndex, 'currentLuckys:', currentLuckys);
dataManager.state.currentLuckys = currentLuckys;
// 保存中奖用户到对应奖品
if (!dataManager.state.basicData.luckyUsers[currentPrizeIndex]) {
dataManager.state.basicData.luckyUsers[currentPrizeIndex] = [];
}
dataManager.state.basicData.luckyUsers[currentPrizeIndex].push(...currentLuckys);
// 更新轮次信息
dataManager.updateCurrentRound();
// 展示中奖动画
console.log('executeLottery - calling selectCard');
await renderer3D.selectCard?.(selectedCardIndex, currentLuckys);
console.log('executeLottery - selectCard completed');
} else {
console.error('后端抽奖返回数据格式错误:', response);
throw new Error('抽奖失败:后端返回数据格式错误');
}
} catch (error) {
console.error('抽奖请求失败:', error);
// 如果后端请求失败,可以回退到前端随机抽奖逻辑
console.log('回退到前端随机抽奖逻辑');
const totalCards = dataManager.getTotalCards();
const leftUsers = dataManager.state.basicData.leftUsers;
let selectedCardIndex = [];
let currentLuckys = [];
let leftCount = leftUsers.length;
// 随机抽取中奖用户和卡片索引
for (let i = 0; i < actualPerCount && leftCount > 0; i++) {
const luckyId = getRandomInt(leftCount);
const selectedUser = leftUsers.splice(luckyId, 1)[0];
// 确保数据格式一致
currentLuckys.push({
jwcode: selectedUser.jwcode || selectedUser[0] || "",
username: selectedUser.username || selectedUser[1] || "",
company: selectedUser.company || selectedUser[2] || "PSST"
});
leftCount--;
let cardIndex = getRandomInt(totalCards);
while (selectedCardIndex.includes(cardIndex)) {
cardIndex = getRandomInt(totalCards);
}
selectedCardIndex.push(cardIndex);
}
console.log('executeLottery - selectedCardIndex:', selectedCardIndex, 'currentLuckys:', currentLuckys);
dataManager.state.currentLuckys = currentLuckys;
// 保存中奖用户到对应奖品
if (!dataManager.state.basicData.luckyUsers[currentPrizeIndex]) {
dataManager.state.basicData.luckyUsers[currentPrizeIndex] = [];
}
dataManager.state.basicData.luckyUsers[currentPrizeIndex].push(...currentLuckys);
// 更新轮次信息
dataManager.updateCurrentRound();
// 展示中奖动画
console.log('executeLottery - calling selectCard');
await renderer3D.selectCard?.(selectedCardIndex, currentLuckys);
console.log('executeLottery - selectCard completed');
}
// 抽奖完成后更新奖品列表数据
try {
await dataManager.updatePrizeList();
console.log('抽奖完成后奖品列表已更新');
} catch (error) {
console.error('更新奖品列表失败:', error);
}
dataManager.setLotteryStatus(false);
isLotting.value = false;
}
function performLottery() {
// 已合并到 executeLottery
}
function changePrize() {
// 可根据中奖情况切换奖品
dataManager.updateCurrentPrize();
}
async function resetLottery() {
if (isLotting.value) return;
dataManager.resetAllData();
renderer3D.addHighlight && renderer3D.addHighlight();
lotteryStore.setLotteryState('idle'); // 直接用
await renderer3D.resetCard([]);
await dataManager.resetData();
// 重置轮次
dataManager.state.currentRound = 1;
renderer3D.switchScreen && renderer3D.switchScreen('enter');
}
return {
isLotting,
executeLottery,
performLottery,
changePrize,
resetLottery
};
}