1.0.11 • Published 2 years ago

@ccchain/carbon-sdk v1.0.11

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

私有云SDK

一、 DTO定义

1. CloudChainInfo

描述:私有链信息对象,属性如下:

address: string 请求地址,如 47.11.xxx.xxx:7894 或 cloud.brl.com

name: string 链名称,如 brldelian

protocol: 协议,http/https

2. PubChainInfo

描述:公链信息对象结构,属性如下:

address: string 请求地址,如 47.11.xxx.xxx:7894 或 benneng.carbon.com

name: string 链名称,如 ccchain

protocol: 协议,http/https

openUrl: 开放的给私有链的url, 如:"/ccchain", 以便于私有链向例如 https://www.ccchain.com/ccchain的路由发起请求

3. 投稿信息

描述:上传的资源数据类型,属性如下:

assetInfo: 资源信息

assetInfo.title: 标题
assetInfo.is_publish: number 发布状态 0:私有,1:投稿
assetInfo.description(可选): string

aseetAccessToken: 稿件专属访问id,审核后失效

cloudChainInfo: 私有云信息

cloudChainInfo.address 私有链地址
cloudChainInfo.name 私有链名称
cloudChainInfo.protocol 私有链协议

二、User guides

1. 导入SDK

import CarbonSDK from '@ccchain/carbon-sdk';

2. 实例化SDK

实例化SDK时,传入的数组是可用公链列表。第一个元素为当前需要选用的公链。

/**
 * name: string, 公链名称
 * address: string, 公链地址,为 域名 或 ip:port
 * protocol: string, 协议,http/https
 * openUrl: string, 公链开放给私有云的路径, 如:"/ccchain", 以便于私有链向例如 https://benneng.carbon.com/ccchain的路由发起请求
 */
let carbon = new CarbonSDK([{
    name: "PUBCHAIN"
    address: "benneng.carbon.com",
    protocol: "https",
    openUrl: "/ccchain"
}]);

3. 启动监听,用于接收用户投稿

// 1. 监听express请求
const express = express();
// 这里的"/ccchain"与上文提到的openURL相对应
app.use("/ccchain",function (express) {
    carbon.handleExpressReq(req, res, (result: any)=>{

        /**
        * result: 回调函数代表需要进行存储的内容,建议将其存入数据库,result 数据类型与投稿信息一致
        */
        console.log(result);
    });
})

// or
(async () => {
    let result = await carbon.handleExpressReq(req, res)
    console.log(result);
})()

// 2. 监听原生node请求
carbon.handleReq(req, res, (result: any)=>{
    /**
    * result: 回调函数代表需要进行存储的内容
    */
    console.log(result);
});
// or
(async () => {
    let result = await carbon.handleReq(req, res)
    console.log(result);
})()

// 3. 监听koa请求,还未完成

4. 访问稿件的内容

用于审核后端私有云查询文稿内容

(async() => {
    // 根据稿件id去私有云获取稿件内容
    const result = await carbon.accessAsset({
        id:"稿件id",
        accessToken:"稿件专属token"
    },
        address: 'ip:port' // 私有云的ip:port
    ););
})

5. 审核稿件内容

用于审核后端私有云保存文稿审核结果(通过/拒绝)

(async() => {
    // 根据稿件id去私有云获取稿件内容
    const result = await carbon.approveOrReject({
        id:"稿件id",
        status: 0 | 1 // 0: 拒绝稿件  1: 通过稿件
        opinion: "审核意见"
    },
        address: 'ip:port' // 私有云的ip:port
    );
})

6. 获取公链信息 (客户端用)

用于客户端/App公链后端查询可用公链信息

(async() => {
    // 获取公链信息
    const result = await carbon.getPubChainInfo();
    console.log(result.url)
})
1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago