var USERAGENT = '', IEversion = 0;
var Safariversion=0;
var ua = navigator.userAgent.toLowerCase();
if(window.openDatabase){
if(ua.match(/version\/([\d.]+)/)!=null && ua.match(/version\/([\d.]+)/)!='null'){
Safariversion = ua.match(/version\/([\d.]+)/)[1];
}
}
function ieversion(){
USERAGENT = navigator.userAgent.toLowerCase();
USERAGENT.replace(/compatible;\smsie\s(\d+)\.\d+;.+/ig, function($0, $1) {
IEversion = parseInt($1);
});
if(IEversion ==0 && USERAGENT.indexOf('trident') !== -1) {
USERAGENT.replace(/mozilla\/\d+\.\d+\s\(windows.*?rv\:(\d+)\.\d+\) like gecko/ig, function($0, $1) {
IEversion = parseInt($1);
});
}
return IEversion;
}
ieversion();
function IEDOM(){
if(IEversion >0){
$('body').addClass('ie ie_'+IEversion);
}
}
/**
* location.href 兼容性处理
* 用法保持一致
*/
function location_href(url){
var hosts = HOST;
if(url.indexOf(HOST) == 0){
hosts = '';
}
if(url.indexOf('./') == 0){
url = url.substring(2);
}
window.location.href = hosts+url;
}
/**
* JS调试函数
*/
function debug(data){
if(typeof data =='object'){
data = JSON.stringify(data);//debug
}
console.log(data);//debug
}
/*关闭下载提示*/
function closeUptip(){
$('.js-updateBrowser').hide();
}
/**
* AJAX请求列表数据为空时输出统一格式数据
*/
function list_emp(column,text){
if(!text) text = '暂无数据';
text = '<div class="cl list_emp">'+text+'</div>';
if(column){
text = '<tr class="list_emp_tr"><td colspan="'+column+'" class="list_emp_td">'+text+'</td></tr>';
}
return text;
}
/**
* 获取对象数组元素个数
*/
function arrayCount(dt){
var i = 0;
if(dt && typeof(dt) =='object'){
for(K in dt){
i ++;
}
}
return i;
}
/**
* 四舍五入处理
* val=处理对象(传入带小数的float) n=保留位数 s=是否用0自动补充小数位(string)
*/
function Num_round(val, n, s) {
val = parseFloat(val);
n = parseInt(n);
var s = 1;
if(n > 0) {
for(i = 0; i < n; i++) {
s = s * 10;
}
val = Math.round(val * s) / s;
} else {
val = Math.round(val);
}
if(s && Math.ceil(val) == val){
var u = '';
for(i = 0; i < n; i++) {
u += '0';
}
val = val+'.'+u;
}
return val;
}
/**
* 表单模拟点击状态重建函数
*/
function Buttons_checkbox(obj) {
obj = $(obj);
obj.each(function(index, ele) {
ele = $(ele);
if(ele.find('input').prop('checked') == true) {
ele.addClass('a');
} else {
ele.removeClass('a');
}
});
$(obj).click(function(e) {
// e.preventDefault();
var T = $(this);
var In = T.find('input');
if(T.hasClass('a')) {
In.prop('checked', false);
T.removeClass('a');
} else {
In.prop('checked', true);
T.addClass('a');
}
});
}
/**
* 表单模拟点击状态重建函数
*/
function Buttons_radiobox(obj) {
obj = $(obj);
obj.each(function(index, ele) {
ele = $(ele);
if(ele.find('input').prop('checked') == true) {
ele.addClass('a');
} else {
ele.removeClass('a');
}
});
$(obj).click(function() {
var T = $(this);
var In = T.find('input');
$('input[name="' + In.attr('name') + '"]').prop('checked', false);
$('input[name="' + In.attr('name') + '"]').parent('.button_radio').removeClass('a');
In.prop('checked', true);
T.addClass('a');
});
}
/**
* TAB切换函数
* id=ID标识 / num=当前序号 / Max=TAB数量
用法:
<li id="test_1" οnclick="showTab('test',1,3)" class="a">TAB 1</li>
<li id="test_2" οnclick="showTab('test',2,3)">TAB 1</li>
<li id="test_3" οnclick="showTab('test',3,3)">TAB 1</li>
<div id="test_1_c"></div>
<div id="test_2_c" style="display:none"></div>
<div id="test_3_c" style="display:none"></div>
*/
function showTab(id, Num, Max) {
for(i = 1; i <= Max; i++) {
$('#' + id + '_' + i).removeClass('a');
$('#' + id + '_' + i + '_c').hide();
}
$('#' + id + '_' + Num + '_c').show();
$('#' + id + '_' + Num).addClass('a');
}
function Times(Val) {
var Times = new Date(); //中国标准时间
if(Val) {
Val = Val.toString();
if(/^[0-9]+$/.test(Val)){//如果是纯数字 则对其进行处理
if(Val.length ==10){//如果位数刚好10位则追加为13位
Val = parseInt(Val);
Val = Val * 1000;
}else{//转int类型
Val = parseInt(Val);
}
}else{//非数字型Y-m-d 格式化为 Y/m/d
Val = Val.toString();
Val = Val.replace(/-/g,'/');
}
Times = new Date(Val);
}
var D = {
Y: Times.getFullYear(), //年
M: Times.getMonth() + 1, //月
D: Times.getDate(), //日
H: Times.getHours(), //时
I: Times.getMinutes(), //分
S: Times.getSeconds(), //秒,
W: Times.getDay(), //周(原生)
timestamp: 0
};
D.timestamp = Times_to_Timestamp(D.Y, D.M, D.D, D.H, D.I, D.S);
return D;
}
/*function Times(Val) {
var Times = new Date(); //中国标准时间
if(Val) {
if(IEversion && Val){
Val = Val.toString();
Val = Val.replace(/-/g,'/');
if(/^[0-9]+$/.test(Val)){
Val = parseInt(Val);
}
}
Times = new Date(Val);
}
var D = {
Y: Times.getFullYear(), //年
M: Times.getMonth() + 1, //月
D: Times.getDate(), //日
H: Times.getHours(), //时
I: Times.getMinutes(), //分
S: Times.getSeconds(), //秒,
W: Times.getDay(), //周(原生)
timestamp: 0
};
D.timestamp = Times_to_Timestamp(D.Y, D.M, D.D, D.H, D.I, D.S);
return D;
}*/
/**
* 返回指定时间的10位时间戳
*/
function Times_to_Timestamp(y, m, d, h, i, s) {
if(!arguments.length) {
//如果没有传入则调当前系统时间
var dt = Times();
y = dt.Y;
m = dt.M;
d = dt.D;
h = dt.H;
i = dt.I;
s = dt.S;
} else {
if(y) y = y.toString();
if(m) m = m.toString();
if(d) d = d.toString();
if(h) h = h.toString();
if(i) i = i.toString();
if(s) s = s.toString();
}
if(y && (y.indexOf('-') != -1 || y.indexOf('/') != -1)) {
var dt = Times(y);
y = dt.Y;
m = dt.M;
d = dt.D;
h = dt.H;
i = dt.I;
s = dt.S;
}
var x = '-';
if(IEversion || Safariversion) {
x = '/';
}
var t = (y + x + (m ? m : '01') + x + (d ? d : '01') + ' ' + (h ? h : '00') + ':' + (i ? i : '00') + ':' + (s ? s : '00'));
return Math.round(Date.parse(t) / 1000);
}
/**
* 10位时间戳转日期正常日期
*/
function Timestamp_to_Times(t, s) {
if(t) {
t = parseInt(t) * 1000;
}
var dt = Times(t);
if(dt) {
dt.M = dt.M < 10 ? parseInt('0' + dt.M) : dt.M;
dt.D = dt.D < 10 ? parseInt('0' + dt.D) : dt.D;
dt.H = dt.H < 10 ? parseInt('0' + dt.H) : dt.H;
dt.I = dt.I < 10 ? parseInt('0' + dt.I) : dt.I;
dt.S = dt.S < 10 ? parseInt('0' + dt.S) : dt.S;
if(s) {
s = s.replace(/y/i, dt.Y);
s = s.replace(/m/i, dt.M);
s = s.replace(/d/i, dt.D);
s = s.replace(/h/i, dt.H);
s = s.replace(/i/i, dt.I);
s = s.replace(/s/i, dt.S);
return s;
} else {
return dt.Y + '-' + dt.M + '-' + dt.D + ' ' + dt.H + ':' + dt.I + ':' + dt.S;
}
}
}
/* 倒计时函数
* Author:cr180
* V = 传入日期格式 2016-09-01 01:00:00
* callFun = 回调函数 返回JSON {Y:*,M:*,D:*,H:*,I:*,S:*}
*/
function Down_Time(V, callFun) {
if(IEversion || Safariversion) {
V = V.replace(/-/g, '/');
}
var Time = Date.parse(new Date(V));
//把时间戳转换为日期格式
var Auto;
var Start = function() {
Time = Time - 1000;
var D = Times(Time);
var i;
for(i in D) {
if(D[i] < 10) {
D[i] = '0' + D[i];
}
}
if(callFun && typeof(callFun) == 'function') {
callFun(D, Auto);
}
}
Start();
Auto = window.setInterval(function() {
Start();
}, 1000);
}
/* canvas画圆
* BOX 对象ID , WIDTH 圆圈宽度 , N画圆百分比
*/
function Angle_m(BOX, N, LineW, Color) {
var canvas = document.getElementById(BOX),
width = canvas.width,
height = canvas.height,
OBJ = canvas.getContext('2d');
var Step, Start, End, Add = Math.PI * 2 / 100;
OBJ.shadowOffsetX = 0; // 设置水平位移
OBJ.shadowOffsetY = 0; // 设置垂直位移
OBJ.shadowBlur = 10; // 设置模糊度
OBJ.lineWidth = LineW ? LineW : 2; // 线宽度
//圆心位置
var X = canvas.width / 2;
//圆边缘衰减
var R = X - (OBJ.lineWidth / 2);
var S = 5;
var Int;
var Step = 1;
var Start = 0;
OBJ.strokeStyle = Color ? Color : '#000'; //圆圈颜色
Int = setInterval(function() {
if(Step <= N) {
End = Start + Add;
ARC(Start, End);
Start = End;
Step++;
} else {
clearInterval(Int);
}
}, S);
var ARC = function(s, e) {
OBJ.beginPath();
OBJ.arc(X, X, R, s, e, false);
OBJ.stroke();
}
}
var WIN_ZINDEX = 200;
function hideshowWin(K, Remove) {
$('#' + K).fadeOut(200, function() {
if(Remove) {
$('#' + K).remove();
}
});
var cover = $('#' + K + '_showcover');
if(cover.length) {
if(Remove) {
cover.remove();
} else {
cover.remove();
}
}
}
function dragMenu(menuObj, e, op) {
var Obj = $('#' + menuObj);
e = e ? e : window.event;
var posX;
var posY;
var Move = function(e) {
if(e == null)
e = window.event; //IE
Obj.css('left', (e.clientX - posX) + "px");
Obj.css('top', (e.clientY - posY) + "px");
}
Obj.mousedown(function(e) {
e = e ? e : window.event;
posX = e.clientX - parseInt(Obj.offset().left);
posY = e.clientY - parseInt(Obj.offset().top);
$(document).bind('mousemove', Move);
});
Obj.mouseup(function() {
$(document).unbind('mousemove', Move);
});
}
function showWin_error(IDobj, msg) {
var IDobj = $('#' + IDobj);
if(IDobj.length) {
var msg = '<div class="win_error_c">' + msg + '</div>';
var errOBJ = IDobj.find('.win_error');
errOBJ.html(msg);
errOBJ.fadeIn(300);
window.setTimeout(function() {
errOBJ.html(msg).fadeOut(1000);
}, 1000);
}
}
function showWin(ID, title, Url) {
var K = ID;
var Msg = '';
var Class = '';
var IDobj = $('#' + ID);
if(IDobj.length && IDobj.attr('prepend') != '1') {
WIN_ZINDEX++;
Msg = IDobj.html();
Class = IDobj.attr('class');
IDobj.remove();
IDobj = null;
}
if(!IDobj.length || IDobj.attr('prepend') != '1') {
var obj = document.createElement('div');
obj.id = ID;
obj.className = 'winbox';
obj.style.display = 'none';
$('#append_parent').append(obj);
IDobj = $(obj);
obj = null;
var title = title ? title : IDobj.attr('title');
if(!IDobj.hasClass('winbox')) IDobj.addClass('winbox');
var H = '<div class="win_t" style="cursor:move" οnmοusedοwn="dragMenu(\'' + K + '\', event, 1)" οndblclick="hideshowWin(\'' + K + '\')">';
H += '<a href="javascript:;" class="win_close" οnclick="hideshowWin(\'' + K + '\')">x</a>';
H += '<div class="win_tit ' + Class + '">' + title + '</div>';
H += '</div>';
H += '<div class="win_c" id="win_' + K + '_c">' + Msg + '</div>';
if(Url) {
_Ajax(Url, {}, function(data) {
$('#win_' + K + '_c').html(data);
}, {
dataType: 'XML'
});
}
IDobj.prepend(H).attr('prepend', 1);
}
var M_left = ($(window).outerWidth() - IDobj.outerWidth(true)) / 2;
var M_top = ($(window).outerHeight() - IDobj.outerHeight(true)) / 2;
IDobj.fadeIn(200).css({
'left': M_left + 'px',
'top': M_top + 'px'
});
IDobj.css('z-index', WIN_ZINDEX);
}
/* 全局消息提示类
* Author:cr180
* 参数:
* 1. ID = 唯一标识或box ID,如IDbox已存在,则读取该模型中的HTML插入到showbox中
* 2. Title = 框架标题
* 3. Msg = 提示内容
* 4. Mod = 模式 值(0||null=普通HTML / 1=操作成功提示类 / 2=操作失败类 / 3=警告提示类) Msg参数必须存在
* 5. TimeOut = 自动关闭该提示框时间(秒) 0=不自动关闭
* 6. CallFun = 回调函数,运行结束后的回调,传入function(obj){... obj为当前box jQuery对象}
* 7. isConfirm = 确认提示框按钮模式,0=否 1=自动追加一个确认按钮 2=自动追加确认与取消按钮
* 两个按钮的触发事件在回调函数中自定义,取消按钮默认关闭当前窗口
*/
function showDialog(ID, Title, Msg, Mod, TimeOut, CallFun, isConfirm) {
var K = ID;
var obj = $('#' + ID);
if(obj.length>0){
return false;
}
if(ID && !Msg && obj.length) {
Msg = obj.html();
obj.remove();
obj = null;
} else if(Mod && Msg) {
Msg = '<div class="showmsg_c">' + Msg + '</div>';
}
obj = document.createElement('div');
obj.id = ID;
obj.className = 'showbox' + (Mod && Msg ? ' showmsgmod showmsgmod_' + Mod : '');
obj.style.display = 'none';
$('#append_parent').append(obj);
obj = $(obj);
var H = '<div class="show_t">';
H += '<a href="javascript:;" class="show_close" οnclick="hideshowWin(\'' + K + '\',1)">x</a>';
if(!Mod) {
H += '<div class="show_tit">' + Title + '</div>';
}
H += '</div>';
if(Mod) {
H += '<div class="show_modtit">' + Title + '</div>';
}
H += '<div class="show_c" id="win_' + K + '_c">' + Msg + '</div>';
if(isConfirm && (isConfirm == 1 || isConfirm == 2)) {
H += '<div class="cl show_btn ' + (isConfirm == 2 ? 'show_btn_float' : '') + '">';
H += '<a href="javascript:;" class="btn show_submit">确 定</a>';
if(isConfirm == 2) {
H += '<a href="javascript:;" class="btn show_exit" οnclick="hideshowWin(\'' + K + '\',1)">取 消</a>';
}
H += '</div>';
}
obj.html(H);
var M_left = ($(window).outerWidth() - obj.outerWidth(true)) / 2;
var M_top = ($(window).outerHeight() - obj.outerHeight(true)) / 2;
obj.fadeIn(200).css({
'left': M_left + 'px',
'top': M_top + 'px'
});
WIN_ZINDEX++;
$('#append_parent').append('<div id="' + K + '_showcover" class="showcover" style="z-index:' + (WIN_ZINDEX) + '"></div>');
WIN_ZINDEX++;
obj.css('z-index', WIN_ZINDEX);
if(TimeOut > 0) {
TimeOut = TimeOut - 1;
window.setTimeout(function() {
hideshowWin(K, 1);
if(CallFun && typeof CallFun == 'function') {
CallFun($('#' + ID));
}
}, TimeOut * 1000);
} else {
if(CallFun && typeof CallFun == 'function') {
CallFun($('#' + ID));
}
}
}
/**
* 全局迷你提示
* 鼠标经过显示,离开移除
*/
function showTips(obj,Html){
obj = $(obj);
var O = document.createElement('div');
O = $(O);
obj.hover(function(){
O.addClass('ptips').html(Html).css({left: $(this).offset().left -20, top: $(this).offset().top + obj.outerHeight(true)});
$('#append_parent').append(O);
},function(){
O.remove();
});
}
function showMenu(ID, AutoHide) {
var X;
var Obj = $('#' + ID);
var To = $('#' + ID + '_menu');
var L = Obj.offset().left;
var T = Obj.offset().top + Obj.outerHeight(true);
if(To.css('display') != 'none') {
if(X)
clearTimeout(X);
To.fadeOut(100);
Obj.removeClass('a');
return;
}
if($(window).outerWidth(true) - (L + To.outerWidth(true)) < 10) {
L -= To.outerWidth(true);
L += Obj.outerWidth(true);
}
To.css({
'left': L,
'top': T
}).fadeIn(100);
Obj.addClass('a');
var Hover = function(o) {
o.hover(function() {
clearTimeout(X);
Obj.addClass('a');
}, function() {
clearTimeout(X);
X = window.setTimeout(function() {
To.fadeOut(100);
Obj.removeClass('a');
}, 100);
});
}
if(AutoHide) {
Hover(Obj);
Hover(To);
}
}
function cp_showDialog(msg, outTime) {
if(msg == 'close') {
$('#showLoadding ,#showLoadding_bg').remove();
} else {
msg = msg ? msg : '请稍后...';
$('#append_parent').append('<div id="showLoadding" class="showLoadding"><span>' + msg + '</span></div><div id="showLoadding_bg" class="showLoadding_bg"></div>');
$('#showLoadding ,#showLoadding_bg').fadeIn(200, function() {
$(this).addClass('a');
});
outTime = parseInt(outTime);
if(outTime) {
setTimeout(function() {
cp_showDialog('close');
}, outTime * 1000);
}
}
}
function cp_ajaxPOST(Formobj) {
var Formobj = $(Formobj);
$.ajax({
type: "POST",
dataType: "json",
url: Formobj.attr('action'),
data: Formobj.serialize(),
success: function(result) {
alert(result);
},
error: function(status) {
alert('Error:' + status);
}
});
return false;
}
/*
* 货币转换函数
* 传入int值(支持浮点小数点)
*/
function money_Format(V) {
if(V) {
V = ('' + V + '');
var Z; //小数点
//提取小数点位
if(/\./g.test(V)) {
V.replace(/(\d+)\.(\d+)/g, function($1, $2, $3) {
V = $2;
Z = $3;
});
}
V = V + '#';
var re = /\d{3}#/;
var S = '';
while(re.test(V)) {
V = V.replace(/(\d{3})#/, function($1, $2) {
return '#' + $2 + S;
});
S = ',';
}
V = V.replace(/#/, '');
if(Z) {
V = V + '.' + Z;
}
return V;
}
}
/**
* 工具函数
*/
var utils = {
/**
* 处理进度数据
* @param {type} data
*/
handleProgressData: function handleProgressData(data) {
var data = Number(data) * 100;
if(data > 0 && data <= 1) {
data = 1
} else if(data <= 99) {
data = Math.floor(data)
} else if(data < 100) {
data = 99
} else {
data = 100
}
return data + "%"
},
/**
* 得到url的查询字符
* @param {string} name
*/
getQueryString : function getQueryString(name) {
if(name != null && name.toString().length > 1) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r != null){
return unescape(r[2]);
}
}
return "";
},
getCookie : function getCookie(c_name){
if (document.cookie.length>0){
var c_start=document.cookie.indexOf(c_name + "=");
if (c_start!==-1){
c_start=c_start + c_name.length+1 ;
var c_end=document.cookie.indexOf(";",c_start);
if (c_end===-1){
c_end=document.cookie.length;
}
return unescape(document.cookie.substring(c_start,c_end));
}
}
return "";
},
formatMoney:function formatMoney(value){
if(value!=""&&value!=null){
var num = value + "";
num = num.toString().replace(/\$|\,/g,'');
// 获取符号(正/负数)
sign = (num == (num = Math.abs(num)));
// 把指定的小数位先转换成整数.多余的小数位四舍五入
num = Math.floor(num*Math.pow(10,2)+0.50000000001);
// 求出小数位数值
cents = num%Math.pow(10,2);
// 求出整数位数值
num = Math.floor(num/Math.pow(10,2)).toString();
// 把小数位转换成字符串,以便求小数位长度
cents = cents.toString();
// 补足小数位到指定的位数
while(cents.length<2)
cents = "0" + cents;
// 对整数部分进行千分位格式化.
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + num + '.' + cents);
}else{
if(value=='0'){
return '0.00';
}else{
return value;
}
}
}
}
/**
* 系统值
*/
var sys = {
// 预期收益计算公式
Interest : {
/**
* 按月付息到期还本
*/
interest: function(amount, year_ratio, month) {
var month_ratio = year_ratio / 100 / 12;
var total_interest = amount * month_ratio * month;
return {
total_interest: total_interest.toFixed(2),
total: (amount + total_interest).toFixed(2)
};
},
/**
* 一次性还本付息
*/
bullet_repayment: function(amount, year_ratio, month, days) {
var month_ratio = year_ratio / 100 / 12;
var day_ratio = year_ratio / 100 / 365;
var total_interest = amount * month_ratio * month + amount * day_ratio * days;
return {
total_interest: total_interest.toFixed(2),
total: (amount + total_interest).toFixed(2)
};
},
/**
* 等额本息
*/
equal_installment: function(amount, year_ratio, month) {
var month_ratio = year_ratio / 100 / 12;
var repay_monthly = amount * ((month_ratio * Math.pow(1 + month_ratio, month)) / (Math.pow(1 + month_ratio, month) - 1));
var total_interest = repay_monthly * month - amount;
return {
total_interest: total_interest.toFixed(2),
repay_monthly: repay_monthly.toFixed(2),
total: (amount + total_interest).toFixed(2)
};
},
/**
* 等额本金
*/
equal_principal: function(amount, year_ratio, month) {
var month_ratio = year_ratio / 100 / 12;
var principal_monthly = amount / month;
var total_interest = 0;
var repay_first = principal_monthly;
for(var i = 0; i < month; i++) {
total_interest += ((amount - principal_monthly * i) * month_ratio);
if(i === 0) {
repay_first += total_interest;
}
}
return {
total_interest: total_interest.toFixed(2), // 总利息
repay_first: repay_first, // 首次还款金额
total: (amount + total_interest).toFixed(2) // 本息合计
};
},
calc: function(amount, year_radio, month, days, repayment) {
var result,
othis=this;
if(repayment === 'INTEREST') {
result = sys.Interest.interest(amount, year_radio, month);
} else if(repayment === 'BULLET_REPAYMENT') {
if(month !== 0) {
result = sys.Interest.bullet_repayment(amount, year_radio, month, 0);
} else {
result = sys.Interest.bullet_repayment(amount, year_radio, 0, days);
}
} else if(repayment === 'EQUAL_INSTALLMENT') {
result = sys.Interest.equal_installment(amount, year_radio, month);
} else if(repayment === 'EQUAL_PRINCIPAL') {
result = sys.Interest.equal_principal(amount, year_radio, month);
}
return result;
}
}
}
常用公共js
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.