common.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. Array.prototype.indexOf = function (val, property) {
  2. for (var i = 0; i < this.length; i++) {
  3. if (!property && this[i] == val) {
  4. return i;
  5. } else if (property && this[i][property] == val) {
  6. return i;
  7. }
  8. }
  9. return -1;
  10. };
  11. Array.prototype.remove = function (val, property) {
  12. if(!(this instanceof Array)){
  13. return;
  14. }
  15. var index = this.indexOf(val, property);
  16. if (index > -1) {
  17. this.splice(index, 1);
  18. }
  19. };
  20. Array.prototype.unique = function (arr2) {
  21. if(!(this instanceof Array)){
  22. return;
  23. }
  24. for (var i = 0; i < this.length; i++) {
  25. for (var j = 0; j < arr2.length; j++) {
  26. if (this[i] === arr2[j]) {
  27. this.splice(i, 1); //利用splice函数删除元素,从第i个位置,截取长度为1的元素
  28. }
  29. }
  30. }
  31. //alert(arr1.length)
  32. for (var i = 0; i < arr2.length; i++) {
  33. this.push(arr2[i]);
  34. }
  35. return arr1;
  36. };
  37. Array.prototype.array2Obj = function(key, value){
  38. var obj = {};
  39. if(this instanceof Array){
  40. for(var i = 0; i < this.length; i++){
  41. obj[this[i][key]] = value ? this[i][value] : this[i];
  42. }
  43. }
  44. return obj;
  45. }
  46. String.prototype.trim = function() {
  47. return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
  48. }
  49. if (typeof String.prototype.startsWith != 'function') {
  50. String.prototype.startsWith = function (prefix){
  51. return this.slice(0, prefix.length) === prefix;
  52. };
  53. }
  54. String.prototype.endWith = function(str){
  55. if(str == null || str == "" || this.length == 0 || str.length > this.length){
  56. return false;
  57. }
  58. if(this.substring(this.length - str.length) == str){
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. return true;
  64. }
  65. String.prototype.replaceAll = function(s1, s2){
  66. return this.replace(new RegExp(s1, "gm"), s2);
  67. }
  68. alert = function(msg){
  69. layui.use(['layer'], function(){
  70. var layer = layui.layer; //获得laydate模块
  71. layer.msg(msg);
  72. });
  73. }
  74. confirm = function(msg, func, needConfirm){
  75. if(needConfirm != false){
  76. layui.use(['layer'], function(){
  77. var layer = layui.layer; //获得laydate模块
  78. layer.confirm(msg, {icon: 3, title:'提示',skin:'agree-skin'}, function(index){
  79. layer.close(index);
  80. func();
  81. });
  82. });
  83. } else {
  84. func();
  85. }
  86. }
  87. msg = function(msg, icon){
  88. layui.use(['layer'], function(){
  89. var layer = layui.layer; //获得laydate模块
  90. layer.msg(msg, {icon: (icon || 5)});
  91. });
  92. }
  93. loading = function(type) {
  94. layui.use(['layer'], function(){
  95. var layer = layui.layer; //获得laydate模块
  96. layer.load(type ? type : 1);
  97. });
  98. }
  99. closeLoading = function() {
  100. layui.use(['layer'], function(){
  101. var layer = layui.layer; //获得laydate模块
  102. layer.closeAll("loading");
  103. });
  104. }
  105. if (layui) {
  106. layui.use(['layer'], function(){
  107. window.layer = layui.layer;
  108. });
  109. }
  110. function shuffle(arr) {
  111. var length = arr.length, randomIndex, temp;
  112. while (length) {
  113. randomIndex = Math.floor(Math.random() * (length--));
  114. temp = arr[randomIndex];
  115. arr[randomIndex] = arr[length];
  116. arr[length] = temp
  117. }
  118. return arr;
  119. }
  120. function addNum(number1, number2) {
  121. var n1, n2, m;
  122. try {
  123. n1 = number1.toString().split(".")[1].length;
  124. } catch(e) {
  125. n1 = 0;
  126. }
  127. try {
  128. n2 = number2.toString().split(".")[1].length;
  129. } catch(e) {
  130. n2 = 0;
  131. }
  132. m = Math.pow(100, Math.max(n1, n2));
  133. return (number1 * m + number2 * m) / m;
  134. }
  135. function randomNumBoth(min, max){
  136. var range = max - min;
  137. var rand = Math.random();
  138. var num = min + Math.round(rand * range); //四舍五入
  139. return num;
  140. }
  141. const _w = () => {
  142. return (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth);
  143. }
  144. const _h = () => {
  145. return (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight);
  146. }
  147. const dictsConcatAfterOrder = (arr1 = [], arr2 = [], orderProperty = 'order') => {
  148. let arr = [].concat(arr2);
  149. arr1.forEach(item => {
  150. let index = arr.indexOf(item.value, 'value');
  151. if (index != -1) {
  152. arr.splice(index, 1);
  153. }
  154. });
  155. arr = arr1.concat(arr);
  156. if (orderProperty) {
  157. arr.sort((a, b) => {
  158. return a[orderProperty] - b[orderProperty];
  159. })
  160. } else {
  161. arr.sort((a, b) => {
  162. return a - b;
  163. })
  164. }
  165. return arr;
  166. }