-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
159 lines (133 loc) · 6.26 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
156
157
158
159
"use strict"
var dappAddress = "n1sVyZibojfY8h2AKfuC9uw1azrjDyHPUkC";
var nebulas = require("nebulas"),
Account = nebulas.Account,
neb = new nebulas.Neb();
neb.setRequest(new nebulas.HttpRequest("https://mainnet.nebulas.io"));
// 搜索功能: 查找Super-Dictionary 中有没有该词条
$("#search_book").click(function(){
// $("#search_value").val() 搜索框内的值
var from = Account.NewAccount().getAddressString();
var value = "0";
var nonce = "0"
var gas_price = "1000000"
var gas_limit = "2000000"
var callFunction = "get";
var callArgs = "[\"" + $("#book_name_search").val() + "\"]"; //in the form of ["args"]
var contract = {
"function": callFunction,
"args": callArgs
}
neb.api.call(from,dappAddress,value,nonce,gas_price,gas_limit,contract).then(function (resp) {
cbSearch(resp)
}).catch(function (err) {
//cbSearch(err)
console.log("error:" + err.message)
})
})
//return of search,
function cbSearch(resp) {
var result = resp.result ////resp is an object, resp.result is a JSON string
console.log("return of rpc call: " + JSON.stringify(result))
if (result === 'null' || result == ''){
$("#error_message").addClass("hide");
$(".result_faile").removeClass("hide");
$(".result_success").addClass("hide");
$("#book_name").val("");
$("#book_author").val("");
$("#book_rate").val("");
$("#book_download").val("");
$("#book_description").val("");
} else{
//if result is not null, then it should be "return value" or "error message"
try{
result = JSON.parse(result)
}catch (err){
//result is the error message
}
if (result.key !== undefined && result.writer !== undefined){ //"return value"
$(".result_faile").addClass("hide");
$(".book_info_p").removeClass("hide");
$("#book_name_p").text("Book Name: " + result.key)
$("#book_author_p").text("Author: " + result.writer)
$("#book_rate_p").text("Rate: " + result.rate)
$("#book_download_p").text("Download Address: " + result.linkAddress)
$("#book_description_p").text(result.description);
$("#book_contributor_p").text("Contributed By: " + result.author)
$(".result_success").removeClass("hide");
$("#error_message").addClass("hide");
}else { //"error message"
$(".result_faile").addClass("hide");
$(".result_success").addClass("hide");
$("#error_message").removeClass("hide");
$("#error_message").text("Error: The book name should not be empty");
// $(".result_success").removeClass("hide");
console.log(JSON.stringify(result) + result.key);
}
}
$("#book_name_search").text("");
}
var NebPay = require("nebpay"); //https://github.com/nebulasio/nebPay
var nebPay = new NebPay();
var serialNumber
var callbackUrl = NebPay.config.mainnetUrl;
$("#push_book").click(function() {
var to = dappAddress;
var value = "0";
var callFunction = "save"
var book_name = $("#book_name").val()
var book_author = $("#book_author").val()
var book_rate = $("#book_rate").val()
var book_linkAddress = $("#book_download").val()
console.log("BOOK ADDRESS:" + book_linkAddress);
var book_description = $("#book_description").val()
var callArgs = "[\"" + book_name + "\",\"" + book_author + "\",\"" + book_rate + "\",\"" + book_linkAddress + "\",\"" + book_description + "\"]"
console.log(callArgs);
serialNumber = nebPay.call(to, value, callFunction, callArgs, { //使用nebpay的call接口去调用合约,
listener: cbPush, //设置listener, 处理交易返回信息
callback: callbackUrl
});
intervalQuery = setInterval(function () {
funcIntervalQuery(book_name, book_author, book_rate, book_linkAddress, book_description);
}, 15000);
});
var intervalQuery
function funcIntervalQuery(book_name, book_author, book_rate, book_linkAddress, book_description) {
var options = {
callback: callbackUrl
}
nebPay.queryPayInfo(serialNumber,options) //search transaction result from server (result upload to server by app)
.then(function (resp) {
console.log("tx result: " + resp) //resp is a JSON string
var respObject = JSON.parse(resp)
if(respObject.code === 0){
// alert(`set ${$("#book_name_p").val()} succeed!`)
$("#exampleModal").modal('toggle');
clearInterval(intervalQuery);
$(".result_faile").addClass("hide");
$("#book_name_p").text("Book Name: " + book_name)
$("#book_author_p").text("Author: " + book_author)
$("#book_rate_p").text("Rate: " + book_rate)
// console.log("=======BOOK ADDRESS: " + book_linkAddress + "=========");
$("#book_download_p").text("Download Address: " + book_linkAddress)
$("#book_description_p").text(book_description);
$("#book_contributor_p").addClass("hide");
$("#book_name_search").val("");
$(".result_success").removeClass("hide");
$("#error_message").addClass("hide");
}
})
.catch(function (err) {
console.log(err);
});
}
function cbPush(resp) {
console.log("response of push: " + JSON.stringify(resp))
var respString = JSON.stringify(resp);
if(respString.search("rejected by user") !== -1){
clearInterval(intervalQuery)
alert(respString)
}else if(respString.search("txhash") !== -1){
//alert("wait for tx result: " + resp.txhash)
}
}