@dcp-designable/utils
Version:
工具类
150 lines • 4.34 kB
JavaScript
import React from 'react';
import { message, notification, Modal } from 'dcp-design-react';
import { ExclamationCircleOutlined } from '@ant-design/icons';
/**
* @description 获取全局上下文
* @returns {object} global context data
*/
export function getGlobalContext() {
return window?.__PLT_GLOBAL_DATA__ || {};
}
/**
* @description 获取父窗口的本地存储
* @param {string} key 本地存储的 key 值
* @returns {object} 本地存储的 value 值
*/
export function getParentLocal(key) {
try {
const val = localStorage.getItem(key) || window.parent.localStorage.getItem(key) || '{}';
return JSON.parse(val);
}
catch (err) {
console.warn('iframe is cross-origin!');
}
return {};
}
/**
* @author 朱思禹
* @date 2022-06-01
* @function 去掉字符串前后空格
* @param str 需要去掉前后空格的字符串
* @returns 去掉前后空格后的字符串
*/
export function trim(str) {
return str.replace(/(^\s*)|(\s*$)/g, '');
}
/**
* 获取地址栏参数
* @return {object} obj
*/
export function getURLParams() {
// 获取url参数
const result = {};
const query = window.location.search.substring(1);
if (query !== '') {
const vars = query.split('&');
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
result[pair[0]] = decodeURIComponent(pair[1]);
}
}
return result;
}
/**
* 判断类型的函数
* @param val 待判断数据
* @param type 传入具体类型
* @returns
*/
export function isType(val, type) {
return Object.prototype.toString.call(val) === `[object ${type}]`;
}
/**
* 获取地址栏某个参数
* @param url
* @param name
*/
export function getQueryVariable(variable) {
// 获取url参数
const query = window.location.search.substring(1);
const vars = query.split('&');
for (let i = 0; i < vars.length; i++) {
const pair = vars[i].split('=');
if (pair[0] == variable) {
return pair[1];
}
}
return '';
}
/**
* 格式化日期
* @param fmt 格式化格式
* @param date 日期
* @returns
*/
export function dateFormatter(fmt, date) {
let ret;
const opt = {
'Y+': date.getFullYear().toString(),
'M+': (date.getMonth() + 1).toString(),
'D+': date.getDate().toString(),
'h+': date.getHours().toString(),
'm+': date.getMinutes().toString(),
's+': date.getSeconds().toString(), // 秒
// 有其他格式化字符需求可以继续添加,必须转化成字符串
};
for (const k in opt) {
ret = new RegExp(`(${k})`).exec(fmt);
if (ret) {
fmt = fmt.replace(ret[1], (ret[1].length == 1) ? (opt[k]) : (opt[k].padStart(ret[1].length, '0')));
}
}
return fmt;
}
/**
* @description Message 消息提示
* @param {ReactNode} msg 提示的文本
* @param {string} type 提示类型
* @param {number} delay 延迟的时间,单位 秒
* @returns
*/
export function Message(msg = '', type = 'info', delay = 2) {
message[type](msg, delay);
}
/**
* @description Notification 通知提示
* @param {ReactNode} msg 提示的文本
* @param {string} type 提示类型
* @param {number} delay 延迟的时间,单位 秒,如果值是 0,为手动关闭模式
* @returns
*/
export function Notification(msg = '', type = 'success', delay = 4.5) {
notification[type]({
message: '提示信息',
description: msg,
duration: delay,
});
}
/**
* @description 需要确认的操作提示
* @param {ReactNode} msg 提示的文本
* @param {string} type 提示类型
* @returns
*/
export async function Confirm(msg = '') {
return new Promise((resolve, reject) => {
Modal.confirm({
title: '提示信息',
icon: React.createElement(ExclamationCircleOutlined),
content: msg || '确认进行此操作?',
className: 'confirm-info',
onOk() {
resolve();
},
onCancel() {
reject();
},
});
});
}
//# sourceMappingURL=tool.js.map