utils.js 2.1 KB

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