base.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 (pageId: string): Promise<PageConfig | null> => {
  27. const res = await get<PageConfig>(apis.PAGE_CONFIG, { pageId });
  28. return res.data;
  29. };
  30. // 获取消息列表
  31. export const getMessages = async (current: number, size: number, type?: string) => {
  32. const params: any = { current, size };
  33. if (type) params.type = type;
  34. const res = await get(apis.MESSAGE, params);
  35. return res.success ? res.data : null;
  36. };
  37. // 获取消息列表(分页)
  38. export const getMessageList = async (current: number, size: number, type?: string) => {
  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 : { records: [], total: 0 };
  43. };
  44. // 获取参数配置
  45. export const getParamConfig = async (code: string) => {
  46. const res = await get(apis.PARAM_CONFIG, { code });
  47. return res.data;
  48. };
  49. // 提交反馈
  50. export const submitFeedback = async (data: any) => {
  51. const res = await post(apis.FEEDBACK, data);
  52. return res.data;
  53. };
  54. // 追踪
  55. export const track = async () => {
  56. const res = await get(apis.TRACK);
  57. return res.data;
  58. };
  59. // 上传文件
  60. export const uploadFile = async (fileUri: string, folder = 'avatar'): Promise<string | null> => {
  61. try {
  62. const formData = new FormData();
  63. // 获取文件名
  64. const fileName = fileUri.split('/').pop() || 'image.jpg';
  65. // 创建文件对象 (React Native 格式)
  66. const fileObj = {
  67. uri: fileUri,
  68. type: 'image/jpeg',
  69. name: fileName,
  70. } as any;
  71. formData.append('file', fileObj);
  72. formData.append('appId', 'supermart-acetoys');
  73. formData.append('folder', folder);
  74. // 使用http.ts中的getToken获取token
  75. const { getToken } = require('./http');
  76. const token = getToken();
  77. const response = await fetch('https://mm.acetoys.cn/api/oss/file/upload', {
  78. method: 'POST',
  79. headers: {
  80. // 不设置 Content-Type,让 fetch 自动处理 multipart/form-data 边界
  81. 'Authentication': token || '',
  82. },
  83. body: formData,
  84. });
  85. // 检查响应状态
  86. if (!response.ok) {
  87. const errorText = await response.text();
  88. console.error('上传响应状态:', response.status, '响应内容:', errorText);
  89. return null;
  90. }
  91. const text = await response.text();
  92. if (!text) {
  93. console.error('上传响应为空');
  94. return null;
  95. }
  96. const result = JSON.parse(text);
  97. // code 可能是数字或字符串
  98. if ((result.code === 0 || result.code === '0') && result.data?.url) {
  99. return result.data.url;
  100. }
  101. console.error('上传返回错误:', result);
  102. return null;
  103. } catch (error) {
  104. console.error('上传文件失败:', error);
  105. return null;
  106. }
  107. };
  108. export default {
  109. getPageConfig,
  110. getMessages,
  111. getMessageList,
  112. getParamConfig,
  113. submitFeedback,
  114. track,
  115. uploadFile,
  116. };