common.js

var Common = {
//得到url参数(例:"http://localhost:1239?a=1")
GetQueryString: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
//if (r != null) return unescape(r[2]); return null;
if (r != null) return decodeURI(r[2]); return null;
},
//回到头部(参数为距顶部的距离,默认没有距离)
GoTop: function (marginTop) {
$('body,html').animate({ scrollTop: marginTop || 0 }, 500);
},
//回到尾部(参数为距尾部的距离,默认没有距离)
GoBottom: function (marginBottom) {
var sc = $(document).height() - (marginBottom || 0);
$('body,html').animate({ scrollTop: sc }, 500);
},
//得到选择器
ReturnObj: function (obj) {
try {
var object = typeof obj == "string" ? $("#" + obj).length ? $("#" + obj.replace("#", "")) : $("." + obj).length ? $("." + obj.replace(".", "")) : $(obj) : $(obj);
return object;
}
catch (e) {
return $(obj);
}
},
//切换验证码
ToggleCode: function (obj, codeurl) {
$(obj).children("img").eq(0).attr("src", codeurl + "?time=" + Math.random());
return false;
},
//四舍五入的函数
ForDight: function (Dight, How) {
//例:Common.ForDight(1.413, 1); 保留一位小数 结果为1.4
Dight = Math.round(Dight * Math.pow(10, How)) / Math.pow(10, How);
return Dight;
},
//toFixed() 方法可把 Number 四舍五入为指定小数位数的数字
//eg:var num = new Number(1);alert(num.toFixed(2)); 结果为1.00
ForFixed: function (num, obj) {
var num = new Number(num);
return num.toFixed(obj);
},
ForNumber: function (number) {
return number / 100;
},

//显示信息
MyMsg: {
//成功提示
SuccessMsg: function (msg, showTime, successFunction) {
Common.MyMsg.ShowMsg(msg, 1, showTime, successFunction);
},
//错误提示
ErrorMsg: function (msg, showTime) {
Common.MyMsg.ShowMsg(msg, 2, showTime);
},
//显示信息(icon 1为成功,2为失败,3为询问,4为锁,5为哭脸,6为笑脸,7为警告)
//相当于一个重载的方法 参数icon不传入的话,就是没有任何符号的提示
//如:Common.MyMsg.ShowMsg("真的",3,2000, a); a为方法名 参数必须要齐全,少一个调用方法都会失败
ShowMsg: function (msg, icon, time, successFunction) {
try {
layer.msg(msg, {
icon: icon,
time: time || 2000 //2秒关闭
}, successFunction);//提示后可以执行特定的function
} catch (e) {
alert(msg || e.message);
}
},
},
//加载层
MyLoad: {
//load下标
LoadIndexs: null,
//显示加载
ShowLoad: function () {
Common.LoadIndexs = Common.LoadIndexs || new Array();
try {
Common.LoadIndexs[Common.LoadIndexs.length] = layer.load(2);
//Common.LoadIndexs[Common.LoadIndexs.length] = layer.open({
// type: 3,
// shade: [0.4, '#000']
//});
}
catch (e)
{ }
},
//关闭所有层
CloseLoad: function () {
if (Common.LoadIndexs) {
$.each(Common.LoadIndexs, function (index, ele) {
layer.close(ele);
});
}
}
},
//可以是输入后验证的错误提示
//msg提示信息 obj是选中的对象 如:按钮、文本框等
MyTips: function (obj, msg) {
layer.tips(msg, obj, {
tips: [1, '#3595CC'],
time: 2000
});
},
//询问框
MyConForm: function (msg, successFunction) {
try {
layer.confirm(msg, { icon: 3, title: "提示" }, function (index) {
successFunction();
layer.close(index);
});
}
catch (e) {
if (confirm(msg)) {
successFunction();
}
}
},
MyAjax: {
//发起ajax请求
Ajax: function (type, url, data, async, dataType, successFunction, isShowLoad) {
//弹出遮罩
//$(document).ajaxStart(function () {
// Common.MyLoad.ShowLoad();
//});
//关闭遮罩
$(document).ajaxStop(function () {
Common.MyLoad.CloseLoad();
});
//发起请求
$.ajax({
type: type,
url: url,
data: data, //$.extend(data, { M: Math.random() }),
global: isShowLoad,
async: async,//true 异步 默认值,false 同步
contentType: dataType,//dataType
success: successFunction,
error: Common.MyAjax.AjaxError
});
},
//发起ajaxPost请求,返回json
GetJsonByPost: function (url, data, async, successFunction, isShowLoad) {
Common.MyAjax.Ajax("POST", url, data, async, "application/json", successFunction, isShowLoad);
},
//发起ajaxGet请求,返回json
GetJsonByGet: function (url, data, async, successFunction, isShowLoad) {
Common.MyAjax.Ajax("GET", url, data, async, "application/json", successFunction, isShowLoad);
},
//发起ajaxPost请求,返回html
GetHtmlByPost: function (url, data, async, successFunction, isShowLoad) {
Common.MyAjax.Ajax("POST", url, data, async, "html", successFunction, isShowLoad);
},
//发起ajaxGet请求,返回html
GetHtmlByGet: function (url, data, async, successFunction, isShowLoad) {
Common.MyAjax.Ajax("GET", url, data, async, "html", successFunction, isShowLoad);
},
//ajax错误时调用
AjaxError: function (XMLHttpRequest, textStatus, errorThrown) {
//dialog({ title: '提示', content: "状态:" + textStatus + ";出错提示:" + errorThrown, okValue: '确定', ok: function () { } }).showModal();
}
},
//操作cookies
MyCookie: {
//写cookies(过期时间默认为7天)
SetCookie: function (name, value, expiresDays) {
var exp = new Date();
expiresDays = expiresDays || 7;
exp.setTime(exp.getTime() + expiresDays * 24 * 60 * 60 * 1000);//";//
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/;";//domain=" + Common.DomainName.MaxName();//domain=testcubejoy.com
},
//读取cookies
GetCookie: function (name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
return decodeURI(unescape(arr[2]));
//return unescape(arr[2]);
}
else
return null;
},
//删除cookies
DelCookie: function (name) {
var exp = new Date();
exp.setTime(exp.getTime() - 8 * 24 * 60 * 60 * 1000);
var cval = Common.MyCookie.GetCookie(name);//";//
if (cval != null)
document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + "; path=/;";//domain=" + Common.DomainName.MaxName();//domain=testcubejoy.com
//$.cookie("idphonemail", null);
},
DelDomainCookies: function (domain) {
var cookies = document.cookie.split(/; */);
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
if (cookie.indexOf(domain) != -1) {
var eqPos = cookie.indexOf("=");
var name = cookie.substr(0, eqPos);
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT; domain=" + domain;
}
}
}
},
MyVerification: {
//验证数字
VerificationInt: function (value) {
var patrn = /^[0-9]{1,20}$/;
if (typeof value == "number") {
return true;
}
else if (typeof value == "string" && !patrn.test(value)) {
Common.MyMsg.ErrorMsg("请输入正确的数字");
return false;
}
},
//验证手机(参数1为值,参数2为是否显示默认提示)
VerificationMobile: function (value, isShowMsg) {
var patrn = /^(13[0-9]|15[0|3|6|7|8|9]|18[0-9])d{8}$/;
if (patrn.test(value)) {
return true;
}
else {
if (isShowMsg) {
Common.MyMsg.ErrorMsg("请输入有效的手机号");
}
return false;
}
},
},

}

原文地址:https://www.cnblogs.com/xiaxiaomo/p/8985038.html