From e6b99141f3998e02e2ca2ade657b44b6c19c4f29 Mon Sep 17 00:00:00 2001 From: master1lan <278457198@qq.com> Date: Thu, 19 Jan 2023 17:13:20 +0800 Subject: [PATCH] fix:cors --- src/main.tsx | 2 +- src/utils/fetch/index.ts | 11 ++-- src/utils/fetch/tool.ts | 1 + src/vite-env.d.ts | 1 + vite.config.ts | 126 ++++++++++++++++++++------------------- 5 files changed, 73 insertions(+), 68 deletions(-) create mode 100644 src/utils/fetch/tool.ts diff --git a/src/main.tsx b/src/main.tsx index 4f2681f..58e1f6d 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -8,6 +8,7 @@ import ErrorPage from "./routers/error"; //preview import ScreenProview from "@components/proview/screenSize"; import MUIThemePreview from "@components/proview/themePreview"; +import SearchFocuspreview from "@components/proview/searchFocus"; import { Provider } from "react-redux"; import store from "@store/index"; //ployfill @@ -16,7 +17,6 @@ import "./normalize.css"; import "loading-attribute-polyfill"; import "whatwg-fetch"; import "./index.less"; -import SearchFocuspreview from "@components/proview/searchFocus"; const router = createBrowserRouter([ { diff --git a/src/utils/fetch/index.ts b/src/utils/fetch/index.ts index 2949e74..0353ca8 100644 --- a/src/utils/fetch/index.ts +++ b/src/utils/fetch/index.ts @@ -2,6 +2,7 @@ * 类型文件导入 */ import { IFetchVideoParams, RFetchVideoRes } from "./fetchtype"; +import { Host_Url } from "./tool"; /** * video视频数据获取接口 @@ -9,20 +10,18 @@ import { IFetchVideoParams, RFetchVideoRes } from "./fetchtype"; export function fetchVideos( params: IFetchVideoParams ): Promise { - const fetchUrl = `/v1/video-interface/advanced-search?order=${ + const fetchUrl = `${Host_Url}/v1/video-interface/advanced-search?order=${ params.order || "score" }&page=${params.page}${ params.copyright ? `©right=${params.copyright}` : "" } ${params.q ? `&q=${params.q}` : ""} - ${params.tname ? `&tname=${params.tname}` : ""}` + ${ + params.tname ? `&tname=${params.tname}` : "" + }&subscription-key=3cc4284fbb864965a7a9ad0f28af8496` .replace(/\s+/g, "") .trim(); return fetch(fetchUrl, { method: "GET", - headers: { - "ocp-Apim-Subscription-Key": "3cc4284fbb864965a7a9ad0f28af8496", - origin: "https://portal.api.eoe.best", - }, }).then((response) => response.json() as Promise); } diff --git a/src/utils/fetch/tool.ts b/src/utils/fetch/tool.ts new file mode 100644 index 0000000..1e1d51b --- /dev/null +++ b/src/utils/fetch/tool.ts @@ -0,0 +1 @@ +export const Host_Url = isdev ? "" : `https://api.eoe.best/eoefans-api`; diff --git a/src/vite-env.d.ts b/src/vite-env.d.ts index d5337f4..cd54c60 100644 --- a/src/vite-env.d.ts +++ b/src/vite-env.d.ts @@ -1,2 +1,3 @@ /// declare const __APP_VERSION__: string; +declare const isdev: string; diff --git a/vite.config.ts b/vite.config.ts index 90a263c..af952b1 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,4 +1,4 @@ -import { defineConfig } from "vite"; +import { defineConfig, loadEnv } from "vite"; import path from "path"; import react from "@vitejs/plugin-react-swc"; // 可视化打包文件 @@ -8,70 +8,74 @@ import viteCompression from "vite-plugin-compression"; // polyfill import legacy from "@vitejs/plugin-legacy"; // https://vitejs.dev/config/ -export default defineConfig({ - define: { - __APP_VERSION__: "0.1", - }, - resolve: { - alias: { - "@utils": path.resolve(__dirname, "src/utils"), - "@components": path.resolve(__dirname, "src/components"), - "@store": path.resolve(__dirname, "src/store"), - "@routers": path.resolve(__dirname, "src/routers"), - "@assets": path.resolve(__dirname, "src/assets"), + +export default defineConfig(({ mode }) => { + return { + define: { + __APP_VERSION__: 0.01, + isdev: mode === "development", }, - }, - plugins: [ - react(), - legacy({ - // 设置目标浏览器,browserslist 配置语法 - targets: [ - "defaults", - "iOS >= 9, Android >= 4.4, last 2 versions, > 0.2%, not dead", - ], - }), - { ...visualizer(), apply: "build" }, - { ...viteCompression(), apply: "build" }, - ], - build: { - // target: "es2015", - minify: "esbuild", - rollupOptions: { - output: { - manualChunks: { - react: ["react", "react-dom"], - "react-router": ["react-router-dom"], - "react-redux": ["@reduxjs/toolkit", "react-redux"], - lib: [ - "@mui/icons-material", - "@mui/lab", - "@mui/material", - "@emotion/react", - "@emotion/styled", - ], - "dnd-tool": [ - "@dnd-kit/core", - "@dnd-kit/modifiers", - "@dnd-kit/sortable", - "@dnd-kit/utilities", - ], - ployfill: [ - "whatwg-fetch", - "intersection-observer", - "react-flip-toolkit", - "react-intersection-observer", - "dayjs", - ], + resolve: { + alias: { + "@utils": path.resolve(__dirname, "src/utils"), + "@components": path.resolve(__dirname, "src/components"), + "@store": path.resolve(__dirname, "src/store"), + "@routers": path.resolve(__dirname, "src/routers"), + "@assets": path.resolve(__dirname, "src/assets"), + }, + }, + plugins: [ + react(), + legacy({ + // 设置目标浏览器,browserslist 配置语法 + targets: [ + "defaults", + "iOS >= 9, Android >= 4.4, last 2 versions, > 0.2%, not dead", + ], + }), + { ...visualizer(), apply: "build" }, + { ...viteCompression(), apply: "build" }, + ], + build: { + // target: "es2015", + minify: "esbuild", + rollupOptions: { + output: { + manualChunks: { + react: ["react", "react-dom"], + "react-router": ["react-router-dom"], + "react-redux": ["@reduxjs/toolkit", "react-redux"], + lib: [ + "@mui/icons-material", + "@mui/lab", + "@mui/material", + "@emotion/react", + "@emotion/styled", + ], + "dnd-tool": [ + "@dnd-kit/core", + "@dnd-kit/modifiers", + "@dnd-kit/sortable", + "@dnd-kit/utilities", + ], + ployfill: [ + "whatwg-fetch", + "intersection-observer", + "react-flip-toolkit", + "react-intersection-observer", + "dayjs", + ], + }, }, }, }, - }, - server: { - proxy: { - "/v1": { - target: "https://api.eoe.best/eoefans-api", - changeOrigin: true, + server: { + proxy: { + "/v1": { + target: "https://api.eoe.best/eoefans-api", + changeOrigin: true, + }, }, }, - }, + }; });