index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import Base64 from './base64'
  2. export const dateFormat = (date, fmt) => {
  3. var o = {
  4. "M+": date.getMonth() + 1, //月份
  5. "d+": date.getDate(), //日
  6. "h+": date.getHours(), //小时
  7. "m+": date.getMinutes(), //分
  8. "s+": date.getSeconds(), //秒
  9. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  10. "S": date.getMilliseconds() //毫秒
  11. };
  12. if (/(y+)/.test(fmt)) {
  13. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  14. }
  15. for (var k in o) {
  16. if (new RegExp("(" + k + ")").test(fmt)) {
  17. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  18. }
  19. }
  20. return fmt;
  21. }
  22. export const copyText = (text) => {
  23. const input = document.createElement('input')
  24. document.body.appendChild(input);
  25. input.setAttribute('value', text);
  26. input.select()
  27. document.execCommand('copy')
  28. document.body.removeChild(input)
  29. }
  30. export const throttle = (fn, mis = 500) => {
  31. let time
  32. return (...args) => {
  33. clearTimeout(time)
  34. time = setTimeout(() => fn(...args), mis)
  35. }
  36. }
  37. export function randomWord(randomFlag, min, max) {
  38. let str = ''
  39. let range = min
  40. let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
  41. // 随机产生
  42. if (randomFlag) {
  43. range = Math.round(Math.random() * (max - min)) + min
  44. }
  45. for (var i = 0; i < range; i++) {
  46. let pos = Math.round(Math.random() * (arr.length - 1))
  47. str += arr[pos]
  48. }
  49. return str
  50. }
  51. export const encryption = (str, strv = '') => {
  52. str = Base64.btoa(str)
  53. const NUM = 2
  54. const front = randomWord(false, 8)
  55. const middle = randomWord(false, 8)
  56. const end = randomWord(false, 8)
  57. let str1 = str.substring(0, NUM)
  58. let str2 = str.substring(NUM)
  59. if (strv) {
  60. let strv1 = strv.substring(0, NUM)
  61. let strv2 = strv.substring(NUM)
  62. return [front + str2 + middle + str1 + end, front + strv2 + middle + strv1 + end]
  63. }
  64. return front + str2 + middle + str1 + end
  65. }
  66. /**
  67. *获取id
  68. */
  69. export const guid = () => {
  70. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  71. let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  72. return v.toString(16);
  73. });
  74. }
  75. export const downloadFile = (url, fileName) => {
  76. var x = new XMLHttpRequest();
  77. x.open("GET", url, true);
  78. x.responseType = 'blob';
  79. x.onload = function(e) {
  80. var url = window.URL.createObjectURL(x.response)
  81. var a = document.createElement('a');
  82. a.href = url;
  83. console.log(e)
  84. a.setAttribute('download', fileName)
  85. a.click();
  86. }
  87. x.send();
  88. }