main.js 3.6 KB

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