|
@@ -1,10 +1,9 @@
|
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
|
import axios, { AxiosRequestConfig } from 'axios';
|
|
|
import Constants from 'expo-constants';
|
|
import Constants from 'expo-constants';
|
|
|
|
|
+import { fetch, FetchRequestInit } from 'expo/fetch';
|
|
|
import { Platform } from 'react-native';
|
|
import { Platform } from 'react-native';
|
|
|
import { type AccessToken } from './auth';
|
|
import { type AccessToken } from './auth';
|
|
|
|
|
|
|
|
-import * as fs from 'expo-file-system/legacy';
|
|
|
|
|
-
|
|
|
|
|
const { api: apiConfig, jsVersion } = require('@/config.json') as AppConfig;
|
|
const { api: apiConfig, jsVersion } = require('@/config.json') as AppConfig;
|
|
|
let accessToken: AccessToken | undefined | null = null;
|
|
let accessToken: AccessToken | undefined | null = null;
|
|
|
|
|
|
|
@@ -67,7 +66,7 @@ export class ApiError extends HttpError {
|
|
|
|
|
|
|
|
interface ApiResponse<T> {
|
|
interface ApiResponse<T> {
|
|
|
code?: number;
|
|
code?: number;
|
|
|
- message?: string;
|
|
|
|
|
|
|
+ msg?: string;
|
|
|
data?: T;
|
|
data?: T;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -154,6 +153,7 @@ if (__DEV__) {
|
|
|
type Options = Omit<AxiosRequestConfig, 'headers'> & { headers?: Record<string, any> };
|
|
type Options = Omit<AxiosRequestConfig, 'headers'> & { headers?: Record<string, any> };
|
|
|
async function request<T>(url: string, method: 'get' | 'post' | 'put' | 'delete', data?: any, config?: Options): Promise<T> {
|
|
async function request<T>(url: string, method: 'get' | 'post' | 'put' | 'delete', data?: any, config?: Options): Promise<T> {
|
|
|
const headers: Record<string, string> = {
|
|
const headers: Record<string, string> = {
|
|
|
|
|
+ 'Content-Type': 'application/json',
|
|
|
...config?.headers
|
|
...config?.headers
|
|
|
};
|
|
};
|
|
|
|
|
|
|
@@ -176,7 +176,7 @@ async function request<T>(url: string, method: 'get' | 'post' | 'put' | 'delete'
|
|
|
|
|
|
|
|
|
|
|
|
|
if (`${res?.code}` !== '1') {
|
|
if (`${res?.code}` !== '1') {
|
|
|
- throw new ApiError(res?.message || response.statusText, res?.code || 0, res?.data);
|
|
|
|
|
|
|
+ throw new ApiError(res?.msg || response.statusText, res?.code || 0, res?.data);
|
|
|
}
|
|
}
|
|
|
return res?.data as T;
|
|
return res?.data as T;
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
@@ -211,26 +211,27 @@ const rawRequest = async <T>(req: AxiosRequestConfig) => {
|
|
|
return res;
|
|
return res;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-async function uploadFile<T>(url: string, fileUri: string, config?: Options): Promise<T> {
|
|
|
|
|
|
|
+async function uploadFile<T>(url: string, config?: FetchRequestInit): Promise<T> {
|
|
|
const headers: Record<string, any> = {
|
|
const headers: Record<string, any> = {
|
|
|
- 'Content-Type': 'multipart/form-data',
|
|
|
|
|
|
|
+ // 'Content-Type': 'multipart/form-data',
|
|
|
...config?.headers,
|
|
...config?.headers,
|
|
|
};
|
|
};
|
|
|
const token = getAccessToken();
|
|
const token = getAccessToken();
|
|
|
if (token?.token) {
|
|
if (token?.token) {
|
|
|
headers['Authorization'] = `Bearer ${token.token}`;
|
|
headers['Authorization'] = `Bearer ${token.token}`;
|
|
|
}
|
|
}
|
|
|
- const result = await fs.uploadAsync(`${apiConfig.url}${url}`, fileUri, {
|
|
|
|
|
- fieldName: 'file',
|
|
|
|
|
- httpMethod: 'POST',
|
|
|
|
|
|
|
+ const result = await fetch(`${apiConfig.url}${url}`, {
|
|
|
|
|
+ method: 'POST',
|
|
|
|
|
+ ... config,
|
|
|
headers
|
|
headers
|
|
|
});
|
|
});
|
|
|
|
|
+ console.log('上传文件响应状态:', result);
|
|
|
if (result.status !== 200) {
|
|
if (result.status !== 200) {
|
|
|
- throw new HttpError(result.body, result.status);
|
|
|
|
|
|
|
+ throw new HttpError((await result.bytes()).toString(), result.status);
|
|
|
}
|
|
}
|
|
|
- const res = JSON.parse(result.body) as ApiResponse<T>;
|
|
|
|
|
|
|
+ const res = await result.json() as ApiResponse<T>;
|
|
|
if (`${res?.code}` !== '1') {
|
|
if (`${res?.code}` !== '1') {
|
|
|
- throw new ApiError(res?.message || 'upload error', res?.code || 0, res?.data);
|
|
|
|
|
|
|
+ throw new ApiError(res?.msg || 'upload error', res?.code || 0, res?.data);
|
|
|
}
|
|
}
|
|
|
return res.data as T;
|
|
return res.data as T;
|
|
|
}
|
|
}
|