123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- 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 = ""; //'<div class="time-tips"><span>' + timer + '</span></div>';
- 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 += '<div class="time-tips"><span>' + time + '</span></div>';
- }
- // console.error(contentMsg);
- //type:1.文字 2.图片 3.音频
- var temp = '<dl ' + _class + '><dt style="height: auto; text-align: center;"><a><img src=" ' + doctor_img + '" class="c-images-cycle" /></a><span class="c-f12">' + doctor_name + '</span></dt>';
- switch(typeMsg) {
- case 1:
- temp += '<dd class="word-bread"><span>' + contentMsg + '</span></dd></dl>';
- break;
- case 2:
- temp += '<dd class="word-bread"><img width="100" height="100" src=" ' + contentMsg + '" /></dd></dl>';
- break;
- case 3:
- temp += '<dd class="word-bread audio" data-audio=" ' + contentMsg + '" onclick="playRecord(this)"><img src="../images/more-tb3.png" alt="" /></dd></dl>';
- 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);
- });
|