补充
class TypeFn {
isString (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'String'
}
isNumber (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Number'
}
isBoolean (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Boolean'
}
isFunction (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Function'
}
isNull (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Null'
}
isUndefined (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Undefined'
}
isObj (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Object'
}
isArray (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Array'
}
isDate (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Date'
}
isRegExp (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'RegExp'
}
isError (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Error'
}
isSymbol (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Symbol'
}
isPromise (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Promise'
}
isSet (o) {
return Object.prototype.toString.call(o).slice(8, -1) === 'Set'
}
isFalse (o) {
if (o == '' || o == undefined || o == null || o == 'null' || o == 'undefined' || o == 0 || o == false || o == NaN) return true
return false
}
isTrue (o) {
return !this.isFalse(o)
}
isIos () {
var u = navigator.userAgent;
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
return false
} else if (u.indexOf('iPhone') > -1) {
return true
} else if (u.indexOf('iPad') > -1) {
return false
} else if (u.indexOf('Windows Phone') > -1) {
return false
}else{
return false
}
}
isPC () {
var userAgentInfo = navigator.userAgent;
var Agents = ["Android", "iPhone",
"SymbianOS", "Windows Phone",
"iPad", "iPod"];
var flag = true;
for (var v = 0; v < Agents.length; v++) {
if (userAgentInfo.indexOf(Agents[v]) > 0) {
flag = false;
break;
}
}
return flag;
}
browserType(){
var userAgent = navigator.userAgent;
var isOpera = userAgent.indexOf("Opera") > -1;
var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera;
var isIE11 = userAgent.indexOf('Trident') > -1 && userAgent.indexOf("rv:11.0") > -1;
var isEdge = userAgent.indexOf("Edge") > -1 && !isIE;
var isFF = userAgent.indexOf("Firefox") > -1;
var isSafari = userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") == -1;
var isChrome = userAgent.indexOf("Chrome") > -1 && userAgent.indexOf("Safari") > -1;
if (isIE) {
var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
reIE.test(userAgent);
var fIEVersion = parseFloat(RegExp["$1"]);
if(fIEVersion == 7) return "IE7"
else if(fIEVersion == 8) return "IE8";
else if(fIEVersion == 9) return "IE9";
else if(fIEVersion == 10) return "IE10";
else return "IE7以下"
}
if (isIE11) return 'IE11';
if (isEdge) return "Edge";
if (isFF) return "FF";
if (isOpera) return "Opera";
if (isSafari) return "Safari";
if (isChrome) return "Chrome";
}
checkStr (str, type) {
switch (type) {
case 'phone':
return /^1[3|4|5|6|7|8][0-9]{9}$/.test(str);
case 'tel':
return /^(0\d{2,3}-\d{7,8})(-\d{1,4})?$/.test(str);
case 'card':
return /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(str);
case 'pwd':
return /^[a-zA-Z]\w{5,17}$/.test(str)
case 'postal':
return /[1-9]\d{5}(?!\d)/.test(str);
case 'QQ':
return /^[1-9][0-9]{4,9}$/.test(str);
case 'email':
return /^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/.test(str);
case 'money':
return /^\d*(?:\.\d{0,2})?$/.test(str);
case 'URL':
return /(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/.test(str)
case 'IP':
return /((?:(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|[01]?\\d?\\d))/.test(str);
case 'date':
return /^(\d{4})\-(\d{2})\-(\d{2}) (\d{2})(?:\:\d{2}|:(\d{2}):(\d{2}))$/.test(str) || /^(\d{4})\-(\d{2})\-(\d{2})$/.test(str)
case 'number':
return /^[0-9]$/.test(str);
case 'english':
return /^[a-zA-Z]+$/.test(str);
case 'chinese':
return /^[\u4E00-\u9FA5]+$/.test(str);
case 'lower':
return /^[a-z]+$/.test(str);
case 'upper':
return /^[A-Z]+$/.test(str);
case 'HTML':
return /<("[^"]*"|'[^']*'|[^'">])*>/.test(str);
default:
return true;
}
}
isCardID(sId) {
if (!/(^\d{15}$)|(^\d{17}(\d|X|x)$)/.test(sId)) {
alert('你输入的身份证长度或格式错误')
return false
}
var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};
if(!aCity[parseInt(sId.substr(0,2))]) {
alert('你的身份证地区非法')
return false
}
var sBirthday=(sId.substr(6,4)+"-"+Number(sId.substr(10,2))+"-"+Number(sId.substr(12,2))).replace(/-/g,"/"),
d = new Date(sBirthday)
if(sBirthday != (d.getFullYear()+"/"+ (d.getMonth()+1) + "/" + d.getDate())) {
alert('身份证上的出生日期非法')
return false
}
var sum = 0,
weights = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2],
codes = "10X98765432"
for (var i = 0; i < sId.length - 1; i++) {
sum += sId[i] * weights[i];
}
var last = codes[sum % 11];
if (sId[sId.length-1] != last) {
alert('你输入的身份证号非法')
return false
}
return true
}
}
class DateFn {
formatTime(time, cFormat) {
if (arguments.length === 0) return null
if ((time + '').length === 10) {
time = +time * 1000
}
var format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}', date
if (typeof time === 'object') {
date = time
} else {
date = new Date(time)
}
var formatObj = {
y: date.getFullYear(),
m: date.getMonth() + 1,
d: date.getDate(),
h: date.getHours(),
i: date.getMinutes(),
s: date.getSeconds(),
a: date.getDay()
}
var time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
var value = formatObj[key]
if (key === 'a') return ['一', '二', '三', '四', '五', '六', '日'][value - 1]
if (result.length > 0 && value < 10) {
value = '0' + value
}
return value || 0
})
return time_str
}
getMonths(time, len, direction) {
var mm = new Date(time).getMonth()+1,
yy = new Date(time).getFullYear(),
direction = isNaN(direction) ? 3 : direction,
index = mm;
var cutMonth = function(index) {
var arr
if (direction === 1) {
arr = formatPre(index).reverse()
}else if(direction === 2) {
arr = formatNext(index)
}else{
arr = formatPre(index).reverse().slice(len/2).concat(formatNext(index).slice(1, len/2+1))
}
return arr.sort(function(t1, t2){
return new Date(t1).getTime() - new Date(t2).getTime()
})
}
var formatPre = function(index) {
var currNum = index,
preNum = 0,
currArr = [],
preArr = []
if (index-len < 0) {
preNum = len-currNum
}
for (var i = 0; i < currNum; i++) {
currArr.push([yy+'-'+(currNum-i)])
}
for (var i = 1; i <= preNum; i++) {
preArr.push([(yy-Math.ceil(i/12))+'-'+(12-(i-1)%12)])
}
return currArr.concat(preArr)
}
var formatNext = function(index) {
var currNum = 12-index,
nextNum = 0,
currArr = [],
nextArr = []
if (len-currNum > 0) {
nextNum = len-currNum
}
for (var i = 0; i <= currNum; i++) {
currArr.push([yy+'-'+(index+i)])
}
for (var i = 1; i < nextNum; i++) {
nextArr.push([(yy+Math.ceil(i/12))+'-'+(i%13 === 0 ? 1:i%13)])
}
return currArr.concat(nextArr)
}
return cutMonth(index)
}
getDays(time, len, diretion) {
var tt = new Date(time)
var getDay = function(day) {
var t = new Date(time)
t.setDate(t.getDate() + day)
var m = t.getMonth()+1
return t.getFullYear()+'-'+m+'-'+t.getDate()
}
var arr = []
if (diretion === 1) {
for (var i = 1; i <= len; i++) {
arr.unshift(getDay(-i))
}
}else if(diretion === 2) {
for (var i = 1; i <= len; i++) {
arr.push(getDay(i))
}
}else {
for (var i = 1; i <= len; i++) {
arr.unshift(getDay(-i))
}
arr.push(tt.getFullYear()+'-'+(tt.getMonth()+1)+'-'+tt.getDate())
for (var i = 1; i <= len; i++) {
arr.push(getDay(i))
}
}
return diretion === 1 ? arr.concat([tt.getFullYear()+'-'+(tt.getMonth()+1)+'-'+tt.getDate()]) :
diretion === 2 ? [tt.getFullYear()+'-'+(tt.getMonth()+1)+'-'+tt.getDate()].concat(arr) : arr
}
formatHMS (s) {
var str = ''
if (s > 3600) {
str = Math.floor(s/3600)+'h'+Math.floor(s%3600/60)+'m'+s%60+'s'
}else if(s > 60) {
str = Math.floor(s/60)+'m'+s%60+'s'
}else{
str = s%60+'s'
}
return str
}
getMonthOfDay (time) {
var date = new Date(time)
var year = date.getFullYear()
var mouth = date.getMonth() + 1
var days
if (mouth == 2) {
days = (year%4==0 && year%100==0 && year%400==0) || (year%4==0 && year%100!=0) ? 28 : 29
} else if (mouth == 1 || mouth == 3 || mouth == 5 || mouth == 7 || mouth == 8 || mouth == 10 || mouth == 12) {
days = 31
} else {
days = 30
}
return days
}
getYearOfDay (time) {
var firstDayYear = this.getFirstDayOfYear(time);
var lastDayYear = this.getLastDayOfYear(time);
var numSecond = (new Date(lastDayYear).getTime() - new Date(firstDayYear).getTime())/1000;
return Math.ceil(numSecond/(24*3600));
}
getFirstDayOfYear (time) {
var year = new Date(time).getFullYear();
return year + "-01-01 00:00:00";
}
getLastDayOfYear (time) {
var year = new Date(time).getFullYear();
var dateString = year + "-12-01 00:00:00";
var endDay = this.getMonthOfDay(dateString);
return year + "-12-" + endDay + " 23:59:59";
}
getDayOfYear (time) {
var firstDayYear = this.getFirstDayOfYear(time);
var numSecond = (new Date(time).getTime() - new Date(firstDayYear).getTime())/1000;
return Math.ceil(numSecond/(24*3600));
}
getDayOfYearWeek (time) {
var numdays = this.getDayOfYear(time);
return Math.ceil(numdays / 7);
}
}
class ArrayFn {
contains (arr, val) {
return arr.indexOf(val) != -1 ? true : false;
}
each (arr, fn) {
fn = fn || Function;
var a = [];
var args = Array.prototype.slice.call(arguments, 1);
for(var i = 0; i < arr.length; i++) {
var res = fn.apply(arr, [arr[i], i].concat(args));
if(res != null) a.push(res);
}
}
map (arr, fn, thisObj) {
var scope = thisObj || window;
var a = [];
for(var i = 0, j = arr.length; i < j; ++i) {
var res = fn.call(scope, arr[i], i, this);
if(res != null) a.push(res);
}
return a;
}
sort (arr, type = 1) {
return arr.sort( (a, b) => {
switch(type) {
case 1:
return a - b;
case 2:
return b - a;
case 3:
return Math.random() - 0.5;
default:
return arr;
}
})
}
unique (arr) {
if ( Array.hasOwnProperty('from') ) {
return Array.from(new Set(arr));
}else{
var n = {},r=[];
for(var i = 0; i < arr.length; i++){
if (!n[arr[i]]){
n[arr[i]] = true;
r.push(arr[i]);
}
}
return r;
}
}
union (a, b) {
var newArr = a.concat(b);
return this.unique(newArr);
}
intersect (a, b) {
var _this = this;
a = this.unique(a);
return this.map(a, function(o) {
return _this.contains(b, o) ? o : null;
});
}
remove (arr, ele) {
var index = arr.indexOf(ele);
if(index > -1) {
arr.splice(index, 1);
}
return arr;
}
formArray (ary) {
var arr = [];
if(Array.isArray(ary)) {
arr = ary;
} else {
arr = Array.prototype.slice.call(ary);
};
return arr;
}
max (arr) {
return Math.max.apply(null, arr);
}
min (arr) {
return Math.min.apply(null, arr);
}
sum (arr) {
return arr.reduce( (pre, cur) => {
return pre + cur
})
}
average (arr) {
return this.sum(arr)/arr.length
}
}
class StringFn {
trim (str, type) {
type = type || 1
switch (type) {
case 1:
return str.replace(/\s+/g, "");
case 2:
return str.replace(/(^\s*)|(\s*$)/g, "");
case 3:
return str.replace(/(^\s*)/g, "");
case 4:
return str.replace(/(\s*$)/g, "");
default:
return str;
}
}
changeCase (str, type) {
type = type || 4
switch (type) {
case 1:
return str.replace(/\b\w+\b/g, function (word) {
return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase();
});
case 2:
return str.replace(/\b\w+\b/g, function (word) {
return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase();
});
case 3:
return str.split('').map( function(word){
if (/[a-z]/.test(word)) {
return word.toUpperCase();
}else{
return word.toLowerCase()
}
}).join('')
case 4:
return str.toUpperCase();
case 5:
return str.toLowerCase();
default:
return str;
}
}
checkPwd (str) {
var Lv = 0;
if (str.length < 6) {
return Lv
}
if (/[0-9]/.test(str)) {
Lv++
}
if (/[a-z]/.test(str)) {
Lv++
}
if (/[A-Z]/.test(str)) {
Lv++
}
if (/[\.|-|_]/.test(str)) {
Lv++
}
return Lv;
}
filterTag (str) {
str = str.replace(/&/ig, "&");
str = str.replace(/</ig, "<");
str = str.replace(/>/ig, ">");
str = str.replace(" ", " ");
return str;
}
}
class NumberFn {
random (min, max) {
if (arguments.length === 2) {
return Math.floor(min + Math.random() * ( (max+1) - min ))
}else{
return null;
}
}
numberToChinese (num) {
var AA = new Array("零", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十");
var BB = new Array("", "十", "百", "仟", "萬", "億", "点", "");
var a = ("" + num).replace(/(^0*)/g, "").split("."),
k = 0,
re = "";
for(var i = a[0].length - 1; i >= 0; i--) {
switch(k) {
case 0:
re = BB[7] + re;
break;
case 4:
if(!new RegExp("0{4}//d{" + (a[0].length - i - 1) + "}$")
.test(a[0]))
re = BB[4] + re;
break;
case 8:
re = BB[5] + re;
BB[7] = BB[5];
k = 0;
break;
}
if(k % 4 == 2 && a[0].charAt(i + 2) != 0 && a[0].charAt(i + 1) == 0)
re = AA[0] + re;
if(a[0].charAt(i) != 0)
re = AA[a[0].charAt(i)] + BB[k % 4] + re;
k++;
}
if(a.length > 1)
{
re += BB[6];
for(var i = 0; i < a[1].length; i++)
re += AA[a[1].charAt(i)];
}
if(re == '一十')
re = "十";
if(re.match(/^一/) && re.length == 3)
re = re.replace("一", "");
return re;
}
changeToChinese (Num) {
if(typeof Num == "number") {
Num = new String(Num);
};
Num = Num.replace(/,/g, "")
Num = Num.replace(/ /g, "")
Num = Num.replace(/¥/g, "")
if(isNaN(Num)) {
return "";
};
var part = String(Num).split(".");
var newchar = "";
for(var i = part[0].length - 1; i >= 0; i--) {
if(part[0].length > 10) {
return "";
}
var tmpnewchar = ""
var perchar = part[0].charAt(i);
switch(perchar) {
case "0":
tmpnewchar = "零" + tmpnewchar;
break;
case "1":
tmpnewchar = "壹" + tmpnewchar;
break;
case "2":
tmpnewchar = "贰" + tmpnewchar;
break;
case "3":
tmpnewchar = "叁" + tmpnewchar;
break;
case "4":
tmpnewchar = "肆" + tmpnewchar;
break;
case "5":
tmpnewchar = "伍" + tmpnewchar;
break;
case "6":
tmpnewchar = "陆" + tmpnewchar;
break;
case "7":
tmpnewchar = "柒" + tmpnewchar;
break;
case "8":
tmpnewchar = "捌" + tmpnewchar;
break;
case "9":
tmpnewchar = "玖" + tmpnewchar;
break;
}
switch(part[0].length - i - 1) {
case 0:
tmpnewchar = tmpnewchar + "元";
break;
case 1:
if(perchar != 0) tmpnewchar = tmpnewchar + "拾";
break;
case 2:
if(perchar != 0) tmpnewchar = tmpnewchar + "佰";
break;
case 3:
if(perchar != 0) tmpnewchar = tmpnewchar + "仟";
break;
case 4:
tmpnewchar = tmpnewchar + "万";
break;
case 5:
if(perchar != 0) tmpnewchar = tmpnewchar + "拾";
break;
case 6:
if(perchar != 0) tmpnewchar = tmpnewchar + "佰";
break;
case 7:
if(perchar != 0) tmpnewchar = tmpnewchar + "仟";
break;
case 8:
tmpnewchar = tmpnewchar + "亿";
break;
case 9:
tmpnewchar = tmpnewchar + "拾";
break;
}
var newchar = tmpnewchar + newchar;
}
if(Num.indexOf(".") != -1) {
if(part[1].length > 2) {
part[1] = part[1].substr(0, 2)
}
for(i = 0; i < part[1].length; i++) {
tmpnewchar = ""
perchar = part[1].charAt(i)
switch(perchar) {
case "0":
tmpnewchar = "零" + tmpnewchar;
break;
case "1":
tmpnewchar = "壹" + tmpnewchar;
break;
case "2":
tmpnewchar = "贰" + tmpnewchar;
break;
case "3":
tmpnewchar = "叁" + tmpnewchar;
break;
case "4":
tmpnewchar = "肆" + tmpnewchar;
break;
case "5":
tmpnewchar = "伍" + tmpnewchar;
break;
case "6":
tmpnewchar = "陆" + tmpnewchar;
break;
case "7":
tmpnewchar = "柒" + tmpnewchar;
break;
case "8":
tmpnewchar = "捌" + tmpnewchar;
break;
case "9":
tmpnewchar = "玖" + tmpnewchar;
break;
}
if(i == 0) tmpnewchar = tmpnewchar + "角";
if(i == 1) tmpnewchar = tmpnewchar + "分";
newchar = newchar + tmpnewchar;
}
}
while(newchar.search("零零") != -1)
newchar = newchar.replace("零零", "零");
newchar = newchar.replace("零亿", "亿");
newchar = newchar.replace("亿万", "亿");
newchar = newchar.replace("零万", "万");
newchar = newchar.replace("零元", "元");
newchar = newchar.replace("零角", "");
newchar = newchar.replace("零分", "");
if(newchar.charAt(newchar.length - 1) == "元") {
newchar = newchar + "整"
}
return newchar;
}
}
class Http {
ajax(setting){
var opts={
method: (setting.method || "GET").toUpperCase(),
url: setting.url || "",
async: setting.async || true,
dataType: setting.dataType || "json",
data: setting.data || "",
success: setting.success || function(){},
error: setting.error || function(){}
}
function params_format (obj) {
var str = ''
for (var i in obj) {
str += i + '=' + obj[i] + '&'
}
return str.split('').slice(0, -1).join('')
}
var xhr=new XMLHttpRequest();
if(opts.method == 'GET'){
xhr.open(opts.method, opts.url + "?" + params_format(opts.data), opts.async);
xhr.send();
}else{
xhr.open(opts.method, opts.url, opts.async);
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send(opts.data);
}
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 304)) {
switch(opts.dataType){
case "json":
var json = JSON.parse(xhr.responseText);
opts.success(json);
break;
case "xml":
opts.success(xhr.responseXML);
break;
default:
opts.success(xhr.responseText);
break;
}
}
}
xhr.onerror = function(err) {
opts.error(err);
}
}
fetch(url, setting) {
let opts={
method: (setting.method || 'GET').toUpperCase(),
headers : setting.headers || {},
credentials : setting.credentials || true,
body: setting.body || {},
mode : setting.mode || 'no-cors',
redirect : setting.redirect || 'follow',
cache : setting.cache || 'default'
}
let dataType = setting.dataType || "json",
data = setting.data || ""
function params_format (obj) {
var str = ''
for (var i in obj) {
str += `${i}=${obj[i]}&`
}
return str.split('').slice(0, -1).join('')
}
if (opts.method === 'GET') {
url = url + (data?`?${params_format(data)}`:'')
}else{
setting.body = data || {}
}
return new Promise( (resolve, reject) => {
fetch(url, opts).then( async res => {
let data = dataType === 'text' ? await res.text() :
dataType === 'blob' ? await res.blob() : await res.json()
resolve(data)
}).catch( e => {
reject(e)
})
})
}
}
class DomFn {
$ (selector){
var type = selector.substring(0, 1);
if (type === '#') {
if (document.querySelecotor) return document.querySelector(selector)
return document.getElementById(selector.substring(1))
}else if (type === '.') {
if (document.querySelecotorAll) return document.querySelectorAll(selector)
return document.getElementsByClassName(selector.substring(1))
}else{
return document['querySelectorAll' ? 'querySelectorAll':'getElementsByTagName'](selector)
}
}
hasClass (ele, name) {
return ele.className.match(new RegExp('(\\s|^)' + name + '(\\s|$)'));
}
addClass (ele, name) {
if (!this.hasClass(ele, name)) ele.className += " " + name;
}
removeClass (ele, name) {
if (this.hasClass(ele, name)) {
var reg = new RegExp('(\\s|^)' + name + '(\\s|$)');
ele.className = ele.className.replace(reg, '');
}
}
replaceClass (ele, newName, oldName) {
this.removeClass(ele, oldName);
this.addClass(ele, newName);
}
siblings (ele) {
var chid = ele.parentNode.children,eleMatch = [];
for(var i = 0, len = chid.length; i < len; i ++){
if(chid[i] != ele){
eleMatch.push(chid[i]);
}
}
return eleMatch;
}
getByStyle (obj,name){
if(obj.currentStyle){
return obj.currentStyle[name];
}else{
return getComputedStyle(obj,false)[name];
}
}
}
class StorageFn {
constructor () {
this.ls = window.localStorage;
this.ss = window.sessionStorage;
}
setCookie (name, value, day) {
var setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
for (var i in setting) {
var oDate = new Date();
oDate.setDate(oDate.getDate() + day);
document.cookie = i + '=' + setting[i] + ';expires=' + oDate;
}
}else{
var oDate = new Date();
oDate.setDate(oDate.getDate() + day);
document.cookie = name + '=' + value + ';expires=' + oDate;
}
}
getCookie (name) {
var arr = document.cookie.split('; ');
for (var i = 0; i < arr.length; i++) {
var arr2 = arr[i].split('=');
if (arr2[0] == name) {
return arr2[1];
}
}
return '';
}
removeCookie (name) {
this.setCookie(name, 1, -1);
}
setLocal(key, val) {
var setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
for(var i in setting){
this.ls.setItem(i, JSON.stringify(setting[i]))
}
}else{
this.ls.setItem(key, JSON.stringify(val))
}
}
getLocal(key) {
if (key) return JSON.parse(this.ls.getItem(key))
return null;
}
removeLocal(key) {
this.ls.removeItem(key)
}
clearLocal() {
this.ls.clear()
}
setSession(key, val) {
var setting = arguments[0];
if (Object.prototype.toString.call(setting).slice(8, -1) === 'Object'){
for(var i in setting){
this.ss.setItem(i, JSON.stringify(setting[i]))
}
}else{
this.ss.setItem(key, JSON.stringify(val))
}
}
getSession(key) {
if (key) return JSON.parse(this.ss.getItem(key))
return null;
}
removeSession(key) {
this.ss.removeItem(key)
}
clearSession() {
this.ss.clear()
}
}
class OtherFn {
deepClone (obj) {
if (obj === null || typeof obj !== 'object') return obj
var isType = function(obj, type) {
var flag,
typeString = Object.prototype.toString.call(obj)
switch(type) {
case 'Array':
flag = typeString === '[object Array]'
break
case 'Date':
flag = typeString === '[object Date]'
break
case 'RegExp':
flag = typeString === '[object RegExp]'
break
default:
flag = false
}
return flag
}
var getRegExp = function(re) {
var flags = ''
if (re.global) flags += 'g'
if (re.ignoreCase) flags += 'i'
if (re.multiline) flags += 'm'
return flags
}
var _clone = function(parent) {
var child, proto, parents = [], children = []
if (isType(parent, 'Array')) {
child = [];
} else if (isType(parent, 'RegExp')) {
child = new RegExp(parent.source, getRegExp(parent));
if (parent.lastIndex) child.lastIndex = parent.lastIndex;
} else if (isType(parent, 'Date')) {
child = new Date(parent.getTime());
} else {
proto = Object.getPrototypeOf(parent);
child = Object.create(proto);
}
var index = parents.indexOf(parent);
if (index != -1) {
return children[index];
}
parents.push(parent);
children.push(child);
for (var i in parent) {
child[i] = _clone(parent[i]);
}
return child;
}
return _clone(obj)
}
debounce(fn, delay, immediate) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) fn.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, delay);
if (callNow) fn.apply(context, args);
};
}
throttle (func,delay){
var prev = Date.now();
return function(){
var context = this;
var args = arguments;
var now = Date.now();
if(now-prev>=delay){
func.apply(context,args);
prev = Date.now();
}
}
}
photoCompress(file, obj, cb) {
function convertBase64UrlToBlob(urlData){
var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], {type:mime});
}
function canvasDataURL(oldBase64){
var img = new Image();
img.src = oldBase64;
img.onload = function(){
var that = this;
var w = that.width,
h = that.height,
scale = w / h;
w = obj.width || w;
h = obj.height || (w / scale);
var quality = 0.7;
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
var anw = document.createAttribute("width");
anw.nodeValue = w;
var anh = document.createAttribute("height");
anh.nodeValue = h;
canvas.setAttributeNode(anw);
canvas.setAttributeNode(anh);
ctx.drawImage(that, 0, 0, w, h);
if(obj.quality && obj.quality <= 1 && obj.quality > 0){
quality = obj.quality;
}
var base64 = canvas.toDataURL('image/jpeg', quality);
if (obj.blob) {
cb && cb(convertBase64UrlToBlob(base64), convertBase64UrlToBlob(oldBase64))
}else{
cb && cb(base64, oldBase64);
}
}
}
var ready=new FileReader();
ready.readAsDataURL(file);
ready.onload=function(){
var re=this.result;
canvasDataURL(re)
}
}
getURL(name){
var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
var r = decodeURI(window.location.search).substr(1).match(reg);
if(r!=null) return r[2]; return null;
}
getUrlAllParams (url) {
var url = url ? url : window.location.href;
var _pa = url.substring(url.indexOf('?') + 1),
_arrS = _pa.split('&'),
_rs = {};
for (var i = 0, _len = _arrS.length; i < _len; i++) {
var pos = _arrS[i].indexOf('=');
if (pos == -1) {
continue;
}
var name = _arrS[i].substring(0, pos),
value = window.decodeURIComponent(_arrS[i].substring(pos + 1));
_rs[name] = value;
}
return _rs;
}
delParamsUrl(url, name){
var baseUrl = url.split('?')[0] + '?';
var query = url.split('?')[1];
if (query.indexOf(name)>-1) {
var obj = {}
var arr = query.split("&");
for (var i = 0; i < arr.length; i++) {
arr[i] = arr[i].split("=");
obj[arr[i][0]] = arr[i][1];
};
delete obj[name];
var url = baseUrl + JSON.stringify(obj).replace(/[\"\{\}]/g,"").replace(/\:/g,"=").replace(/\,/g,"&");
return url
}else{
return url;
}
}
getRandomColor () {
return '#' + (function(h) {
return new Array(7 - h.length).join("0") + h;
})((Math.random() * 0x1000000 << 0).toString(16));
}
imgLoadAll(arr,callback){
var arrImg = [];
for (var i = 0; i < arr.length; i++) {
var img = new Image();
img.src = arr[i];
img.onload = function(){
arrImg.push(this);
if (arrImg.length == arr.length) {
callback && callback();
}
}
}
}
loadAudio(src, callback) {
var audio = new Audio(src);
audio.onloadedmetadata = callback;
audio.src = src;
}
domToStirng(htmlDOM){
var div= document.createElement("div");
div.appendChild(htmlDOM);
return div.innerHTML
}
stringToDom(htmlString){
var div= document.createElement("div");
div.innerHTML=htmlString;
return div.children[0];
}
setCursorPosition (dom,val,posLen) {
var cursorPosition = 0;
if(dom.selectionStart){
cursorPosition = dom.selectionStart;
}
this.insertAtCursor(dom,val);
dom.focus();
console.log(posLen)
dom.setSelectionRange(dom.value.length,cursorPosition + (posLen || val.length));
}
insertAtCursor(dom, val) {
if (document.selection){
dom.focus();
sel = document.selection.createRange();
sel.text = val;
sel.select();
}else if (dom.selectionStart || dom.selectionStart == '0'){
let startPos = dom.selectionStart;
let endPos = dom.selectionEnd;
let restoreTop = dom.scrollTop;
dom.value = dom.value.substring(0, startPos) + val + dom.value.substring(endPos, dom.value.length);
if (restoreTop > 0){
dom.scrollTop = restoreTop;
}
dom.focus();
dom.selectionStart = startPos + val.length;
dom.selectionEnd = startPos + val.length;
} else {
dom.value += val;
dom.focus();
}
}
}