base.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // 基础服务 - 对应 supermart-mini/service/base.js
  2. import { Platform } from "react-native";
  3. import { get, getToken, post } from "./http";
  4. const apis = {
  5. PAGE_CONFIG: "/api/app/page/getByPageId",
  6. MESSAGE: "/api/app/message",
  7. TRACK: "/api/track",
  8. FEEDBACK: "/api/app/feedback/submit",
  9. PARAM_CONFIG: "/param/paramConfig",
  10. };
  11. export interface BannerItem {
  12. id: string;
  13. cover: string;
  14. path?: { url: string };
  15. }
  16. export interface TabItem {
  17. title: string;
  18. cover: string;
  19. path?: { url: string };
  20. }
  21. export interface PageConfig {
  22. components: Array<{
  23. elements: any[];
  24. }>;
  25. }
  26. // 获取页面配置
  27. export const getPageConfig = async (
  28. pageId: string,
  29. ): Promise<PageConfig | null> => {
  30. const res = await get<PageConfig>(apis.PAGE_CONFIG, { pageId });
  31. return res.data;
  32. };
  33. // 获取消息列表
  34. export const getMessages = async (
  35. current: number,
  36. size: number,
  37. type?: string,
  38. ) => {
  39. const params: any = { current, size };
  40. if (type) params.type = type;
  41. const res = await get(apis.MESSAGE, params);
  42. return res.success ? res.data : null;
  43. };
  44. // 获取消息列表(分页)
  45. export const getMessageList = async (
  46. current: number,
  47. size: number,
  48. type?: string,
  49. ) => {
  50. const params: any = { current, size };
  51. if (type) params.type = type;
  52. const res = await get(apis.MESSAGE, params);
  53. return res.success ? res.data : { records: [], total: 0 };
  54. };
  55. // 获取参数配置
  56. export const getParamConfig = async (code: string) => {
  57. const res = await get(apis.PARAM_CONFIG, { code });
  58. return res.data;
  59. };
  60. // 提交反馈
  61. export const submitFeedback = async (data: any) => {
  62. const res = await post(apis.FEEDBACK, data);
  63. return res.data;
  64. };
  65. // 追踪
  66. export const track = async () => {
  67. const res = await get(apis.TRACK);
  68. return res.data;
  69. };
  70. // 上传文件
  71. export const uploadFile = async (
  72. fileUri: string,
  73. folder = "avatar",
  74. externalToken?: string,
  75. ): Promise<string | { error: string }> => {
  76. try {
  77. let processedUri = decodeURI(fileUri);
  78. if (Platform.OS === "ios") {
  79. if (!processedUri.startsWith("file://") && !processedUri.startsWith("http")) {
  80. processedUri = `file://${processedUri}`;
  81. }
  82. }
  83. const fileName = processedUri.split("/").pop() || `avatar_${Date.now()}.jpg`;
  84. const fileExt = fileName.split(".").pop()?.toLowerCase() || "jpg";
  85. const mimeType = fileExt === "png" ? "image/png" : "image/jpeg";
  86. console.log("[uploadFile] uri:", processedUri.substring(0, 80), "name:", fileName);
  87. const formData = new FormData();
  88. formData.append("file", {
  89. uri: processedUri,
  90. type: mimeType,
  91. name: fileName,
  92. } as any);
  93. formData.append("appId", "supermart-acetoys");
  94. formData.append("folder", folder);
  95. const token = externalToken || getToken();
  96. console.log("[uploadFile] token:", token ? token.substring(0, 10) + "..." : "空!");
  97. const response = await fetch("https://mm.acefig.com/api/oss/file/upload", {
  98. method: "POST",
  99. headers: {
  100. Authentication: token || "",
  101. },
  102. body: formData,
  103. });
  104. const responseText = await response.text();
  105. console.log("[uploadFile] status:", response.status, "body:", responseText?.substring(0, 200));
  106. if (!response.ok) {
  107. return { error: `HTTP ${response.status}: ${responseText?.substring(0, 100)}` };
  108. }
  109. if (!responseText) {
  110. return { error: "服务器响应为空" };
  111. }
  112. const result = JSON.parse(responseText);
  113. if ((result.code === 0 || result.code === "0") && result.data?.url) {
  114. return result.data.url;
  115. }
  116. return { error: `code:${result.code} msg:${result.msg || JSON.stringify(result).substring(0, 80)}` };
  117. } catch (error: any) {
  118. console.error("[uploadFile] 异常:", error);
  119. return { error: `异常: ${error?.message || String(error).substring(0, 100)}` };
  120. }
  121. };
  122. export default {
  123. getPageConfig,
  124. getMessages,
  125. getMessageList,
  126. getParamConfig,
  127. submitFeedback,
  128. track,
  129. uploadFile,
  130. };