import { layuiObj } from './index.js';
import { ajax } from '../api/ajax.js';
import { movedeviceurl,stationurl } from '../../../public/js/url.js';
import { timestampFormatTime } from '../../../public/js/timestamp.js';
var notifyIndex=null;
var notifyTemplate={
"holiday":'节日期间,人流密集,请各单位组织排查火灾情况,加强巡逻和值班工作。',
"hightemp":'高温天气预,请各单位组织排查火灾情况,并做好防暑工作。',
"lightningstorm":'雷电暴雨天气预,请各单位排查建筑物,谨防坠物伤人。'
};
var notifyDeviceArr=null;
function popNotify(deviceArr){
notifyDeviceArr=JSON.parse(JSON.stringify(deviceArr));
if(notifyIndex==null){
var notifyType="moveDevice";
if(notifyDeviceArr.length>1){
var title=`通知群发[${notifyDeviceArr.length}]`;
}else{
var title=notifyDeviceArr[0]["showname"];
}
notifyIndex=layuiObj["layer"].open({
type: 1,
title: false, //不显示标题栏
closeBtn: false,
area: ['446px', '430px'],
shade: 0.4,
id: new Date().getTime(), //设定一个id,防止重复弹出
btnAlign: 'c',
move:'.layui-move',
moveType: 1, //拖拽模式,0或者1
content:`
通知发送
`,
success: function(layero){
sendnotifyEvent();
}
});
}
layuiObj.layuiForm.render("select","sendnotifyForm");
layuiObj.layuiForm.render("checkbox","sendnotifyForm");
layuiObj.layuiForm.on('select(notifyType)', function(data){
var value=data["value"];
value=notifyTemplate[value];
$("#notifyContent").val(value);
layuiObj.layuiForm.render("checkbox","sendnotifyForm");
})
//关闭
$(".notifyTitle>img").click(function(){
layuiObj["layer"].close(notifyIndex);
notifyIndex=null;
})
}
function getnotifyContent(){
var title=$("#notifyTitle").val();
var type=$("#notifyType").find("option:selected").text();
var content=$("#notifyContent").val();
var timestamp=(new Date().getTime());
var pts= (new Date()).valueOf();
var obj={};
obj["title"]=title;
obj["content"]=content;
obj["type"]=type;
obj["time"]=timestampFormatTime(timestamp),
obj["timestamp"]=timestamp.toString()
obj["pts"]=pts
return obj;
}
function sendnotifyEvent(){
//发送通知
$("#NotifySubmit").off('click');
$("#NotifySubmit").on('click',function(){
var notifyInfo=getnotifyContent();
var notifySendALL=[];
notifyDeviceArr.map((item)=>{
notifySendALL.push(sendNotifyReq(notifyInfo,item));
notifySendALL.push(sendNotifyMqttReq(notifyInfo,item));
})
Promise.all(notifySendALL).then((res) => {
layuiObj["layer"].close(notifyIndex);
notifyIndex=null;
layuiObj["layer"].msg("发送成功");
});
})
}
function sendNotifyReq(notifyInfo,item){
var nUrl=movedeviceurl+"/mdev/notice/send";
var unameInfo=JSON.parse(sessionStorage.getItem("unameInfoStr"));//获取用户登录信息
var deviceid=item.deviceid;
var obj={
content:notifyInfo["content"],
noticeid:notifyInfo["timestamp"],
receiver:deviceid,
sender:unameInfo["user_name"],
sendtype:"toclient",
title:notifyInfo["title"]
}
return ajax(nUrl,obj);
}
function sendNotifyMqttReq(notifyInfo,item){
var unameInfo=JSON.parse(sessionStorage.getItem("unameInfoStr"));//获取用户登录信息
var deviceid=item.deviceid;
var showname=item.showname;
var station=item.station;
var messageData={
"title":"notify",
"sender":unameInfo.user_name,
"department":"",
"scope":"",
"receiver":[deviceid],
"video":"",
"rtm":notifyInfo["timestamp"],
"data":{
"number":notifyInfo["timestamp"],
"title":notifyInfo["title"],
"content":notifyInfo["content"],
"sender":unameInfo["user_name"],
"time":notifyInfo["time"],
"pts":notifyInfo["timestamp"],
"receiver":"alone",
"alias":showname,
"cid":"",
"noticeid":notifyInfo["timestamp"]
},
"userScope":"",
"userBragide":"",
"userDetachchment":"",
"userBorough":"",
"name":"web"
}
var obj={
// topic:station+"/ter_cmd",
topic:deviceid+"_rtmtranfer",
message:JSON.stringify(messageData)
}
if(stationurl=="http://43.15.198.61:8083"){
var nUrl=stationurl+"/firectrl/client/notice/sendudp";
}else{
var nUrl=stationurl+"/firectrl/client/notice/sendmqtt";
}
return axios({
method:'post',
url:nUrl,
data:obj
})
}
export {popNotify}