-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
155 lines (150 loc) · 5.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
const vm = new Vue({
el: '#app',
data() {
return {
// 原始信息
original: [
{ id: 1, name: '徐潇航', avatar: './avatars/xxh.jpg' },
{ id: 2, name: '郝世杰', avatar: './avatars/hsj.jpg' },
{ id: 3, name: '王家政', avatar: './avatars/wjz.jpg' },
{ id: 4, name: '李佞偈', avatar: './avatars/lnj.jpg' },
{ id: 5, name: '王绍勇', avatar: './avatars/wsy.jpg' },
{ id: 6, name: '王宗辉', avatar: './avatars/wzh.jpg' },
{ id: 7, name: '顾欣怡', avatar: './avatars/gxy.jpg' },
{ id: 8, name: '陆涵之', avatar: './avatars/lhz.jpg' },
{ id: 9, name: '陈佳龙', avatar: './avatars/cjl.jpg' },
{ id: 10, name: '李想', avatar: './avatars/lx.jpg' },
{ id: 11, name: '高睿昊', avatar: './avatars/grh.jpg' },
{ id: 12, name: '徐易', avatar: './avatars/xy.jpg' },
{ id: 13, name: '宋毅恒', avatar: './avatars/syh.jpg' },
{ id: 14, name: '万思远', avatar: './avatars/wsy2.jpg' },
{ id: 15, name: '向欣雅', avatar: './avatars/xxy.jpg' },
].map(k => Object.assign(k, {
total: 0,
normalTotal: 0,
specialTotal: 0,
})),
// 投票结果
candidateRes: null,
candidates: null,
pendingCandidates: [],
duration: 15000,
shown: false,
animating: false
}
},
async created() {
// 一个深拷贝
this.candidateRes = this.deepClone(this.original)
// 获取投票结果
let res = await axios.get('https://xgbxscwx.seu.edu.cn/api/yxl/vote-result')
.then(
ret =>{
return ret.data.success ? ret.data.result : []
}
)
res.forEach((item) => {
let candidateID = + Object.keys(item)[0]
this.candidateRes[candidateID - 1].total = +item[candidateID]
this.candidateRes[candidateID - 1].normalTotal = +item[candidateID]
})
console.log('load')
this.candidates = this.original
// 监听键盘输入
document.addEventListener('keydown', e => {
// 展示投票结果
if(e.key === 'Enter'){
if(!this.shown && !this.animating){
console.log('show')
this.load({ candidate: this.candidateRes })
this.show()
}
}
// 清空投票结果
if(e.key === 'c'){
location.reload()
}
})
// 拖入文件加载票数
// document.body.ondragleave = e => e.preventDefault()
// document.body.ondrop = async e => {
// e.stopPropagation()
// e.preventDefault()
// let file = e.dataTransfer.files[0]
// let reader = new FileReader()
// let skeleton = JSON.parse(JSON.stringify(this.original))
// reader.onload = e => {
// let data = e.target.result
// let workbook = XLSX.read(data, { type: 'binary' })
// let csv = XLSX.utils.sheet_to_csv(workbook.Sheets.Sheet1)
// csv = csv.split('\n').filter(k => /^.+(,.+){4}$/.test(k)).slice(1).map(k => {
// let [ id, name, total, specialTotal, normalTotal ] = k.split(',')
// id = parseInt(id)
// total = parseFloat(total)
// normalTotal = parseFloat(normalTotal)
// specialTotal = parseFloat(specialTotal)
// return { id, name, total, specialTotal, normalTotal }
// }).sort((a, b) => a.id - b.id).slice(0, 20).map((k, i) => Object.assign(skeleton[i], k))
// this.load({ candidate: csv })
// }
// reader.readAsBinaryString(file)
// }
// document.body.ondragenter = e => e.preventDefault()
// document.body.ondragover = e => e.preventDefault()
},
computed: {
maxTotal() {
// 时时计算最大投票数
return this.candidates.map(k => Number(k.total)).reduce((a, b) => a > b ? a : b, 1)
}
},
methods: {
async load(data) {
// console.log('load')
let { candidate } = data
// 正经复制一个
this.pendingCandidates = this.deepClone(candidate)
},
show() {
if (this.shown || this.animating) {
return
}
// console.log('show')
this.animating = true
this.candidates = this.pendingCandidates
setTimeout(() => {
this.pendingCandidates = null
this.candidates.sort((a, b) => b.total - a.total)
this.shown = true
this.animating = false
setTimeout(() => {
this.lighten()
}, 1000)
}, this.duration)
},
// 工具函数
deepClone(obj){
let objClone = Array.isArray(obj)?[]:{};
if(obj && typeof obj==="object"){
for(key in obj){
if(obj.hasOwnProperty(key)){
if(obj[key]&&typeof obj[key] ==="object"){
objClone[key] = this.deepClone(obj[key]);
}else{
objClone[key] = obj[key];
}
}
}
}
return objClone;
},
// 高调显示前十名
lighten(){
let candidatelist = document.getElementsByClassName('candidate')
// 高调现实
for(let i = 0; i < 10; i++){
candidatelist[i].style.background = 'linear-gradient(180deg, rgba(255, 175, 115, 0.56) 0%, rgba(255, 175, 115, 0) 100%)'
}
}
}
})