tools.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. export default {
  2. // 获取单一的一个参数
  3. getSingleParam: function getSingleParam(name) {
  4. if (!name) {
  5. return null;
  6. }
  7. var getType = Object.prototype.toString;
  8. if (getType.call(name) !== '[object String]') {
  9. throw name + ' is not defined';
  10. }
  11. var reg = new RegExp('[?&]' + name + '=([^&?#]*)', 'ig');
  12. return location.href.match(reg) ? location.href.match(reg)[0].replace(reg, function(res, $1) {
  13. return decodeURIComponent($1);
  14. }) : null;
  15. },
  16. // 验证类型
  17. typeTesters: {
  18. // Follow https://www.w3.org/TR/html5/infrastructure.html#floating-point-numbers
  19. number: /^-?(\d*\.)?\d+(e[-+]?\d+)?$/i,
  20. integer: /^-?\d+$/,
  21. digits: /^\d+$/,
  22. alphanum: /^\w+$/i,
  23. url: new RegExp("^" +
  24. // protocol identifier
  25. "(?:(?:https?|ftp):)?//" + // ** mod: make scheme optional
  26. // user:pass authentication
  27. "(?:\\S+(?::\\S*)?@)?" + "(?:" +
  28. // IP address exclusion
  29. // private & local networks
  30. // "(?!(?:10|127)(?:\\.\\d{1,3}){3})" + // ** mod: allow local networks
  31. // "(?!(?:169\\.254|192\\.168)(?:\\.\\d{1,3}){2})" + // ** mod: allow local networks
  32. // "(?!172\\.(?:1[6-9]|2\\d|3[0-1])(?:\\.\\d{1,3}){2})" + // ** mod: allow local networks
  33. // IP address dotted notation octets
  34. // excludes loopback network 0.0.0.0
  35. // excludes reserved space >= 224.0.0.0
  36. // excludes network & broacast addresses
  37. // (first & last IP address of each class)
  38. "(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])" + "(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}" +
  39. "(?:\\.(?:[1-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))" + "|" +
  40. // host name
  41. '(?:(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)' +
  42. // domain name
  43. '(?:\\.(?:[a-z\\u00a1-\\uffff0-9]-*)*[a-z\\u00a1-\\uffff0-9]+)*' +
  44. // TLD identifier
  45. '(?:\\.(?:[a-z\\u00a1-\\uffff]{2,}))' + ")" +
  46. // port number
  47. "(?::\\d{2,5})?" +
  48. // resource path
  49. "(?:/\\S*)?" + "$", 'i')
  50. },
  51. sendError(errorMsg, error, tag) {
  52. // 记录错误信息
  53. const ua = navigator.userAgent;
  54. errorMsg === undefined && (errorMsg = '');
  55. let image = new Image();
  56. let src = ``;
  57. if (error) {
  58. src += `&error=${JSON.stringify(error)}`;
  59. }
  60. if (tag) {
  61. src += `&traceid=${tag}`;
  62. }
  63. image.src = src;
  64. },
  65. lazyScript(url) {
  66. var _hmt = _hmt || [];
  67. (function() {
  68. var hm = document.createElement("script");
  69. hm.src = url;
  70. var s = document.getElementsByTagName("script")[0];
  71. s.parentNode.insertBefore(hm, s);
  72. })();
  73. },
  74. generateUUID() { // uuid
  75. var d = new Date().getTime();
  76. var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
  77. var r = (d + Math.random() * 16) % 16 | 0;
  78. d = Math.floor(d / 16);
  79. return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  80. });
  81. return uuid;
  82. },
  83. isEmptyObject(object) {
  84. if(object===undefined){
  85. console.log('参数不存在');
  86. return false;
  87. }
  88. if (Object.keys(object).length === 0) {
  89. return true // 如果为空,返回false
  90. }
  91. return false // 如果不为空,则会执行到这一步,返回true
  92. },
  93. urlEncode(param, key, encode) {
  94. if (param === null) return '';
  95. var paramStr = '';
  96. var t = typeof(param);
  97. if (t == 'string' || t == 'number' || t == 'boolean') {
  98. paramStr += '&' + key + '=' + ((encode == null || encode) ? encodeURIComponent(param) : param);
  99. } else {
  100. for (var i in param) {
  101. var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
  102. paramStr +=this.urlEncode(param[i], k, encode);
  103. }
  104. }
  105. return paramStr;
  106. }
  107. }