index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import Base64 from 'Base64'
  2. import user from '@/state/user'
  3. import router from '@/router/index'
  4. export const dateFormat = (date, fmt) => {
  5. var o = {
  6. "M+": date.getMonth() + 1, //月份
  7. "d+": date.getDate(), //日
  8. "h+": date.getHours(), //小时
  9. "m+": date.getMinutes(), //分
  10. "s+": date.getSeconds(), //秒
  11. "q+": Math.floor((date.getMonth() + 3) / 3), //季度
  12. "S": date.getMilliseconds() //毫秒
  13. };
  14. if (/(y+)/.test(fmt)) {
  15. fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  16. }
  17. for (var k in o) {
  18. if (new RegExp("(" + k + ")").test(fmt)) {
  19. fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  20. }
  21. }
  22. return fmt;
  23. }
  24. export const copyText = (text) => {
  25. const input = document.createElement('input')
  26. document.body.appendChild(input);
  27. input.setAttribute('value', text);
  28. input.select()
  29. document.execCommand('copy')
  30. document.body.removeChild(input)
  31. }
  32. export const throttle = (fn, mis = 500) => {
  33. let time
  34. return (...args) => {
  35. clearTimeout(time)
  36. time = setTimeout(() => fn(...args), mis)
  37. }
  38. }
  39. export function randomWord(randomFlag, min, max) {
  40. let str = ''
  41. let range = min
  42. 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']
  43. // 随机产生
  44. if (randomFlag) {
  45. range = Math.round(Math.random() * (max - min)) + min
  46. }
  47. for (var i = 0; i < range; i++) {
  48. let pos = Math.round(Math.random() * (arr.length - 1))
  49. str += arr[pos]
  50. }
  51. return str
  52. }
  53. export const encryption = (str, strv = '') => {
  54. str = Base64.btoa(str)
  55. const NUM = 2
  56. const front = randomWord(false, 8)
  57. const middle = randomWord(false, 8)
  58. const end = randomWord(false, 8)
  59. let str1 = str.substring(0, NUM)
  60. let str2 = str.substring(NUM)
  61. if (strv) {
  62. let strv1 = strv.substring(0, NUM)
  63. let strv2 = strv.substring(NUM)
  64. return [front + str2 + middle + str1 + end, front + strv2 + middle + strv1 + end]
  65. }
  66. return front + str2 + middle + str1 + end
  67. }
  68. /**
  69. *获取id
  70. */
  71. export const guid = () => {
  72. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  73. let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
  74. return v.toString(16);
  75. });
  76. }
  77. export const downloadFile = (url, fileName) => {
  78. var x = new XMLHttpRequest();
  79. x.open("GET", url, true);
  80. x.responseType = 'blob';
  81. x.onload = function(e) {
  82. var url = window.URL.createObjectURL(x.response)
  83. var a = document.createElement('a');
  84. a.href = url;
  85. console.log(e)
  86. a.setAttribute('download', fileName)
  87. a.click();
  88. }
  89. x.send();
  90. }
  91. /**
  92. *获取权限
  93. */
  94. export const getRoke = (val) => {
  95. if(!val){
  96. return false
  97. }
  98. let replacelist = {
  99. 'organizationlist':'organization',
  100. }
  101. let roleKeyList = user._value.roleKeyList
  102. let routeName = router.currentRoute._value.name
  103. let rokeName = `${replacelist[routeName] || routeName}:${val}`
  104. if(!roleKeyList.includes(rokeName)){
  105. return true
  106. }else{
  107. return false
  108. }
  109. }
  110. /**
  111. *遍历树
  112. **/
  113. export const assembleTree = (tree) => {
  114. if(!tree){
  115. return
  116. }
  117. let list = {}
  118. const assemble = (data,parentList) => {
  119. parentList = parentList?parentList:[]
  120. data.map(ele => {
  121. let newparentList = JSON.parse(JSON.stringify(parentList))
  122. newparentList.push(ele.id)
  123. if(ele.children){
  124. list[ele.id] = newparentList
  125. return assemble(ele.children,newparentList)
  126. }else{
  127. list[ele.id] = newparentList
  128. return {[ele.id]:newparentList}
  129. }
  130. })
  131. return list
  132. }
  133. let treeList = assemble(tree)
  134. return treeList
  135. }