var User = JSON.parse(localStorage.getItem("_yz_TU"));
// User.avatar = "/images/boy-01.png"
var userId = JSON.parse(localStorage.getItem("_yz_TT")).ylId;
var _Author = JSON.parse(localStorage.getItem("_yz_TT")).ylTok;
var allHis = [];
var chatLi = [];
var allCamera = [];
var nowStr = null;
var nowPage = 1;
var allPage = 1;
var timeId = null;
var timeSNum = 60;
var shipLi = [];
// -------------------------- 聊天设备窗口 ----------------------
function ShowChat(_type) {
if(_type == undefined){
shipLi = ShipData()
};
$.Deferred(function(){});
$.ajax({
url: "https://eims.bds100.com/api/map/peopleGroup?keyword=&traceTime=0",
type: "GET",
traditional: true,
dataType: "json",
async: false,
headers:{
"Authorization":_Author,
"Content-Type": "application/x-www-form-urlencoded"
},
data: {},
error: function(XMLHttpRequest, textStatus, errorThrown) {
loginOut();
},
success: function(res) {
if(res.data.length){
let _list = res.data[0].peopleData;
let _arrs = [], _num = 0;
_list.forEach(item => {
let ind = shipLi.findIndex(sun =>{ return sun.chatId == item.username});
if(ind != -1){
_arrs.push(item);
_num += (item.status == 1?1:0);
}
})
let _len = _num +'/'+ _arrs.length
if(_type == "mainPh"){
allCamera = _arrs;
$("#sbLen").text(_len);
$("#SBLen").text(_len);
$(".carmeBox").html(getSbli(_arrs));
$(".SBli").html(getSbli(_arrs));
}else{
setHtml(_arrs,_len);
}
};
}
})
}
function getSbli(_obj){
let _str = "";
if(_obj.length){
for(var i=0;i<_obj.length;i++){
_str += `
${_obj[i].nickName}
`
}
}else{
_str = '暂无设备
'
}
return _str;
}
function setHtml(_obj,_run){
allCamera = _obj;
var boarddiv =`
${User.nickName}
${User.nickName}
`;
$(".yz_chat").append(boarddiv);
}
$(document).on("click",".chat_room .icon-cha",function(){
$(".chat_room").css({"bottom":"-600px","transition":"bottom 300ms linear"});
})
$(document).on("input","#searchInp",function(){
let _key = $(this).val();
let newArr = [];
let _obj = allCamera;
if(_key == ""){
newArr = _obj;
}else{
for(var i=0;i<_obj.length;i++){
let _str = _obj[i].nickName;
if(_str.indexOf(_key) != -1){
newArr.push(_obj[i]);
}
}
}
$(".SBli").html(getSbli(newArr));
})
$(document).on("click",".SBli li",function(){
let _id = $(this).data("id");
getChatData(allCamera[_id]);
})
$(document).on("click",".chating li",function(){
let _id = $(this).data("id");
getChatData(chatLi[_id]);
})
function getChatData(_arr,_type){
// if(nowStr!=null&&(_arr.id == nowStr.id)){
// return;
// }
$.Deferred(function(){});
$.ajax({
url: "https://eims.bds100.com/api/msg/records?userId="+userId+"&userId="+_arr.id+"&msgId=&oan=0&count=50",
type: "GET",
traditional: true,
dataType: "json",
async: false,
headers:{
"Authorization":_Author,
"Content-Type": "application/x-www-form-urlencoded"
},
data: {},
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
success: function(res) {
if(_type == "mainPh"){
setHtmlPh(res);
}else{
nowStr = _arr;
let _ind = chatLi.findIndex(item =>{return item.id == nowStr.id;});
if(_ind == -1){
chatLi.unshift(nowStr);
}
openRoom(res,_ind);
}
}
})
}
// -------------------------- 聊天设备窗口 ----------------------
// -------------------------- 聊天历史记录 ----------------------
$(document).on("click",".hisRoom .icon-cha",function(){
$(".hisRoom").css({"right":"-400px","transition":"right 300ms linear"});
})
$(document).on("click",".chatRoom .icon-Time",function(){
nowPage = 1;
setHis(function acb(_obj,_page){
if($(".yz_main_m").find(".hisRoom").length){
$("#hisContent").html(getLi(_obj,_page));
$("#hisName").text(nowStr.nickName);
$("#pageTxt").text(1);
$("#allPgage").text(_page);
}else{
let _html = ``
$(".yz_main_m").append(_html);
}
$(".hisRoom").css({"right":"0","transition":"right 300ms linear"});
});
})
function getLi(_obj,_page){
let _str = "";
if(_page == 0){
_str = '暂无聊天记录';
}else{
for(var key in _obj){
_str +=''+key+'';
for(var i=0;i<_obj[key].length;i++){
_str += `
${_obj[key][i].date}
${_obj[key][i].content}
`;
}
}
}
return _str;
}
function setHis(callback){
$.ajax({
url: "https://eims.bds100.com/api/msg/list",
type: "GET",
traditional: true,
dataType: "json",
async: false,
headers:{
"Authorization":_Author,
"Content-Type": "application/json"
},
data: {
blurry: "",
senderId: nowStr.id,
receiverId: userId,
createTime: "",
page: nowPage,
size: 20,
sort: "createTime,desc"
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
success: function(res) {
if(res.content.length){
let _page = Math.ceil(res.totalElements /20);
let _obj = res.content;
let newobj = {};
for(var i=0;i<_obj.length;i++){
let key = _obj[i].date.split(" ")[0];
if(newobj[key] == undefined){
newobj[key] = [_obj[i]];
}else{
newobj[key].push(_obj[i]);
}
}
callback(newobj,_page);
allPage = _page;
}else{
callback([],0);
allPage = 0;
}
}
})
}
$(document).on("click",".hisRoom .iconfont",function(){
let _key=$(this).data("num");
if(parseInt(_key)==-1&&nowPage==1){
ShowLoading("已经是第一页了!",1);
return;
}else if(parseInt(_key)==1&&nowPage==allPage){
ShowLoading("已经是最后一页了!",1);
return;
}else{
nowPage+=parseInt(_key);
}
$("#pageTxt").text(nowPage);
setHis(function acf(_obj,_page){
$("#hisContent").html(getLi(_obj));
});
})
// -------------------------- 聊天历史记录 ----------------------
// -------------------------- 聊天窗口 ----------------------
// 打开聊天窗口
function openRoom(_obj,_id){
let _str = "";
allHis = _obj;
if(_obj.length){
for(var i=0;i<_obj.length;i++){
_str += `
${_obj[i].date}
${_obj[i].content}
`;
}
}
if($(".yz_main_m").find(".chatRoom").length){
$("#Content").html(_str);
$("#receiverName").text(nowStr.nickName);
if(chatLi.length>1){
$(".chating").css("display","block");
$(".chating ul").html(getSbli(chatLi));
$(".chating li").removeClass("act");
$(".chating li").eq(_id == -1?0:_id).addClass("act");
}
}else{
let _html = ``
$(".yz_main_m").append(_html);
}
$(".contFat").animate({
scrollTop: 60 * _obj.length
}, 300);
}
$(document).on("click",".chatRoom .icon-cha",function(){
$(".chatRoom").remove();
chatLi = [];
nowStr = null;
})
$(document).on("click",".chatRoom .bg-red",function(){
$(".chatRoom").remove();
})
// 发送消息
$(document).on("click",".chatRoom .bg-blue",function(){
let _txt = $(".chatRoom textarea").val();
sendContent(_txt);
})
function sendContent(_txt,_type){
if(_txt == ""){
ShowLoading("请先输入要发送的内容!",1);
return;
}
if(timeId != null){
return;
}
$.ajax({
url: "https://eims.bds100.com/api/msg",
type: "POST",
traditional: true,
dataType: "json",
async: false,
headers:{
"Authorization":_Author,
"Content-Type": "application/json"
},
data: JSON.stringify({
content: _txt,
receiverId: nowStr.id,
type: 1
}),
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
success: function(res) {
if(res.date != undefined){
allHis.push(res);
$("textarea").val("");
ShowLoading("发送成功",1);
if(_type == "mainPh"){
setHtmlPh(allHis);
}else{
let _ind = chatLi.findIndex(item =>{return item.id == nowStr.id;});
openRoom(allHis,_ind);
}
if(nowStr.status == 0){
timeId = setInterval(()=>{
if(timeSNum<1){
clearInterval(timeId);
timeSNum = 60;
timeId = null;
$(".chatRoom .bg-blue").text("发送");
}else{
timeSNum --;
$(".chatRoom .bg-blue").text(timeSNum+"s后发送");
}
},1000)
}
}else{
$("textarea").val("");
ShowLoading("发送失败",1);
}
}
})
}
function setHtmlPh(_obj){
allHis = _obj;
let _str = "";
if(_obj.length){
for(var i=0;i<_obj.length;i++){
_str += `
${_obj[i].date}
${_obj[i].content}
`;
}
}
$("#ContTxt").html(_str);
$("#ContTxt").parent().animate({
scrollTop: 55 * _obj.length
}, 300);
}
// -------------------------- 聊天窗口 ----------------------
// -------------------------- 未读通知 ----------------------
function getNoReady(_type) {
$.Deferred(function(){});
$.ajax({
url: "https://eims.bds100.com/api/msg/records?userId="+userId+"&msgId=&read=0&oan=0&count=20",
type: "GET",
traditional: true,
dataType: "json",
async: false,
headers:{
"Authorization":_Author,
"Content-Type": "application/json"
},
data: {},
error: function(data, XMLHttpRequest, textStatus, errorThrown) {
console.log("失败",data, XMLHttpRequest, textStatus, errorThrown)
},
success: function(res) {
if(_type == "main"){
$("#noredy").text(res.length);
let _str = '';
if(res.length){
for(var i=0;i${res[i].senderName}${res[i].content}${res[i].date}`
}
$("#noredy").css("display","block");
}else{
$("#noredy").css("display","none");
_str = '暂无消息'
}
$(".noReadli ul").html(_str);
}else{
$(".footer_yz .drog").css("display",res.length?'block':'none');
}
}
})
}
function sendAlread(){
$.ajax({
url: "https://eims.bds100.com/api/msg/markRead",
type: "PUT",
traditional: true,
dataType: "json",
async: false,
headers:{
"Authorization":_Author,
"Content-Type": "application/json"
},
data: JSON.stringify(
["all"]
),
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
success: function(res) {
getNoReady("main");
$('.noReadli').css('display','none');
}
})
}
// -------------------------- 未读通知 ----------------------
// -------------------------- 绑定手机号 ----------------------
function setPeople(_txt,_arr){
let _str = "";
if(_arr != undefined&&_arr != ""){
let _acb = _arr.split(",");
for(var i=0;i<_acb.length;i++){
if(_acb[i] != ""){
_str += '联系人'+(i+1)+':'+_acb[i]+'
'
}
}
}else{
_str += '暂无联系人
';
}
return _str;
}
var nowPhone = {};
$(document).on("click",".chatRoom .icon-yangshi_icon_tongyong_settings",function(){
nowPhone = {};
nowId = nowStr.username;
getPhone("main",function(_data){
nowPhone = _data;
let _check = _data.isSmsNotification == 0?'':'';
yzkj.open({
title: ["短信通知信息"],
area: ["660px", "auto"],
content: `
平台联系人
${setPeople("plat",_data.platformContact)}
终端联系人
${setPeople("term",_data.terminalContact)}
`,
btn:["提交","取消"],
share:0,
yes:function(){
if(nowPhone.id == undefined){
setPhone("/equipmentPhone/add");
}else{
setPhone("/equipmentPhone",nowPhone.id);
}
},
no:function(){
for(var key in timeObj){
clearInterval(timeObj[key]);
}
timeObj = {
"plat_1":null,
"term_1":null
};
timeNum = {
"plat_1":60,
"term_1":60
};
}
})
});
})
$(document).on("click",".mask .open_title .icon-cha",function(){
$(".mask").remove();
for(var key in timeObj){
clearInterval(timeObj[key]);
}
timeObj = {
"plat_1":null,
"term_1":null
};
timeNum = {
"plat_1":60,
"term_1":60
};
})
$(document).on("click",".mask .icon-shanchu",function(){
let _htm = $(this).parent().find("span");
let _pho = _htm.text().split(":")[1];
let _nam = _htm.attr("name");
let _obj = {};
if(_nam == "plat"){
_obj = {
"id":nowPhone.id,
"platformContact":nowPhone.platformContact.replace(_pho+",","")
}
}else{
_obj = {
"id":nowPhone.id,
"terminalContact":nowPhone.terminalContact.replace(_pho+",","")
}
}
$.ajax({
url: vipUrl +"/equipmentPhone/equipmentPhone",
type: "PUT",
dataType: "JSON",
async: false, //同步请求
data:JSON.stringify(_obj),
contentType: 'application/json; charset=UTF-8',
headers:{
"Authorization":"Bearer "+_Tok
},
error: function(XMLHttpRequest) {
console.log(11111)
},
success:function (res) {
if(res.code == 200){
ShowLoading("删除成功",1);
getPhone("main",function(_data){
$("#hasPhone").html(`
平台联系人
${setPeople("plat",_data.platformContact)}
终端联系人
${setPeople("term",_data.terminalContact)}
`);
})
}else{
ShowLoading("删除失败",1);
}
}
})
})
// -------------------------- 绑定手机号 ----------------------