main.js 3.3 KB

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