useData.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /** @format */
  2. import { getGoodsListApi } from "~/api/model/goods"
  3. import { PageSizeEnum } from "~/enums/sizeEnum"
  4. import { getCategoryListApi } from "~/api/model/common"
  5. import { updateURLParameter } from "~/utils/object"
  6. type FeatureType = "Newest" | "Trending" | " Exlusive"
  7. export function useData() {
  8. const categories = ref<any>([])
  9. const goodsList = ref<any>([])
  10. const selectedCategory = ref()
  11. const page_size = ref(15)
  12. const currentPage = ref(PageSizeEnum.PAGE)
  13. const total = ref()
  14. const top_data = ref({
  15. entityQty: 0,
  16. })
  17. const form = ref<any>({
  18. orderby: "",
  19. trendingCollections: [],
  20. recommend: [],
  21. texture: [],
  22. categoryId: "",
  23. })
  24. const getCategories = async () => {
  25. const route = useRoute()
  26. const slug = route.params.id
  27. const data = await getCategoryListApi({ slug, all: true, np: 1 })
  28. top_data.value = data.shift()
  29. categories.value = data
  30. top_data.value.entityQty = data.reduce((total: number, item: any) => {
  31. total += item.entityQty
  32. return total
  33. }, 0)
  34. }
  35. const getGoodsList = async (
  36. params?: any,
  37. pageNo = currentPage.value,
  38. pageSize = page_size.value
  39. ) => {
  40. const data: any = await getGoodsListApi({
  41. ...params,
  42. pageNo,
  43. pageSize,
  44. })
  45. total.value = data.total
  46. currentPage.value = data.current
  47. data.records.forEach((item: any) => {
  48. if (form.value.recommend.length && item.recommend) {
  49. if (item.recommend.includes(form.value.recommend[0]))
  50. item.recommend = form.value.recommend[0]
  51. }
  52. })
  53. goodsList.value = data.records
  54. }
  55. const handleSelectedCategory = (value: any) => {
  56. selectedCategory.value = value
  57. form.value.categoryId = selectedCategory.value
  58. getGoodsList(form.value, PageSizeEnum.PAGE, page_size.value)
  59. // 将地址栏中的secondKey参数改为选中的值key
  60. const route = useRoute()
  61. const secondLevel = route.query.secondKey
  62. if (secondLevel) {
  63. updateURLParameter("secondKey", value)
  64. }
  65. }
  66. const handleSelectedFilters = (type: any, value: any) => {
  67. if (type === "clearAll") {
  68. form.value.recommend = []
  69. form.value.texture = []
  70. form.value.trendingCollections = []
  71. } else {
  72. ;(form.value as any)[type] = value
  73. }
  74. getGoodsList(form.value, PageSizeEnum.PAGE, page_size.value)
  75. }
  76. const handleOrderBy = (value: any) => {
  77. ;(form.value as any).orderby = value
  78. getGoodsList(form.value, PageSizeEnum.PAGE, page_size.value)
  79. }
  80. const changePage = (current: number, size: number) => {
  81. if (!selectedCategory.value) {
  82. const route = useRoute()
  83. selectedCategory.value = route.query.key
  84. }
  85. form.value.categoryId = selectedCategory.value
  86. getGoodsList(form.value, current, size)
  87. const dom: any = document.getElementById("app-scroller")
  88. dom.scrollTo({
  89. top: 400,
  90. behavior: "smooth",
  91. })
  92. }
  93. return {
  94. goodsList,
  95. getCategories,
  96. categories,
  97. selectedCategory,
  98. page_size,
  99. changePage,
  100. currentPage,
  101. total,
  102. top_data,
  103. form,
  104. getGoodsList,
  105. handleSelectedCategory,
  106. handleSelectedFilters,
  107. handleOrderBy,
  108. }
  109. }