Commit 033cee84 authored by rendong's avatar rendong

Update lottery.js

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