base.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // 基础服务 - 对应 supermart-mini/service/base.js
  2. import { get, post } from "./http";
  3. const apis = {
  4. PAGE_CONFIG: "/api/app/page/getByPageId",
  5. MESSAGE: "/api/app/message",
  6. TRACK: "/api/track",
  7. FEEDBACK: "/api/app/feedback/submit",
  8. PARAM_CONFIG: "/param/paramConfig",
  9. };
  10. export interface BannerItem {
  11. id: string;
  12. cover: string;
  13. path?: { url: string };
  14. }
  15. export interface TabItem {
  16. title: string;
  17. cover: string;
  18. path?: { url: string };
  19. }
  20. export interface PageConfig {
  21. components: Array<{
  22. elements: any[];
  23. }>;
  24. }
  25. // 获取页面配置
  26. export const getPageConfig = async (
  27. pageId: string,
  28. ): Promise<PageConfig | null> => {
  29. const res = await get<PageConfig>(apis.PAGE_CONFIG, { pageId });
  30. return res.data;
  31. };
  32. // 获取消息列表
  33. export const getMessages = async (
  34. current: number,
  35. size: number,
  36. type?: string,
  37. ) => {
  38. const params: any = { current, size };
  39. if (type) params.type = type;
  40. const res = await get(apis.MESSAGE, params);
  41. return res.success ? res.data : null;
  42. };
  43. // 获取消息列表(分页)
  44. export const getMessageList = async (
  45. current: number,
  46. size: number,
  47. type?: string,
  48. ) => {
  49. const params: any = { current, size };
  50. if (type) params.type = type;
  51. const res = await get(apis.MESSAGE, params);
  52. return res.success ? res.data : { records: [], total: 0 };
  53. };
  54. // 获取参数配置
  55. export const getParamConfig = async (code: string) => {
  56. const res = await get(apis.PARAM_CONFIG, { code });
  57. return res.data;
  58. };
  59. // 提交反馈
  60. export const submitFeedback = async (data: any) => {
  61. const res = await post(apis.FEEDBACK, data);
  62. return res.data;
  63. };
  64. // 追踪
  65. export const track = async () => {
  66. const res = await get(apis.TRACK);
  67. return res.data;
  68. };
  69. // 上传文件
  70. export const uploadFile = async (
  71. fileUri: string,
  72. folder = "avatar",
  73. ): Promise<string | null> => {
  74. try {
  75. const formData = new FormData();
  76. // 获取文件名
  77. const fileName = fileUri.split("/").pop() || "image.jpg";
  78. // 创建文件对象 (React Native 格式)
  79. const fileObj = {
  80. uri: fileUri,
  81. type: "image/jpeg",
  82. name: fileName,
  83. } as any;
  84. formData.append("file", fileObj);
  85. formData.append("appId", "supermart-acetoys");
  86. formData.append("folder", folder);
  87. // 使用http.ts中的getToken获取token
  88. const { getToken } = require("./http");
  89. const token = getToken();
  90. const response = await fetch("https://mm.acefig.com/api/oss/file/upload", {
  91. method: "POST",
  92. headers: {
  93. // 不设置 Content-Type,让 fetch 自动处理 multipart/form-data 边界
  94. Authentication: token || "",
  95. },
  96. body: formData,
  97. });
  98. // 检查响应状态
  99. if (!response.ok) {
  100. const errorText = await response.text();
  101. console.error("上传响应状态:", response.status, "响应内容:", errorText);
  102. return null;
  103. }
  104. const text = await response.text();
  105. if (!text) {
  106. console.error("上传响应为空");
  107. return null;
  108. }
  109. const result = JSON.parse(text);
  110. // code 可能是数字或字符串
  111. if ((result.code === 0 || result.code === "0") && result.data?.url) {
  112. return result.data.url;
  113. }
  114. console.error("上传返回错误:", result);
  115. return null;
  116. } catch (error) {
  117. console.error("上传文件失败:", error);
  118. return null;
  119. }
  120. };
  121. export default {
  122. getPageConfig,
  123. getMessages,
  124. getMessageList,
  125. getParamConfig,
  126. submitFeedback,
  127. track,
  128. uploadFile,
  129. };