Development of blockchain applications

 After the baptism of the previous two articles ( [Suitable for Xiaobai] I use a visual way to deploy Webase in the blockchain , and I use visual Webase to develop smart contracts in the blockchain ), I believe everyone has a little understanding of the blockchain. Well, in this chapter, the editor will show you the development of blockchain applications.

First, you need to export the smart contract project that you just compiled and deployed.

Before exporting a project, the project must be compiled. The project name and package name can be customized. channelIp is the IP address of the node, and channelPort is the port number of the node. There is no requirement for user selection.

Here, the editor provides you with a SpringBoot front-end and back-end separation framework for you to facilitate your development.

Link: https://pan.baidu.com/s/1T50RdvhWYMvbRLtzogoeBA 
Extraction code: r1h3

In this chapter, the editor will also use this framework as an example to demonstrate to you.

First open this framework with integrated development tools, here I use IDEA.

After opening, as shown in the picture, here is a suggestion from the editor, that is, Chinese should not appear in the path of the project! This is just a demonstration example and I have not modified it. In this framework, the front-end framework is ruoyi-trace, and what we need to modify are ruoyi-admin and ruoyi-trace (this is actually the contract project we exported in webase, but we need to merge it into this framework).

 this is this

For the corresponding content under the contract project, we need to replace all the files under com.ruoyi.trace and resources (replace with the corresponding project file just exported from webase).

 Replaced as shown in the figure.

Next we need to write our contract layer in tuoyi-admin

Create a new trace project under this controller, and write the code, as shown in the figure below.

Under trace, we create a new class and start writing our code.

code show as below:

package com.ruoyi.web.controller.trace;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.example.demo.model.bo.TraceAddProductInputBO;
import org.example.demo.model.bo.TraceAddTraceByMiddlemanInputBO;
import org.example.demo.model.bo.TraceAddTraceBySuperMarketInputBO;
import org.example.demo.model.bo.TraceGetTraceInputBO;
import org.fisco.bcos.sdk.transaction.model.dto.CallResponse;
import org.fisco.bcos.sdk.transaction.model.dto.TransactionResponse;
import org.springframework.web.bind.annotation.*;
import org.example.demo.service.TraceService;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.ArrayList;

/**
 * @ClassName: TraceChainController
 * @Description:
 * @Author
 * @Date 2023/2/7
 * @Version 1.0
 */
@RestController
@RequestMapping("/trace")
public class TraceChainController extends BaseController {

    @Resource
    private TraceService traceService;

    /**
     * 新增水果接口
     * @param traceFruitNewFruitInputBO
     * @return
     */
    @PostMapping("/addProduct")
    public AjaxResult addProduct(@RequestBody TraceAddProductInputBO traceFruitNewFruitInputBO) {
        try {
            TransactionResponse response = traceService.addProduct(traceFruitNewFruitInputBO);
            System.out.println(response);
            response.getReceiptMessages();
            if(response.getReturnCode()==0){//success
                return AjaxResult.success(response);
            }else{
                return error(response.getReceiptMessages());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(1);
    }

    /**
     *中间商操作接口
     * @param traceFruitAddTraceInfoByMiddlemanInputBO
     * @return
     */
    @PostMapping("/addTraceInfoByMiddleman")
    public AjaxResult addTraceInfoByMiddleman(@RequestBody TraceAddTraceByMiddlemanInputBO traceFruitAddTraceInfoByMiddlemanInputBO) {
        try {
            TransactionResponse response = traceService.addTraceByMiddleman(traceFruitAddTraceInfoByMiddlemanInputBO);
            System.out.println(response);
            response.getReceiptMessages();
            if(response.getReturnCode()==0){//success
                return AjaxResult.success(response);
            }else{
                return error(response.getReceiptMessages());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(1);
    }

    /**
     * 超市操作接口
     * @param traceFruitAddTraceInfoBySuperMarketInputBO
     * @return
     */
    @PostMapping("/addTraceInfoBySuperMarket")
    public AjaxResult addTraceInfoBySuperMarket(@RequestBody TraceAddTraceBySuperMarketInputBO traceFruitAddTraceInfoBySuperMarketInputBO) {
        try {
            TransactionResponse response = traceService.addTraceBySuperMarket(traceFruitAddTraceInfoBySuperMarketInputBO);
            System.out.println(response);
            response.getReceiptMessages();
            if(response.getReturnCode()==0){//success
                return AjaxResult.success(response);
            }else{
                return error(response.getReceiptMessages());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(1);
    }

    /**
     * 查询溯源信息接口
     * @param traceNumber
     * @return
     */
    @GetMapping("/getTraceInfo")
    public AjaxResult getTraceInfo(@RequestParam("traceNumber") BigInteger traceNumber) {
        try {
            TraceGetTraceInputBO traceFruitGetTraceInfoInputBO = new TraceGetTraceInputBO();
            traceFruitGetTraceInfoInputBO.setTraceNumber(traceNumber);
            CallResponse response = traceService.getTrace(traceFruitGetTraceInfoInputBO);
            System.out.println(response);
            if(response.getReturnCode()==0){//success
                ArrayList  rsList= (ArrayList) response.getReturnObject().get(0);
                if(!(boolean)rsList.get(4)){
                    return error("溯源码不存在");
                }
                return AjaxResult.success(response.getReturnObject());
            }else{
                return error(response.getReturnMessage());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return toAjax(1);
    }
}

Next, we need to copy some related configuration files under application.properties to aoolication.dev.yml (the corresponding data of mysql master also needs to be slightly modified), as shown in the figure:

It should be noted that the IP address under peers must be the IP address of the configuration node. 

 

There are sql statements in this file to create a data table. We can use tools to create this table. Here, the editor uses navicat.

Choose a good sql path to run. 

Guess you like

Origin blog.csdn.net/LforikQ/article/details/130756719