/*
* 公用方法
*/
var mtCommon = mtCommon || {};
(function () {
var yyTap = 'ontouchstart' in document ? 'touchend' : 'click';
mtCommon.searchEvent = function () {
$(".mtpl-searchBtn").on(yyTap, function () {
var keyWord = $(".mtpl-schbox input").val();
if (!keyWord) return;
mtCommon.searchWord.setSearchWords(keyWord);
keyWord = encodeURIComponent(keyWord);
location.href = "/product/list?key=" + keyWord;
})
$(".mtpl-schbox input").off('keypress').on('keypress', function (e) {
if (e.keyCode == '13') { //按下回车
e.preventDefault();
var keyWord = $(this).val();
if (!keyWord) return;
location.href = "/product/list?key=" + keyWord;
}
})
}
//时间戳转年月日
mtCommon.unixTime_get_yyyy_MM_dd = function (millinSeconds) {
var date = new Date(millinSeconds);
var year = date.getFullYear(); //年
var monthArr = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
var month = monthArr[date.getMonth()]; //月
var day = date.getDate();
if (day < 10)
day = "0" + day;
return year + "年" + month + "月" + day+"日";
}
//时间戳转年月日
mtCommon.unixTime_get_yyyy=function (millinSeconds) {
var date = new Date(millinSeconds);
var year = date.getFullYear(); //年
return year;
}
mtCommon.unixTime_get_mm = function (millinSeconds) {
var date = new Date(millinSeconds);
var monthArr = new Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
var month = monthArr[date.getMonth()]; //月
return month;
}
mtCommon.unixTime_get_dd = function (millinSeconds) {
var date = new Date(millinSeconds);
var day = date.getDate();
if (day < 10)
day = "0" + day;
return day;
}
mtCommon.noDataHtml = function () {
var str = "";
str += '
';
str += '
';
str += '
';
if (yylocalize) {
str += yylocalize['noData'];
}
str += '
';
str += '
';
return str;
}
//滚动至某个锚点
mtCommon.scrollToTop=function () {
var winWt = parseInt($(window).width());
if (winWt < 992) {
$("html, body").animate({
scrollTop: $("#list").offset().top - parseInt($('.header').get(0).offsetHeight + 20)
}, { duration: 500, easing: "swing" });
}
else {
$("html, body").animate({
scrollTop: $("#list").offset().top - 10
}, { duration: 500, easing: "swing" });
}
}
//获取URL参数
mtCommon.getQueryString=function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return decodeURI(r[2]); return null;
}
//字段过滤
var arrMg = ["alert", "confirm", "prompt", "http", "embed", "script", "onload", "position", "iframe", "http", "https", "location", "href", "meta", "object", "document", "onmouseover", "write", "autofocus", "onfocus", "SELECT", "like", "UNION", "UPDATE", "TRUNCATE"];
mtCommon.filter=function(inputContent) {
// 显示的内容--showContent
var showContent = inputContent;
// 正则表达式
if (inputContent != null && inputContent != "") {
for (var i = 0; i < arrMg.length; i++) {
// 创建一个正则表达式
var r = new RegExp(arrMg[i], "ig");
showContent = showContent.replace(r, "*");
showContent = showContent.replace(/(^\s*)|(\s*$)/g, "");
}
}
// 显示的内容--showInput
return showContent;
}
//腾讯股票信息
mtCommon.getStock = function (stockCode) {
$.ajax({
type: "get",
url: "https://qt.gtimg.cn/q=" + stockCode,
success: function (data) {
console.log(data);
var stockInfo = data.split('~');
return stockInfo;
},
error: function () {
}
});
}
//微信二维码 需与qrcode.min.js一块使用
mtCommon.weChat = function () {
new QRCode(document.getElementById("qrcode"), {
text: location.href, //设置二维码内容
render: "canvas", //设置渲染方式
width: 100, //设置宽度,默认生成的二维码大小是 256×256
height: 100, //设置高度
typeNumber: -1, //计算模式
background: "#ffffff", //背景颜色
foreground: "#000000" //前景颜色
});
}
//paramName=参数名称,replaceWith=参数值,isRefresh=是否刷新(0不刷新,1刷新)
//使用方法:replaceParamVal("userId", "333", 0)
mtCommon.changeURLArg = function (title,paramName, replaceWith) {
var stateObject = {};
var oUrl = window.location.href.toString();
var re = eval('/(' + paramName + '=)([^&]*)/gi');
var nUrl = oUrl.replace(re, paramName + '=' + replaceWith);
history.pushState(stateObject, title, nUrl); //进行修改
}
mtCommon.getfilesize = function (size) {
if (!size) return "";
var num = 1024.8; //byte
if (size < num)
return size + "B";
if (size < Math.pow(num, 2))
return (size / num).toFixed(2) + "KB"; //kbif (size < Math.pow(num, 3))
return (size / Math.pow(num, 2)).toFixed(2) + "MB"; //M
if (size < Math.pow(num, 4))
return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G
return (size / Math.pow(num, 4)).toFixed(2) + "T"; //T
}
})();