Page({ data: { names: [] as string[], newName: '', result: '', isRolling: false }, onLoad() { // Initialize with some default placeholder data optionally, or keep empty // this.setData({ names: ['张三', '李四', '王五'] }); }, onInput(e: any) { this.setData({ newName: e.detail.value }); }, addName() { const name = this.data.newName.trim(); if (!name) { wx.showToast({ title: '请输入名字', icon: 'none' }); return; } // Check duplication if (this.data.names.includes(name)) { wx.showToast({ title: '名字已存在', icon: 'none' }); return; } const names = [...this.data.names, name]; this.setData({ names, newName: '' }); }, removeName(e: any) { if (this.data.isRolling) return; const index = e.currentTarget.dataset.index; const names = [...this.data.names]; names.splice(index, 1); this.setData({ names }); // If deleted the current result, clear result if (this.data.names.indexOf(this.data.result) === -1) { // actually result is a string copy, but if logic requires reset: // this.setData({ result: '' }); } }, startDraw() { if (this.data.isRolling) return; const names = this.data.names; if (names.length < 2) { wx.showToast({ title: '至少需要两个人才能抽取哦', icon: 'none' }); return; } this.setData({ isRolling: true, result: '' }); let count = 0; // Speed up first then slow down? Or simple uniform interval. // Let's do a simple one first. let baseInterval = 50; let totalRolls = 30; const roll = () => { const randomIndex = Math.floor(Math.random() * names.length); this.setData({ result: names[randomIndex] }); count++; if (count < totalRolls) { // dynamic interval could be fun, but keeping it simple for now setTimeout(roll, baseInterval + (count * 5)); // slowing down } else { this.setData({ isRolling: false }); wx.vibrateShort({ type: 'heavy' }); } }; roll(); } });