diff --git a/src/errorLog.js b/src/errorLog.js deleted file mode 100644 index 00b18b72..00000000 --- a/src/errorLog.js +++ /dev/null @@ -1,19 +0,0 @@ -import Vue from 'vue' -import store from './store' - -// you can set only in production env show the error-log -if (process.env.NODE_ENV === 'production') { - Vue.config.errorHandler = function(err, vm, info, a) { - // Don't ask me why I use Vue.nextTick, it just a hack. - // detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500 - Vue.nextTick(() => { - store.dispatch('addErrorLog', { - err, - vm, - info, - url: window.location.href - }) - console.error(err, info) - }) - } -} diff --git a/src/main.js b/src/main.js index 856ebd3e..225c4691 100644 --- a/src/main.js +++ b/src/main.js @@ -15,8 +15,8 @@ import router from './router' import i18n from './lang' // Internationalization import './icons' // icon -import './errorLog' // error log import './permission' // permission control +import './utils/errorLog' // error log import * as filters from './filters' // global filters diff --git a/src/settings.js b/src/settings.js index 27e7e65a..7e3e133d 100644 --- a/src/settings.js +++ b/src/settings.js @@ -1,4 +1,5 @@ export default { + title: 'vue-element-admin', /** * @type {boolean} true | false @@ -16,7 +17,7 @@ export default { * @type {string | array} 'production' | ['production','development'] * @description Need show err logs component. * The default is only used in the production env - * If you want to use it in dev, you can pass ['production','development'] + * If you want to also use it in dev, you can pass ['production','development'] */ errorLog: 'production' } diff --git a/src/utils/errorLog.js b/src/utils/errorLog.js new file mode 100644 index 00000000..c3c0597f --- /dev/null +++ b/src/utils/errorLog.js @@ -0,0 +1,35 @@ +import Vue from 'vue' +import store from '@/store' +import { isString, isArray } from '@/utils/validate' +import settings from '@/settings' + +// you can set in settings.js +// errorLog:'production' | ['production','development'] +const { errorLog: needErrorLog } = settings + +function checkNeed(arg) { + const env = process.env.NODE_ENV + if (isString(needErrorLog)) { + return env === needErrorLog + } + if (isArray(needErrorLog)) { + return needErrorLog.includes(env) + } + return false +} + +if (checkNeed()) { + Vue.config.errorHandler = function(err, vm, info, a) { + // Don't ask me why I use Vue.nextTick, it just a hack. + // detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500 + Vue.nextTick(() => { + store.dispatch('addErrorLog', { + err, + vm, + info, + url: window.location.href + }) + console.error(err, info) + }) + } +} diff --git a/src/utils/validate.js b/src/utils/validate.js index 5e4056f5..ba93d1c3 100644 --- a/src/utils/validate.js +++ b/src/utils/validate.js @@ -11,36 +11,41 @@ export function validUsername(str) { return valid_map.indexOf(str.trim()) >= 0 } -/* 合法uri*/ export function validURL(url) { const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ return reg.test(url) } -/* 小写字母*/ export function validLowerCase(str) { const reg = /^[a-z]+$/ return reg.test(str) } -/* 大写字母*/ export function validUpperCase(str) { const reg = /^[A-Z]+$/ return reg.test(str) } -/* 大小写字母*/ export function validAlphabets(str) { const reg = /^[A-Za-z]+$/ return reg.test(str) } -/** - * validate email - * @param email - * @returns {boolean} - */ export function validEmail(email) { - const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ - return re.test(email) + const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + return reg.test(email) +} + +export function isString(str) { + if (typeof str === 'string' || str instanceof String) { + return true + } + return false +} + +export function isArray(arg) { + if (typeof Array.isArray === 'undefined') { + return Object.prototype.toString.call(arg) === '[object Array]' + } + return Array.isArray(arg) } diff --git a/vue.config.js b/vue.config.js index 4a9c12b2..5d46f51b 100644 --- a/vue.config.js +++ b/vue.config.js @@ -1,14 +1,12 @@ 'use strict' const path = require('path') +const settings = require('./src/settings.js') +const { name } = settings function resolve(dir) { return path.join(__dirname, dir) } -// TODO: change to Settings -const port = 9527 -const name = 'vue-element-admin' - // Explanation of each configuration item You can find it in https://cli.vuejs.org/config/ module.exports = { /** @@ -25,7 +23,7 @@ module.exports = { lintOnSave: process.env.NODE_ENV === 'development' ? 'error' : false, productionSourceMap: false, devServer: { - port: port, + port: 9527, open: true, overlay: { warnings: false,