123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334 |
- 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<len; i++){
- var $item = $($news[i]),
- json = JSON.parse($item.attr("data-json"));
- choosed.push(json.code);
- }
- openWebview("choose-patient.html", {key: key, sn: snCode, isManage: isManage, choosed: choosed});
- });
-
- $(".info-card").on('click', ".edit-btn", function(){
- var key = $(this).attr("data-key"),
- jsonStr = $(this).attr("data-json"),
- action = $(this).attr("data-action");
-
- if(action == "unbind"){
- confirmUnBind(jsonStr, this);
- }else{
- var $news = $(".new"),
- len = $news.length,
- choosed = [];
- for(i=0; i<len; i++){
- var $item = $($news[i]),
- json = JSON.parse($item.attr("data-json"));
- choosed.push(json.code);
- }
- openWebview("choose-patient.html", {key: key, sn: snCode, isManage: isManage, choosed: choosed});
- }
- });
-
- window.addEventListener("setPatientInfo", function(arg){
- var info = arg.detail,
- key = info.key,
- obj = info.obj;
-
- var $target = $(".choose-patient[data-key="+key+"]"),
- $grant_parent = $target.parent().parent();
-
- var html = template("patient-info-tmp", info);
- if($target.length > 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<ress.length; i++){
- var rs = ress[i];
- if(rs.status != 200){
- success = false;
- errorMsg = rs.msg;
- }
- }
- if(success){
- openWebview("bind-success.html",{isManage: isManage});
- }else{
- mui.toast(errorMsg);
- }
-
- })
- }
- function confirmUnBind(jsonStr, e){
- var obj = jsonStr ? JSON.parse(jsonStr) : undefined;
- if(!obj){
- return false;
- }
- dialog({
- content: "当前设备已绑定至"+obj.userName+"账户,解绑设备后,将无法自动从设备获取相关数据,是否确认解绑?",
- okValue: "立即解绑",
- ok: function(){
- unbindKey(obj, e);
- },
- cancelValue: "不了,谢谢",
- cancel:function(){}
- }).showModal();
- }
- function unbindKey(obj, target){
- plus.nativeUI.showWaiting();
- var url = "/doctor/device/DeletePatientDevice",
- params = {
- id: obj.patientDeviceId//绑定设备的记录id
- };
- sendPost(url, params, null, function(res){
- plus.nativeUI.closeWaiting();
- if(res.status == 200){
- mui.toast("解绑成功");
- var $parent = $(target).parent().parent(),
- key = obj.userType < 0 ? 1 : obj.userType,
- html = '<div class="ui-col-1">'+
- '<div class="choose-patient" data-key="'+key+'">选择绑定居民</div>'+
- '</div>';
- $(".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);
- })
- }
|