Let's use AI to Earn!
TypeScript auto-publishdouyindouyin-apielectron-appelectron-reactkuaishoukwaipublishedshipinhaotoolxiaohongshu
feat: 添加代理和存储方式改为【localStorage】
gaoSir5885c9ccc7cc
diff --git a/.env b/.envnew file mode 100644index 00000000..dd8c5281--- /dev/null+++ b/.env@@ -0,0 +1,2 @@+# baseURL 代理地址+NEXT_PUBLIC_API_URL_PROXY="/baseUrlProxy/"\ No newline at end of filediff --git a/.env.development b/.env.developmentnew file mode 100644index 00000000..3fbbf513--- /dev/null+++ b/.env.development@@ -0,0 +1,3 @@+NEXT_PUBLIC_EVN=dev++NEXT_PUBLIC_API_URL=http://localhost:3000/api/\ No newline at end of filediff --git a/.env.production b/.env.productionnew file mode 100644index 00000000..7cb88fb8--- /dev/null+++ b/.env.production@@ -0,0 +1,4 @@+NEXT_PUBLIC_EVN=prod++NEXT_PUBLIC_API_URL=http://localhost:3000/api/+diff --git a/next.config.mjs b/next.config.mjsindex 076d6ea9..e0a1fa57 100644--- a/next.config.mjs+++ b/next.config.mjs@@ -6,4 +6,43 @@ const nextConfig = {}};+const CorsHeaders = [+ { key: "Access-Control-Allow-Credentials", value: "true" },+ { key: "Access-Control-Allow-Origin", value: "*" },+ {+ key: "Access-Control-Allow-Methods",+ value: "*",+ },+ {+ key: "Access-Control-Allow-Headers",+ value: "*",+ },+ {+ key: "Access-Control-Max-Age",+ value: "86400",+ },+];++nextConfig.headers = async () => {+ return [+ {+ source: "/api/:path*",+ headers: CorsHeaders,+ },+ ];+};++nextConfig.rewrites = async () => {+ const ret = [+ {+ source: `${process.env.NEXT_PUBLIC_API_URL_PROXY}:path*`,+ destination: `${process.env.NEXT_PUBLIC_API_URL}:path*`,+ },+ ];++ return {+ beforeFiles: ret,+ };+};+export default nextConfig;diff --git a/src/app/globals.css b/src/app/globals.cssindex 99508bf5..1f446792 100644--- a/src/app/globals.css+++ b/src/app/globals.css@@ -22,6 +22,7 @@ body {max-width: 100vw;overflow-x: hidden;font-size: var(--fs-sm);+ min-height: 50vh;}body {diff --git a/src/constant.ts b/src/constant.tsindex 2daf028b..e69de29b 100644--- a/src/constant.ts+++ b/src/constant.ts@@ -1,3 +0,0 @@-// api url-// export const API_BASE_URL = "https://api.yikart.cn/api";-export const API_BASE_URL = "http://localhost:3000/api";diff --git a/src/middleware.ts b/src/middleware.tsindex 47b2a661..6ebc8a07 100644--- a/src/middleware.ts+++ b/src/middleware.ts@@ -12,6 +12,10 @@ export const config = {};export function middleware(req: NextRequest) {+ if (req.nextUrl.pathname.includes(process.env.NEXT_PUBLIC_API_URL_PROXY!)) {+ return NextResponse.next();+ }+if (req.nextUrl.pathname.indexOf("icon") > -1 ||req.nextUrl.pathname.indexOf("chrome") > -1diff --git a/src/utils/request.ts b/src/utils/request.tsindex 65532a35..0d6c878c 100644--- a/src/utils/request.ts+++ b/src/utils/request.ts@@ -1,6 +1,5 @@import FetchService from "@/utils/FetchService/FetchService";import { RequestParams } from "@/utils/FetchService/types";-import { API_BASE_URL } from "@/constant";import { useUserStore } from "@/store/user";import { message } from "antd";@@ -12,7 +11,7 @@ type ResponseType<T> = {};export const fetchService = new FetchService({- baseURL: `${API_BASE_URL}/`,+ baseURL: `${process.env.NEXT_PUBLIC_API_URL_PROXY}`,requestInterceptor(requestParams) {const token = useUserStore.getState().token;diff --git a/src/utils/storage.ts b/src/utils/storage.tsindex 45f7c413..49253f99 100644--- a/src/utils/storage.ts+++ b/src/utils/storage.ts@@ -1,45 +1,24 @@import { StateStorage } from "zustand/middleware";-import { get, set, del, clear } from "idb-keyval";class IndexedDBStorage implements StateStorage {public async getItem(name: string): Promise<string | null> {- try {- const value = (await get(name)) || localStorage.getItem(name);- return value;- } catch (error) {- return localStorage.getItem(name);- }+ return localStorage.getItem(name);}public async setItem(name: string, value: string): Promise<void> {- try {- const _value = JSON.parse(value);- if (!_value?.state?._hasHydrated) {- console.warn("skip setItem", name);- return;- }- await set(name, value);- } catch (error) {- if (typeof window !== "undefined") {- localStorage.setItem(name, value);- }- }+ localStorage.setItem(name, value);}public async removeItem(name: string): Promise<void> {- try {- await del(name);- } catch (error) {- localStorage.removeItem(name);- }+ localStorage.removeItem(name);}public async clear(): Promise<void> {- try {- await clear();- } catch (error) {- localStorage.clear();- }+ // try {+ // await clear();+ // } catch (error) {+ // localStorage.clear();+ // }}}