// 基本信息(包括userAgent) var baseInfo = null, // 基础环境信息(包括当前webview、encry相关) baseEnv = null; // 咨询医生 var $yishengInput = $("#zixun_yisheng").find("input"), // 咨询问题 $wentiInput = $("#goutong_info").find("textarea"), // 添加附件图标按钮 $imgArea = $('.add-img'), // 提交咨询 $sumit = $('#submit_btn'); var self; var docInfo; var imgIndex, imgs, imgUrls; // 关闭重新登录检查(防止一个ajax error导致所有请求成功无法回调) var checkReload = false; //保存患者咨询的内容 var consultLog = {}; // 获取登录相关信息 var getBaseInfo = function() { docInfo = JSON.parse(plus.storage.getItem("docInfo")); // 登录的相关信息 var userAgent = JSON.parse(plus.storage.getItem("userAgent")); return { userAgent: userAgent, accessData: $.extend({},baseEnv.webview.accessData) } }, // 获取基础环境信息 getBaseEnvPromise = function () { var env = { webview: plus.webview.currentWebview() }; // 因为有异步请求,需要等待数据返回之后进行处理,所以返回Promise return Promise.resolve().then(function(res) { return env; }); }, // 获取表单数据 getInputs = function() { var data = { // docName: $yishengInput.val().trim(), // 姓名 question: $wentiInput.val().trim(), images: null// 附件 }; return data; }, // 必输验证 validRequired = function(data) { var fieldsStr = "question", fieldMap = { question: "沟通情况" }, fields = fieldsStr.split(","), invalidField; // 某个字段为""、null、undefined,则该字段不能通过必输校验 invalidField = _.find(fields,function(key) { return !data[key]&&data[key]!==0; }); if(invalidField) { mui.toast(fieldMap[invalidField]+'不能为空'); return false; } else { return true; } }, // 所有输入验证 validInputsPromise = function() { // 表单数据 var data = getInputs(); return Promise.resolve() .then(function() { if(!(self.type == 1 || self.aa == 3)){ //健管求助全科时,求助内容非必填 if(!validRequired(data)) throw new Error("必输校验失败"); } }) .then(function() { return data; }); }, // 图片缩放 scaleRefresh = function (dom) { ImagesZoom.init({ "elem": dom, "delBack": function(index) { $(".pic-count").text($(".pic-count").text() - 1); //删除图片后的回调 $(dom).find("li").eq(index).remove(); } }); }, //获取压缩图片路径 uploadCompressImg = function (cb) { imgIndex = 0; imgs = document.querySelectorAll(".add-img li img"); if(imgs.length > 0) { compressImg(cb); } else { cb(); } }, upImg = function(imgUrl, cb) { 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, "/"); self.type==1? imgUrls.push({content: imgUrl, type: 2}) : imgUrls.push(imgUrl); cb(); } else { mui.toast("上传图片失败!"); } }); task.addFile(imgUrl, {}); task.start(); }, //压缩图片 compressImg = function (cb) { if(imgIndex >= imgs.length){ if(cb) cb(); return; } var path, prepath = "_doc/press_img/"; path = imgs[imgIndex].getAttribute("src"); imgIndex++; var aImg = path.split("/"); var imgName = aImg[aImg.length - 1]; plus.zip.compressImage({ src: path, dst: prepath + imgName, quality: 20, overwrite: true }, function(succ) { var url = succ.target; var size = succ.size; var width = succ.width; var height = succ.height; upImg(url, function(){ compressImg(cb); }); }, function(err) { console.error("压缩失败:" + err.message); if(err.message == "文件不存在") { mui.toast(err.message); plus.nativeUI.closeWaiting(); plus.webview.currentWebview().reload(); } }); }, // 上传并获取图片url地址(多张图片以“,”分割) getImagesPromise = function() { return new Promise(function(resolve, reject) { var uploadImgUrl = getCompressImg(); if(uploadImgUrl) { resolve({urls:uploadImgUrl}); } else { // 没有附件 resolve({urls: null}) } }); }, //获取患者咨询的内容 getConsultLog = function(){ sendPost("doctor/consult/getConsultLog", { consultCode:self.consultInfo.consult }, null, function(res){ consultLog = res.data; }); }, // 发送消息 sendMsg = function( data){ Promise.all(_.map(imgUrls,function(msg){ return new Promise(function(resolve, reject) { im.sendGroupMsg(docInfo.code, data.code, msg.content, msg.type,function() { resolve(true); }, 2, function() { resolve(true); }) }) })).then(function() { plus.nativeUI.closeWaiting(); //跳转求助im var zhidingzixun = plus.webview.getWebviewById("zhidingzixun"); if(zhidingzixun) mui.fire(zhidingzixun, 'refresh') mui.openWindow({ url:"../../message/html/qiuzhuqunliao.html", id: "qiuzhuqunliao"+ new Date().getTime(), extras: { groupCode: data.code, groupName: data.members[0].groupName, openId: self.openId } }) }).catch(function(){ plus.nativeUI.closeWaiting(); }) }, closeOpener = function(wv){ if(wv){ if(self.openId == wv.id){ wv.close(); } else { closeOpener(wv.opener()); wv.close(); } } }, bindEvents = function() { // 添加附件 $imgArea.on('tap', '.add', function() { showActionSheet($imgArea[0], this); }); // 提交咨询 $sumit.on('tap', function() { plus.nativeUI.showWaiting(); validInputsPromise().then(function(data) { return data; }).then(function(data) { //type 1咨询 2求助 if(self.type == 1){ var parms = { type: self.type, talkDoctor: self.doctor, talkDoctorName: self.doctorName, doctorType: self.doctorType, consult: self.consultInfo.consult, patient: self.consultInfo.patient, patientName: self.consultInfo.name, patientInclude: self.type==1? 1: "", name: self.consultInfo.name + "咨询(" + new Date().format('MM-dd')+")" } sendPost("/doctor/talkgroup/create", parms, null, function(res1){ if(res1.status == 200){ //将患者的咨询信息也发布到讨论组里,类型值为8 var c = 0; Promise.all(_.map(consultLog,function(log){ //解决将健管的回复的内容也发布到讨论组中 if(log.type != 1){ //咨询的信息,文本类型值为8,图片类型值为9 var type = 8; if(log.chatType == 2){ type = 9; } c = c+1; return new Promise(function(resolve, reject) { im.sendGroupMsg(self.consultInfo.patient, res1.data.code, log.content, type,function() { console.log(c); resolve(true); }, 2, function() { resolve(true); }) }); } })).then(function() { im.sendPrivateMsg( docInfo.code, self.consultInfo.patient, docInfo.name+"已邀请"+ self.doctorName +"参与咨询:"+res1.data.code, 5, function(res){ imgUrls = []; if(data.question){ imgUrls.push({content: "沟通情况:"+data.question, type: 6}); } uploadCompressImg(function() { sendMsg(res1.data); }); }, function(res){ plus.nativeUI.closeWaiting(); mui.toast("发送求助信息失败!"); throw new Error("发送求助信息失败!"+ JSON.stringify(res)); }) }); } else { plus.nativeUI.closeWaiting(); mui.toast("求助失败!"); throw new Error("求助失败!"+ JSON.stringify(res1)); } }) } else { imgUrls = []; var isSwitch = $('#switch').hasClass('mui-active'); uploadCompressImg(function() { sendPost("/doctor/consult/seekHelp", { symptoms: data.question, isSend: isSwitch? 1 : 0, doctorCode: self.doctor, images: imgUrls? imgUrls.join(",") : "", oldConsultCode: self.consultInfo.consult }, null, function(res){ if(res.status == 200){ mui.toast("求助成功!"); //跳转求助im var zhidingzixun = plus.webview.getWebviewById("zhidingzixun"); if(zhidingzixun) mui.fire(zhidingzixun, 'refresh') var wv = mui.openWindow({ url:"../../message/html/p2p.html", id: "p2p", extras: { otherCode: self.doctor, otherName: self.doctorName, otherSex: self.doctorSex, otherPhoto: self.doctorPhoto, from: "faqiqiuzhu" } }) mui.later(function(){ closeOpener(self); }, 1000); wv.show(); } else { plus.nativeUI.closeWaiting(); mui.toast("求助失败!"); throw new Error("求助失败!"+ JSON.stringify(res)); } }) }); } }).catch(function(res){ console.error("err:"+JSON.stringify(res)) plus.nativeUI.closeWaiting(); }) }); // 删除图片 mui(".add-img").on("tap", ".icon-del", function() { var oli = this.parentElement; var oul = this.parentElement.parentElement; oul.removeChild(oli); }); }; // 页面业务处理流程开始 new Promise(function(resolve, reject) { mui.init({ beforeback: function() { if($('.imgzoom-pack').css("display")!=="none"){ $('.imgzoom-x').trigger('click'); return false; } } }); mui.plusReady(function() { // plus已经准备好,可以往下执行 resolve(true); }); }).then(function() { // TODO 防止因为其它ajax error导致存在isLoginOut标识,所有请求回调无法执行的问题 window.localStorage.removeItem("isLoginOut"); // 获取基础环境信息 return getBaseEnvPromise().then(function(env) { baseEnv = env; }).then(function() { self = plus.webview.currentWebview(); if(self.type == 2){ $('#oldConsultCheck').show(); } // 获取登录医生信息 baseInfo = getBaseInfo(); // $yishengInput.val(baseInfo.accessData.name||""); $('#zixun_wenti textarea').val(self.consultInfo.title); if(self.type == 1 || self.aa == 3){ $("#notice").show(); //获得患者咨询的内容 getConsultLog(); } //图片缩放 scaleRefresh(".upload-img"); // 绑定页面事件 bindEvents(); }) }).catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); });