helper.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // 判断arr是否为一个数组,返回一个bool值
  2. function isArray(arr) {
  3. return Object.prototype.toString.call(arr) === '[object Array]';
  4. }
  5. // 深度克隆
  6. function deepClone(obj) {
  7. // 对常见的“非”值,直接返回原来值
  8. if ([null, undefined, NaN, false].includes(obj)) return obj;
  9. if (typeof obj !== "object" && typeof obj !== 'function') {
  10. //原始类型直接返回
  11. return obj;
  12. }
  13. var o = isArray(obj) ? [] : {};
  14. for (let i in obj) {
  15. if (obj.hasOwnProperty(i)) {
  16. o[i] = typeof obj[i] === "object" ? deepClone(obj[i]) : obj[i];
  17. }
  18. }
  19. return o;
  20. }
  21. function getUUid(len = 32, firstU = true, radix = null) {
  22. let chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split('');
  23. let uuid = [];
  24. radix = radix || chars.length;
  25. if (len) {
  26. // 如果指定uuid长度,只是取随机的字符,0|x为位运算,能去掉x的小数位,返回整数位
  27. for (let i = 0; i < len; i++) uuid[i] = chars[0 | Math.random() * radix];
  28. } else {
  29. let r;
  30. // rfc4122标准要求返回的uuid中,某些位为固定的字符
  31. uuid[8] = uuid[13] = uuid[18] = uuid[23] = '-';
  32. uuid[14] = '4';
  33. for (let i = 0; i < 36; i++) {
  34. if (!uuid[i]) {
  35. r = 0 | Math.random() * 16;
  36. uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
  37. }
  38. }
  39. }
  40. // 移除第一个字符,并用u替代,因为第一个字符为数值时,该guuid不能用作id或者class
  41. if (firstU) {
  42. uuid.shift();
  43. return 'u' + uuid.join('');
  44. } else {
  45. return uuid.join('');
  46. }
  47. }
  48. function platform () {
  49. let val = null;
  50. // #ifdef VUE3
  51. val = 'VUE3'
  52. // #endif
  53. // #ifdef APP-PLUS
  54. val = 'APP-PLUS'
  55. // #endif
  56. // #ifdef APP-PLUS-NVUE || APP-NVUE
  57. val = 'NVUE'
  58. // #endif
  59. //#ifdef H5
  60. val = 'H5'
  61. //#endif
  62. // #ifdef MP-WEIXIN
  63. val = 'MP-WEIXIN'
  64. // #endif
  65. // #ifdef MP-ALIPAY
  66. val = 'MP-ALIPAY'
  67. // #endif
  68. // #ifdef MP-BAIDU
  69. val = 'MP-BAIDU'
  70. // #endif
  71. // #ifdef MP-TOUTIAO
  72. val = 'MP-TOUTIAO'
  73. // #endif
  74. // #ifdef MP-LARK
  75. val = 'MP-LARK'
  76. // #endif
  77. // #ifdef MP-TOUTIAO
  78. val = 'MP-QQ'
  79. // #endif
  80. // #ifdef MP-KUAISHOU
  81. val = 'MP-KUAISHOU'
  82. // #endif
  83. // #ifdef MP-360
  84. val = 'MP-360'
  85. // #endif
  86. // #ifdef QUICKAPP-WEBVIEW
  87. val = 'QUICKAPP-WEBVIEW'
  88. // #endif
  89. // #ifdef QUICKAPP-WEBVIEW-UNION
  90. val = 'QUICKAPP-WEBVIEW-UNION'
  91. // #endif
  92. // #ifdef QUICKAPP-WEBVIEW-HUAWEI
  93. val = 'QUICKAPP-WEBVIEW-HUAWEI'
  94. // #endif
  95. return val;
  96. }
  97. export {
  98. deepClone,
  99. getUUid,
  100. platform
  101. };