预览:http://icelily.xyz/lottery
完整项目:https://github.com/tinslychong/vue_lottery
部分实现与代码:
1、单次抽奖逻辑:
lotteryOne() { var pos = []; pos.push(0); var count = 0; for (var i = 0; i < this.tableData.length; i++) { count += parseFloat(this.tableData[i].value); pos.push(count); } var ran = Math.random() * 100; for (var i = 0; i < pos.length - 1; i++) { if (ran >= pos[i] && ran < pos[i + 1]) { break; } } return this.tableData[i]; } }, |
这里tabledata的数据结构为{name:xx,value:xx}
value为物品的概率(百分比)
2、载入和保存配置
载入:
onLoadConfig() { this.$reqs.get(this.selected_config+"?"+Math.random()*1000).then(resp => { _this.tableData = resp.data.data; }); }, |