Browse Source

修改所有api,不再使用硬编码,而是从userinfo与product中获取请求headers所需要的信息

dengdx 2 months ago
parent
commit
1abb6dc99b

+ 6 - 8
src/API/bodyPart.ts

@@ -1,4 +1,4 @@
-import axios from 'axios';
+import axiosInstance from './interceptor';
 
 
 export interface BodyPartParams {
 export interface BodyPartParams {
   patient_type?: string;
   patient_type?: string;
@@ -22,19 +22,17 @@ export interface BodyPart {
 
 
 export async function fetchBodyParts(
 export async function fetchBodyParts(
   params: BodyPartParams,
   params: BodyPartParams,
+
   token: string,
   token: string,
+  // eslint-disable-next-line
   language = 'en',
   language = 'en',
+  // eslint-disable-next-line
   product = 'DROS',
   product = 'DROS',
+  // eslint-disable-next-line
   source = 'Electron'
   source = 'Electron'
 ): Promise<BodyPart[]> {
 ): Promise<BodyPart[]> {
-  const response = await axios.get('/dr/api/v1/auth/protocol/body_part', {
+  const response = await axiosInstance.get('/auth/protocol/body_part', {
     params,
     params,
-    headers: {
-      Authorization: `Bearer ${token}`,
-      Language: language,
-      Product: product,
-      Source: source,
-    },
   });
   });
   // 兼容接口返回结构
   // 兼容接口返回结构
   if (
   if (

+ 27 - 0
src/API/interceptor.ts

@@ -0,0 +1,27 @@
+import axios from 'axios';
+import store from '../states/store';
+import { API_BASE_URL } from './config';
+
+const axiosInstance = axios.create({
+  baseURL: API_BASE_URL,
+});
+
+axiosInstance.interceptors.request.use(
+  (config) => {
+    const state = store.getState();
+    const token = state.userInfo.token;
+    const { productName, language, source } = state.product;
+
+    config.headers.Authorization = `Bearer ${token}`;
+    config.headers.Language = language;
+    config.headers.Product = productName;
+    config.headers.Source = source;
+
+    return config;
+  },
+  (error) => {
+    return Promise.reject(error);
+  }
+);
+
+export default axiosInstance;

+ 2 - 9
src/API/patient/procedureActions.ts

@@ -1,4 +1,4 @@
-import axios from 'axios';
+import axiosInstance from '../interceptor';
 
 
 export interface Procedure {
 export interface Procedure {
   id: string;
   id: string;
@@ -36,14 +36,7 @@ export const fetchProcedures = async (
   bodyPart: string | null,
   bodyPart: string | null,
   isEnabled: boolean
   isEnabled: boolean
 ): Promise<ProcedureResponse> => {
 ): Promise<ProcedureResponse> => {
-  const response = await axios.get('dr/api/v1/auth/protocol/procedure', {
-    headers: {
-      Authorization:
-        'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTEyNzc5NzAsImlkIjoxLCJuYW1lIjoiYWRtaW4ifQ.ooTGwBXaNhtunbKbpqteWbjDwJLjnRmSIl80r5dp1pY',
-      Language: 'en',
-      Product: 'DROS',
-      Source: 'Electron',
-    },
+  const response = await axiosInstance.get('/auth/protocol/procedure', {
     params: {
     params: {
       patient_type: patientType,
       patient_type: patientType,
       body_part: bodyPart,
       body_part: bodyPart,

+ 2 - 14
src/API/patient/viewActions.ts

@@ -1,4 +1,4 @@
-import axios from 'axios';
+import axiosInstance from '../interceptor';
 
 
 export interface View {
 export interface View {
   internal_id: string;
   internal_id: string;
@@ -43,19 +43,7 @@ export const fetchViews = async (
   is_enabled: boolean,
   is_enabled: boolean,
   procedure_id: string | null | undefined
   procedure_id: string | null | undefined
 ): Promise<FetchViewsResponse> => {
 ): Promise<FetchViewsResponse> => {
-  const response = await axios.get('/dr/api/v1/auth/protocol/view', {
-    //todo get header info
-    //   authorization: string,
-    //   language: 'en' | 'zh',
-    //   product: 'DROS' | 'VETDROS',
-    //   source: 'Electron' | 'Browser' | 'Android'
-    headers: {
-      Authorization:
-        'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTEyNzc5NzAsImlkIjoxLCJuYW1lIjoiYWRtaW4ifQ.ooTGwBXaNhtunbKbpqteWbjDwJLjnRmSIl80r5dp1pY',
-      Language: 'en',
-      Product: 'DROS',
-      Source: 'Electron',
-    },
+  const response = await axiosInstance.get('/auth/protocol/view', {
     params: {
     params: {
       patient_type,
       patient_type,
       body_part,
       body_part,

+ 2 - 10
src/API/patient/workActions.ts

@@ -1,4 +1,4 @@
-import axios from 'axios';
+import axiosInstance from '../interceptor';
 
 
 interface View {
 interface View {
   view_id: string;
   view_id: string;
@@ -119,15 +119,7 @@ const registerWork = async (
 ): Promise<RegisterWorkResponse> => {
 ): Promise<RegisterWorkResponse> => {
   console.log('Work object:', JSON.stringify(work, null, 2));
   console.log('Work object:', JSON.stringify(work, null, 2));
 
 
-  const response = await axios.post('/dr/api/v1/auth/study', work, {
-    headers: {
-      Authorization:
-        'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTEyNzc5NzAsImlkIjoxLCJuYW1lIjoiYWRtaW4ifQ.ooTGwBXaNhtunbKbpqteWbjDwJLjnRmSIl80r5dp1pY',
-      Language: 'en',
-      Product: 'DROS',
-      Source: 'Electron',
-    },
-  });
+  const response = await axiosInstance.post('/auth/study', work);
   return response.data;
   return response.data;
 };
 };
 
 

+ 6 - 8
src/API/patientType.ts

@@ -1,4 +1,4 @@
-import axios from 'axios';
+import axiosInstance from './interceptor';
 
 
 export interface PatientTypeParams {
 export interface PatientTypeParams {
   is_enabled?: string;
   is_enabled?: string;
@@ -27,19 +27,17 @@ export interface PatientTypeResponse {
 
 
 export async function fetchPatientTypes(
 export async function fetchPatientTypes(
   params: PatientTypeParams,
   params: PatientTypeParams,
+
   token: string,
   token: string,
+  // eslint-disable-next-line
   language = 'en',
   language = 'en',
+  // eslint-disable-next-line
   product = 'DROS',
   product = 'DROS',
+  // eslint-disable-next-line
   source = 'Electron'
   source = 'Electron'
 ): Promise<PatientType[]> {
 ): Promise<PatientType[]> {
-  const response = await axios.get('/dr/api/v1/auth/protocol/patient_type', {
+  const response = await axiosInstance.get('/auth/protocol/patient_type', {
     params,
     params,
-    headers: {
-      Authorization: `Bearer ${token}`,
-      Language: language,
-      Product: product,
-      Source: source,
-    },
   });
   });
   // 兼容接口返回结构
   // 兼容接口返回结构
   if (
   if (

+ 1 - 12
src/API/security/userActions.ts

@@ -1,17 +1,6 @@
-import axios from 'axios';
-import { API_BASE_URL } from '../config';
+import axiosInstance from '../interceptor';
 
 
 // 创建带默认请求头的axios实例
 // 创建带默认请求头的axios实例
-const axiosInstance = axios.create({
-  baseURL: API_BASE_URL,
-  headers: {
-    Authorization:
-      'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3NTAwNTU5MzYsImlkIjoxLCJuYW1lIjoiYWRtaW4ifQ.LLveItQ_K0vvcmyN4qLQHxaOcz3EzY7ZeUQ97hR9R9w',
-    Language: 'en',
-    Product: 'DROS',
-    Source: 'browser',
-  },
-});
 
 
 // 登录接口
 // 登录接口
 export function login(username: string, password: string) {
 export function login(username: string, password: string) {

+ 26 - 0
src/states/productSlice.ts

@@ -0,0 +1,26 @@
+import { createSlice, PayloadAction } from '@reduxjs/toolkit';
+
+interface ProductState {
+  productName: 'DROS' | 'VETDROS';
+  language: string;
+  source: 'Electron' | 'Browser' | 'Android';
+}
+
+const initialState: ProductState = {
+  productName: 'DROS',
+  language: 'en',
+  source: 'Browser',
+};
+
+const productSlice = createSlice({
+  name: 'product',
+  initialState,
+  reducers: {
+    setProduct: (state, action: PayloadAction<ProductState>) => {
+      state = action.payload;
+    },
+  },
+});
+
+export const { setProduct } = productSlice.actions;
+export default productSlice.reducer;

+ 2 - 0
src/states/store.ts

@@ -4,6 +4,7 @@ import viewSelectionReducer from './patient/viewSelection';
 import patientTypeReducer from './patientTypeSlice';
 import patientTypeReducer from './patientTypeSlice';
 import bodyPartReducer from './bodyPartSlice';
 import bodyPartReducer from './bodyPartSlice';
 import selectionReducer from './patient/register/SelectionTypeSlice';
 import selectionReducer from './patient/register/SelectionTypeSlice';
+import productReducer from './productSlice';
 
 
 const store = configureStore({
 const store = configureStore({
   reducer: {
   reducer: {
@@ -12,6 +13,7 @@ const store = configureStore({
     patientType: patientTypeReducer,
     patientType: patientTypeReducer,
     bodyPart: bodyPartReducer,
     bodyPart: bodyPartReducer,
     selection: selectionReducer,
     selection: selectionReducer,
+    product: productReducer,
   },
   },
 });
 });