misc.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. export const randomRange = (min: number, max: number) => {
  2. return Math.random() * (max - min) + min
  3. }
  4. export const scrollToElement = (selector: string) => {
  5. const el = document.querySelector(selector)
  6. if (el) {
  7. setTimeout(() => {
  8. el.scrollIntoView({
  9. behavior: 'smooth'
  10. })
  11. }, 17)
  12. }
  13. }
  14. export const findTopCatBySubCatId = (classes: any[], catId: string): any[] | null => {
  15. for (const topCat of classes) {
  16. const cats = topCat.categories
  17. const matchedCat = cats && cats.length && cats.find((cat) => cat.cat_id === catId)
  18. if (matchedCat) {
  19. return [topCat, matchedCat]
  20. }
  21. }
  22. return null
  23. }
  24. export const getTopCatSubCatIds = (topCat) => {
  25. return topCat && topCat.categories && topCat.categories.map((cat) => cat.cat_id) || []
  26. }
  27. export const parseCatIdAndServiceType = (input: {
  28. cat_id: string
  29. st: string
  30. classes: any[]
  31. serviceTypes: any[]
  32. }) => {
  33. const {
  34. cat_id,
  35. st,
  36. classes = [],
  37. serviceTypes = []
  38. } = input
  39. let topCatId = '_'
  40. let catId = '_'
  41. let serviceType = '_'
  42. let matchedCatIds: string[] = []
  43. let topCatName!: string
  44. let catName!: string
  45. if (cat_id) {
  46. let topCat
  47. let cat
  48. if (cat_id.length === 8) {
  49. topCat = classes.find((topCat) => topCat.hash_id === cat_id)
  50. matchedCatIds = getTopCatSubCatIds(topCat)
  51. } else {
  52. [topCat, cat] = findTopCatBySubCatId(classes, cat_id) || []
  53. if (cat) {
  54. catId = cat_id
  55. catName = cat.name
  56. matchedCatIds = [catId]
  57. }
  58. }
  59. if (topCat) {
  60. topCatId = topCat.hash_id
  61. topCatName = topCat.name
  62. }
  63. }
  64. if (st) {
  65. const matchedServiceType = serviceTypes.find((item) => item.hash_id === st)
  66. if (matchedServiceType) {
  67. serviceType = st
  68. }
  69. }
  70. return {
  71. topCatId,
  72. topCatName,
  73. catId,
  74. catName,
  75. serviceType,
  76. matchedCatIds
  77. }
  78. }
  79. export const genCatIdSearchSentence = (catIds: string[], serviceType: string) => {
  80. return [catIds.join('|'), serviceType].filter((val) => !!val && val != '_').join('&&')
  81. }