perf:发布版本1.0.2
This commit is contained in:
97
src/utils/Common.js
Normal file
97
src/utils/Common.js
Normal file
@@ -0,0 +1,97 @@
|
||||
/**
|
||||
* 获取Url中的参数
|
||||
*/
|
||||
const getParamFormUrl = (key, host) => {
|
||||
let arr;
|
||||
let reg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)');
|
||||
try {
|
||||
let testHost = window.location.href;
|
||||
if (host) {
|
||||
testHost = host;
|
||||
}
|
||||
if ((arr = testHost.split('?')[1].match(reg))) {
|
||||
return decodeURI(arr[2]);
|
||||
}
|
||||
} catch (e) {
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 是否为空
|
||||
* @param value
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const isEmpty = (value) => {
|
||||
return value === undefined || value === null || value === '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 转换为json
|
||||
* @param value
|
||||
* @param def
|
||||
*/
|
||||
export const parseJSON = (value, def = {}) => {
|
||||
let obj;
|
||||
try {
|
||||
obj = JSON.parse(value);
|
||||
} catch (e) {
|
||||
}
|
||||
if (!obj) {
|
||||
obj = def;
|
||||
}
|
||||
return obj;
|
||||
};
|
||||
|
||||
/**
|
||||
* 分割对象
|
||||
* @param obj
|
||||
* @param seq
|
||||
*/
|
||||
export const split = (obj, seq = ',') => {
|
||||
if (obj) {
|
||||
return obj.split(seq);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
/**
|
||||
* 判断是否是开发环境
|
||||
* @param key
|
||||
* @param url
|
||||
*/
|
||||
export const isDevelopment = () => {
|
||||
return process.env.NODE_ENV === 'development';
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* 替换全部字符
|
||||
* @param string
|
||||
* @param s1
|
||||
* @param s2
|
||||
*/
|
||||
const replaceAll = (string, s1, s2) => {
|
||||
return string.replace(new RegExp(s1, 'gm'), s2);
|
||||
};
|
||||
|
||||
/**
|
||||
* 结尾
|
||||
* @param string
|
||||
* @param endStr
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export const endWith = (string = '', endStr) => {
|
||||
let d = string.length - endStr.length;
|
||||
return d >= 0 && string.lastIndexOf(endStr) == d;
|
||||
};
|
||||
|
||||
export default {
|
||||
getParamFormUrl,
|
||||
isEmpty,
|
||||
parseJSON,
|
||||
split,
|
||||
isDevelopment,
|
||||
replaceAll,
|
||||
endWith,
|
||||
};
|
||||
42
src/utils/Storage.js
Normal file
42
src/utils/Storage.js
Normal file
@@ -0,0 +1,42 @@
|
||||
import Common from "./Common";
|
||||
|
||||
///////////////////////////////////////// 用户token
|
||||
|
||||
/**
|
||||
* 保存用户token
|
||||
* @param token
|
||||
*/
|
||||
const saveUserToken = (token) => {
|
||||
save('token', token);
|
||||
};
|
||||
|
||||
const getUserToken = () => {
|
||||
return get('token') || '';
|
||||
};
|
||||
|
||||
/**
|
||||
* 保存数据
|
||||
* @param key
|
||||
* @param value
|
||||
*/
|
||||
const save = (key, value) => {
|
||||
localStorage.setItem(key, JSON.stringify({data: value}));
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取数据
|
||||
* @param key
|
||||
* @returns {string}
|
||||
*/
|
||||
const get = (key) => {
|
||||
return Common.parseJSON(localStorage.getItem(key), {data: ""}).data;
|
||||
};
|
||||
|
||||
///////////////////////////////////////// 导出模块
|
||||
|
||||
export default {
|
||||
save,
|
||||
get,
|
||||
saveUserToken,
|
||||
getUserToken,
|
||||
};
|
||||
Reference in New Issue
Block a user