SteemJS中文手册lemooljiang (75)in #starnote • 6 years ago (edited)steemjs的调用、参数和案例。以一个应用的需求来进行编写。 -> 前往星空笔记 #steemjs #steemdev #javascript #vuejs
SteemJS手册
参考1
参考2
steemsnippets
参考3
steem-python
Nodejs, vue.js 中使用,所以,请自行安装所需环境。
cnpm install steem --save //main.js import steem from 'steem' //设置steem节点,挂载到全局 steem.api.setOptions({ url: 'https://anyx.io' }) Vue.prototype.steem= steem查询
//公共节点,前面加上 https://
steem.broadcast.accountUpdate(wif, account, owner, active, posting, memoKey, jsonMetadata, function(err, result) { console.log(err, result) })一般依据标签对文章进行分类。tags在json_metadata中,这是字符串,要先JSON.parse转成对象形式
getCategory() { let category_net = "songofera" for (let i = 0; i < 100; i++) { let data_str = this.posts[i].json_metadata let data_obj = JSON.parse(data_str) if(data_obj.tags.includes(category_net)) { this.afterposts.push(this.posts[i]) console.log(898, this.posts[i]) } } }steem.api.getTrendingTags('cn', 15, function(err, result) { //获取在cn后的15个流行标签 ,留空表示从最热门的开始 console.log(123, err, result) })post.json_metadata还可以按需求写入其它的数据,有点类似MongoDB。这样的话易于对文章进行过滤,也可保存一些特殊的数据。
json_metadata: "{"tags":["cn","network-institute","markdown","html"],"app":"steemit\/0.1","format":"markdown"}" eg: let jsonMetadata = {"tags": tags, "dapp": "moochain", "format": "markdown", "cover": hash, "text": text} JSON.stringify(jsonMetadata)steem.api.getContent(author, permlink, function(err, result) { console.log(err, result) }) eg: steem.api.getContent('lemooljiang', 'steemjs-41', function(err, result) { console.log(934,err, result) })SteemJS中有些函数可以加上
Async,以得到Promise的写法。比如:steem.api.getContentAsync//获取某篇文章 steem.api.getContentAsync(author, permlink) //获取一篇文章的回复 steem.api.getContentRepliesAsync(author, permlink) //连续获取文章 steem.api.getDiscussionsByAuthorBeforeDateAsync(author, null, beforeDate, 5) .then(res=>{ console.log(222, res) }) //获取作者的评论 let query = { limit : 10, start_author:'lemooljiang', "start_permlink": pPermLink} comments = await this.steem.api.getDiscussionsByCommentsAsync(query) //获取帐户历史 steem.api.getAccountHistoryAsync(account, from, limit) .then(res=>{ console.log(122, res)} )帐户数据几乎都在这里了,包括创建的时间,余额,点赞能量等等。
steem.api.getAccounts(['lemooljiang'], function(err, result) { console.log(789,err, result); });let query = { limit : 5, start_author:'lemooljiang', start_permlink: ""} this.steem.api.getDiscussionsByComments(query, function(err, result) { console.log(265, err, result) })this.steem.api.getFollowers('lemooljiang', '', 'blog', 10, function(err, result) { console.log(111, err, result) })this.steem.api.getFollowing('lemooljiang', '', 'blog', 10, function(err, result) { console.log(222, err, result) })网页打不开,好得可以点赞。
来自于 [WhereIn Android] (http://www.wherein.io)
我用手机试了一下,确实很慢!github在国内估计又被墙了!
Congratulations @lemooljiang! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :
You can view your badges on your Steem Board and compare to others on the Steem Ranking
If you no longer want to receive notifications, reply to this comment with the word
STOPTo support your work, I also upvoted your post!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!
steem.api.getContentReplies(author, permlink, function(err, result) { console.log(err, result) }) eg: steem.api.getContentReplies('lemooljiang', 'steemjs-41', function(err, result) { console.log(err, result) })steem.broadcast.vote(wif, voter, author, permlink, weight, function(err, result) { console.log(err, result) }) //函数讲解 steem.broadcast.vote ( wif, //发帖密钥 voter, //点赞人 author, //作者 permlink, // URL地址 weight //点赞比重,100*100 ) eg: steem.broadcast.vote( "5KRSmEMffffff", "lemooljiang", "starnote", '6ekczn-2019', 10000, function(err, result) { console.log(8888, err, result) })acc = "starnote" following = "lemooljiang" key = "5Jmwkffffffffff" json = JSON.stringify( ['follow', { follower: acc, following: following, what: ['blog'] }]) this.steem.broadcast.customJson(key, [], [acc], 'follow', json, function(err, result) { console.log(111,err, result) })steem.api.streamTransactions("head", function(err, result) { if (result && !err) { console.log(result) } }) eg: 实时获取作者的文章或评论 getData() { this.steem.api.streamTransactions("head", function (err, result) { if (err) { alert("网络有问题!请更换API节点!") } let txType = result.operations[0][0] //获取操作类别 let txData = result.operations[0][1] //获取操作内容 if (txType == "comment" & txData.author == "lemooljiang") { //获取作者的文章或评论 let commentBody = txData.body let permlink = txData.permlink console.log(12345,commentBody) } }) }可以获取用户的余额、代理和公钥等数据
async getStates(){ let username = "timool" let s = await this.steem.api.getAccountsAsync([username]) console.log(789, s[0].balance) }async getDelegateSp(){ let user = 'lemooljiang' let res = await this.steem.api.getVestingDelegationsAsync(user, 'nutbox.mine', 1) if(res.length === 0){ return 0 } else{ return parseFloat(res[0].vesting_shares) } // let delegatedSp = parseFloat(res[0].vesting_shares) / spToVests //刚查了下,如果滑有代理或是取消代理,就查询不到任何数值,为空,而不是0! },let author = "lemooljiang" let permlink = "zn69yg" let res = await this.steem.api.getActiveVotesAsync(author, permlink) console.log(566, res)