| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- // 是否为空
- const isEmpty = val => {
- if (val instanceof Array) {
- if (val.length === 0) return true;
- } else if (val instanceof Object) {
- if (!Object.keys(val).length) return true;
- } else {
- if (
- val === 'null' ||
- val === null ||
- val === 'undefined' ||
- val === undefined ||
- val === ''
- )
- return true;
- return false;
- }
- return false;
- };
- // 是否为数组
- const isArray = arr => {
- return typeof arr == 'object' && arr.constructor == Array;
- };
- // 是否为字符串
- const isString = str => {
- return typeof str == 'string' && str.constructor == String;
- };
- // 是否为对象
- const isObject = obj => {
- return typeof obj == 'object' && obj.constructor == Object;
- };
- // 是否为数字
- const isNumber = num => {
- return typeof num == 'number' && num.constructor == Number;
- };
- // 是否为日期类型
- const isDate = date => {
- return typeof date == 'object' && date.constructor == Date;
- };
- // 是否为函数
- const isFunction = obj => {
- return typeof obj == 'object' && obj.constructor == Function;
- };
- /** *
- * 对象参数转为url参数
- * @parmas query 拼接得参数对象
- * @parmas isFrist 为true时开头为&
- */
- export const parse = (query, isFrist = false) => {
- let str = Object.keys(query)
- .filter(key => !isEmpty(query[key]))
- .reduce((result, key) => {
- const value = query[key];
- // in查询特殊处理
- if (Array.isArray(value) && !isEmpty(value)) {
- return `${result}&${value.reduce(
- (val, cVal) => `${val ? `${val}&` : val}${key}=${cVal}`,
- ''
- )}`;
- }
- // between查询做特殊处理
- if (typeof value === 'object' && !isEmpty(value)) {
- const [start, end] = value;
- return `${result}&${key}[]=${start}&${key}[]=${end}`;
- }
- return `${result}&${key}=${value}`;
- }, '');
- return isFrist ? str : str.replace(/^&/, '?');
- };
- const $checkName = str => {
- return /^[\u4E00-\u9FA5A-Za-z\s]+(·[\u4E00-\u9FA5A-Za-z]+)*$/.test(str);
- };
- /** *
- * 全局时间转换
- * @parmas num 时间戳默认为11位时间戳
- * @parmas fmt 默认转换的时间格式
- * @parmas all 为true时time为13位时间戳
- */
- export const $formatDate = (num, fmt = 'YYYY-MM-DD HH:mm', all) => {
- num = all ? num : num * 1000;
- let date = new Date();
- date.setTime(num);
- let o = {
- 'M+': date.getMonth() + 1,
- 'D+': date.getDate(),
- 'h+': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12,
- 'H+': date.getHours(),
- 'm+': date.getMinutes(),
- 's+': date.getSeconds(),
- 'q+': Math.floor((date.getMonth() + 3) / 3),
- S: date.getMilliseconds()
- };
- let week = {
- '0': '\u65e5',
- '1': '\u4e00',
- '2': '\u4e8c',
- '3': '\u4e09',
- '4': '\u56db',
- '5': '\u4e94',
- '6': '\u516d'
- };
- if (/(Y+)/.test(fmt)) {
- fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
- }
- if (/(E+)/.test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- (RegExp.$1.length > 1 ? (RegExp.$1.length > 2 ? '\u661f\u671f' : '\u5468') : '') +
- week[date.getDay() + '']
- );
- }
- for (let k in o) {
- if (new RegExp('(' + k + ')').test(fmt)) {
- fmt = fmt.replace(
- RegExp.$1,
- RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
- );
- }
- }
- return fmt;
- };
- export default {
- isEmpty,
- parse,
- $checkName,
- $formatDate
- };
|