js实现手机号码抽奖
实现手机号码抽奖的JavaScript方案
数据准备与存储
手机号码通常需要预先存储在数组中或通过接口动态获取。示例数据格式:
const phoneNumbers = [
'13800138000',
'13912345678',
'15098765432',
'18600001111',
'19987654321'
];
随机抽取算法
使用Fisher-Yates洗牌算法实现公平随机:
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
抽奖核心功能
实现单次抽取和批量抽取功能:
function drawLottery(numbers, count = 1) {
const shuffled = shuffleArray([...numbers]);
return shuffled.slice(0, count);
}
号码脱敏处理
中奖结果显示时建议进行隐私保护处理:
function maskPhone(phone) {
return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1$2');
}
完整示例实现
const lotteryApp = {
participants: [],
init(numbers) {
this.participants = numbers;
},
draw(prizeCount) {
if (prizeCount > this.participants.length) {
throw new Error('奖项数量超过参与者人数');
}
const winners = drawLottery(this.participants, prizeCount);
return winners.map(phone => ({
original: phone,
display: maskPhone(phone)
}));
}
};
// 使用示例
lotteryApp.init(phoneNumbers);
const result = lotteryApp.draw(3);
console.log('中奖号码:', result);
界面交互增强
建议添加可视化抽奖效果:

function animateSelection(element, duration) {
let start = null;
const phones = [...phoneNumbers];
function step(timestamp) {
if (!start) start = timestamp;
const progress = timestamp - start;
const index = Math.floor(progress % phones.length);
element.textContent = maskPhone(phones[index]);
if (progress < duration) {
window.requestAnimationFrame(step);
}
}
window.requestAnimationFrame(step);
}
注意事项
- 大规模数据时应使用Web Worker避免界面卡顿
- 实际项目建议添加去重逻辑和验证机制
- 商业用途需遵守《反不正当竞争法》相关规定
- 敏感数据需进行HTTPS传输和服务器端校验
扩展功能
- 添加权重抽奖系统
- 实现多级奖项分配
- 增加中奖记录保存功能
- 开发实时大屏展示组件






