Library.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. let library ={
  2. formatSeconds(value) {
  3. let result = parseInt(value);
  4. // let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600);
  5. // let m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  6. let m = Math.floor((result / 60 % 60)) < 10 ? '' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60));
  7. let s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60));
  8. let res = '';
  9. // if(h !== '00') res += `${h}h`;
  10. // if(m !== '00') res += `${m}min`;
  11. // res += `${s}s`;
  12. // if(h !== '00') res += `${h}:`;
  13. res += `${m}:`;
  14. res += `${s}`;
  15. return res;
  16. },
  17. randomInt(min, max){
  18. return Math.floor(Math.random() * (max+1 - min)) + min;
  19. },
  20. getRandom(start, end, fixed=0) {
  21. //fixed 保留几位小数
  22. let differ = end - start
  23. let random = Math.random()
  24. return (start + differ * random).toFixed(fixed)
  25. },
  26. removeObj(arr,obj)
  27. {
  28. let index = this.indexOf(arr,obj);
  29. if (index > -1) {
  30. arr.splice(index, 1);
  31. }
  32. },
  33. indexOf(arr,obj) {
  34. for (var i = 0; i < arr.length; i++) {
  35. if (arr[i] == obj) return i;
  36. }
  37. return -1;
  38. },
  39. insert(arr,index, item) {
  40. arr.splice(index, 0, item);
  41. },
  42. //数组元素位置替换
  43. swapArray: function(arr, index1, index2) {
  44. arr[index1] = arr.splice(index2, 1, arr[index1])[0];
  45. return arr;
  46. },
  47. swapLeft : function(arr) {
  48. let element = arr.shift();
  49. arr.push(element);
  50. },
  51. //生成指定范围随机整数
  52. randomFromIntRange: function(min, max) {
  53. return Math.floor(Math.random() * (max - min)) + min;
  54. },
  55. happenRate: function(rate) {
  56. let result = this.randomFromIntRange(0,100);
  57. if(result<rate)
  58. {
  59. return true;
  60. }
  61. else
  62. {
  63. return false;
  64. }
  65. return;
  66. },
  67. openInWebview () {
  68. let ua = navigator.userAgent.toLowerCase()
  69. if (ua.match(/MicroMessenger/i) == 'micromessenger') { // 微信浏览器判断
  70. return false
  71. } else if (ua.match(/QQ/i) == 'qq') { // QQ浏览器判断
  72. return false
  73. } else if (ua.match(/WeiBo/i) == "weibo") {
  74. return false
  75. } else {
  76. if (ua.match(/Android/i) != null) {
  77. return ua.match(/browser/i) == null
  78. } else if (ua.match(/iPhone/i) != null) {
  79. return ua.match(/safari/i) == null
  80. } else {
  81. return (ua.match(/macintosh/i) == null && ua.match(/windows/i) == null)
  82. }
  83. }
  84. },
  85. isMobile()
  86. {
  87. let isChrome;
  88. if(isChrome == window.google && window.chrome){
  89. return false;
  90. }
  91. else{
  92. return true;
  93. }
  94. // console.log('11111')
  95. // if(/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
  96. // return true;
  97. // } else {
  98. // return false;
  99. // }
  100. },
  101. remove(arr, item) {
  102. let index = arr.indexOf(item);
  103. if (index > -1) {
  104. arr.splice(index, 1);
  105. }
  106. },
  107. setImageBase64(base64Url,callback){
  108. let img = new Image();
  109. img.src = base64Url;
  110. img.onload = function(){
  111. let texture = new cc.Texture2D();
  112. texture.initWithElement(img);
  113. texture.handleLoadedTexture();
  114. if (callback)
  115. callback(texture);
  116. }
  117. },
  118. };
  119. module.exports = library;