common常用方法和部分算法
var commonindex = function() {}; commonindex.prototype = { ajaxRequest: function(request) { $.ajax({ type: 'POST', url: request.url, data: request.data, // dataType: 'json', // contentType: "application/json", success: function(data) { //console.log(data); request.handler_success(data); }, error: function(xhr, type) { request.handler_ex(xhr); } }); }, toast: function(msg) { var str = "<div class='toast' style='display:none'>" + msg + "</div>"; $('body').append(str); $('.toast').stop().fadeIn(100).delay(1000).fadeOut(400); //fade out after 3 seconds setTimeout(function() { $('.toast').remove(); }, 3000); }, reset: function(ok, cannel) { $("#toggleCSS").attr("href", "../../style/alertify.default.css"); alertify.set({ labels: { ok: ok, cancel: cannel }, delay: 5000, buttonReverse: false, buttonFocus: "ok" }); }, getChannelDomain: function(type) { switch (type) { case 'meizu': return config.caiqr_domain; break; case 'caiqiu': return config.caiqr_domain_mobile; break; } }, getReferrer: function() { var referrer = ''; try { referrer = window.top.document.referrer; } catch (e) { if (window.parent) { try { referrer = window.parent.document.referrer; } catch (e2) { referrer = ''; } } } if (referrer === '') { referrer = document.referrer; } return referrer; }, // 数组内排序 systemSort: function(arr) { return arr.sort(function(a, b) { return a - b; }); }, // 从指定数组中随机n个数字 getArrayItems: function(arr, num) { //新建一个数组,将传入的数组复制过来,用于运算,而不要直接操作传入的数组; var temp_array = new Array(); arr.forEach(function(item) { temp_array.push(item); }); //取出的数值项,保存在此数组 var return_array = new Array(); for (var i = 0; i < num; i++) { //判断如果数组还有可以取出的元素,以防下标越界 if (temp_array.length > 0) { //在数组中产生一个随机索引 var arrIndex = Math.floor(Math.random() * temp_array.length); //将此随机索引的对应的数组元素值复制出来 return_array[i] = temp_array[arrIndex]; //然后删掉此索引的数组元素,这时候temp_array变为新的数组 temp_array.splice(arrIndex, 1); } else { //数组中数据项取完后,退出循环,比如数组本来只有10项,但要求取出20项. break; } } return return_array; }, // ajax get json getJsonRequest: function(request) { $.getJSON(request.url, function(responsedata, status, xhr) { console.log('status======', status); if (status == 'success') { request.handler_success(responsedata); } else { request.handler_ex(xhr); } }); }, // 生成随机数函数 randomFn: function(max, min) { return parseInt(Math.random() * (max - min + 1) + min, 10); }, // 从max -min 中挑选n个不重复的数字 randomNrepatFn: function(n, min, max) { var arr = []; for (i = 0; i < n; i++) { arr[i] = parseInt(Math.random() * (max - min + 1) + min); for (j = 0; j < i; j++) { if (arr[i] == arr[j]) { i = i - 1; break; } } } return arr; }, //倒计时插件,doc元素节点,time:倒计时时间秒数,fun:回调函数,计时完成后回调该函数 getCountdown: function(doc, time, fun) { if (doc) { var minnew = Math.floor(time / 60); var secnew = Math.floor(time % 60); var hournew = 0; if (time > 60) { minnew = parseInt(time / 60); if (minnew > 60) { hournew = parseInt(min / 60); minnew = parseInt(min % 60); } else { hournew = 0; } } // 单位数加0 if (hournew < 10) { hournew = '0' + hournew; } if (secnew < 10) { secnew = '0' + secnew; } if (minnew < 10) { minnew = '0' + minnew; } // 展示问题 if (hournew > 0 && minnew > 0) { doc.innerText = hournew + ":" + minnew + ":" + secnew; } else if (hournew == 0 && minnew > 0) { doc.innerText = minnew + ":" + secnew; } else if (hournew == 0 && minnew == 0) { doc.innerText = '00:' + secnew; } } var timer = setInterval(function() { var min = Math.floor(time / 60); var sec = Math.floor(time % 60); var hour = 0; if (time > 60) { min = parseInt(time / 60); if (min > 60) { hour = parseInt(min / 60); min = parseInt(min % 60); } else { hour = 0; } } time--; // 单位数加0 if (hour < 10) { hour = '0' + hour; } if (sec < 10) { sec = '0' + sec; } if (min < 10) { min = '0' + min; } // 展示问题 if (hour > 0 && min > 0) { doc.innerText = hour + ":" + min + ":" + sec; } else if (hour == 0 && min > 0) { doc.innerText = min + ":" + sec; } else if (hour == 0 && min == 0) { doc.innerText = '00:' + sec; } // 执行回调函数 if (time <= 0) { fun(); clearInterval(timer); return; } }, 1000); }, // 大数组内进行排列组合 comarray: function(arr) { var sarr = [ [] ]; for (var i = 0; i < arr.length; i++) { var tarr = []; for (var j = 0; j < sarr.length; j++) for (var k = 0; k < arr[i].length; k++) { tarr.push(sarr[j].concat(arr[i][k])); } sarr = tarr; } return sarr; }, //大写转化为小写 toLowerCase: function(comeStr) { return comeStr.toLowerCase() } }; //数组的组合 Array.prototype.combinate = function(iItems, aIn) { if (!aIn) { var aIn = new Array(); this.combinate.aResult = new Array(); } for (var i = 0; i < this.length; i++) { var a = aIn.concat(this[i]); var aRest = this.concat(); aRest.splice(0, i + 1); if (iItems && iItems - 1 <= aRest.length) { aRest.combinate(iItems - 1, a); if (iItems == 1) this.combinate.aResult.push(a); } } return this.combinate.aResult; }; Array.prototype.add = function(number) { return this.map(function(item) { return item + number; }); }; //根据域名修改渠道id var commonObject = new commonindex(); commonindex.prototype.ajaxRequest = function(request) { if (window.location.href.indexOf(commonObject.getChannelDomain('meizu')) > -1) { request.data.channel = "meizuh5"; request.data.client_type = '4'; } if (window.location.href.indexOf(commonObject.getChannelDomain('caiqiu')) > -1) { request.data.channel = "lingshengcaijiandashi01"; } $.ajax({ type: 'POST', url: request.url, data: request.data, // dataType: 'json', // contentType: "application/json", success: function(data) { //console.log(data); request.handler_success(data); }, error: function(xhr, type) { request.handler_ex(xhr); } }); };