| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- // 基础服务 - 对应 supermart-mini/service/base.js
- import { Platform } from "react-native";
- import { get, getToken, post } from "./http";
- const apis = {
- PAGE_CONFIG: "/api/app/page/getByPageId",
- MESSAGE: "/api/app/message",
- TRACK: "/api/track",
- FEEDBACK: "/api/app/feedback/submit",
- PARAM_CONFIG: "/param/paramConfig",
- };
- export interface BannerItem {
- id: string;
- cover: string;
- path?: { url: string };
- }
- export interface TabItem {
- title: string;
- cover: string;
- path?: { url: string };
- }
- export interface PageConfig {
- components: Array<{
- elements: any[];
- }>;
- }
- // 获取页面配置
- export const getPageConfig = async (
- pageId: string,
- ): Promise<PageConfig | null> => {
- const res = await get<PageConfig>(apis.PAGE_CONFIG, { pageId });
- return res.data;
- };
- // 获取消息列表
- export const getMessages = async (
- current: number,
- size: number,
- type?: string,
- ) => {
- const params: any = { current, size };
- if (type) params.type = type;
- const res = await get(apis.MESSAGE, params);
- return res.success ? res.data : null;
- };
- // 获取消息列表(分页)
- export const getMessageList = async (
- current: number,
- size: number,
- type?: string,
- ) => {
- const params: any = { current, size };
- if (type) params.type = type;
- const res = await get(apis.MESSAGE, params);
- return res.success ? res.data : { records: [], total: 0 };
- };
- // 获取参数配置
- export const getParamConfig = async (code: string) => {
- const res = await get(apis.PARAM_CONFIG, { code });
- return res.data;
- };
- // 提交反馈
- export const submitFeedback = async (data: any) => {
- const res = await post(apis.FEEDBACK, data);
- return res.data;
- };
- // 追踪
- export const track = async () => {
- const res = await get(apis.TRACK);
- return res.data;
- };
- // 上传文件
- export const uploadFile = async (
- fileUri: string,
- folder = "avatar",
- ): Promise<string | null> => {
- try {
- // iOS ImagePicker 可能返回各种格式的路径,统一处理
- let processedUri = decodeURI(fileUri);
- if (Platform.OS === "ios") {
- // 确保有 file:// 前缀
- if (!processedUri.startsWith("file://") && !processedUri.startsWith("http")) {
- processedUri = `file://${processedUri}`;
- }
- }
- const formData = new FormData();
- // 获取文件名
- const fileName = processedUri.split("/").pop() || `avatar_${Date.now()}.jpg`;
- // 创建文件对象 (React Native 格式)
- const fileObj = {
- uri: processedUri,
- type: "image/jpeg",
- name: fileName,
- } as any;
- formData.append("file", fileObj);
- formData.append("appId", "supermart-acetoys");
- formData.append("folder", folder);
- // 获取 token
- const token = getToken();
- console.log("[uploadFile] token:", token ? "有" : "无", "uri:", processedUri.substring(0, 50));
- // 构建 track header(与 Vue 原项目 http.js 一致)
- const track = JSON.stringify({
- os: Platform.OS === "ios" ? 5 : 6,
- version: "1.0.5",
- deviceCode: "",
- bundleId: "com.wonderful.mart",
- channel: "wonderful",
- });
- const response = await fetch("https://mm.acefig.com/api/oss/file/upload", {
- method: "POST",
- headers: {
- // 不设置 Content-Type,让 fetch 自动处理 multipart/form-data 边界
- Authentication: token || "",
- track: track,
- },
- body: formData,
- });
- console.log("[uploadFile] 响应状态:", response.status);
- // 检查响应状态
- if (!response.ok) {
- const errorText = await response.text();
- console.error("[uploadFile] 上传失败 状态:", response.status, "内容:", errorText);
- return null;
- }
- const text = await response.text();
- console.log("[uploadFile] 响应内容:", text?.substring(0, 200));
- if (!text) {
- console.error("[uploadFile] 上传响应为空");
- return null;
- }
- const result = JSON.parse(text);
- // code 可能是数字或字符串
- if ((result.code === 0 || result.code === "0") && result.data?.url) {
- console.log("[uploadFile] 成功:", result.data.url);
- return result.data.url;
- }
- console.error("[uploadFile] 上传返回错误:", JSON.stringify(result));
- return null;
- } catch (error) {
- console.error("[uploadFile] 上传文件异常:", error);
- return null;
- }
- };
- export default {
- getPageConfig,
- getMessages,
- getMessageList,
- getParamConfig,
- submitFeedback,
- track,
- uploadFile,
- };
|