记录fisco bcos的学习过程,在应用中调用智能合约上链(八)

前言:

上一章节,留言的智能合约代码已经编写成功,并且测试通过。这个章节我们改造我们的应用程序。

 

正文:

 

改造前后台代码,从下面的代码我们可以在// todo 处添加我们上链的代码,因为上链需要的参数全部在comment对象中了。

@RequiresPermissions("comment:audit")

@PostMapping("/audit")

@BussinessLog("审核评论")

public ResponseVO audit(Comment comment, String contentText, Boolean sendEmail) {

try {

commentService.updateSelective(comment);

if(!StringUtils.isEmpty(contentText)){

comment.setContent(contentText);

commentService.commentForAdmin(comment);

}

if(null != sendEmail && sendEmail){

Comment commentDB = commentService.getByPrimaryKey(comment.getId());

mailService.send(commentDB, TemplateKeyEnum.TM_COMMENT_AUDIT, true);

}

// todo

 

 

} catch (Exception e) {

e.printStackTrace();

return ResultUtil.error("评论审核失败!");

}

return ResultUtil.success(ResponseStatus.SUCCESS);

}

 

 

 

 

改造后:

@RequiresPermissions("comment:audit")

@PostMapping("/audit")

@BussinessLog("审核评论")

public ResponseVO audit(Comment comment, String contentText, Boolean sendEmail) {

try {

commentService.updateSelective(comment);

if(!StringUtils.isEmpty(contentText)){

comment.setContent(contentText);

commentService.commentForAdmin(comment);

}

if(null != sendEmail && sendEmail){

Comment commentDB = commentService.getByPrimaryKey(comment.getId());

mailService.send(commentDB, TemplateKeyEnum.TM_COMMENT_AUDIT, true);

}

// 上链

String body="{\n" +

" \n" +

" \"groupId\" :1,\n" +

" \"signUserId\": \"bbc341c4e982xxxxxxxxxxx76167f00ab4842e\",\n" +

" \"contractAbi\":[{\"constant\":true,\"inputs\":[{\"name\":\"id\",\"type\":\"string\"}],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"},{\"name\":\"\",\"type\":\"int256\"},{\"name\":\"\",\"type\":\"string\"},{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"id\",\"type\":\"string\"},{\"name\":\"sid\",\"type\":\"int256\"},{\"name\":\"nickname\",\"type\":\"string\"},{\"name\":\"content\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[{\"name\":\"\",\"type\":\"int256\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"name\":\"count\",\"type\":\"int256\"}],\"name\":\"SetResult\",\"type\":\"event\"}],\n" +

" \"contractAddress\":\"0xef860c2xxxx24b38bxxe1b317ad2\",\n" +

" \"funcName\":\"set\",\n" +

" \"funcParam\":[\""+comment.getId()+"\","+comment.getSid()+",\""+comment.getNickname()+"\",\""+comment.getContent()+"\"]\n" +

"\n" +

"}";

String url="http://119.xxxxxx:5002/WeBASE-Front/trans/handleWithSign";

String res= HttpUtil.post(url,body );

// 后续可以把上链后反回的信息根据需要保存在关系数据库中

} catch (Exception e) {

e.printStackTrace();

return ResultUtil.error("评论审核失败!");

}

return ResultUtil.success(ResponseStatus.SUCCESS);

}

 

想直接用上面代码发请求,只需引入下面的依赖:

<dependency>

<groupId>cn.hutool</groupId>

<artifactId>hutool-all</artifactId>

<version>5.4.0</version>

</dependency>

 

还没完:

这里我们考虑到接口的安全性,我们需要配置相应的防火墙安全策略只能让我们的应用服务器有权限去访问合约的接口。

记录fisco bcos的学习过程,在应用中调用智能合约上链(八)

 

然后 部署我们的应用。此处省去1万字。。。。。

开始测试

审核留言信箱

记录fisco bcos的学习过程,在应用中调用智能合约上链(八)

 

记录fisco bcos的学习过程,在应用中调用智能合约上链(八)

 

之前的建议记录:

记录fisco bcos的学习过程,在应用中调用智能合约上链(八)

上链后的交易记录:

记录fisco bcos的学习过程,在应用中调用智能合约上链(八)

发现区块中多了一条交易,这样就了。打完收工