export default { // 获取单一的一个参数 getSingleParam: function getSingleParam(name) { if (!name) { return null; } var getType = Object.prototype.toString; if (getType.call(name) !== '[object String]') { throw name + ' is not defined'; } var reg = new RegExp('[?&]' + name + '=([^&?#]*)', 'ig'); return location.href.match(reg) ? location.href.match(reg)[0].replace(reg, function(res, $1) { return decodeURIComponent($1); }) : null; }, // 验证类型 typeTesters: { // Follow https://www.w3.org/TR/html5/infrastructure.html#floating-point-numbers number: /^-?(\d*\.)?\d+(e[-+]?\d+)?$/i, integer: /^-?\d+$/, digits: /^\d+$/, alphanum: /^\w+$/i, url: new RegExp("^" + // protocol identifier "(?:(?:https?|ftp):)?//" + // ** mod: make scheme optional // user:pass authentication "(?:\\S+(?::\\S*)?@)?" + "(?:" + // IP address exclusion // private & local networks // "(?!(?:10|127)(?:\\.\\d{1,3}){3})" + // ** mod: allow local networks // "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" + // ** mod: allow local networks // "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" + // ** mod: allow local networks // IP address dotted notation octets // excludes loopback network 0.0.0.0 // excludes reserved space >= 224.0.0.0 // excludes network & broacast addresses // (first & last IP address of each class) "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" + "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + "|" + // host name '(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)' + // domain name '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*' + // TLD identifier '(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))' + ")" + // port number "(?::\\d{2,5})?" + // resource path "(?:/\\S*)?" + "$", 'i') }, sendError(errorMsg, error, tag) { // 记录错误信息 const ua = navigator.userAgent; errorMsg === undefined && (errorMsg = ''); let image = new Image(); let src = ``; if (error) { src += `&error=${JSON.stringify(error)}`; } if (tag) { src += `&traceid=${tag}`; } image.src = src; }, lazyScript(url) { var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = url; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); }, generateUUID() { // uuid var d = new Date().getTime(); var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (d + Math.random() * 16) % 16 | 0; d = Math.floor(d / 16); return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); }); return uuid; }, isEmptyObject(object) { if(object===undefined){ console.log('参数不存在'); return false; } if (Object.keys(object).length === 0) { return true // 如果为空,返回false } return false // 如果不为空,则会执行到这一步,返回true }, urlEncode(param, key, encode) { if (param === null) return ''; var paramStr = ''; var t = typeof(param); if (t == 'string' || t == 'number' || t == 'boolean') { paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param); } else { for (var i in param) { var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i); paramStr +=this.urlEncode(param[i], k, encode); } } return paramStr; } }