utils.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { parse } from 'querystring'
  2. import dayjs from 'dayjs'
  3. const reg =
  4. /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/
  5. export const isUrl = path => reg.test(path)
  6. export const isAntDesignPro = () => {
  7. if (ANT_DESIGN_PRO_ONLY_DO_NOT_USE_IN_YOUR_PRODUCTION === 'site') {
  8. return true
  9. }
  10. return window.location.hostname === 'preview.pro.ant.design'
  11. } // 给官方演示站点用,用于关闭真实开发环境不需要使用的特性
  12. export const isAntDesignProOrDev = () => {
  13. const { NODE_ENV } = process.env
  14. if (NODE_ENV === 'development') {
  15. return true
  16. }
  17. return isAntDesignPro()
  18. }
  19. export const getPageQuery = () => parse(window.location.href.split('?')[1])
  20. /**
  21. * 日期格式化
  22. * @param format - 格式
  23. */
  24. export const dayjsFormat = (date, format = 'YYYY-MM-DD HH:mm:ss') => {
  25. if (date === '0001-01-01 00:00:00' || !date) return ''
  26. return dayjs(date).format(format)
  27. }
  28. /** 处理表格排序字段 */
  29. export const generateSortField = sort => {
  30. if (!sort) return {}
  31. // 处理后的排序,格式{ field: 'xxxxx', order: 'desc/asc' }
  32. const s = { field: null, order: null }
  33. const keys = Object.keys(sort)
  34. const orKey = keys[0]
  35. if (orKey) {
  36. s.field = orKey
  37. s.order = sort[orKey].replace('end', '')
  38. return s
  39. }
  40. return {}
  41. }
  42. /** 处理表格中的筛选(特殊情况) */
  43. export const generateFilterField = filter => {
  44. if (!filter) return {}
  45. const keys = Object.keys(filter)
  46. const nFiter = { ...filter }
  47. const district = keys.find(item => item.startsWith('district'))
  48. if (district?.length) {
  49. const [province = null, city = null, area = null] = filter[district] || []
  50. nFiter.province = province
  51. nFiter.city = city
  52. nFiter.area = area
  53. delete nFiter[district]
  54. }
  55. return nFiter
  56. }
  57. // 可控长度的随机数拼接时间戳成产唯一id
  58. export const createUid = () => URL.createObjectURL(new Blob()).substr(-36)
  59. // Number(Math.random().toString().substr(3, length) + Date.now()).toString(36)
  60. // 禁用当前日期后面的日期,根据类型而定
  61. export function disabledDate(current, type) {
  62. return current && current > dayjs(new Date()).endOf(type)
  63. }