Last active
November 18, 2019 16:48
-
-
Save juntao/25c0aa2a46459f40b9e7e9bc290409ea to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add the following to "dapp -- Resources" | |
* JavaScript: https://code.jquery.com/jquery-3.4.1.min.js | |
* CSS: https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css | |
The "dapp -- CSS" should be empty |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >= 0.4.0; | |
contract Certificates { | |
address owner; | |
string public greetingNew; | |
string public greetingApplied; | |
string public greetingApproved; | |
struct Comment { | |
string name; | |
string email; | |
string comment; | |
uint approved; | |
} | |
mapping (address => Comment) comments; | |
address [] addrs; | |
constructor (string _greetingNew, string _greetingApplied, string _greetingApproved) public { | |
owner = msg.sender; | |
greetingNew = _greetingNew; | |
greetingApplied = _greetingApplied; | |
greetingApproved = _greetingApproved; | |
} | |
function setGreetings (string _greetingNew, string _greetingApplied, string _greetingApproved) public { | |
require (msg.sender == owner); | |
greetingNew = _greetingNew; | |
greetingApplied = _greetingApplied; | |
greetingApproved = _greetingApproved; | |
} | |
function getGreetings () public constant returns (string, string, string) { | |
return (greetingNew, greetingApplied, greetingApproved); | |
} | |
function addComment (string _name, string _email, string _comment) public { | |
comments[msg.sender] = Comment(_name, _email, _comment, 1); | |
addrs.push(msg.sender); | |
} | |
function getComment(address _addr) public constant returns(string, string, string, uint, string) { | |
string memory greeting; | |
if (comments[_addr].approved == 1) { | |
greeting = greetingApplied; | |
} else if (comments[_addr].approved == 2) { | |
greeting = greetingApproved; | |
} else { | |
greeting = greetingNew; | |
} | |
return (comments[_addr].name, comments[_addr].email, comments[_addr].comment, comments[_addr].approved, greeting); | |
} | |
function getAddrs () public constant returns (address []) { | |
return addrs; | |
} | |
function approve (address _addr) public { | |
comments[_addr].approved = 2; | |
} | |
function disapprove (address _addr) public { | |
comments[_addr].approved = 3; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="container"> | |
<br/> | |
<div class="jumbotron"> | |
<p class="lead" id="greeting">请稍等 。。。</p> | |
<hr/> | |
<form id="form"> | |
<div class="form-group"> | |
<label for="name">您的姓名</label> | |
<input type="text" class="form-control" id="name" placeholder=""> | |
</div> | |
<div class="form-group"> | |
<label for="email">您的联系方式(不会公开显示)</label> | |
<input type="email" class="form-control" id="email" placeholder=""> | |
</div> | |
<div class="form-group"> | |
<label for="comment">您的意见</label> | |
<input type="text" class="form-control" id="comment" placeholder=""> | |
</div> | |
<button type="button" id="submit" class="btn btn-primary">发送</button> | |
</form> | |
<div id="formSubmitted" style="display:none">正在记录上链,请等 30 秒 ...</div> | |
</div> | |
<h4>已经确认的参会人</h4> | |
<table class="table table-striped"> | |
<thead> | |
<tr> | |
<th scope="col">姓名</th> | |
<th scope="col">意见</th> | |
</tr> | |
</thead> | |
<tbody id="likes"> | |
</tbody> | |
</table> | |
<p style="text-align:center">Created with <a target="_blank" href="https://docs.secondstate.io/buidl-developer-tool/why-buidl">the BUIDL IDE</a>.</p> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Don't modify */ | |
var instance = null; | |
window.addEventListener('web3Ready', function() { | |
var contract = web3.ss.contract(abi); | |
instance = contract.at(cAddr); | |
reload(); | |
}); | |
function reload() { | |
// instance.greeting(function (e, r) { | |
// $("#greeting").html(r); | |
// }); | |
// $("#form").css("display", "block"); | |
$("#formSubmitted").css("display", "none"); | |
web3.ss.getAccounts(function (e, address) { | |
if (!e) { | |
instance.getComment(address, function (ee, result) { | |
$("#greeting").html(result[4].replace("___", result[0])); | |
console.log(result); | |
if (result[3] == 1 || result[3] == 2) { | |
$("#form").css("display", "none"); | |
} else { | |
$("#form").css("display", "block"); | |
} | |
}); | |
var likes = ""; | |
instance.getAddrs(function (ee, addrs) { | |
addrs.forEach(function(addr) { | |
instance.getComment(addr, function (ee, r) { | |
if (!ee) { | |
if (r[3] == 2) { | |
likes = likes + "<tr><td>" + r[0] + "</td><td>" + r[2] + "</td></tr>"; | |
$("#likes").html(likes); | |
} | |
} | |
}); | |
}); | |
}); | |
$("#likes").html(likes); | |
} | |
}); | |
} | |
$("#submit").click(function() { | |
if ((!$("#name").val().trim()) || (!$("#comment").val().trim())) { | |
alert("请输入姓名与意见"); | |
return false; | |
} | |
web3.ss.getAccounts(function (e, address) { | |
if (!e) { | |
$("#form").css("display", "none"); | |
$("#formSubmitted").css("display", "block"); | |
instance.addComment ($("#name").val(), $("#email").val(), $("#comment").val(), { | |
gas: 499000, | |
gasPrice: 0 | |
}, function (ee, r) { | |
if (ee) { | |
window.alert("Failed at " + address); | |
} | |
}); | |
setTimeout(function () { | |
reload (); | |
}, 25 * 1000); | |
} | |
}); | |
return false; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment