|
|
@@ -1,25 +1,25 @@
|
|
|
// import { getAccessToken } from '@/apis/auth';
|
|
|
-import axios from 'axios';
|
|
|
+import axios, { AxiosRequestConfig } from 'axios';
|
|
|
import Constants from 'expo-constants';
|
|
|
import { Platform } from 'react-native';
|
|
|
import { type AccessToken } from './auth';
|
|
|
-const {api: apiConfig, jsVersion} = require('@/config.json') as AppConfig;
|
|
|
+const { api: apiConfig, jsVersion } = require('@/config.json') as AppConfig;
|
|
|
let accessToken: AccessToken | undefined | null = null;
|
|
|
|
|
|
export function setAccessToken(token?: AccessToken | null) {
|
|
|
-
|
|
|
+
|
|
|
accessToken = token;
|
|
|
}
|
|
|
|
|
|
export function getAccessToken() {
|
|
|
- if (!accessToken) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- const now = Date.now() / 1000;
|
|
|
- if (accessToken.expiresAt < now - 30) {
|
|
|
- accessToken = null;
|
|
|
- }
|
|
|
- return accessToken;
|
|
|
+ if (!accessToken) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ const now = Date.now() / 1000;
|
|
|
+ if (accessToken.expiresAt < now - 30) {
|
|
|
+ accessToken = null;
|
|
|
+ }
|
|
|
+ return accessToken;
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -60,7 +60,7 @@ export class HttpError {
|
|
|
|
|
|
|
|
|
export class ApiError extends HttpError {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -79,7 +79,7 @@ const verString = (() => {
|
|
|
const apiClient = axios.create({
|
|
|
baseURL: apiConfig.url,
|
|
|
timeout: apiConfig.timeout || 10000,
|
|
|
-
|
|
|
+
|
|
|
headers: {
|
|
|
"x-app-name": Constants.expoConfig?.slug || 'unknown',
|
|
|
"x-app-version": verString,
|
|
|
@@ -90,7 +90,7 @@ const apiClient = axios.create({
|
|
|
validateStatus: (status) => {
|
|
|
return status > 199 && status < 500;
|
|
|
},
|
|
|
-
|
|
|
+
|
|
|
});
|
|
|
|
|
|
// 开发模式下输出请求和响应数据
|
|
|
@@ -166,13 +166,13 @@ async function request<T>(url: string, method: 'get' | 'post' | 'put' | 'delete'
|
|
|
headers,
|
|
|
});
|
|
|
if (200 !== response.status) {
|
|
|
- throw new HttpError(response.statusText || 'http error', response.status);
|
|
|
+ throw new HttpError(response.statusText || 'http error', response.status);
|
|
|
}
|
|
|
const res = response.data;
|
|
|
-
|
|
|
+
|
|
|
|
|
|
if (`${res?.code}` !== '1') {
|
|
|
- throw new ApiError(res?.message || response.statusText, res?.code||0, res?.data);
|
|
|
+ throw new ApiError(res?.message || response.statusText, res?.code || 0, res?.data);
|
|
|
}
|
|
|
return res?.data as T;
|
|
|
} catch (error) {
|
|
|
@@ -184,6 +184,29 @@ async function request<T>(url: string, method: 'get' | 'post' | 'put' | 'delete'
|
|
|
throw new HttpError(error?.message || (`${error}`) || "unknown error", 500);
|
|
|
}
|
|
|
}
|
|
|
+const rawRequest = async <T>(req: AxiosRequestConfig) => {
|
|
|
+
|
|
|
+ const headers: Record<string, string> = {
|
|
|
+ ...req.headers as any,
|
|
|
+ "x-app-name": Constants.expoConfig?.slug || 'unknown',
|
|
|
+ "x-app-version": verString,
|
|
|
+ "x-app-platform": Platform.OS,
|
|
|
+ };
|
|
|
+ let token = getAccessToken();
|
|
|
+ if (token?.token) {
|
|
|
+ headers['Authorization'] = `Bearer ${token.token}`;
|
|
|
+ }
|
|
|
+
|
|
|
+ const res = await axios.request<T>({
|
|
|
+ ...req,
|
|
|
+ timeout: apiConfig.timeout || 10000,
|
|
|
+ headers
|
|
|
+ });
|
|
|
+ if (res.status !== 200) {
|
|
|
+ throw new HttpError(res.statusText, res.status, res.data as string);
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+}
|
|
|
|
|
|
|
|
|
async function get<T>(api: string, params?: Record<string, any>): Promise<T> {
|
|
|
@@ -201,7 +224,7 @@ async function put<T>(api: string, data?: any): Promise<T> {
|
|
|
async function deleted<T>(api: string): Promise<T> {
|
|
|
return await request<T>(api, 'delete', {}, undefined);
|
|
|
} const api = {
|
|
|
- post,get,put,deleted
|
|
|
+ post, get, put, deleted, request, rawRequest
|
|
|
}
|
|
|
|
|
|
|