// 基本信息(包括userAgent) var baseInfo = null, // 基础环境信息(包括当前webview、encry相关) baseEnv = null; // 咨询医生 var $yishengInput = $("#zixun_yisheng").find("input"), // 咨询问题 $wentiInput = $("#zixun_wenti").find("textarea"), // 添加附件图标按钮 $imgArea = $('.add-img'), // 提交咨询 $sumit = $('#sumit'); // 关闭重新登录检查(防止一个ajax error导致所有请求成功无法回调) var checkReload = false; // 获取登录相关信息 var getBaseInfo = function() { // 登录的相关信息 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(!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) { var imgs = document.querySelectorAll(".add-img li img"); if(imgs.length > 0) { plus.nativeUI.showWaiting(); var path, prepath = "_doc/press_img/"; for(var i = 0; i < imgs.length; i++) { path = imgs[i].getAttribute("src"); var aImg = path.split("/"); var imgName = aImg[aImg.length - 1]; compressImg(path, prepath + imgName, cb); } return imgs.length; } return 0; }, //压缩图片 compressImg = function (path, dstpath, cb) { plus.zip.compressImage({ src: path, dst: dstpath, quality: 20, overwrite: true }, function(succ) { var url = succ.target; var size = succ.size; var width = succ.width; var height = succ.height; if(cb && $.isFunction(cb)) { cb(url); } }, 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}) } }); }, toP2pPage = function(sessionId) { mui.openWindow({ url: "../../message/html/p2p.html", id:"p2p", // 跳转页面传参 extras: { sessionId: sessionId, sessionName: baseInfo.accessData.name } }); }, bindEvents = function() { // 添加附件 $imgArea.on('tap', '.add', function() { showActionSheet($imgArea[0], this); }); // 提交咨询 $sumit.on('tap', function() { plus.nativeUI.showWaiting(); var url = server+"/doctor/work_scheduling/is_doctor_working"; var data={doctor:baseInfo.accessData.doctor}; getReqPromise(url,data).then(function(res) { if(res.status == 200) { return res.data; } else { mui.toast(res.msg); return null; } }).then(function(data) { var status = data.status; if(status) {//0:医生不接受咨询 1:医生接受咨询 2:医生当前不在工作时间 if(status=="0"){ plus.nativeUI.closeWaiting(); dialog({ content: '对不起,该医生已暂停接受咨询,您可选择其他医生进行咨询', cancelValue: '我知道了', cancel: function () { return; } }).showModal(); return false; }else if(status=="1"){ validInputsPromise().then(function(data) { return data; }).then(function(data) { var msgSended = false; var images = []; var imageNum = uploadCompressImg(function(imgUrl) { 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, "/"); images.push(imgUrl); } }); task.addFile(imgUrl, {}); task.start(); }); if(imageNum) { var count = 1000; var timer = window.setInterval(function() { count--; if(count==0 || imageNum == images.length) { window.clearInterval(timer); timer = null; var param = {doctorCode: baseInfo.accessData.doctor, images:images.join(",")} if(data.question) { param.symptoms = data.question } getReqPromise("doctor/consult/famousAdd",param) .then(function(res) { plus.nativeUI.closeWaiting(); if(res.status == 200) { toP2pPage(res.data.session_id); } else { console && console.error(res.msg); } }); } },500) } else { data.question && getReqPromise("doctor/consult/famousAdd",{doctorCode: baseInfo.accessData.doctor, symptoms: data.question}) .then(function(res) { plus.nativeUI.closeWaiting(); if(res.status == 200) { toP2pPage(res.data.session_id) } else { console && console.error(res.msg); } }); } }).catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); }); }else if(data=="2"){ plus.nativeUI.closeWaiting(); dialog({ content: '医生不在工作时间内哦,请在医生咨询计划时间段内咨询', cancelValue: '我知道了', cancel: function () { return; } }).showModal(); } } }).catch(function(e) { console && console.error(e); }); }); // 删除图片 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() { // 获取登录医生信息 baseInfo = getBaseInfo(); $yishengInput.val(baseInfo.accessData.name||""); //图片缩放 scaleRefresh(".upload-img"); // 绑定页面事件 bindEvents(); }) }).catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); });