Node-Red 数据对接

使用nodered搭建简易控制

[
    {
        "id": "939f47f717739fa0",
        "type": "tab",
        "label": "流程 1",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "d82102602e7c69ab",
        "type": "http in",
        "z": "939f47f717739fa0",
        "name": "控制长输项目点位",
        "url": "/jncs/control",
        "method": "post",
        "upload": false,
        "swaggerDoc": "",
        "x": 230,
        "y": 420,
        "wires": [
            [
                "b50be88667257c0e"
            ]
        ]
    },
    {
        "id": "333aeb3ee6994a5b",
        "type": "http request",
        "z": "939f47f717739fa0",
        "name": "获取 Token",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "http://172.18.125.89:30103/auth/v2/login",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 390,
        "y": 340,
        "wires": [
            [
                "acc0242154b98d94"
            ]
        ]
    },
    {
        "id": "7eba6bd3d0a60249",
        "type": "inject",
        "z": "939f47f717739fa0",
        "name": "",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "21600",
        "crontab": "",
        "once": true,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "{\"username\":\"csgw\",\"password\":\"Z59Ppm2VA4KezjjQShwPrmnqrAxAzhkQjqD9H2dx+NSg9En67yNk2gciU7jsF1y91qfcOFrCtsYWNxkRxhB7yg==\"}",
        "payloadType": "json",
        "x": 230,
        "y": 340,
        "wires": [
            [
                "333aeb3ee6994a5b"
            ]
        ]
    },
    {
        "id": "b50be88667257c0e",
        "type": "function",
        "z": "939f47f717739fa0",
        "name": "组装控制请求头与请求体",
        "func": "var token = flow.get('token');\n\nvar headers = {\n    'Content-Type': 'application/json',\n    'Authorization': 'Bearer ' + token \n};\n\nvar httpRequest = {\n    headers: headers,\n    payload: [{\n        'vpnUserId': msg.req.body.vpnUserId,\n        'cmdGuid': Date.now().toString(),\n        'targetValue': msg.req.body.targetValue\n    }]\n};\n\nflow.set('session', msg.res);\n\nconsole.log(httpRequest);\nreturn httpRequest;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 470,
        "y": 420,
        "wires": [
            [
                "95eb2000e713abb5"
            ]
        ]
    },
    {
        "id": "95eb2000e713abb5",
        "type": "http request",
        "z": "939f47f717739fa0",
        "name": "模拟控制指令下发",
        "method": "POST",
        "ret": "obj",
        "paytoqs": "ignore",
        "url": "http://172.18.125.89:30103/long-haul-network-mgr/commandIssuance/simulation/CmdReception",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 710,
        "y": 420,
        "wires": [
            [
                "c23f0943c76ee323"
            ]
        ]
    },
    {
        "id": "c23f0943c76ee323",
        "type": "function",
        "z": "939f47f717739fa0",
        "name": "准备响应",
        "func": "console.log(msg);\n\nreturn {\n    headers: msg.headers,\n    payload: msg.payload[0],\n    statusCode: msg.statusCode,\n    res: flow.get('session')\n}",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 900,
        "y": 420,
        "wires": [
            [
                "c823916d505f6d58"
            ]
        ]
    },
    {
        "id": "acc0242154b98d94",
        "type": "function",
        "z": "939f47f717739fa0",
        "name": "设置流程上下文级别的 Token",
        "func": "console.log('new Token: ', msg.payload.data.access_token);\nflow.set(\"token\", msg.payload.data.access_token);\n\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 620,
        "y": 340,
        "wires": [
            []
        ]
    },
    {
        "id": "c823916d505f6d58",
        "type": "http response",
        "z": "939f47f717739fa0",
        "name": "响应消息",
        "statusCode": "",
        "headers": {},
        "x": 1060,
        "y": 420,
        "wires": []
    }
]

token获取流程

许多场景下 请求第三方控制下发 需要申请并携带第三方token

流程级别参数

flow.get(“key”) flow.set(“key”,object)

http request node

前置输入格式

{
    url: url
    headers: headers,
    payload: [{
        'vpnUserId': msg.req.body.vpnUserId,
        'cmdGuid': Date.now().toString(),
        'targetValue': msg.req.body.targetValue
    }]
};

http response node

前置输入格式

{
    headers: msg.headers,
    payload: msg.payload[0],
    statusCode: msg.statusCode,
    res: flow.get('session')
}