-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvote.html
257 lines (241 loc) · 11.1 KB
/
vote.html
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="viewport" content="width=device-width,initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>进行投票</title>
<link rel="shortcut icon" href="http://7xt5qt.com1.z0.glb.clouddn.com/favicon.ico">
<!-- <link href="./lib/voteStyle.css" rel="stylesheet"> -->
<link href="http://7xt5qt.com1.z0.glb.clouddn.com/voteStyle.css" rel="stylesheet">
<link href="https://cdn.bootcss.com/bootstrap/2.3.2/css/bootstrap.min.css" rel="stylesheet">
<style>
body {
background: #f8f8f8;
}
.name {
margin-top: 40px;
}
.tip {
margin-top: 10px;
}
.vote-percent {
margin-left: 20px;
cursor: default;
}
</style>
</head>
<body>
<div class="nav-top-wrap">
<ul class="nav-container f-cb">
<li class="f-fl logos">
<a href="./index.html">
<img class="logo-nebula" src="http://7xt5qt.com1.z0.glb.clouddn.com/vote-logo.png" alt="" />
</a>
</li>
<li class="f-fl item"><a href="./index.html">首页</a></li>
<li class="f-fl item"><a href="./help.html">使用说明</a></li>
<li class="f-fl item active"><a href="./vote.html">进行投票</a></li>
<li class="f-fr item"><a href="./myvote.html">我的投票</a></li>
</ul>
</div>
<div class="container">
<div class="j-votecontent hide">
<form>
<fieldset>
<legend class="j-description"></legend>
<div class="j-list"></div>
</fieldset>
</form>
<a href="#" class="btn btn-large btn-primary" id="submit">提交</a>
</div>
<p class="tip text-warning hide">你已投票</p>
</div>
<div class="noExtension hide" id="noExtension">
<div class="tip-content">
<h2>提示</h2>
<p class="detail">为保证APP正常使用,请安装 <a target="_blank" href="https://github.com/ChengOrangeJu/WebExtensionWallet">WebExtensionWallet</a></p>
</div>
</div>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/bootstrap/2.3.2/js/bootstrap.min.js"></script>
<script src="http://7xt5qt.com1.z0.glb.clouddn.com/nebPay.js"></script>
<!-- <script src="http://7xt5qt.com1.z0.glb.clouddn.com/nebulas.js"></script> -->
<!-- <script src=lib/voteUtil.js></script> -->
<script src="http://7xt5qt.com1.z0.glb.clouddn.com/voteUtil.js"></script>
<script>
var NebPay = require("nebpay"); //https://github.com/nebulasio/nebPay
var nebPay = new NebPay();
var choiceVoteCountArr = [];
var userVotedChoice = -1;
var voteId;
var urlParam = util.parseUrlParam();
if (urlParam.id) {
voteId = urlParam.id;
} else {
alert('投票不合要求');
}
setTimeout(function() {
if(typeof(webExtensionWallet) === "undefined"){
$("#noExtension").removeClass("hide")
}
}, 1000);
getVoteDetail();
function getVoteDetail() {
util.showLoading();
var to = util.dappAddress;
var value = "0";
var callFunction = "getQuestion";
var callArgs = "[\"" + voteId + "\"]";
nebPay.simulateCall(to, value, callFunction, callArgs, { //使用nebpay的call接口去调用合约,
listener: function(resp) {
$('.j-votecontent').removeClass('hide');
util.hideLoading();
var result = resp.result
console.log("return of vote detail: " + JSON.stringify(result))
if (result === 'null'){
var _emptyHtml = '<p class="empty text-warning">还没发起过投票</p>';
$('.j-mycreatelist').html(_emptyHtml)
} else {
try {
result = JSON.parse(result);
if (!result) {
var _emptyHtml = '<p class="empty text-warning">还没发起过投票</p>';
$('.j-mycreatelist').html(_emptyHtml)
} else {
var description = result.description;
var answers = result.answers.split(util.answersSplitFlag);
var _htmlStr = '';
var htmlStr = '';
for (var i=0; i<answers.length; i++) {
var _voteCountStr = '';
if (choiceVoteCountArr[i]) {
_voteCountStr = choiceVoteCountArr[i] + '票';
}
htmlStr += ('<label class="radio"><input type="radio" name="optionsRadios" class="radio-input" value="' + i +
'">' + answers[i] + '<span class="j-votepercent vote-percent text-info">' + _voteCountStr + '</span></label>');
}
$(".j-list").html(htmlStr);
$(".j-description").html(description);
if (userVotedChoice >= 0) {
for (var i=0; i<$('.radio-input').length; i++) {
if ($('.radio-input').eq(i).val() == userVotedChoice) {
$('.radio-input').eq(i).attr('checked', true);
$(".radio-input").attr("disabled",true);
$("#submit").addClass("disabled");
$(".tip").removeClass("hide");
break;
}
}
}
}
} catch (err) {
// alert("已经提交过")
}
}
}
});
}
getQuestionVoteRecord();
function getQuestionVoteRecord() {
var to = util.dappAddress;
var value = "0";
var callFunction = "getQuestionVoteInfo";
var callArgs = "[\"" + voteId + "\"]";
nebPay.simulateCall(to, value, callFunction, callArgs, { //使用nebpay的call接口去调用合约,
listener: function (resp) {
console.log(resp);
var result = resp.result
console.log("return of question vote record: " + JSON.stringify(result))
if (result === 'null'){
$('.j-votepercent').hide();
} else {
try {
result = JSON.parse(result);
choiceVoteCountArr = result.split(util.answersSplitFlag);
for (var i=0; i<choiceVoteCountArr.length; i++) {
$(".j-votepercent").eq(i).html(choiceVoteCountArr[i]+'票');
}
} catch (err) {
console.log(err);
}
}
}
});
}
getUserQuestionVoteRecord();
function getUserQuestionVoteRecord() {
var to = util.dappAddress;
var value = "0";
var callFunction = "getUserQuestionVoteInfo";
var callArgs = "[\"" + voteId + "\"]";
nebPay.simulateCall(to, value, callFunction, callArgs, { //使用nebpay的call接口去调用合约,
listener: function (resp) {
var result = resp.result
console.log("return of user question vote record: " + JSON.stringify(result))
if (result === 'null'){
} else {
try {
result = JSON.parse(result);
userVotedChoice = result.choice;
for (var i=0; i<$('.radio-input').length; i++) {
if ($('.radio-input').eq(i).val() == userVotedChoice) {
$('.radio-input').eq(i).attr('checked', true);
$(".radio-input").attr("disabled", true);
$("#submit").addClass("disabled");
$(".tip").removeClass("hide");
break;
}
}
} catch (err) {
console.log(err);
}
}
}
});
}
// 搜索功能: 查找Super-Dictionary 中有没有该词条
$("#submit").click(function(){
if ($("#submit").hasClass('disabled')) {
if (!$(".tip").hasClass("hide")) {
alert("你已经投票")
}
return;
}
var val = $('input[name="optionsRadios"]:checked').val();
if (val === undefined) {
alert('请选择');
return;
}
var to = util.dappAddress;
var value = "0";
var callFunction = "vote";
var callArgs = "[\"" + voteId + "\", \"" + val + "\"]";
// nebPay.simulateCall(to, value, callFunction, callArgs, { //使用nebpay的simulateCall接口去执行get查询, 模拟执行.不发送交易,不上链
// listener: cbSearch //指定回调函数
// });
nebPay.call(to, value, callFunction, callArgs, { //使用nebpay的simulateCall接口去执行get查询, 模拟执行.不发送交易,不上链
listener: function() { //指定回调函数
util.showLoading({tip: '区块同步需要15秒左右,耐心等待'});
setTimeout(function(){
location.reload();
}, 20000)
} //指定回调函数
});
})
//return of search,
function cbVote(resp) {
var result = resp.result
console.log("return of rpc call: " + JSON.stringify(result))
if (result === 'null'){
} else {
try {
result = JSON.parse(result);
} catch (err) {
// alert("已经提交过")
}
}
}
</script>
</body>
</html>