common.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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-s003/guestbook/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)
  29. handleData(item.children)
  30. })
  31. }
  32. return data
  33. }
  34. /**
  35. * 获取分类数据
  36. * @returns
  37. */
  38. export async function getBannerDataApi(data: any) {
  39. return await useMyRequest().get(Api.BannerData, data)
  40. }
  41. /**
  42. * 获取字典列表数据
  43. * @returns
  44. */
  45. export async function getDictListApi(code: string) {
  46. return await useMyRequest().get(Api.DictUrl, {
  47. code,
  48. })
  49. }
  50. /**
  51. * 获取产品标签 + Trending
  52. * @returns
  53. */
  54. export async function getProductLabelAndTrendApi(data: any) {
  55. return await useMyRequest().get(Api.labels, data)
  56. }
  57. /**
  58. * 上传文件
  59. * @param file
  60. * @returns
  61. */
  62. export async function ossUploadApi(file: File) {
  63. const formData = new FormData()
  64. formData.append('file', file)
  65. try {
  66. const result = await useMyRequest().post(Api.OssUploadFile, formData)
  67. return result
  68. }
  69. catch (error) {
  70. console.error('Upload failed:', error)
  71. }
  72. }
  73. /**
  74. * 订阅
  75. */
  76. export async function submitSubscribeApi(params: any) {
  77. return await useMyRequest().post(Api.Subscribe, params)
  78. }
  79. /**
  80. * 联系我们
  81. */
  82. export async function getContactUsApi(params: any) {
  83. return await useMyRequest().post(Api.ContactUs, params)
  84. }
  85. /**
  86. * 获取未读提醒列表
  87. */
  88. export async function getNoticeRemindApi(params: any) {
  89. return await useMyRequest().get(Api.NoticeRemind, params)
  90. }