var util = {
isWx: function(){
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i) == 'micromessenger'){
return true;
}else{
return false;
}
},
previewImg: function(objs, obj, ua, f) {
if(!ua){
ua = this.isWx()? 'wx' : 'h5';
}
if(ua == 'wx') {
var imgArray = [],
curImageSrc = f ? obj.data('src') : obj.attr('src'),
href = obj.parent().attr('href'),
hasHref = !href || href == 'javascript:;' || href == 'javascript:void(0);';
if(curImageSrc && hasHref) {
objs.each(function(index, el) {
var itemSrc = f ? $(el).data('src') : $(el).attr('src');
imgArray.push(itemSrc);
});
wx.previewImage({
current: curImageSrc,
urls: imgArray
});
}
} else {
if(location.href.split('?')[0].substr(-4) == 'html'){
$('body').append('');
$('body').append('
');
var idx = f ? obj.closest('.preview').index() : obj.closest('.item').index();
var html = [];
html.push('');
for(var i = 0; i < objs.length; i++) {
html.push('');
}
html.push('
');
$('#bigImg .bd').append(html.join(''));
var winh = $(window).height();
$('#bigImg').show();
TouchSlide({
slideCell: '#bigImg',
mainCell: '.bd ul',
effect: 'left',
defaultIndex: idx
});
$('#bigImg').find('.bd li').each(function() {
var _this = $(this);
$(this).find('img')[0].onload = function(){
var h = _this.height();
_this.css({
'margin-top': (winh - h) / 2 + 'px'
});
}
});
$('body').on('click', '#bigImg', function(){
$(this).remove();
});
}
}
},
numJoin: function(value){ //111,111.00
var newValue = '';
var count = 0;
var v = value.toString().split('.'); //取小数部分
var r = '';
if (v[1]) {
r = '.' + v[1].substr(0, 2);
} else { }
if (value > 999) {
value = v[0];
for (var i = value.length - 1; i >= 0; i--) {
count++;
if (count % 3 == 0 && i > 0) {
newValue += value[i] + ',';
} else {
newValue += value[i];
}
}
newValue = newValue.split('').reverse().join('') + r;
} else {
newValue = v[0] + r;
}
return newValue;
},
telValidate:function(tel){
var reg = /^(13[0-9]|14[579]|15[0-3,5-9]|17[0135678]|18[0-9])\d{8}$/;
if(!reg.test(tel)){
return false;
}else{
return true;
}
},
formatNumber: function (num, precision, separator) { //111,111.00
var parts;
// 判断是否为数字
if (!isNaN(parseFloat(num)) && isFinite(num)) {
// 把类似 .5, 5. 之类的数据转化成0.5, 5, 为数据精度处理做准, 至于为什么
// 不在判断中直接写 if (!isNaN(num = parseFloat(num)) && isFinite(num))
// 是因为parseFloat有一个奇怪的精度问题, 比如 parseFloat(12312312.1234567119)
// 的值变成了 12312312.123456713
num = Number(num);
// 处理小数点位数
num = (typeof precision !== 'undefined' ? num.toFixed(precision) : num).toString();
// 分离数字的小数部分和整数部分
parts = num.split('.');
// 整数部分加[separator]分隔, 借用一个着名的正则表达式
parts[0] = parts[0].toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + (separator || ','));
return parts.join('.');
}
return NaN;
},
formatTime: function(ts, div, flag){
var dd = new Date(ts);
var y = dd.getFullYear();
var m = dd.getMonth() + 1;
var d = dd.getDate();
var h = dd.getHours();
var mm = dd.getMinutes();
var s = dd.getSeconds();
return [y,m,d].map(this.formatNum).join(div) + ' ' + [h,mm,s].map(this.formatNum).join(':');
},
formatNum: function(n){
n = n.toString();
return n[1] ? n : '0' + n;
},
getcookie:function(name){
var strcookie = document.cookie;
var arrcookie = strcookie.split("; ");
for(var i = 0; i < arrcookie.length; i++) {
var arr = arrcookie[i].split("=");
if(arr[0] == name) return decodeURIComponent(arr[1]); //增加对特殊字符的解析
}
return "";
},
addcookie:function(name, value, expireMin){
var cookieString = name + "=" + escape(value) + "; path=/";
//判断是否设置过期时间
if(expireMin > 0) {
var date = new Date();
date.setTime(date.getTime + expireMin * 60 * 1000);
cookieString = cookieString + "; expire=" + date.toGMTString();
}
document.cookie = cookieString;
},
showTips: function(txt, duration) {
if(!duration){
duration = 2000;
}
if($('#tips').length > 0) return false;
$('body').append('');
var $tip = $('#tips');
$tip.html(txt).fadeIn();
setTimeout(function() {
$tip.fadeOut(function() {
$tip.remove();
});
}, duration);
},
getPageStyle: function($objs){
var winw = $('body').width();
if($objs){
$objs.each(function(){
$(this).width(winw);
})
}
document.documentElement.style.fontSize = Math.round(winw * 10 / 375) + 90 + 'px';
},
getQueryString:function(name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
var r = window.location.search.substr(1).match(reg);
if(r != null)
return decodeURI(r[2]);
return null;
},
showPayLoading: function(txt){
$('body').append(''+txt+'
');
$('#loading').fadeIn();
},
setPayTxt: function(txt){
$('#load p').html(txt);
},
hidePayLoading: function(){
$('#loading').remove();
},
showLoading: function(txt){
txt = txt ? txt : '加载中...';
var html = ''+ txt +'
'
$('body').append(html);
},
hideLoading: function(){
$('#loadingBox').remove();
},
imgFix: function(f){
var $imgs = $('img[mode=fix]');
$imgs.each(function(){
if(!f){
var flag = false;
this.onload = function(){
var h = $(this).parent().height();
var hh = $(this).height();
if(hh < h){
$(this).css({width: 'auto', height: '100%', transform: 'translate3d(-50%, 0, 0)', left: '50%', top: 0});
}
flag = true;
}
setTimeout(function(){
if(!flag){
var h = $(this).parent().height();
var hh = $(this).height();
if(hh < h){
$(this).css({width: 'auto', height: '100%', transform: 'translate3d(-50%, 0, 0)', left: '50%', top: 0});
}
}
}.bind(this),100);
}else{
var h = $(this).parent().height();
var hh = $(this).height();
if(hh < h){
$(this).css({width: 'auto', height: '100%', transform: 'translate3d(-50%, 0, 0)', left: '50%', top: 0});
}
}
})
},
lazyload: function(obj){
var container = obj || window;
$("img.lazy").lazyload({
'effect': 'fadeIn',
'threshold': 100,
'event': 'scroll',
'container': container,
'failurelimit': 2
});
},
wxShare: function(obj){
$.ajax({
type: "get",
url: 'https://www.17xs.org/vote/share.do',
data: {url: location.href},
success: function(res){
if(res.code == 1){
var r = res.result;
wx.config({
debug: false,
appId: r.appId,
timestamp: r.timestamp,
nonceStr: r.noncestr,
signature: r.signature,
jsApiList: ['onMenuShareAppMessage', 'onMenuShareTimeline', 'previewImage']
});
wx.ready(function() {
wx.onMenuShareAppMessage({
title: '【善园网】'+ obj.title,
desc: obj.desc,
link: obj.link,
imgUrl: obj.imgUrl,
type: 'link',
success: function() {},
cancel: function() {}
});
wx.onMenuShareTimeline({
title: '【善园网】'+ obj.title,
link: obj.link,
imgUrl: obj.imgUrl,
success: function() {},
cancel: function() {}
});
});
}else{
this.showTips('分享参数出错!');
}
}
});
},
pageNavDBClick: (function(){
if($('#pageNav').length > 0){
var startTime = 0;
$('body').on('click', '#pageNav .item.on', function(){
if($(this).hasClass('cen')) return;
if(new Date().getTime() - startTime < 500){
window.location.reload();
}else{
startTime = new Date().getTime();
}
});
}
return false;
})()
};