main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. import Vue from 'vue'
  2. import Cookies from 'js-cookie'
  3. // import 'normalize.css/normalize.css' // a modern alternative to CSS resets
  4. import Element from 'element-ui'
  5. import VueHighlightJS from 'vue-highlightjs'
  6. // import enLang from 'element-ui/lib/locale/lang/en'// 如果使用中文语言包请默认支持,无需额外引入,请删除该依赖
  7. import './permission' // permission control
  8. // import 'font-awesome/css/font-awesome.min.css'
  9. import 'element-ui/lib/theme-chalk/index.css';
  10. import App from './App'
  11. import store from './store'
  12. import router from './router'
  13. import * as filters from './filters' // global filters
  14. /**
  15. * If you don't want to use mock-server
  16. * you want to use MockJs for mock api
  17. * you can execute: mockXHR()
  18. *
  19. * Currently MockJs will be used in the production environment,
  20. * please remove it before going online ! ! !
  21. */
  22. if (process.env.NODE_ENV === 'production') {
  23. const { mockXHR } = require('../mock')
  24. mockXHR()
  25. }
  26. Vue.use(Element, {
  27. size: Cookies.get('size') || 'medium' // set element-ui default size
  28. // locale: enLang // 如果使用中文,无需设置,请删除
  29. })
  30. Vue.use(VueHighlightJS)
  31. // register global utility filters
  32. Object.keys(filters).forEach(key => {
  33. Vue.filter(key, filters[key])
  34. })
  35. Vue.config.productionTip = false
  36. Vue.prototype.$alert = function(message, ...values) {
  37. Element.MessageBox.alert(this['$tc'](message, ...values), '系统提示')
  38. }
  39. Vue.prototype.returnUrl = function (url) {
  40. const urlRegex = /^(https?:\/\/)?([\da-z.-]+)\.([a-z.]{2,6})(:[0-9]{1,5})?(\/.*)?$/i;
  41. // 测试字符串是否匹配正则表达式
  42. if (!urlRegex.test(url)) {
  43. return location.origin + '/' + url
  44. }
  45. return url;
  46. }
  47. Vue.prototype.strLen = function(str) {
  48. if (typeof str === 'string') {
  49. return str.replace(/[^\x00-\xff]/g, '01').length
  50. }
  51. }
  52. Vue.prototype.cutByte = function(str, len, endstr) {
  53. var len = +len
  54. var endstr = typeof (endstr) === 'undefined' ? '' : endstr.toString()
  55. var endstrBl = Vue.prototype.strLen(endstr)
  56. function n2(a) {
  57. var n = a / 2 | 0
  58. return (n > 0 ? n : 1)
  59. }// 用于二分法查找
  60. if (!(str + '').length || !len || len <= 0) {
  61. return ''
  62. }
  63. if (len < endstrBl) {
  64. endstr = ''
  65. endstrBl = 0
  66. }
  67. var lenS = len - endstrBl
  68. var _lenS = 0
  69. var _strl = 0
  70. while (_strl <= lenS) {
  71. var _lenS1 = n2(lenS - _strl)
  72. var addn = Vue.prototype.strLen(str.substr(_lenS, _lenS1))
  73. if (addn == 0) {
  74. return str
  75. }
  76. _strl += addn
  77. _lenS += _lenS1
  78. }
  79. if (str.length - _lenS > endstrBl || Vue.prototype.strLen(str.substring(_lenS - 1)) > endstrBl) {
  80. return str.substr(0, _lenS - 1) + endstr
  81. } else {
  82. return str
  83. }
  84. }
  85. Vue.prototype.$info = function(message, ...values) {
  86. this['$message'].info(this['$tc'](message, ...values))
  87. }
  88. Vue.prototype.$error = function(message, ...values) {
  89. this['$message'].error(this['$tc'](message, ...values))
  90. }
  91. Vue.prototype.$warning = function(message, ...values) {
  92. this['$message'].warning(this['$tc'](message, ...values))
  93. }
  94. Vue.prototype.$success = function(message, ...values) {
  95. this['$message'].success(this['$tc'](message, ...values))
  96. }
  97. Vue.prototype.generateSlug = function(title) {
  98. return title.trim()
  99. .split(/[^\w\u4e00-\u9fa5]/) // 更简洁的正则
  100. .filter(Boolean)
  101. .map(segment => segment.toLowerCase())
  102. .join('-')
  103. .replace(/-+/g, '-')
  104. .replace(/^-|-$/g, '') // 新增首尾连字符清理
  105. }
  106. let isInit = false
  107. Vue.prototype.$getIsInit = function() { return isInit }
  108. Vue.prototype.$setIsInit = function() { isInit = true }
  109. Vue.prototype.$bus = new Vue()
  110. new Vue({
  111. el: '#app',
  112. store,
  113. router,
  114. render: h => h(App)
  115. })