引用节点
- node-red-contrib-postgresql
- node-red-dashboard
目的
通过Node-RED的Dashboard组件绘制登录界面,查询数据库PostgreSQL中账号信息验证用户名密码,并弹框提示。
实现效果
实现步骤
-
安装Node-red
- 自行百度安装,
-
安装PostgresSQL
- 自行百度安装,
- 非本地部署,注意开启PostgresSQL远程访问
- 创建用户表
-
Node-red建立流程
- 添加用户名、密码输入框
- 用户名、密码传参
- 按钮触发拼接查询语句,注意单双引号转换拼接。建议先用数据库直接查询成功后,再将SQL转参拼接。
var USER_ID=flow.get("USER_ID"); var USER_PASSWORD=flow.get("USER_PASSWORD"); //查询数据 //msg.payload = "SELECT * FROM \"USER_WEIGHT\" WHERE \"USER_ID\"=\'" + USER_ID + "\' and \"USER_PASSWORD\"=\'" +USER_PASSWORD+"\';"; //判断是否正确 msg.payload = "SELECT COUNT(*) > 0 FROM \"USER_WEIGHT\" WHERE \"USER_ID\"=\'" + USER_ID + "\' and \"USER_PASSWORD\"=\'" +USER_PASSWORD+"\';"; return msg;
- 连接数据库执行查询,注意开启Postgresql远程访问,否则无法连接会报错。
- 判断查询结果,注意msg.payload[0]["?column?"] 避免路径写错,可用Debug获取
- 执行弹框提示
-
源码
[
{
"id": "f4a4d8eab7935bc8",
"type": "tab",
"label": "流程 3",
"disabled": false,
"info": "",
"env": []
},
{
"id": "b704c867.534e98",
"type": "ui_button",
"z": "f4a4d8eab7935bc8",
"name": "",
"group": "698e17f83c344bd8",
"order": 3,
"width": 0,
"height": 0,
"passthru": false,
"label": "登录",
"tooltip": "",
"color": "",
"bgcolor": "",
"className": "",
"icon": "",
"payload": "",
"payloadType": "str",
"topic": "",
"topicType": "str",
"x": 630,
"y": 180,
"wires": [
[
"124c49f4442dc067"
]
]
},
{
"id": "3d808cbd.363e64",
"type": "ui_text_input",
"z": "f4a4d8eab7935bc8",
"name": "",
"label": "输入用户名:",
"tooltip": "",
"group": "698e17f83c344bd8",
"order": 1,
"width": 0,
"height": 0,
"passthru": true,
"mode": "text",
"delay": 300,
"topic": "topic",
"sendOnBlur": true,
"className": "",
"topicType": "msg",
"x": 210,
"y": 180,
"wires": [
[
"8a97e4e8e6c2906c"
]
]
},
{
"id": "8a97e4e8e6c2906c",
"type": "function",
"z": "f4a4d8eab7935bc8",
"name": "USER_ID",
"func": "flow.set(\"USER_ID\",msg.payload);\nreturn USER_ID;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 400,
"y": 180,
"wires": [
[
"b704c867.534e98"
]
]
},
{
"id": "354f5fe9f3d6dd80",
"type": "ui_text_input",
"z": "f4a4d8eab7935bc8",
"name": "",
"label": "输入密码",
"tooltip": "",
"group": "698e17f83c344bd8",
"order": 2,
"width": 0,
"height": 0,
"passthru": true,
"mode": "password",
"delay": 300,
"topic": "topic",
"sendOnBlur": true,
"className": "",
"topicType": "msg",
"x": 200,
"y": 260,
"wires": [
[
"2958e8bee3400e60"
]
]
},
{
"id": "2958e8bee3400e60",
"type": "function",
"z": "f4a4d8eab7935bc8",
"name": "USER_PASSWORD",
"func": "flow.set(\"USER_PASSWORD\",msg.payload);\nreturn USER_PASSWORD ;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 440,
"y": 260,
"wires": [
[
"b704c867.534e98"
]
]
},
{
"id": "124c49f4442dc067",
"type": "function",
"z": "f4a4d8eab7935bc8",
"name": "登录验证",
"func": "var USER_ID=flow.get(\"USER_ID\");\nvar USER_PASSWORD=flow.get(\"USER_PASSWORD\");\n\n//查询数据\n//msg.payload = \"SELECT * FROM \\\"USER_WEIGHT\\\" WHERE \\\"USER_ID\\\"=\\'\" + USER_ID + \"\\' and \\\"USER_PASSWORD\\\"=\\'\" +USER_PASSWORD+\"\\';\";\n//判断是否正确\nmsg.payload = \"SELECT COUNT(*) > 0 FROM \\\"USER_WEIGHT\\\" WHERE \\\"USER_ID\\\"=\\'\" + USER_ID + \"\\' and \\\"USER_PASSWORD\\\"=\\'\" +USER_PASSWORD+\"\\';\";\n\nreturn msg;",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 780,
"y": 180,
"wires": [
[
"2ed8dbc2e0cbe7ce"
]
]
},
{
"id": "bb7a6f487a3bb5b7",
"type": "switch",
"z": "f4a4d8eab7935bc8",
"name": "判断是否成功",
"property": "payload[0][\"?column?\"]",
"propertyType": "msg",
"rules": [
{
"t": "true"
},
{
"t": "false"
},
{
"t": "else"
}
],
"checkall": "true",
"repair": false,
"outputs": 3,
"x": 240,
"y": 440,
"wires": [
[
"052e3c30d279b3b9"
],
[
"c657ba085d4081e7",
"8bb573c29810d009"
],
[]
]
},
{
"id": "c657ba085d4081e7",
"type": "ui_toast",
"z": "f4a4d8eab7935bc8",
"position": "bottom right",
"displayTime": "3",
"highlight": "",
"sendall": true,
"outputs": 0,
"ok": "OK",
"cancel": "Cancel",
"raw": false,
"className": "",
"topic": "登陆失败,用户名或密码错误",
"name": "登陆失败",
"x": 520,
"y": 500,
"wires": []
},
{
"id": "8bb573c29810d009",
"type": "debug",
"z": "f4a4d8eab7935bc8",
"name": "debug 6",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 500,
"y": 600,
"wires": []
},
{
"id": "052e3c30d279b3b9",
"type": "ui_toast",
"z": "f4a4d8eab7935bc8",
"position": "bottom right",
"displayTime": "3",
"highlight": "",
"sendall": true,
"outputs": 0,
"ok": "OK",
"cancel": "Cancel",
"raw": false,
"className": "",
"topic": "登陆成功",
"name": "登陆成功",
"x": 520,
"y": 360,
"wires": []
},
{
"id": "2ed8dbc2e0cbe7ce",
"type": "postgresql",
"z": "f4a4d8eab7935bc8",
"name": "",
"query": "{{{msg.payload}}}",
"postgreSQLConfig": "411afda02ad6f17c",
"split": false,
"rowsPerMsg": 1,
"outputs": 1,
"x": 970,
"y": 180,
"wires": [
[
"bb7a6f487a3bb5b7"
]
]
},
{
"id": "698e17f83c344bd8",
"type": "ui_group",
"name": "用户登录",
"tab": "bec6119a26bcc753",
"order": 2,
"disp": true,
"width": "6",
"collapse": false,
"className": ""
},
{
"id": "411afda02ad6f17c",
"type": "postgreSQLConfig",
"name": "PRD_JNMES",
"host": "192.168.175.128",
"hostFieldType": "str",
"port": "5432",
"portFieldType": "num",
"database": "PRD_JNMES",
"databaseFieldType": "str",
"ssl": "false",
"sslFieldType": "bool",
"applicationName": "",
"applicationNameType": "str",
"max": "10",
"maxFieldType": "num",
"idle": "1000",
"idleFieldType": "num",
"connectionTimeout": "10000",
"connectionTimeoutFieldType": "num",
"user": "postgres",
"userFieldType": "str",
"password": "123456",
"passwordFieldType": "str"
},
{
"id": "bec6119a26bcc753",
"type": "ui_tab",
"name": "用户登录",
"icon": "",
"disabled": false,
"hidden": false
}
]