// 登录者相关信息(包括userAgent) var loginerInfo = null, // 基础环境信息(包括当前webview、encry相关) baseEnv = null; var self; // 专科医生li容器 var $doctorZK = $('#doctor_zk'), // 慢病类别li容器 $disease = $("#disease"), // 添加附件图标按钮 $imgArea = $('.add-img'), // 扫描医保卡自动识别居民信息提示栏 $scanSSCInfo = $('#scan_ssc_info'), // 重新扫描医保卡提示栏 $rescanSSCInfo = $('#rescan_ssc_info'), // 下一步按钮 $nextStep = $('#next_step'), // 表单的ul容器 $infoList = $("#info_list"), // 姓名 $nameInput = $("#name").find("input"), // 身份证 $idcardInput = $("#idcard").find("input"), // 医保卡号 $sscInput = $("#ssc").find("input"), // 医疗保险号 $medicareNumberInput = $("#medicareNumber").find("input"), // 手机号码 $mobileInput = $("#mobile").find("input"), //居委会 $committee = $("#selectType").find("input") // 应急人联系电话 $emerMoblieInput = $("#emerMoblie").find("input"); // 关闭重新登录检查(防止一个ajax error导致所有请求成功无法回调) var checkReload = false; // 获取登录相关信息 var getLoginerInfo = function() { // 登录的相关信息 var userAgent = JSON.parse(plus.storage.getItem("userAgent")), // 登录的医生类别 docType = plus.storage.getItem("docType"), // 登录的医生信息 docInfo= JSON.parse(plus.storage.getItem("docInfo")), // 该医生(登录者)所在社区(机构)编码 hospital = plus.storage.getItem("hospital"); return { userAgent: userAgent, docInfo: $.extend(docInfo,{docType:docType},{hospital: hospital}) } }, // 获取基础环境信息 getBaseEnvPromise = function () { var env = { webview: plus.webview.currentWebview(), encryURL: "login/public_key", encryKey: "" }; plus.nativeUI.showWaiting(); // 因为有异步请求,需要等待数据返回之后进行处理,所以返回Promise return getReqPromise(env.encryURL,null).then(function(res) { plus.nativeUI.closeWaiting(); if(res.status == 200) { var mod = res.data.modulus; var exp = res.data.exponent; // key = RSAUtils.getKeyPair(exp, "", mod); env.encryKey = RSAUtils.getKeyPair(exp, "", mod); return env; } else { mui.toast(res.msg); } }).catch(function(e) { plus.nativeUI.closeWaiting(); }); }, // 根据身份证获取医保卡号 getSSCNoByIdcard = function(idcard) { var checkUrl = "doctor/patient_cardno"; getReqPromise(checkUrl,{idcard:idcard}).then(function(res) { if(res.status=='200'){ $sscInput.val(res.data); if(res.data){ $sscInput.attr("readonly","readonly"); } else { $sscInput.removeAttr("readonly"); } } else { mui.toast(res.msg); } }); }, // 图片缩放 scaleRefresh = function (dom) { ImagesZoom.init({ "elem": dom, "delBack": function(index) { $(".pic-count").text($(".pic-count").text() - 1); //删除图片后的回调 $(dom).find("li").eq(index).remove(); } }); }, // 上传并获取图片url地址(多张图片以“,”分割) getImagesPromise = function() { return new Promise(function(resolve, reject) { if(plus.networkinfo.getCurrentType() == plus.networkinfo.CONNECTION_NONE) { mui.toast("您的网络有点问题哦,请检查网络无误后重试~"); throw new Error("无网络连接"); } var uploadImgUrl = getCompressImg(); if(uploadImgUrl) { setTimeout(function() { uploadImg(uploadImgUrl, function(uploadObj) { var res = JSON.parse(uploadObj.responseText); resolve(res); }); }, 1000); } else { // 没有附件 resolve({urls: null}) } }); }, // 自动识别医保卡图片 autoRecSSCImagePromise = function(imgPath) { // var ocrReqUrl = "http://192.168.131.109:3001/ocr"; //recUrl = "http://192.168.131.132:3000/ocr/result?credit="; console.log(ocrReqUrl); return new Promise(function(resolve, reject) { var task = plus.uploader.createUpload(ocrReqUrl, { method: "post", timeout: 20, retry: 0 }, function( t , status) { if (status == 200) { //fileHttpUrl = JSON.parse(t.responseText).urls; //resolve(JSON.parse(t.responseText)); resolve(JSON.parse(t.responseText)); } else { if(plus.networkinfo.getCurrentType() != plus.networkinfo.CONNECTION_NONE) { mui.confirm("对不起,识别失败,请确认图片是否符合以下要求:\n ①医保卡边框需在照片内;\n ②医保卡边框未损坏;\n ③图片清晰。", "提示", ["手工录入", "重新扫描"], function(e) { if(e.index == 1) { $scanSSCInfo.trigger("tap"); } }) } else { mui.toast("您的网络有点问题哦,请检查网络无误后重试~"); } resolve(false); } }); task.addFile(imgPath, {key:"file"}) task.start(); }) .then(function(data) { if (data===false) { return data; } // 医保卡图片识别数据 if(data) { $nameInput.val(data["姓名"]); $idcardInput.val(data["身份证"]); $sscInput.val(data["卡号"]); } if( !$nameInput.val().trim() || !validName($nameInput.val(),false) || !$idcardInput.val().trim() || !validIdCard($idcardInput.val(),false) || !$sscInput.val().trim() || !validSSC($sscInput.val(),false) ) { mui.confirm("对不起,识别信息不完整,请确认图片是否符合以下要求:\n ①医保卡边框需在照片内;\n ②医保卡边框未损坏;\n ③图片清晰。", "", ["手工录入", "重新扫描"], function(e) { if(e.index == 1) { $scanSSCInfo.trigger("tap"); } }) } return data; }); }, // 获取表单数据 getInputs = function() { var data = { name: $nameInput.val().trim(), // 姓名 unencIdcard: $idcardInput.val().trim(), // 未加密的身份证 ssc: $sscInput.val().trim().toUpperCase(), // 医保卡号 medicareNumber: $medicareNumberInput.val().trim(), // 医疗保险号 mobile: $mobileInput.val().trim(), // 手机号码 emerMoblie: $emerMoblieInput.val().trim(), // 应急人联系电话 countryCode:$committee.attr('data-code'),//居委会 images: null// 附件 }; // 加密后的身份证 data.idcard = RSAUtils.encryStr(baseEnv.encryKey, data.unencIdcard); return data; }, // 必输验证 validRequired = function(data) { var fieldsStr = "name,idcard,ssc,countryCode,medicareNumber", fieldMap = { name: "姓名",idcard:"身份证",ssc:"医保卡号",countryCode:"居委会",medicareNumber: "医疗保险号" }, 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; } }, // 校验姓名格式 validName = function(idcard,isshow) { var nameReg = /^[\u4E00-\u9FA5]{2,}$/; if(!idcard) return ; if(!nameReg.test(idcard)) { $nameInput.css("color","red"); if(isshow!=false){ mui.toast("请填写有效姓名"); } return false; } $nameInput.css("color",""); return true; }, // 校验身份证号格式 validIdCard = function(idcard,isshow) { var idCardReg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/; if(!idcard) return ; if(idcard.length == 15) { idCardReg = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$/; } if(!idCardReg.test(idcard)) { $idcardInput.css("color","red") if(isshow!=false){ mui.toast("请填写有效身份证号"); } return false; } $idcardInput.css("color",""); return true; }, // 校验医保卡号格式 validSSC = function(ssc,isshow) { //var sscReg = /^[a-zA-Z0-9]+$/; var sscReg = /^([a-zA-Z]{1}[a-zA-Z0-9]{8}|[0-9]{12})$/; if(!ssc) return ; if(!sscReg.test(ssc)) { $sscInput.css("color","red") if(isshow!=false){ mui.toast("请填写有效的医保卡卡号"); } return false; } $sscInput.css("color",""); return true; }, // 校验手机号格式 validMobile = function(mobile) { var mobileReg = /^[1][3578][0-9]{9}$/; if(!mobile) return ; if(!mobileReg.test(mobile)) { $mobileInput.css("color","red"); mui.toast("请填写有效的手机号码"); return false; } $mobileInput.css("color",""); return true; }, // 校验联系人电话 validEmerMobile = function(mobile) { var mobileReg = /^[1][3578][0-9]{9}$/; if(!mobile) return ; if(!mobileReg.test(mobile)) { $emerMoblieInput.css("color","red"); mui.toast("请填写有效的联系人电话"); return false; } $emerMoblieInput.css("color",""); return true; }, // 输入格式验证 validFormat = function(data) { return validName(data.name) &&validIdCard(data.unencIdcard) && validSSC(data.ssc) && (!data.mobile || validMobile(data.mobile)) && (!data.emerMoblie || validEmerMobile(data.emerMoblie)); }, // 检查姓名、身份证号、医保卡号是否一致 checkInfoConsistency = function(infoConsistencyRes) { if(infoConsistencyRes.status == 200) { return true; } else { mui.toast(infoConsistencyRes.msg); return false; } }, // 判断其是否已注册 checkRegistered = function(res) { // 当signStatus=1或3时才有这个字段,不为空表示这个身份证患者已注册 var isRegistered = res.data.patient; if(res.status == 200 ) { if(isRegistered && isRegistered!==undefined) { return true; } return false; } else { mui.toast(res.msg); return false; } }, //// 检查手机号是否已签约 //checkMobileSignedStatus = function(checkRes) { // if(checkRes.status==200) { // if(checkRes.data.islive == "false") { // dialog({ // content: "该手机号已签约!", // ok: function() { // $mobileInput.val(""); // } // }).showModal(); // return false; // } else { // return true; // } // } else { // mui.toast(checkRes.msg); // // $mobileInput.val(""); // return false; // } //}, // 手机号是否未被注册使用 checkMobileIsNotUsed = function (res) { // if(res.status==200) { // return true; // } // mui.toast("该手机号已被注册!"); return true; }, // 检查是否已签约家庭医生 checkSignedFamilyDoctor = function(res) { if(res.status == 200) { // signStatus: 0-->病人未注册或三师跟家庭签约都没有,1-->有三师签约,2-->有签约家庭医生 if(res.data.signStatus==2){ mui.toast("病人已申请过家庭签约!"); return false; } else { return true; } } else { mui.toast(res.msg); return false; } }, // 检查是否签约三师 checkSignedSanshi = function(signedData) { // signStatus: 0-->病人未注册或三师跟家庭签约都没有,1-->有三师签约,2-->有签约家庭医生 var signStatus = signedData.signStatus; return signStatus==1; }, // 检查三师签约中全科医生是否在当前医生所在社区 checkSanshiComnunity = function(loginerData,signedData) { // 三师中全科医生所在社区主编码(截取前8位,后两位是服务站) var sanShiHospital = signedData.hospital.slice(0,8), // 该医生(登录者)所在社区主编码(截取前8位,后两位是服务站) hospital = loginerData.hospital.slice(0,8); return sanShiHospital == hospital; }, // 与数据库校验 validInputByDBPromise = function(data) { var reqs = [ {url: "doctor/family_contract/patient_sanshi_signinfo",data: {idCard: data.unencIdcard}}, //检查姓名、身份证号、医保卡号是否一致 {url:'doctor/validatePatient',data:{ name: data.name, // 姓名 idcard: data.unencIdcard, // 未加密的身份证 ssc: data.ssc // 医保卡号 }}]; if(data.mobile) { // 检查手机号是否签约 reqs.push({url: "doctor/family_contract/checkMoblie",data:{mobile: data.mobile}},{url: "doctor/patient_group/checkMobile",data:{mobile: data.mobile}}) } return getReqPromises(reqs).then(function(res) { var checkSignedFamilyDoctorRes = res[0], infoConsistencyRes = res[1], checkRes = res[2], mobileIsUsedRes = res[3]; return checkSignedFamilyDoctor(checkSignedFamilyDoctorRes) && (function(){ // 如果已经签约三师,并且三师签约中全科医生不在当前医生所在社区,则不允许其进行代理签约 /* if(checkSignedSanshi(checkSignedFamilyDoctorRes.data)&&!checkSanshiComnunity(loginerInfo.docInfo, checkSignedFamilyDoctorRes.data)) { mui.toast("居民已签约三师,不可与其签约"); return false; }*/ return true; })() &&checkInfoConsistency(infoConsistencyRes) &&(function(){ // 签约居民已注册,且有填写手机号 if(checkRegistered(checkSignedFamilyDoctorRes) && data.mobile.trim()) { // 注册时用的手机号 var signedMobile = $.trim(checkSignedFamilyDoctorRes.data.patient.mobile); // 填写的手机手机号与注册时的不一致 if(signedMobile && signedMobile != data.mobile.trim()) { mui.toast("所填手机号与居民注册时使用的手机号不一致!"); return false; } return true; } else { if(!data.mobile) return true; return checkMobileIsNotUsed(mobileIsUsedRes); } })() //&&checkMobileSignedStatus(checkRes); }); }, // 所有输入验证 validInputsPromise = function() { // 表单数据 var data = getInputs(); return Promise.resolve() .then(function() { if(!validRequired(data)) throw new Error("必输校验失败"); }) .then(function() { if(!validFormat(data)) throw new Error("格式校验失败"); }) .then(function() { // TODO 接口抛错,暂时关闭数据库验证 // return validInputByDBPromise(data); return true }) .then(function(flag) { if(!flag) throw new Error("数据有效性校验失败"); }) .then(function() { // 所有校验成功 $infoList.find('input').css("color",""); return data; }); }, bindEvents = function() { //填充信息 if(self.kind == 1){//建档跳过来 $nameInput.val(self.name) $idcardInput.val(self.idCard) $committee.val(self.countryName?self.countryName:'') $committee.attr('data-code',self.countryCode?self.countryCode:'') $sscInput.val(self.ssc?self.ssc:'') $mobileInput.val(self.mobile?self.mobile:'') } //选择居委会 selectCommittee() // 扫描医保卡 $scanSSCInfo.on('tap', function() { getAutoRecCompressImageLocalPath(function(imgPath) { plus.nativeUI.showWaiting(); autoRecSSCImagePromise(imgPath).then(function(res) { if(!res) throw new Error("图片识别失败"); plus.nativeUI.closeWaiting(); $('#auto_scan_ssc_photo').remove(); $imgArea.find('li.add').before('
'); $scanSSCInfo.hide(); $rescanSSCInfo.show(); }).catch(function(e){ plus.nativeUI.closeWaiting(); console && console.error(e); }); }); }); $rescanSSCInfo.on('tap', function() { $scanSSCInfo.trigger("tap"); }); // 添加附件 $imgArea.on('tap', '.add', function() { showActionSheet($imgArea[0], this); }); // 下一步 $nextStep.on('tap', function() { plus.nativeUI.showWaiting(); validInputsPromise().then(function(data) { // 上传附件,并获取存储的URL,返回新的Promise对象 return getImagesPromise().then(function(res) { if(res) { data.images = res.urls return data; } }); }).then(function(data) { plus.nativeUI.closeWaiting(); var view = plus.webview.getWebviewById("dailiqianyue-next"); var createNew = false; if(view){ // 如果页面已经缓存 view.close(); createNew = true; } if(data) { mui.openWindow({ url: "dailiqianyue-next.html", id:"dailiqianyue-next", // 是否重新创建页面webview,防止页面缓存 // createNew: createNew, // 跳转页面传参 extras: { accessData:data, // 表单数据 docInfo: loginerInfo.docInfo // 登录者信息 }, }); } }).catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); }); }); // 获取焦点时恢复输入的字体颜色 $infoList.on('focus','input',function() { $(this).css("color",""); }); $idcardInput.on('blur',function() { validIdCard($(this).val().trim()); }); $sscInput.on('blur',function() { validSSC($(this).val().trim()); }); $mobileInput.on('blur',function() { validMobile($(this).val().trim()); }); $emerMoblieInput.on('blur',function() { validEmerMobile($(this).val().trim()); }); // 删除图片 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已经准备好,可以往下执行 mui.back = function(){ var self = plus.webview.currentWebview(); if(self.opener().id == 'jiandangxiangqing'){ self.close() }else{ backToPage(self); } } 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(); var name = self.name, idcard = self.idcard; $nameInput.val(name); // 姓名 $idcardInput.val(idcard); // 未加密的身份证 // 获取登录医生信息 loginerInfo = getLoginerInfo(); //图片缩放 scaleRefresh(".upload-img"); // 绑定页面事件 bindEvents(); }) }).catch(function(e) { plus.nativeUI.closeWaiting(); console && console.error(e); }); var closeList = []; function backToPage(wv){ if(wv.id == "home2.html"){ var self = plus.webview.currentWebview(); wv.show(); mui.later(function(){ for(i=0; i