index.ts 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. type TargetContext = '_self' | '_parent' | '_blank' | '_top';
  2. export const openWindow = (
  3. url: string,
  4. opts?: { target?: TargetContext; [key: string]: any }
  5. ) => {
  6. const { target = '_blank', ...others } = opts || {};
  7. window.open(
  8. url,
  9. target,
  10. Object.entries(others)
  11. .reduce((preValue: string[], curValue) => {
  12. const [key, value] = curValue;
  13. return [...preValue, `${key}=${value}`];
  14. }, [])
  15. .join(',')
  16. );
  17. };
  18. export const regexUrl = new RegExp(
  19. '^(?!mailto:)(?:(?:http|https|ftp)://)(?:\\S+(?::\\S*)?@)?(?:(?:(?:[1-9]\\d?|1\\d\\d|2[01]\\d|22[0-3])(?:\\.(?:1?\\d{1,2}|2[0-4]\\d|25[0-5])){2}(?:\\.(?:[0-9]\\d?|1\\d\\d|2[0-4]\\d|25[0-4]))|(?:(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)(?:\\.(?:[a-z\\u00a1-\\uffff0-9]+-?)*[a-z\\u00a1-\\uffff0-9]+)*(?:\\.(?:[a-z\\u00a1-\\uffff]{2,})))|localhost)(?::\\d{2,5})?(?:(/|\\?|#)[^\\s]*)?$',
  20. 'i'
  21. );
  22. /**
  23. * @description 格式化时间
  24. * @param time
  25. * @param cFormat
  26. * @returns {string|null}
  27. */
  28. export function parseTime(time, cFormat) {
  29. if (arguments.length === 0) {
  30. return null
  31. }
  32. const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}'
  33. let date
  34. if (typeof time === 'object') {
  35. date = time
  36. } else {
  37. if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
  38. time = parseInt(time)
  39. }
  40. if (typeof time === 'number' && time.toString().length === 10) {
  41. time = time * 1000
  42. }
  43. date = new Date(time)
  44. }
  45. const formatObj = {
  46. y: date.getFullYear(),
  47. m: date.getMonth() + 1,
  48. d: date.getDate(),
  49. h: date.getHours(),
  50. i: date.getMinutes(),
  51. s: date.getSeconds(),
  52. a: date.getDay(),
  53. }
  54. return format.replace(/{([ymdhisa])+}/g, (result, key) => {
  55. let value = formatObj[key]
  56. if (key === 'a') {
  57. return ['日', '一', '二', '三', '四', '五', '六'][value]
  58. }
  59. if (result.length > 0 && value < 10) {
  60. value = '0' + value
  61. }
  62. return value || 0
  63. })
  64. }
  65. export default null;