阅行客电子档案
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.6 KiB

2 years ago
  1. import Vue from 'vue'
  2. import VueResource from 'vue-resource'
  3. Vue.use(VueResource)
  4. import themeArray from './themeArray'
  5. // 封装一些全局元素。如全站通用功能函数和http请求等
  6. export const global = {
  7. staticPath: process.env.NODE_ENV === 'development' ? '.' : './',
  8. /**
  9. * 切换主题函数
  10. */
  11. changeTheme: function(themeValue) {
  12. var that = this
  13. // console.log('切换主题颜色值:', themeValue, that.staticPath, JSON.stringify(themeArray))
  14. // 需要移到单独的文件存放
  15. var cssArray = themeArray
  16. removeCss()
  17. for (let i = 0, len = cssArray.length; i < len; i++) {
  18. var itemPath = that.staticPath + '/theme/' + themeValue + '/' + cssArray[i].toLowerCase() + '.css'
  19. console.log(itemPath)
  20. loadCss(itemPath)
  21. }
  22. localStorage.setItem('themeValue', themeValue)
  23. function loadCss(path) {
  24. var head = document.getElementsByTagName('head')[0]
  25. var link = document.createElement('link')
  26. link.href = path
  27. link.rel = 'stylesheet'
  28. link.type = 'text/css'
  29. head.appendChild(link)
  30. }
  31. function removeCss(href) {
  32. const links = document.getElementsByTagName('link')
  33. const head = document.getElementsByTagName('head')[0]
  34. const arr = []
  35. if (links && links.length > 0) {
  36. for (let i = 0, len = links.length; i < len; i++) {
  37. if (links[i]) {
  38. arr.push(links[i])
  39. }
  40. }
  41. for (let i = 0, len = arr.length; i < len; i++) {
  42. head.removeChild(arr[i])
  43. }
  44. }
  45. }
  46. }
  47. }
  48. // export default global