后端是nodejs EXPRESS结构,前端是vue通过axios做的对接,端口跨域代理已经完成。前后端对接不会。
EXPRESS 后端数据类型是'application/x-www-form-urlencoded, 前端通过axios的是application/json'类型。
EXPRESS
app.post("/ais", function (req, res, next) {
ip = req.connection.remoteAddress; //获取客户端ip
ip = ip.replace(/[^0-9]/ig, ' '); //将非数字替换为空格
ip = ip.replace(/\s/ig,''); //去掉全部空格
let conType = req.get("Content-type"); //获取post请求头中声明的请求体数据类型
let doc = req.body || {};
let t_type = doc.type;
let t_mode = doc.mode;
var d = new Date();
date = (d.getFullYear()) + "" +
(d.getMonth() + 1) + "" +
(d.getDate()) + "" +
(d.getHours()) + "" +
(d.getMinutes()) + "" +
(d.getSeconds());
//检查目录是否存在,不存在则以客户端ip为目录名在当前根目录下创建子目录
if (fs.existsSync(ip+'\\')){
//console.log("目录已存在")
}
else {
fs.mkdir(".\\"+ip+"\\",function(err){
if (err){
console.log("创建目录失败");
}
else{
console.log("创建目录成功");
}
});
}
switch(conType)
{
//请求体数据类型为浏览器原生表单或json,bodyParser中间件自动调用对应方法解析
case ('application/x-www-form-urlencoded'): //对应模式2和5
if (t_type == 'HeartBeat' && typeof(t_mode) == 'undefined'){ //模式2和4的心跳应答
console.log("当前时间:"+date);
if (send_flag == 0){
res.json({"status": 200, "PostServer": "OK",
"whitelist_opt_data":JSON.parse(iconv.decode(whitelistData,'gbk'))
})
send_flag = 1;
}
else if (send_flag == 1){
console.log('heart server proc');
res.json({"PostServer": "OK","passwd":passwd})
}
}
else if (t_type == 'heartbeat' && t_mode == '5'){ //模式5的心跳应答
console.log('heart server proc:mode 5');
if (send_flag == 0){
res.json({"error_num":0,"error_str":"no error","passwd":passwd,"whitelist_data":JSON.parse(iconv.decode(whitelistData,'gbk'))}) //下发黑白名单
console.log("心跳下发黑白名单")
send_flag = 1;
}
else if (send_flag == 1){
res.json({"error_num":0,"error_str":"no error","passwd":passwd,"triger_data":{"action":"on"}}) //应答软触发
console.log("心跳软触发")
send_flag = 2;
}
else if (send_flag == 2){
res.json({"error_num":0,"error_str":"no error","passwd":passwd})
}
}
else if (t_type == 'online' || t_type == 'offline' || t_type == 'ioinput'){ //IO事件上报
if (t_mode == 5){
plate = doc.plate_num;
}else{
plate = doc.car_plate;
}
let full_pic = doc.picture;
let closeup_pic = doc.closeup_pic;
let start_time = doc.start_time;
++count; //post请求计数
console.log("---------------------------------- 相机ip:"+ip+" 计数:"+count+" ----------------------------------");
//遍历输出key、value
for(var key in doc){
if (key.indexOf("pic")==-1){
if (key.indexOf("start_time")==0){
//cosole.log(key,date)
console.log("start_time:"+date)
}else{
console.log("%s:%s",key,doc[key])
}
}
}
savePic(full_pic,picType_full); //保存模式2或5的全景图
savePic(closeup_pic,picType_closeup); //保存模式2或5的特写图
if (t_mode == 5){ //模式5循环应答
if (1 == count % 4){
res.json({"error_num":0,"error_str":"no error","passwd":passwd,"gpio_data":
[{"ionum":"io1","action":"on"},{"ionum":"io2","action":"on"}]})
console.log("模式5应答开闸")
}else if (2 == count % 4){
res.json({"error_num":0,"error_str":"no error","passwd":passwd,"rs485_data":
[{"encodetype":"hex2string","data":"73646B2831292073656E64207273323332206461746120746F2063616D6572612E00"},{"encodetype":"base64","data":"5qyi6L+O5YWJ5Li0"}]})
console.log("模式5串口透传")
}else if (0 == count % 4){
res.json({"error_num":0,"error_str":"no error","passwd":passwd,"triger_data":{"action":"on"}})
console.log("模式5软触发")
}
else if (3 == count % 4){
res.json({"error_num":0,"error_str":"no error","passwd":passwd,"whitelist_data":
[{"Action":"add","PlateNumber":"粤B23456","Type":"W","Start":"2019/01/01 00:00:00","End":"2019/10/10 23:59:59"}]})
console.log("下发黑白名单")
}
}else{ //模式2循环应答
doc.park_id=parseInt(doc.park_id);
let sign = doc.park_id % 2; //摄像机id,约定奇数为进口,偶数为出口
if (t_type == 'online' && typeof(t_mode) == 'undefined'){
if (sign == 1) {
if (1 == count % 5){ //轮流发送应答指令
res.json({"status": 200, "speed": 1, "verified": true, "postfix": "欢迎光临," + doc.car_plate})
console.log("模式2入口反馈开闸")
}else if (2 == count % 5){
res.json({"ext": {"name": "test", "type": 0, "device":"IO1", "data": "ON"}})
console.log("模式2应答开闸")
}else if (3 == count % 5){
res.json({"ext": {"name": "test", "type": 0, "device":"IO2", "data": "ON"}})
}else if (4 == count % 5){
res.json({"code":100,"state":"open","msg":"条件符合,道闸开启"})
}else if (0 == count %5){
res.json({"ext": {"name": "test", "type": 0, "device": "RS485", "data": "JXU3Q0E0QjhXNTQldUZGMEMldTZCMjIldThGQ0UldTUxNDkldTRFMzQldUZGMDE="}})
console.log("模式2串口透传")
}
}else {
res.json({"status": 200, "totol_fee": 0.000000, "is_paid": true,"display": "一路顺风," + doc.car_plate})
console.log("模式2出口反馈开闸")
}
}
}
}
break;
前端
<el-container :@keyup.enter.native="getVideo()">
<script>
export default {
data() {
return {
dataList: [],
form: {
type: '',
mode: '5',
plate_num: '',
plate_color: '',
plate_val: '',
confidence: '',
car_logo: '',
car_color: '',
vehicle_type: '',
start_time: '',
park_id: '',
cam_id: '',
cam_ip: '',
vdc_type: '',
is_whitelist: '',
triger_type: '',
picture: '',
closeup_pic: '',
},
getVideo(){
this.axios.post('http://localhost:8001/ais',{
})
.then((response) => {
console.log("响应response") // 测试
response.data.dataList
}).catch((error) =>{
console.log(error) // 捕获异常
})
},
},
};
</script>
希望远程,费用可以私信