util.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. export function inArray(arr,val){
  2. for(let i=0;i<arr.length;i++){
  3. if(arr[i]==val){
  4. return true;
  5. }
  6. }
  7. return false;
  8. }
  9. export function formatDate(date){
  10. if(typeof date=='string'){
  11. return date;
  12. }
  13. let year = date.getFullYear();
  14. let month = date.getMonth() + 1;
  15. if(month<10){
  16. month='0'+month;
  17. }
  18. let day = date.getDate();
  19. if(day<10){
  20. day='0'+day;
  21. }
  22. return year+'-'+month+'-'+day;
  23. }
  24. export function formatDateTime(date){
  25. if(typeof date=='string'){
  26. return date;
  27. }
  28. let year = date.getFullYear();
  29. let month = date.getMonth() + 1;
  30. if(month<10){
  31. month='0'+month;
  32. }
  33. let day = date.getDate();
  34. if(day<10){
  35. day='0'+day;
  36. }
  37. let hour=date.getHours();
  38. if(hour<10){
  39. hour='0'+hour;
  40. }
  41. let minis=date.getMinutes();
  42. if(minis<10){
  43. minis='0'+minis;
  44. }
  45. let seconds=date.getSeconds();
  46. if(seconds<10){
  47. seconds='0'+seconds;
  48. }
  49. return year+'-'+month+'-'+day+' '+hour+':'+minis+':'+seconds;
  50. }
  51. export function formatTime(date){
  52. let hour=date.getHours();
  53. if(hour<10){
  54. hour='0'+hour;
  55. }
  56. let minis=date.getMinutes();
  57. if(minis<10){
  58. minis='0'+minis;
  59. }
  60. let seconds=date.getSeconds();
  61. if(seconds<10){
  62. seconds='0'+seconds;
  63. }
  64. return hour+':'+minis+':'+seconds;
  65. }
  66. export function formatDuration(seconds,showSeconds=true) {
  67. const days = Math.floor(seconds / (24 * 60 * 60));
  68. const hours = Math.floor((seconds % (24 * 60 * 60)) / (60 * 60));
  69. const minutes = Math.floor((seconds % (60 * 60)) / 60);
  70. const remainingSeconds = seconds % 60;
  71. let formattedDuration = "";
  72. if (days > 0) {
  73. formattedDuration += `${days}天 `;
  74. }
  75. if (hours > 0) {
  76. formattedDuration += `${hours}小时 `;
  77. }
  78. if (minutes > 0) {
  79. formattedDuration += `${minutes}分 `;
  80. }
  81. if (remainingSeconds > 0 && showSeconds) {
  82. formattedDuration += `${remainingSeconds}秒`;
  83. }
  84. if(!formattedDuration){
  85. return remainingSeconds+'秒';
  86. }
  87. return formattedDuration.trim();
  88. }
  89. export function rand(n,m){
  90. return Math.floor(Math.random() * (m - n + 1)) + n;
  91. }
  92. export function parseNumber(number){
  93. if(parseInt(number, 10) != number){
  94. return Number(parseFloat(number).toFixed(2));
  95. }
  96. return parseInt(number);
  97. }
  98. //用递归的方式深拷贝对象
  99. export function copyObj(obj){
  100. let r=JSON.parse(JSON.stringify(obj));
  101. for(let i in obj){
  102. if(typeof obj[i]=='function'){
  103. r[i]=obj[i];
  104. }
  105. if(typeof obj[i]=='object'){
  106. r[i]=copyObj(obj[i]);
  107. }
  108. }
  109. return r;
  110. }
  111. export function getUniqid() {
  112. var currentDate = new Date();
  113. var timestamp = currentDate.getTime().toString(36); // 使用36进制
  114. var unique = (currentDate.getUTCMilliseconds() + Math.random()).toString(36).slice(2, 7); // 随机数
  115. return (timestamp + unique).replace('.','');
  116. }
  117. export function getfileImage(filename){
  118. let $domain=location.origin;
  119. let start = filename.lastIndexOf('.');
  120. let $ext= start>-1?filename.slice(start + 1):'';
  121. if (inArray(['jpg', 'png', 'bmp', 'jpeg', 'gif'],$ext)) {
  122. return $domain+'/assets/img/fileicon/image.png';
  123. }else if(inArray(['doc', 'docx'],$ext)) {
  124. return $domain+'/assets/img/fileicon/doc.png';
  125. }else if(inArray(['ppt', 'pptx'],$ext)) {
  126. return $domain+'/assets/img/fileicon/ppt.png';
  127. }else if(inArray(['xls', 'xlsx'],$ext)) {
  128. return $domain+'/assets/img/fileicon/xls.png';
  129. }else if(inArray(['mp3','wav','wma','ogg'],$ext)) {
  130. return $domain+'/assets/img/fileicon/audio.png';
  131. }else if(inArray(['mp4', 'avi', 'rmvb','swf', 'flv','rm', 'ram', 'mpeg', 'mpg', 'wmv', 'mov'],$ext)) {
  132. return $domain+'/assets/img/fileicon/video.png';
  133. }else if(inArray(['zip', 'rar', '7z', 'gz', 'tar'],$ext)) {
  134. return $domain+'/assets/img/fileicon/zip.png';
  135. }else if(inArray(['apk','tiff','exe','html','pdf','psd','visio','svg','txt','xml'],$ext)){
  136. return $domain+'/assets/img/fileicon/'+$ext+'.png';
  137. }else{
  138. return $domain+'/assets/img/fileicon/wz.png';
  139. }
  140. }
  141. export function TreeIdtoString(tree) {
  142. for (let i = 0; i < tree.length; i++) {
  143. if (tree[i].childlist && tree[i].childlist.length > 0) {
  144. tree[i].childlist = TreeIdtoString(tree[i].childlist);
  145. }
  146. if (tree[i].id) {
  147. tree[i].id = tree[i].id.toString();
  148. }
  149. }
  150. return tree;
  151. }