写在前面
需求:方便代码维护,修改方便,以下是代码中的实现方法
备注:好久之前,从一位网友处看到此方法,觉得挺好,我只是代码的搬运工
/**
* Created by user on 2018/9/14.
*/
/*参数定义注释 wwwp 12/29
* STEPLISTONE 存储账户信息数组
* top["sysIndex"] 父页面
* arrToObject() 处理数组为map
* */
var init=(function () {
/* 注入所有方法 抛出*/
var tool = {
arrToObject: function (arr) {
var strJs = '({';
for (var i = 0; i < arr.length; i += 1) {
var as = arr[i].split("=");
strJs = strJs + as[0] + ':' + '"' + as[1] + '"' + ',';
}
strJs = strJs.substr(0, strJs.length - 1) + '})';
return eval(strJs);
}
};
var tempObj={
reader:function(){
mini.parse();
top["sysIndex"] = window;
var _this = this;
var STEPLISTONE=[];
// 地图显示
var map = new BMap.Map("getLatLngMap");
map.centerAndZoom(new BMap.Point(116.413610, 39.911066), 12);
var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});// 左上角,添加比例尺
var top_left_navigation = new BMap.NavigationControl(); //左上角,添加默认缩放平移控件
var top_right_navigation = new BMap.NavigationControl({
anchor: BMAP_ANCHOR_TOP_RIGHT,
type: BMAP_NAVIGATION_CONTROL_SMALL
}); //右上角,仅包含平移和缩放按钮
map.addControl(top_left_control);
map.addControl(top_left_navigation);
map.addControl(top_right_navigation);
map.enableScrollWheelZoom(true);
// 单击获取点击的经纬度
map.addEventListener("click", function (e) {
mini.get("lng").setValue(e.point.lng);
mini.get("lat").setValue(e.point.lat);
/*alert(e.point.lng + "," + e.point.lat);*/
map.clearOverlays();
marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
marker.enableDragging(); //支持拖拽
map.addOverlay(marker);
marker.addEventListener("dragend",function(e) {
mini.get("lat").setValue(e.point.lat);
mini.get("lng").setValue(e.point.lng);
map.clearOverlays();
marker = new BMap.Marker(new BMap.Point(e.point.lng, e.point.lat));
})
});
},
//注入所有的选择器
selector:{
$mySelector:$('#mySelector'),
$partOne:$('#btn_part1'),
$part1:$('.part1'),
$part2:$('.part2'),
$part3:$('.part3'),
$isHide:$('.isHide'),
$name:mini.getByName("name"),
$loginName:document.getElementById("loginName"),
$registerEmail:document.getElementById("registerEmail"),
$nowCity:mini.get('nowCity'),
// $nowStreet:mini.get("nowStreet"),
$nowAddress:mini.get("nowAddress"),
$registerCity:mini.get('registerCity'),
// $registerStreet:mini.get('registerStreet'),
$violationNature:mini.get('violationNature')
},
//注入所有的接口地址,
interface: {
allSave:ctx+'/medicalInstitutionsInformation/medicalRegister',
email:ctx+'/user/emailValidate',
nowAddress: ctx+'/medicalInstitutionsInformation/getTwoLevelLinkageDataByRegion',
checkLoginName:ctx+'/sys/user/checkLoginName',
checkMedicalName:ctx+'/sys/user/checkMedicalName',
checkEmail:ctx+'/sys/user/checkLoginEmail'
},
//注入所有事件
registerEle:{
blur_map:function () {
var county = tempObj.selector.$nowCity.getText();
var address =tempObj.selector.$nowAddress.getValue();
if (!county) {
ebapUtils.tipbox({type: "warning", msg: "为保证地理信息准确请您选择区!"});
return;
}
var map = new BMap.Map("getLatLngMap");
map.centerAndZoom(new BMap.Point(116.413610, 39.911066), 12);
var realityAddress = "北京市" + county + address;
var myGeo = new BMap.Geocoder();
// 将地址解析结果显示在地图上,并调整地图视野
myGeo.getPoint(realityAddress, function (point) {
if (point) {
map.centerAndZoom(point, 15);
// map.addOverlay(new BMap.Marker(point));
map.clearOverlays();
var marker = new BMap.Marker(point);
map.addOverlay(marker);
marker.enableDragging();
marker.addEventListener("dragend", function (e) {
mini.getByName('longitude').setValue(e.point.lng);
mini.getByName('longitude').setText(e.point.lng);
mini.getByName('latitude').setValue(e.point.lat);
mini.getByName('latitude').setText(e.point.lat);
});
mini.get("lng").setValue(point.lng);
mini.get("lat").setValue(point.lat)
} else {
ebapUtils.tipbox({type: "warning", msg: "您选择的地址没有查找到,请到地图上点击选择准确位置!"});
}
}, "北京市");
},
click_partTwo:function () {
var form = new mini.Form("#form");
form.validate();
if(form.isValid()==false)return;
tempObj.selector.$part2.hide();
$(".step li").eq(2).addClass("on");
var file = $("#certificatePictureUrl > input:file")[0];
var data=form.getData();
$.ajaxFileUpload({
url: tempObj.interface.allSave, //用于文件上传的服务器端请求地址
fileElementId:file, //文件上传域的ID
type:"post",
data:data ,
success: function (map) { //服务器成功响应处理函数
tempObj.selector.$part3.show();
//返回map
countdown({
maxTime:15,
ing:function(c){
$("#times").text(c);
},
after:function(){
window.location.href="${ctx}/logout";
}
});
},
error: function () { //服务器响应失败处理函数
ebapUtils.tipbox({
type:"warning",
msg:"服务器响应失败!"
});
}
});
},
click_isHide: function () {
var ifHide = tempObj.selector.$violationNature.getValue();
ifHide == '1' ? tempObj.selector.$isHide.hide() : tempObj.selector.$isHide.show();
},
click_phone: function (e) {
var name = e.sender.name;
if (e.value == "" || e.value == null || e.value == undefined) {
e.value = '';
e.errorText = "请输入正确的11位电话号码";
e.isValid = false;
}
if (!(/^1[3456789]\d{9}$/.test(e.value))&&!(/^0\d{2,3}-\d{7,8}(-\d{1,6})?$/.test(e.value))) {
e.isValid = false;
}
}
},
// ajax请求
ajaxRequest:{
ajax_onEmail: function () {
//先验证登录名 再验证邮箱
init.ajaxRequest.ajax_checkLoginNameExist('true');
},
ajax_onEmail_save:function () {
if (!verifyCheck._click()) return;
var flag = document.getElementById("btn_part1").disabled;
if (flag == 'true') {
return;
}
else {
//校验用户名与校验邮箱 todo
//获得相应的信息
var STEPLISTONE = [];
$(".part1 input").each(function (i, ele) {
STEPLISTONE.push($(ele).attr("name") + '=' + $(ele).val());
});
var data = tool.arrToObject(STEPLISTONE);
document.getElementById("btn_part1").disabled = 'true';
$.post(tempObj.interface.email, data, function (data) {
tempObj.callback.call_onEmail(data);
}).error(function () {
ebapUtils.tipbox({
type: "warning",
msg: "服务器响应失败!"
});
document.getElementById("btn_part1").disabled = 'false';
});
}
},
ajax_onRegionChanged: function () {
//获得区相应的街道
var value=tempObj.selector.$nowCity.value || '';
$.post(tempObj.interface.nowAddress, {region: value}, function (data) {
tempObj.callback.call_onRegionChanged(data);
});
},
ajax_onRegisterChanged: function () {
//获得区相应的街道
var value=tempObj.selector.$registerCity.value || '';
$.post(tempObj.interface.nowAddress, {region: value}, function (data) {
tempObj.callback.call_onRegisterChanged(data);
});
},
//_flag的标志是判断是否在点击下一步进行检验登录名与邮箱
ajax_checkLoginNameExist: function (_flag) {
//检测用户名
var value=tempObj.selector.$loginName.value || '';
$.post(tempObj.interface.checkLoginName, {oldLoginName: "", loginName: value}, function (data) {
if(_flag=='true'){
tempObj.callback.call_checkLoginNameExistFlag(data);
}
else{
tempObj.callback.call_checkLoginNameExist(data);
}
});
},
ajax_checkMedicalName: function () {
//检测医疗机构
var value=tempObj.selector.$name.getValue() || '';
$.post(tempObj.interface.checkMedicalName, {oldMedicalName: "", medicalName: value}, function (data) {
tempObj.callback.call_checkMedicalName(data);
});
},
//公用这个请求
ajax_login_T:function(callback){
//所有接口地址从interface中获取,callback中tempObj.callback中处理
$.post("","",callback);
},
ajax_checkEmailExist:function(_flag){
var value=tempObj.selector.$registerEmail.value || '';
$.post(tempObj.interface.checkEmail, {oldRegisterEmail: "", registerEmail: value}, function (data) {
if(_flag=='true'){
tempObj.callback.call_checkEmailExistFlag(data);
}
else{
tempObj.callback.call_checkEmailExist(data);
}
});
}
},
callback:{
//请求处理回调
call_onEmail:function (data) {
data=eval('(' + data + ')');
if (data.status == 'success') {
mini.alert(data.message, "提示", function () {
ebapUtils.actions.close('cancel');
window.location.reload();
});
ebapUtils.styleAlert();
}
else {
ebapUtils.tipbox({type: "error", msg: data.message});
document.getElementById("btn_part1").disabled = 'false';
}
},
call_onRegionChanged:function(data){
tempObj.selector.$nowStreet.setData(data)
},
call_onRegisterChanged:function(data){
tempObj.selector.$registerStreet.setData(data)
},
//登录名发生变化调用的事件
call_checkLoginNameExist:function (data) {
if (data == 'false') {
ebapUtils.tipbox({"type": "error", "msg": "用户名已经存在!"});
tempObj.selector.$loginName.value = "";
$('body').find("label.blank").addClass('hide');
}
},
//点击下一步保存用户调用的事件
call_checkLoginNameExistFlag:function (data) {
if (data == 'false') {
ebapUtils.tipbox({"type": "error", "msg": "用户名已经存在!"});
tempObj.selector.$loginName.value = "";
$('body').find("label.blank").addClass('hide');
}
else{
init.ajaxRequest.ajax_checkEmailExist('true');
}
},
call_checkMedicalName:function (data) {
if (data == 'false') {
ebapUtils.tipbox({"type": "error", "msg": "该医疗机构已经存在!"});
tempObj.selector.$name.setValue("");
}
},
call_checkEmailExist:function (data) {
if (data == 'false') {
tempObj.selector.$registerEmail.value = "";
$('#registerEmail').next('.blank').addClass('hide');
mini.alert('该邮箱已经注册,请登录!', "提示", function () {
ebapUtils.actions.close('cancel');
});
ebapUtils.styleAlert();
}
},
call_checkEmailExistFlag:function (data) {
if (data == 'false') {
tempObj.selector.$registerEmail.value = "";
$('#registerEmail').next('.blank').addClass('hide');
mini.alert('该邮箱已经注册,请登录!', "提示", function () {
ebapUtils.actions.close('cancel');
});
ebapUtils.styleAlert();
}
else{
init.ajaxRequest.ajax_onEmail_save();
}
},
//公用请求处理回调
call_login_T:function(){
var temp = function(){
};
tempObj.ajaxRequest.ajax_login_T(temp);
}
}
};
// 抛出
var outputObj =function(){
//首先执行reader方法,初始化一些操作
tempObj.reader();
return tempObj;
};
return new outputObj();
})();