index.ts 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import Taro from '@tarojs/taro'
  2. import { PropsWithChildren } from 'react'
  3. export const noop = () => {}
  4. export function wrapProps(props: PropsWithChildren<CustomPropsType>) {
  5. let {
  6. adpId,
  7. width,
  8. onEmitOpenMiniProgram = noop,
  9. onError = noop,
  10. onLoad = noop,
  11. onClose = noop
  12. } = props
  13. if (!width) {
  14. const windowInfo = Taro.getWindowInfo()
  15. width = windowInfo?.windowWidth
  16. }
  17. return {
  18. adpId,
  19. width,
  20. onEmitOpenMiniProgram,
  21. onError,
  22. onLoad,
  23. onClose
  24. }
  25. }
  26. export function generateUUID () {
  27. let d = new Date().getTime();
  28. let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
  29. let r = (d + Math.random() * 16) % 16 | 0;
  30. d = Math.floor(d / 16);
  31. return (c === 'x' ? r : (r & 0x7) | 0x8).toString(16);
  32. });
  33. return uuid;
  34. }
  35. export function parseURLParams(url) {
  36. const search = {}
  37. let hash = ''
  38. let href = ''
  39. url.replace(/([^?=&#]+)=([^?=&#]+)/g, (_, key, value) => search[key] = value)
  40. url.replace(/#([^?=&#]+)/g, (_, h) => hash = h)
  41. href = url.split(/[\?|#]/)[0]
  42. return {
  43. href,
  44. search,
  45. hash
  46. }
  47. }
  48. export function queryURLParams(params) {
  49. let search = Object.entries(params).reduce((cac, cur) => {
  50. const [key, val] = cur
  51. return cac += `${key}=${val}&`
  52. }, '')
  53. return search.slice(0, -1)
  54. }