common.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /** @format */
  2. import { useMyRequest } from "~/composables/useFetchRequest"
  3. enum Api {
  4. CategoryList = "/client/category/merchandise",
  5. NewCategoryList = "/api-s002/categoryUsa/rootList",
  6. OssUploadFile = "/sys/oss/file/otherUpload",
  7. DictUrl = "/shop/sys/dict",
  8. Subscribe = "/client/spCustomSubscription/add",
  9. ContactUs = "/client/guestbook/add",
  10. labels = "/client/centerProductTag/listTree",
  11. BannerData = "/client/category/detail",
  12. NoticeRemind = "/client/notification/unReadList",
  13. }
  14. /**
  15. * 获取分类列表
  16. * @param params
  17. * @returns
  18. */
  19. export async function getCategoryListApi(params?: any) {
  20. const data = await useMyRequest().get(Api.CategoryList, params)
  21. const result = handleData(data)
  22. return result
  23. }
  24. function handleData(data: any) {
  25. if (data && isArray(data)) {
  26. data.forEach((item) => {
  27. item.label = item.title
  28. if (item.children && item.children.length > 0) handleData(item.children)
  29. })
  30. }
  31. return data
  32. }
  33. /**
  34. * 获取分类数据
  35. * @returns
  36. */
  37. export async function getBannerDataApi(data: any) {
  38. return await useMyRequest().get(Api.BannerData, data)
  39. }
  40. /**
  41. * 获取字典列表数据
  42. * @returns
  43. */
  44. export async function getDictListApi(code: string) {
  45. return await useMyRequest().get(Api.DictUrl, {
  46. code,
  47. })
  48. }
  49. /**
  50. * 获取产品标签 + Trending
  51. * @returns
  52. */
  53. export async function getProductLabelAndTrendApi(data: any) {
  54. return await useMyRequest().get(Api.labels, data)
  55. }
  56. /**
  57. * 上传文件
  58. * @param file
  59. * @returns
  60. */
  61. export async function ossUploadApi(file: File) {
  62. const formData = new FormData()
  63. formData.append("file", file)
  64. try {
  65. const result = await useMyRequest().post(Api.OssUploadFile, formData)
  66. return result
  67. } catch (error) {
  68. console.error("Upload failed:", error)
  69. }
  70. }
  71. /**
  72. * 订阅
  73. */
  74. export async function submitSubscribeApi(params: any) {
  75. return await useMyRequest().post(Api.Subscribe, params)
  76. }
  77. /**
  78. * 联系我们
  79. */
  80. export async function getContactUsApi(params: any) {
  81. return await useMyRequest().post(Api.ContactUs, params)
  82. }
  83. /**
  84. * 获取未读提醒列表
  85. */
  86. export async function getNoticeRemindApi(params: any) {
  87. return await useMyRequest().get(Api.NoticeRemind, params)
  88. }