code refactor

This commit is contained in:
MaysWind
2020-11-08 00:53:14 +08:00
parent 7e8b5710c1
commit 69798b88df
6 changed files with 47 additions and 7 deletions
+5 -3
View File
@@ -1,3 +1,5 @@
import utils from './utils.js'
const tokenLocalStorageKey = 'lab_user_token'; const tokenLocalStorageKey = 'lab_user_token';
const userInfoLocalStorageKey = 'lab_user_info'; const userInfoLocalStorageKey = 'lab_user_info';
@@ -15,19 +17,19 @@ function isUserLogined() {
} }
function updateToken(token) { function updateToken(token) {
if (typeof(token) === 'string') { if (utils.isString(token)) {
localStorage.setItem(tokenLocalStorageKey, token); localStorage.setItem(tokenLocalStorageKey, token);
} }
} }
function updateUserInfo(user) { function updateUserInfo(user) {
if (typeof(user) === 'object') { if (utils.isObject(user)) {
localStorage.setItem(userInfoLocalStorageKey, JSON.stringify(user)); localStorage.setItem(userInfoLocalStorageKey, JSON.stringify(user));
} }
} }
function updateTokenAndUserInfo(item) { function updateTokenAndUserInfo(item) {
if (typeof(item) === 'object') { if (utils.isObject(item)) {
if (item.token) { if (item.token) {
updateToken(item.token); updateToken(item.token);
} }
+36
View File
@@ -0,0 +1,36 @@
function isFunction(val) {
return typeof(val) === 'function';
}
function isObject(val) {
return val != null && typeof(val) === 'object';
}
function isArray(val) {
if (isFunction(Array.isArray)) {
return Array.isArray(val);
}
return Object.prototype.toString.call(val) === '[object Array]';
}
function isString(val) {
return typeof(val) === 'string';
}
function isNumber(val) {
return typeof(val) === 'number';
}
function isBoolean(val) {
return typeof(val) === 'boolean';
}
export default {
isFunction,
isObject,
isArray,
isString,
isNumber,
isBoolean,
};
+2
View File
@@ -18,6 +18,7 @@ import version from './lib/version.js';
import settings from './lib/settings.js'; import settings from './lib/settings.js';
import services from './lib/services.js'; import services from './lib/services.js';
import userstate from './lib/userstate.js'; import userstate from './lib/userstate.js';
import utils from './lib/utils.js';
import App from './Mobile.vue'; import App from './Mobile.vue';
Vue.use(VueI18n); Vue.use(VueI18n);
@@ -29,6 +30,7 @@ Framework7.use(Framework7Vue);
const i18n = new VueI18n(getI18nOptions()); const i18n = new VueI18n(getI18nOptions());
Vue.prototype.$version = version.getVersion; Vue.prototype.$version = version.getVersion;
Vue.prototype.$utils = utils;
Vue.prototype.$settings = settings; Vue.prototype.$settings = settings;
Vue.prototype.$getDefaultLanguage = getDefaultLanguage; Vue.prototype.$getDefaultLanguage = getDefaultLanguage;
Vue.prototype.$getAllLanguages = getAllLanguages; Vue.prototype.$getAllLanguages = getAllLanguages;
+1 -1
View File
@@ -144,7 +144,7 @@ export default {
return; return;
} }
if (typeof(data.result.token) === 'string') { if (self.$utils.isString(data.result.token)) {
self.$user.updateTokenAndUserInfo(data.result); self.$user.updateTokenAndUserInfo(data.result);
} }
+1 -1
View File
@@ -161,7 +161,7 @@ export default {
self.loading = false; self.loading = false;
const data = response.data; const data = response.data;
if (!data || !data.success || !data.result || typeof(data.result.enable) !== 'boolean') { if (!data || !data.success || !data.result || !self.$utils.isBoolean(data.result.enable)) {
self.$alert('Unable to get current two factor authentication status', () => { self.$alert('Unable to get current two factor authentication status', () => {
router.back(); router.back();
}); });
+2 -2
View File
@@ -220,11 +220,11 @@ export default {
return; return;
} }
if (typeof(data.result.newToken) === 'string') { if (self.$utils.isString(data.result.newToken)) {
self.$user.updateToken(data.result.newToken); self.$user.updateToken(data.result.newToken);
} }
if (typeof(data.result.user) === 'object') { if (self.$utils.isObject(data.result.user)) {
self.$user.updateUserInfo(data.result.user); self.$user.updateUserInfo(data.result.user);
} }