Commit 033cee84 authored by rendong's avatar rendong

Update lottery.js

parent abe07e6a
......@@ -2,19 +2,17 @@
*
* @param {*} status true: 把中奖名单删除
*/
function getLotteryList(status = false) {
let notWinPersonList = [...personInfoMap.values()];
if (vue.num > notWinPersonList.length) {
vue.num = notWinPersonList.length;
function randomNPerson(num, candidateMap) {
let candidateList = [...candidateMap.values()];
if (num > candidateList.length) {
num = candidateList.length;
}
// 该批次名单
let currentWinPersonMap = new Map();
while (currentWinPersonMap.size < vue.num) {
let random = Math.floor(Math.random() * notWinPersonList.length);
currentWinPersonMap.set(notWinPersonList[random]['name'], notWinPersonList[random]);
if (status) {
notWinPersonList.splice(random, 1); // 删除中奖人员名单
}
while (currentWinPersonMap.size < num) {
let random = Math.floor(Math.random() * candidateList.length);
currentWinPersonMap.set(candidateList[random]['name'], candidateList[random]);
candidateList.splice(random, 1); // 删除中奖人员名单
}
return [...currentWinPersonMap.values()];
}
......@@ -22,7 +20,7 @@ function getLotteryList(status = false) {
/**
* 获取所有未中过奖名单
*/
function getNotWinPersonList() {
function getCandidateMap() {
let winPerson = localStorage.getItem('yit');
if (winPerson) {
winPerson = JSON.parse(winPerson);
......@@ -38,18 +36,21 @@ function getNotWinPersonList() {
/**
* 抽奖开始
*/
function start() {
getNotWinPersonList();
if (personInfoMap.size === 0) {
function start(num) {
var candidateMap = getCandidateMap();
if (candidateMap.size === 0) {
let status = window.confirm('没有未中奖人员, 去往抽奖结果页面');
status && window.open('./list.html');
if (status) {
window.open('./list.html');
} else {
vue.activePersonList = [];
vue.step = 1;
vue.statusText = '抽奖完成';
}
return;
}
vue.interval = setInterval(() => {
vue.activePersonList = getLotteryList();
vue.activePersonList = randomNPerson(num, candidateMap);
}, 100);
}
......@@ -107,7 +108,7 @@ let vue = new Vue({
watch: {
step(newValue) {
if (newValue === 2) {
start();
start(this.num);
}
},
},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment