菜单
本页目录

安装

npm install gm-crypto
npm install fs
npm install uuid
npm install form-data
npm install axios

示例代码

const {SM3} = require('gm-crypto');
const fs = require('fs');
const {v4: uuidv4} = require('uuid');
const FormData = require('form-data');
const axios = require('axios');


async function test() {
    //调用路由模块
    // API路径
    const apiName = '/attestation/file';
    // 生产系统
    const url = 'https://newips.netnotary.cn/shimakaze/api' + apiName;
    // const url = 'http://127.0.0.1:10000/shimakaze/api' + apiName;
    // uuid
    var requestid = uuidv4()
    // 随机数
    var nonce = Math.floor(Date.now() / 1000);
    // appid
    var appid = 'xxxxxxxxxx'
    // 盐值
    const securityKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxx";

    // 待摘要数据
    const data = requestid + appid + nonce;
    const signature = SM3.digest(securityKey + data, 'utf8', 'hex')

    console.log('Request Url: ', url);
    console.log('Salt Key: ', securityKey)
    console.log('Abstract Data: ', data);
    console.log('Signature Data: ', signature);

    // 构建请求头
    const headers = {
        'request-id': requestid,
        'app-id': appid,
        'nonce': nonce, // 时间戳
        'signature': signature
    };

    // 构建文件
    const writeStream = fs.createWriteStream('data.txt');
    writeStream.write('yzwdj-yzwdj');
    writeStream.write('业主我当家报文测试');
    writeStream.end();

    // 构建请求参数
    const fileBuffer = fs.createReadStream('data.txt');

    const formData = new FormData();
    formData.append('file', fileBuffer, {filename: 'data.txt'});
    formData.append('label', '1');

    // 发送文件到目标API
    axios.post(url, formData, {
        headers: {
            'Content-Type': 'multipart/form-data',
            ...headers
        },
    })
        .then(response => {
            console.log('文件上传成功:', response.data);
        })
        .catch(error => {
            console.error('文件上传失败:', error.message);
        });

}

test();