var $nameInput = $("#name"),//姓名
$sscInput = $("#ybcard").find("input"),// 医保卡号
$mobileInput = $("#phonenum").find("input"),// 手机号码
$Birthday = $("#Birthday").find("input"),// 出生日期
$address = $("#address").find("input"),// 户口所在地
$committee = $("#committee"),//所属居委会
$Present_Address = $('#Present_Address').find('input'),//现住址
$infoList = $("#info_list"),// 表单的ul容器
$nextStep = $('#next_step');// 下一步按钮
var nation, blood, marryed;
var nationObj = {},
marryedObj = {},
bloodObj = {};
var signData;//缓存签约数据
var jiandangCase;
var d = dialog({contentType:'load', skin:'bk-popup'});
function toast(msg){
dialog({contentType:'tipsbox',bottom:true, skin:'bk-popup' , content:msg}).show()
}
//请求失败
function queryFailed(){ d.close();toast('请求失败');}
$(function(){
var cases = window.localStorage.getItem('jiandangCase1')
jiandangCase = cases?JSON.parse(cases):''
if(jiandangCase){//有缓存
$('#name').text(jiandangCase.name)
$('#idcard').val(jiandangCase.idcard)
$('#ssc').val(jiandangCase.ssc)
$committee.val(jiandangCase.countryName)
$committee.attr('data-jwcode',jiandangCase.countryCode)
$Birthday.val(jiandangCase.Birthday)
$mobileInput.val(jiandangCase.mobile)
$address.val(jiandangCase.address)
$address.attr('data-code',jiandangCase.addressCode)
$Present_Address.val(jiandangCase.Present_Address)
$('#sex').attr('data-code',jiandangCase.sex)
$('#national').attr('data-code',jiandangCase.national)
$('#national').attr('data-name',jiandangCase.nationalName)
$('#marry').attr('data-code',jiandangCase.marry)
$('#marry').attr('data-name',jiandangCase.marryName)
$('#Blood_type').attr('data-code',jiandangCase.Blood_type)
$('#Blood_type').attr('data-name',jiandangCase.Blood_typeName)
}else{
signData = JSON.parse(window.localStorage.getItem('is_signData'));// 获取缓存
$('#name').text(signData.name)
$('#idcard').val(signData.idcard)
$('#ssc').val(signData.ssc)
$('#committee').val(signData.countryName)
$('#committee').attr('data-jwcode',signData.jwCountryCode)
//提取身份证
var idStr;
var dd = signData.idcard;
if(dd.length == 18){
idStr = dd.substring(6,10)+'-'+dd.substring(10,12)+'-'+dd.substring(12,14)
if(dd.substring(16,17)%2==0){
$('#sex').attr('data-code','2')
}else{
$('#sex').attr('data-code','1')
}
}else if(dd.length == 15){
idStr = '19'+dd.substring(6,8)+'-'+dd.substring(8,10)+'-'+dd.substring(10,12)
if(dd.substring(14,15)%2==0){
$('#sex').attr('data-code','2')
}else{
$('#sex').attr('data-code','1')
}
}
$Birthday.val(idStr)
}
// 获取血型、婚姻、民族字典
sendPost('patient/getNationDict', {}, 'json', 'get',queryFailed, function(res) {
if(res.status==200) {
nationObj = res.data
addOneContent1(nationObj);
}else{
toast('获取民族字典数据失败')
}
})
sendPost('patient/getMarryDict', {}, 'json', 'get',queryFailed, function(res) {
if(res.status==200) {
marryedObj = res.data
addOneContent2(marryedObj);
}else{
toast('获取婚姻字典数据失败')
}
})
sendPost('patient/getBloodDict', {}, 'json', 'get',queryFailed, function(res) {
if(res.status==200) {
bloodObj = res.data
addOneContent3(bloodObj);
}else{
toast('获取血型字典数据失败')
}
})
addOneContent4([{'code':'1','name':'男'},{'code':'2','name':'女'}])
$Birthday.mobiscroll().date({
theme: "ios",
lang: "zh",
display: "bottom",
dateFormat: "yy-mm-dd",
onSelect: function(valueText, inst) {
}
})
// 弹出框生成民族内容
function addOneContent1(arr) {
var len = arr.length;
var html = "";
for(var i = 0; i < len; i++) {
if(jiandangCase&&jiandangCase.national == arr[i].NATION_CODE){
html += ''
}else{
html += ''
}
}
$('#nation').html(html)
$('#nation').mobiscroll().select({
theme: "ios",
lang: "zh",
placeholder:'请选择民族',
display: "bottom",
onSelect: function(valueText, inst) {
$('#national').attr('data-code',inst._tempValue)
$('#national').attr('data-name',valueText)
}
})
}
// 弹出框生成婚姻内容
function addOneContent2(arr) {
var len = arr.length;
var html = "";
for(var i = 0; i < len; i++) {
if(jiandangCase&&jiandangCase.marry == arr[i].code){
html += ''
}else{
html += ''
}
}
$('#marryed').html(html)
$('#marryed').mobiscroll().select({
theme: "ios",
lang: "zh",
placeholder:'请选择婚姻情况',
display: "bottom",
onSelect: function(valueText, inst) {
$('#marry').attr('data-code',inst._tempValue)
$('#marry').attr('data-name',valueText)
}
})
}
// 弹出框生成血型内容
function addOneContent3(arr) {
var len = arr.length;
var html = "";
for(var i = 0; i < len; i++) {
if(jiandangCase&&jiandangCase.Blood_type == arr[i].code){
html += ''
}else{
html += ''
}
}
$('#Bloodtype').html(html)
$('#Bloodtype').mobiscroll().select({
theme: "ios",
lang: "zh",
placeholder:'请选择血型',
display: "bottom",
onSelect: function(valueText,inst) {
$('#Blood_type').attr('data-code',inst._tempValue)
$('#Blood_type').attr('data-name',valueText)
}
})
}
// 弹出框生成性别
function addOneContent4(arr) {
var len = arr.length;
var html = "";
for(var i = 0; i < len; i++) {
if(arr[i].code == $('#sex').attr('data-code') || arr[i].code == jiandangCase.sex){
html += ''
}else{
html += ''
}
}
$('#sexs').html(html)
$('#sexs').mobiscroll().select({
theme: "ios",
lang: "zh",
placeholder:'请选择性别',
display: "bottom",
onSelect: function(valueText, inst) {
$('#sex').attr('data-code',inst._tempValue)
}
})
}
initExpressAddr();
//初始化户口所在地选择下拉框数据
function initExpressAddr(){
picker = new mui.PopPicker({
layer: 2,
layerNames: ["省","市"]
});
//获取省份信息
getDistrict(1, '', 0);
bindPickerEvent();
}
//绑定picker的事件
function bindPickerEvent(){
$(picker.pickers[0].holder).on('change', function(event) {
var eventData = event.originalEvent.detail || {},
item = eventData.item;
if(!item || !item.value) {
return ;
}
//获取市信息
getDistrict(2, item.value, 1);
});
}
/**
* 获得省市区数据
* @param {number} type 1-省,2-市,3-区
* @param {string} code 省市对应的code
* @param {number} level 对应下拉框对应的列 0-省,1-市,2-区
*/
function getDistrict(type, code, level){
var url = 'common/district',
params = {type: type, code: code};
sendPost(url,params,'json', 'post',queryFailed, function(res) {
if(res.status==200) {
var data = _.map(res.list, function(o){
return {
value: o.code,
text: o.name
}
});
picker.pickers[level].setItems(data);
}else{
toast('获取地址数据失败')
}
})
}
//选择地址下拉框
$("#address").on('click', function(){
picker.show(function(selected){
var text = selected[0].text+selected[1].text;
$("#address .select-input").val(text);
$("#address .select-input").attr('data-code',selected[1].value);
})
})
// 获取填写的数据
var getInputs = function() {
var data = {
idcard:jiandangCase?jiandangCase.idcard:signData.idcard,
name: jiandangCase?jiandangCase.name:signData.name, // 姓名
sex:$('#sex').attr('data-code'),
ssc:jiandangCase?jiandangCase.ssc:signData.ssc,
sexName:$('#sex').attr('data-code')=='1'?'男':'女',
mobile: $mobileInput.val().trim(), // 手机号码
Birthday: $Birthday.val(), // 出生日期
address: $address.val(), // 户口所在地
addressCode:$address.attr('data-code'),
national: $('#national').attr('data-code'), // 民族
nationalName: $('#national').attr('data-name'),
marry: $('#marry').attr('data-code'), // 婚姻情况
marryName: $('#marry').attr('data-name'),
Blood_type: $('#Blood_type').attr('data-code'), // 血型
Blood_typeName: $('#Blood_type').attr('data-name'),
countryCode: $committee.attr('data-jwcode'), //居委会
countryName: $committee.val(),
Present_Address: $Present_Address.val().trim(), //现住址
}
return data;
},
// 必输验证
validRequired = function(data) {
var fieldsStr = "mobile,Birthday,address,national,marry,Blood_type,countryCode,Present_Address",
fieldMap = {
mobile: "手机号码",
Birthday: "出生日期",
address: "户口所在地",
national: "民族",
marry: "婚姻情况",
Blood_type: "血型",
countryCode: "居委会",
Present_Address: "现住址"
},
fields = fieldsStr.split(","),
invalidField;
// 某个字段为""、null、undefined,则该字段不能通过必输校验
invalidField = _.find(fields, function(key) {
return !data[key] && data[key] !== 0;
});
if(invalidField) {
toast(fieldMap[invalidField] + '不能为空');
return false;
} else {
return true;
}
},
// 校验医保卡号格式
validSSC = function(ssc, isshow) {
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) {
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");
toast("请填写有效的手机号码");
return false;
}
$mobileInput.css("color", "");
return true;
},
// 输入格式验证
validFormat = function(data) {
return validSSC(data.ssc) && (!data.mobile || validMobile(data.mobile))
},
// 所有输入验证
validInputsPromise = function(fun) {
// 表单数据
var data = getInputs();
return Promise.resolve()
.then(function() {
if(!validRequired(data)) throw new Error("必输校验失败");
})
.then(function() {
if(!validFormat(data)) throw new Error("格式校验失败");
})
.then(function() {
return fun&&fun.call(this,data);
})
.then(function(flag) {
if(!flag) throw new Error("数据有效性校验失败");
})
.then(function() {
// 所有校验成功
$infoList.find('input').css("color", "");
return data;
});
},
bindEvents = function() {
//选择居委会
selectCommittee()
// 预览
$nextStep.on('tap', function() {
validInputsPromise(function(data){
d.show()
window.localStorage.setItem("jiandangCase1",JSON.stringify(data))
window.location.href = "xinxiqueren.html";
})
})
//详细信息
$(".detail").on('tap', function() {
validInputsPromise(function(data){
d.show()
window.localStorage.setItem("jiandangCase1",JSON.stringify(data))
window.location.href = "xinxicaiji.html";
})
})
// 获取焦点时恢复输入的字体颜色
$('input').on('focus',function() {
$(this).css("color", "");
})
$sscInput.on('blur', function() {
validSSC($(this).val().trim());
})
$mobileInput.on('blur', function() {
validMobile($(this).val().trim());
})
}
bindEvents()
})