common.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import qs from 'qs'
  2. import jsSHA from 'jssha'
  3. export function getQuery (search) {
  4. return qs.parse(search, { ignoreQueryPrefix: true }) || {}
  5. }
  6. export function setQuery (obj) {
  7. return qs.stringify(obj, { ignoreQueryPrefix: true })
  8. }
  9. export function downLoadFile (fileName, data) {
  10. if (window.navigator.msSaveOrOpenBlob) {
  11. navigator.msSaveBlob(data, fileName + '.xlsx')
  12. }
  13. const blob = new Blob([data])
  14. const elink = document.createElement('a')
  15. elink.download = `${fileName}.xlsx`
  16. elink.style.display = 'none'
  17. elink.href = URL.createObjectURL(blob)
  18. document.body.appendChild(elink)
  19. elink.click()
  20. URL.revokeObjectURL(elink.href)// 释放 URL 对象
  21. document.body.removeChild(elink)
  22. }
  23. export const encryptPassword = (password) => {
  24. const shaObj = new jsSHA('SHA-512', 'TEXT')
  25. shaObj.update(password)
  26. const hash = shaObj.getHash('HEX')
  27. return hash
  28. }
  29. export function formatDate (value) {
  30. return value < 10 ? `0${value}` : value
  31. }
  32. // 加入收藏夹
  33. export function addFavorite2 () {
  34. const url = window.location
  35. const { title } = document
  36. const ua = navigator.userAgent.toLowerCase()
  37. if (ua.indexOf('360se') > -1) {
  38. alert('由于360浏览器功能限制,请按 Ctrl+D 手动收藏!')
  39. } else if (ua.indexOf('msie 8') > -1) {
  40. window.external.AddToFavoritesBar(url, title)
  41. } else if (document.all) {
  42. try {
  43. window.external.addFavorite(url, title)
  44. } catch (e) {
  45. alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!')
  46. }
  47. } else if (window.sidebar) {
  48. window.sidebar.addPanel(title, url, '')
  49. } else {
  50. alert('您的浏览器不支持,请按 Ctrl+D 手动收藏!')
  51. }
  52. }
  53. // 设为首页
  54. export function SetHome (obj, url) {
  55. try {
  56. obj.style.behavior = 'url(#default#homepage)'
  57. obj.setHomePage(url)
  58. } catch (e) {
  59. if (window.netscape) {
  60. try {
  61. netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect")
  62. } catch (e2) {
  63. alert("此操作被浏览器拒绝!\n请在浏览器地址栏输入“about:config”并回车\n然后将 [signed.applets.codebase_principal_support]的值设置为'true',双击即可。");
  64. }
  65. const prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch)
  66. prefs.setCharPref('browser.startup.homepage', url);
  67. } else {
  68. alert('您的浏览器不支持,请手动设置。');
  69. }
  70. }
  71. }
  72. // 非接口下载文件
  73. function getBlob (url) {
  74. return new Promise(resolve => {
  75. const xhr = new XMLHttpRequest()
  76. xhr.open('GET', url, true)
  77. xhr.responseType = 'blob'
  78. xhr.onload = () => {
  79. if (xhr.status === 200) {
  80. resolve(xhr.response)
  81. }
  82. }
  83. xhr.send()
  84. })
  85. }
  86. /**
  87. * 保存
  88. * @param {Blob} blob
  89. * @param {String} filename 想要保存的文件名称
  90. */
  91. function saveAs (blob, filename) {
  92. if (window.navigator.msSaveOrOpenBlob) {
  93. navigator.msSaveBlob(blob, filename)
  94. } else {
  95. const link = document.createElement('a')
  96. const body = document.querySelector('body')
  97. link.href = window.URL.createObjectURL(blob)
  98. link.download = filename
  99. // fix Firefox
  100. link.style.display = 'none'
  101. body.appendChild(link)
  102. link.click()
  103. body.removeChild(link)
  104. window.URL.revokeObjectURL(link.href)
  105. }
  106. }
  107. export function download (url, filename) {
  108. getBlob(url).then(blob => {
  109. saveAs(blob, filename)
  110. })
  111. }
  112. // 生成数组 ['a,b,c','a1,b1,c1'] => ['a','b','c','a1','b1','c1']
  113. export function generateArray (array) {
  114. const result = []
  115. array.forEach(item => {
  116. const splits = item.split(',')
  117. result.push.apply(result, splits)
  118. })
  119. return result
  120. }