Browse Source

系统启动时获取软件信息,软件信息会用于api请求的头部

dengdx 2 months ago
parent
commit
62ac636679
3 changed files with 64 additions and 2 deletions
  1. 29 0
      src/API/softwareInfo.ts
  2. 6 1
      src/app.tsx
  3. 29 1
      src/states/productSlice.ts

+ 29 - 0
src/API/softwareInfo.ts

@@ -0,0 +1,29 @@
+import axiosInstance from './interceptor';
+
+export interface SoftwareInfo {
+  language: string[];
+  product: string;
+  server: Record<
+    string,
+    {
+      build: string;
+      desc: string;
+      version: string;
+    }
+  >;
+}
+
+export interface SoftwareInfoResponse {
+  code: string;
+  description: string;
+  solution: string;
+  data: SoftwareInfo;
+}
+
+export async function fetchSoftwareInfo(): Promise<SoftwareInfo> {
+  const response = await axiosInstance.get('/pub/software_info');
+  if (response.data && response.data.code === '0x000000') {
+    return response.data.data;
+  }
+  throw new Error('Failed to fetch software info');
+}

+ 6 - 1
src/app.tsx

@@ -1,9 +1,10 @@
-import { PropsWithChildren, useState } from 'react';
+import { PropsWithChildren, useState, useEffect } from 'react';
 import { useLaunch } from '@tarojs/taro';
 import { useLaunch } from '@tarojs/taro';
 import { IntlProvider } from 'react-intl';
 import { IntlProvider } from 'react-intl';
 import { ConfigProvider, Button } from 'antd';
 import { ConfigProvider, Button } from 'antd';
 import { Provider } from 'react-redux';
 import { Provider } from 'react-redux';
 import store from './states/store';
 import store from './states/store';
+import { initializeProductState } from './states/productSlice';
 import './app.css';
 import './app.css';
 import { lightTheme, darkTheme } from './themes';
 import { lightTheme, darkTheme } from './themes';
 import ProductSelector from './components/ProductSelector';
 import ProductSelector from './components/ProductSelector';
@@ -38,6 +39,10 @@ function App({ children }: PropsWithChildren<React.ReactNode>) {
     setCurrentTheme(themeConfig);
     setCurrentTheme(themeConfig);
   };
   };
 
 
+  useEffect(() => {
+    store.dispatch(initializeProductState());
+  }, []);
+
   // children 是将要会渲染的页面
   // children 是将要会渲染的页面
   // return children
   // return children
   return (
   return (

+ 29 - 1
src/states/productSlice.ts

@@ -1,4 +1,5 @@
-import { createSlice, PayloadAction } from '@reduxjs/toolkit';
+import { createSlice, PayloadAction, createAsyncThunk } from '@reduxjs/toolkit';
+import { fetchSoftwareInfo } from '../API/softwareInfo';
 
 
 interface ProductState {
 interface ProductState {
   productName: 'DROS' | 'VETDROS';
   productName: 'DROS' | 'VETDROS';
@@ -12,6 +13,18 @@ const initialState: ProductState = {
   source: 'Browser',
   source: 'Browser',
 };
 };
 
 
+export const initializeProductState = createAsyncThunk(
+  'product/initializeProductState',
+  async () => {
+    const softwareInfo = await fetchSoftwareInfo();
+    return {
+      productName: softwareInfo.product as 'DROS' | 'VETDROS',
+      language: softwareInfo.language[0],
+      source: 'Browser' as const,
+    };
+  }
+);
+
 const productSlice = createSlice({
 const productSlice = createSlice({
   name: 'product',
   name: 'product',
   initialState,
   initialState,
@@ -22,6 +35,21 @@ const productSlice = createSlice({
       state.source = action.payload.source;
       state.source = action.payload.source;
     },
     },
   },
   },
+  extraReducers: (builder) => {
+    builder
+      .addCase(initializeProductState.fulfilled, (state, action) => {
+        state.productName = action.payload.productName;
+        state.language = action.payload.language;
+        state.source = action.payload.source;
+      })
+      .addCase(initializeProductState.rejected, (state, action) => {
+        console.error(
+          'Failed to initialize product state:',
+          action.error,
+          state
+        );
+      });
+  },
 });
 });
 
 
 export const { setProduct } = productSlice.actions;
 export const { setProduct } = productSlice.actions;