var snCode, deviceInfo, isManage, xieyiImg, choosePatient; mui.init(); mui.plusReady(function(){ var self = plus.webview.currentWebview(); deviceInfo = self.info; snCode = self.sn; isManage = self.isManage; templateHelper(); //获取设备的基本信息 fillDeviceInfo(); bindEvents(); }); var old_back = mui.back; mui.back = function(){ var opener = plus.webview.currentWebview().opener(); if(opener){ mui.fire(opener, "reload"); } mui.later(function(){ old_back(); }, 300); } function fillDeviceInfo(){ var keys = []; var multiUser = deviceInfo.multiUser ? JSON.parse(deviceInfo.multiUser) : {}; if(deviceInfo.isMultiUser == "1"){ //设备有多个键需要绑定 for(key in multiUser){ var bindUser = _.findWhere(deviceInfo.bindingInfo, {userType: key}); keys.push({ key: key, name: multiUser[key]+"键", user: bindUser }); } }else{ //设备只有一个键 keys.push({ key: "1", name: "居民", user: deviceInfo.bindingInfo[0] }); } deviceInfo.bindKeys = keys; var html = template("info-tmp", {info: deviceInfo, sn: snCode}); $(".info-card").empty().append(html); } function bindEvents(){ $(".info-card").on('click', ".choose-patient", function(){ var key = $(this).attr("data-key"); var $news = $(".new"), len = $news.length, choosed = []; for(i=0; i 0){ $target.parent().remove(); $grant_parent.append(html); }else{ //修改的情况 var $target1 = $(".user-"+key), $parent1 = $target1.parent(); $target1.remove(); $parent1.append(html); } choosePatient = true; checkBtnEnable(); }); $(".confirm-btn").on('click', function(){ if(!$(this).hasClass("active")){ return false; } bindDeviceInfo(); }); $(".upload-btn").on('click', function(){ //上传协议前需判断是否选择了居民 var $news = $(".new"); if($news.length == 0){ mui.toast("请选择居民"); }else{ mui('#Sheet').popover('toggle'); } }); //重新上传 $(".re-upload-btn").on('click', function(){ mui('#Sheet').popover('toggle'); }); $("#chooseItem").on('click', "li", function(){ var $this = $(this), type = $this.attr("data-val"); if(type == "camera"){ mui('#Sheet').popover('toggle'); getCamera(); }else if(type == "gallery"){ mui('#Sheet').popover('toggle'); getGallery(); } }) } //判断确定按钮是否高亮 function checkBtnEnable(){ if(choosePatient && xieyiImg!=""){ $(".confirm-btn").addClass("active"); } } //绑定设备信息 function bindDeviceInfo(){ var $news = $(".new"); var reqs = []; for(i=0; i<$news.length; i++){ var $item = $($news[i]), json = JSON.parse($item.attr("data-json")), key = $item.attr("data-key"); var obj = { user: json.code, deviceId: deviceInfo.device_id, deviceName: deviceInfo.deviceName, deviceSn: snCode, categoryCode: deviceInfo.deviceType, userType: deviceInfo.isMultiUser ? key : "-1" // deviceActivityType: deviceInfo.deviceActivityType }; if(deviceInfo.sim){ obj.sim = deviceInfo.sim; } reqs.push({ url: "doctor/device/SavePatientDeviceAndCredit", reqType: 'POST', data: { data: JSON.stringify(obj), agreementPhoto: xieyiImg } }); } plus.nativeUI.showWaiting(); getReqPromises(reqs, true).then(function(ress){ plus.nativeUI.closeWaiting(); var success = true, errorMsg = ""; for(i=0; i选择绑定居民'+ ''; $(".user-"+key).remove(); $parent.append(html); }else{ mui.toast(res.msg); } }, 'post', '', true); } //获取相册 function getGallery() { plus.gallery.pick(function(cbFile) { //scb:SuccessCallBack handlePickSucc(cbFile); }, function(ecb) { //ecb:ErrorCallBack }, { filename: "_doc/gallery/", filter: "image" }); } // 调用系统摄像头 function getCamera() { var cmr = plus.camera.getCamera(); cmr.captureImage(function(path) { /** * 拍照成功后,图片本保存在本地,这时候我们需要调用本地文件 * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.resolveLocalFileSystemURL */ plus.io.resolveLocalFileSystemURL(path, function(entry) { /* * 将获取目录路径转换为本地路径URL地址 * http://www.html5plus.org/doc/zh_cn/io.html#plus.io.DirectoryEntry.toLocalURL */ fileUrl = entry.toLocalURL(); handlePickSucc(fileUrl); }); }, function(error) { if(error.code==11 && error.message == "null") { plus.nativeUI.toast("您尚未授权拍照权限,无法使用拍照功能。"); } }, { filename: "_doc/camera/", index: 1 //ios指定主摄像头 }); } var localImg = ""; function handlePickSucc(srcUrl) { var lastIdx = srcUrl.lastIndexOf("/"), imgName = srcUrl; if(lastIdx>-1) { imgName = srcUrl.slice(lastIdx+1); } plus.zip.compressImage({ src: srcUrl, dst: "_doc/compressImg/"+imgName, quality: 20, overwrite: true }, function(succ) { localImg = succ.target; uploader(localImg); }, function(err) { mui.toast("压缩失败: " + err.message); }); } var xieyiImg = ""; function uploader(url) { var uploadServerUrl = server + "upload/image"; plus.nativeUI.showWaiting(); var task = plus.uploader.createUpload(uploadServerUrl, { method: "post" }, function(t, status) { if (status == 200) { xieyiImg = JSON.parse(t.responseText).urls; //填充背景图片 $("#xieyiImage").css("background-image", "url("+localImg+")") $(".upload-btn").parent().hide(); $(".image-panel").show(); $(".confirm-btn").parent().show(); checkBtnEnable(); plus.nativeUI.closeWaiting(); } else { mui.toast("上传协议图片失败,请稍后重试"); plus.nativeUI.closeWaiting(); } }); task.addFile(url, {}) task.start(); } function templateHelper(){ template.helper("getJsonStr", function(obj){ return JSON.stringify(obj); }) }