var winHeight=window.innerHeight;//窗体高度
Date.prototype.format = function(format) {
var date = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S+": this.getMilliseconds()
};
if(/(y+)/i.test(format)) {
format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
}
for(var k in date) {
if(new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
date[k] : ("00" + date[k]).substr(("" + date[k]).length));
}
}
return format;
}
var pageIndex = 1;
var docPhoto, docName;
var otherCode, otherName, otherPhoto, otherSex;
/**
* 初始化
*/
mui.plusReady(function() {
$("#contain").height(winHeight-45-58)//设定聊天模块度
document.addEventListener("resume", function() {
getMsgList(1, 200,true);
}, false);
var self = plus.webview.currentWebview();
otherCode = self.otherCode;
otherName = self.otherName;
otherSex = self.otherSex;
otherPhoto = self.otherPhoto || (otherSex==1 ? "../../../images/d-male.png" : "../../../images/d-female.png");
$('#title').html("名医咨询-"+otherName);
/*
* 医生缓存信息 姓名和头像
*/
var infoStr = plus.storage.getItem("docInfo");
if(infoStr) {
var info = JSON.parse(infoStr);
docName = info.name;
docPhoto = info.photo || (info.sex==1 ? "../../../images/d-male.png" : "../../../images/d-female.png");
}
back2home();
getMsgList(1, 200, true);
});
/**
* 点击结束咨询
*/
$('#close_zixun_btn').on('tap',function() {
mui.back();
});
/**
* 点击查看好友详细信息
*/
$('#qun_detail').on("tap", function(){
mui.openWindow('../../mine/html/zuyuanziliao.html', 'zuyuanziliao', {
extras: {
docCode: otherCode
}
});
})
/*
* 回复消息
*/
function clickReply() {
var text = $("#input_content").val().trim();
$("#input_content").focus()
if(text.length == 0) {
mui.toast("发送内容不能为空,请重新输入");
return;
}
uploadMsg(1, text);
$('#contain').animate({scrollTop:1000000});
}
/**
* 获取当前用户code
*/
function getUserId() {
var userInfo = plus.storage.getItem("userAgent");
if(typeof(userInfo) == "undefind" || userInfo == null) {
mui.toast("登录信息过时,请重新登录!");
return;
}
var userJson = $.parseJSON(userInfo);
return userJson.uid;
}
/**
* 获取消息
*/
function getMsgList(pageIndex, pageSize,isClear) {
var start = 0;
if(pageIndex > 0) {
start = (pageIndex - 1) * pageSize;
};
count = pageSize;
im.getP2pMsg(getUserId(), otherCode, start, count, function(data) {
showMsg(data,isClear);
$('#contain').animate({scrollTop:1000000});
// 刷新数字角标
// var userId = plus.storage.getItem('im_userid');
// im.getBadgeNumber(userId, function(result) {
// console.log(JSON.stringify(result));
// try {
// if (result && result.badge) {
// plus.runtime.setBadgeNumber(result.badge);
// }
// } catch (e) {
//
// }
// });
});
}
/**
* 发送消息
*/
function uploadMsg(type, content) {
im.sendP2p(getUserId(), otherCode, content, type, function(data) {
if(data.errno == 0) {
getMsgList(1, 1,false);
} else {
mui.toast("消息发送失败");
}
})
}
/**
* 显示消息
*/
function showMsg(data,isClear) {
if(data == null || data.list == null) {
return;
}
var html = ""; //'
' + timer + '
';
var preTime = 0;
var msgList = data.list;
var typeMsg = 1;
for(var i = msgList.length - 1; i >= 0; i--) {
var doctor_img = "../../../images/d-default.png";
var doctor_name = "";
if(msgList[i].from_uid == otherCode){
doctor_img = otherPhoto;
doctor_name = otherName;
}
else{
doctor_img = docPhoto;
doctor_name = docName;
}
var contentMsg = "";
if(msgList[i].content != null && msgList[i].content != "") {
typeMsg = msgList[i].type;
contentMsg = msgList[i].content;
}
var _class = 'class="chat-right"';
if(getUserId() != msgList[i].from_uid) {
_class = 'class="chat-left"';
}
if(msgList[i].timestamp - preTime > 60 * 1000) {
preTime = msgList[i].timestamp;
var date = new Date();
date.setTime(msgList[i].timestamp);
var time = date.format('yyyy-MM-dd h:m:s');
html += '' + time + '
';
}
// console.error(contentMsg);
//type:1.文字 2.图片 3.音频
var temp = '- ' + doctor_name + '
';
switch(typeMsg) {
case 1:
temp += '- ' + contentMsg + '
';
break;
case 2:
temp += '';
break;
case 3:
temp += '';
break;
}
html += temp;
}
$("#input_content").val("");
if(typeMsg != null && typeMsg != 1) {
$(".tw-add-detail").hide(500);
}
if(isClear){
$("#contain").html(html);
}else{
$("#contain").append(html);
}
window.scrollTo(0, $("#contain").height());
// scaleRefresh("#contain dd");
}
/*
* 图片缩放
*/
function scaleRefresh(dom) {
ImagesZoom.init({
"elem": dom,
"delBack": function(index) {
}
});
}
/*
* 附加功能
*/
$(".tw-add").click(function() {
$(this).find("img").toggle();
$(".tw-add-detail").toggle(500);
var inputHeight = $("#input_div").height();
});
/*
* 发送照片
*/
function clickGallery() {
plus.gallery.pick(function(path) {
var lastIdx = path.lastIndexOf("/"),
imgName = path;
if(lastIdx>-1) {
imgName = path.slice(lastIdx+1);
}
plus.zip.compressImage({
src: path,
dst: "_doc/chat/gallery/" + imgName,
quality: 20,
overwrite: true
}, function(e) {
var task = plus.uploader.createUpload(server + "upload/chat", {
method: "post"
}, function(t, sta) {
console.log(JSON.stringify(t))
if(sta == 200) {
var msg = t.responseText;
var oImg = JSON.parse(msg);
var imgUrl = oImg.urls;
var re = new RegExp("\\\\", "g");
imgUrl = imgUrl.replace(re, "/");
uploadMsg(2, imgUrl);
}
});
task.addFile(e.target, {});
task.start();
}, function(err) {
console.error("压缩失败:" + err.message);
});
}, function(err) {});
};
/*
* 拍照
*/
function clickCamera() {
var cmr = plus.camera.getCamera();
var res = cmr.supportedImageResolutions[0];
var fmt = cmr.supportedImageFormats[0];
cmr.captureImage(function(path) {
// plus.io.resolveLocalFileSystemURL(path, function(entry) {
plus.io.resolveLocalFileSystemURL(path, function(entry) {
var localUrl = entry.toLocalURL();
var lastIdx = localUrl.lastIndexOf("/"),
imgName = localUrl;
if(lastIdx>-1) {
imgName = localUrl.slice(lastIdx+1);
}
plus.zip.compressImage({
src: localUrl,
dst: "_doc/chat/camera/" + imgName,
quality: 20,
overwrite: true
}, function(e) {
var task = plus.uploader.createUpload(server + "upload/chat", {
method: "post"
}, function(t, sta) {
if(sta == 200) {
var msg = t.responseText;
var oImg = JSON.parse(msg);
var imgUrl = oImg.urls;
var re = new RegExp("\\\\", "g");
imgUrl = imgUrl.replace(re, "/");
console.log(imgUrl);
uploadMsg(2, imgUrl);
}
});
task.addFile(e.target, {});
task.start();
}, function(err) {
console.log("压缩失败: " + err.message);
});
});
}, function(err) {
if(err.code==11 && err.message == "null") {
plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。");
}
console.error("拍照失败:" + err.message);
}, {
index: 1
});
};
/**
* 更新信息事件
*/
window.addEventListener("update", function() {
getMsgList(1, 200, true);
});
template.helper("setPhoto", function(p) {
return getImgUrl(p);
});