Skip to content

Commit ae9bedb

Browse files
committed
refactor: 完善代码
1 parent a104efd commit ae9bedb

File tree

3 files changed

+44
-8
lines changed

3 files changed

+44
-8
lines changed

src/errorLog.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import Vue from 'vue';
2-
import {ErrorLogModule} from '@/store/modules/errorLog';
2+
import { isArray, isString } from '@/utils/validate';
3+
import { ErrorLogModule } from '@/store/modules/errorLog';
4+
import settings from '@/settings';
5+
6+
// you can set in settings.js
7+
// errorLog:'production' | ['production', 'development']
8+
const {errorLog: needErrorLog} = settings;
9+
10+
const checkNeed = () => {
11+
const env = process.env.NODE_ENV as string;
12+
if (isString(needErrorLog)) {
13+
return env === needErrorLog;
14+
}
15+
if (isArray(needErrorLog)) {
16+
return needErrorLog.includes(env);
17+
}
18+
return false;
19+
};
320

421
// you can set only in production env show the error-log
5-
if (process.env.NODE_ENV === 'production') {
22+
if (checkNeed()) {
623
Vue.config.errorHandler = (err, vm, info) => {
724
// Don't ask me why I use Vue.nextTick, it just a hack.
825
// detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500

src/utils/validate.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
1-
export function isvalidUsername(str: string) {
1+
export function isValidUsername(str: string) {
22
const validMap = ['admin', 'editor'];
33
return validMap.indexOf(str.trim()) >= 0;
44
}
55

6-
/* 小写字母*/
6+
// 小写字母
77
export function validateLowerCase(str: string) {
88
const reg = /^[a-z]+$/;
99
return reg.test(str);
1010
}
1111

12-
/* 大写字母*/
12+
// 大写字母
1313
export function validateUpperCase(str: string) {
1414
const reg = /^[A-Z]+$/;
1515
return reg.test(str);
1616
}
1717

18-
/* 大小写字母*/
18+
// 大小写字母
1919
export function validateAlphabets(str: string) {
2020
const reg = /^[A-Za-z]+$/;
2121
return reg.test(str);
2222
}
23+
24+
/**
25+
* @param {Array} arg
26+
* @returns {Boolean}
27+
*/
28+
export function isArray(arg: any) {
29+
if (typeof Array.isArray === 'undefined') {
30+
return Object.prototype.toString.call(arg) === '[object Array]';
31+
}
32+
return Array.isArray(arg);
33+
}
34+
35+
/**
36+
* @param {string} str
37+
* @returns {Boolean}
38+
*/
39+
export function isString(str) {
40+
return typeof str === 'string' || str instanceof String;
41+
}

src/views/login/index.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878

7979
<script lang="ts">
8080
import { Component, Vue, Watch } from 'vue-property-decorator';
81-
import { isvalidUsername } from '@/utils/validate';
81+
import { isValidUsername } from '@/utils/validate';
8282
import SocialSign from './socialSignIn.vue';
8383
import { LangSelect } from '@/components';
8484
import { Route } from 'vue-router';
@@ -87,7 +87,7 @@
8787
import { UserModule } from '@/store/modules/user';
8888
8989
const validateUsername = (rule, value: string, callback) => {
90-
if (!isvalidUsername(value)) {
90+
if (!isValidUsername(value)) {
9191
callback(new Error('Please enter the correct user name'));
9292
} else {
9393
callback();

0 commit comments

Comments
 (0)