var d = dialog({contentType:'load', skin:'bk-popup'}); var dd = dialog({contentType:'load', skin:'bk-popup', content:'提交中...'}); var relation; var family = []; $(function(){ // 判断是否登录 checkUserAgent(); getFamilyMembersPromise() .then(function(res) { if(res.status == 200) { family = res.data.normalmembers; } else { dialog({contentType:'tipsbox', skin:'bk-popup' , content: res.msg||'数据获取失败'}).show(); } }) .catch(function(e) { console && console.error(e) }) // 选择家庭关系 $('#select').mobiscroll({ theme: 'ios', lang: 'zh', formatValue: function(d) { return d; }, customWheels: true, wheels: [ [{ keys: ['1', '2', '3', '4', '5', '6', '0'], values: ['父亲', '母亲', '老公', '老婆', '儿子', '女儿', '其他'] }] ], onSelect: function(valueText, inst) { var dd = eval("[" + valueText + "]"); $('#select').val(dd[0].values); relation = dd[0].keys; } }); // 下一步 $("#next").bind("tap", function () { if(!$(this).hasClass('btn-disabled')) { commit(); } }); }); function checkVal ( cv, attr, d) { var len = d.length, sta = 1; for ( var i = 0; i < len; i++) { if (d[i][attr] == cv) { sta = -1; break; } } return sta; } // 禁止拖动 document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); function getFamilyMembersPromise() { d.show(); return new Promise(function(resolve, reject){ sendPost("patient/family/members", {}, "json", "get", function fail() { d.close(); dialog({contentType:'tipsbox', skin:'bk-popup' , content:'数据获取失败'}).show(); }, function success(res) { d.close(); resolve(res) }); }) } //验证信息 function validate(data) { if (data.name.trim() == "") { dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请输入姓名'}).show(); return false; } if (!isIdcard(data.idcard)) { dialog({contentType:'tipsbox', skin:'bk-popup' , content:'身份证格式不正确'}).show(); return false; } if (data.select.trim() == "") { dialog({contentType:'tipsbox', skin:'bk-popup' , content:'请选择家庭关系'}).show(); return false; } //判断家庭关系与身份证性别是否匹配 var d = data.idcard[data.idcard.length - 2], isMan = d%2 == 1; if(isMan && (relation==2 || relation==4 || relation==6)){ dialog({ contentType:'tipsbox', skin:'bk-popup', content: "您的家人性别与家庭关系不匹配,请核对后再确认" }).show(); return false; } if(!isMan && (relation==1 || relation==3 || relation==5)){ dialog({ contentType:'tipsbox', skin:'bk-popup', content: "您的家人性别与家庭关系不匹配,请核对后再确认" }).show(); return false; } if (checkVal( data.name, 'name', family) == -1 || checkVal( data.idcard, 'idcard', family) == -1) { dialog({contentType:'tipsbox', skin:'bk-popup' , content: data.name + '已是您的家庭成员咯~'}).show(); return false; } return true; } // 提交失败 function commitFailed (res) { dd.close(); if (res && res.msg) { var d1 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show(); } else { var d2 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show(); } } function checkMemberPromise(code) { return new Promise(function(resolve, reject) { sendPost("patient/family/check_member", { memberCode: code }, "json", "get", commitFailed, function(res) { resolve(res) }); }) } function sendWXTempPromise(data) { return new Promise(function(resolve, reject) { sendPost("patient/family/sendWXTemp", data , "json", "POST", commitFailed, function(res) { resolve(res) }); }) } // 提交成功 function commitSuccess (res) { if (res.status == 200) { var data = res.data; if (data.isRegister == -1) { dd.close(); $('#next').removeClass("btn-disabled"); var d4 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'别闹了,这是您自己,快添加您的家人吧'}).show(); } else if (data.isRegister == 0) { dd.close(); $('#next').removeClass("btn-disabled"); dialog({ content:'对不起,目前仅支持添加注册用户为家人哦,是否推荐家人注册?', okValue: '前往推荐', ok: function (){ // 跳转分享 window.location.href = '../html/family-share.html'; }, cancel: function () {} }).showModal(); } else { if (data.name != $("#name").val()) { dd.close(); $('#next').removeClass("btn-disabled"); var d4 = dialog({contentType:'tipsbox', skin:'bk-popup' , content:'姓名与身份证不一致'}).show(); } else { window.localStorage.removeItem("familyCode"); window.localStorage.setItem("familyCode", data.code); window.localStorage.removeItem("familyName"); window.localStorage.setItem("familyName", data.name); window.localStorage.removeItem("familyImgUrl"); window.localStorage.setItem("familyImgUrl", getImgUrl(data.photo)); window.localStorage.removeItem("familyRelation"); window.localStorage.setItem("familyRelation", relation); checkMemberPromise(data.code) .then(function(res) { dd.close(); if(res.status == "200") { var state = res.data.state; if(state == "0") { // 未绑定微信和手机 $('#next').removeClass("btn-disabled"); window.location.href = 'add-step3.html'; } else if(state == "1") { // 仅绑定微信 sendWXTempPromise({ member: data.code, relation: relation }) .then(function(res) { if(res.status=="200") { $('#next').addClass("btn-disabled"); dialog({contentType:'tipsbox', skin:'bk-popup' , content:"已发送添加申请"}).show(); } else { $('#next').removeClass("btn-disabled"); if(res.msg){ dialog({contentType:'tipsbox', skin:'bk-popup' , content:res.msg}).show(); }else dialog({contentType:'tipsbox', skin:'bk-popup' , content:"发送失败"}).show(); } }) } else if(state == "2") { // 仅绑定手机 $('#next').removeClass("btn-disabled"); window.localStorage.removeItem("familyMobile"); window.localStorage.setItem("familyMobile", data.mobile); window.location.href = 'add-step2.html'; } else if(state== "3") { // 手机和微信都绑定 $('#next').removeClass("btn-disabled"); window.localStorage.removeItem("familyMobile"); window.localStorage.setItem("familyMobile", data.mobile); window.location.href = 'add-step2-pre.html'; } } else { dd.close(); dialog({contentType:'tipsbox', skin:'bk-popup' , content:'加载失败'}).show() } }) .catch(function(e) { dd.close(); console && console.error(e) }) // if (data.mobile == "") { // window.location.href = 'add-step3.html'; // } else { // window.localStorage.removeItem("familyMobile"); // window.localStorage.setItem("familyMobile", data.mobile); // window.location.href = 'add-step2.html'; // } } } } else { //非200则为失败 commitFailed(res); } } // 提交添加家人 function commit() { var data = {}; data.name = $("#name").val(); data.idcard = $("#idcard").val(); data.select = $("#select").val(); if (validate(data)) { // 验证通过执行 dd.show(); $('#next').addClass("btn-disabled"); sendPost("patient/family/is_register", data, "json", "get", commitFailed, commitSuccess); } }