From bf9bfa426c76d8e610ad69bc3b15efdf1229164a Mon Sep 17 00:00:00 2001 From: kevin <1026838160@qq.com> Date: Tue, 25 Jun 2024 23:55:41 +0800 Subject: [PATCH] feat: common --- package.json | 4 +- pnpm-lock.yaml | 64 ++++++++++++++++++++++++++--- src/layout/ContentLayout/index.tsx | 7 ++++ src/layout/MainLayout.tsx/index.tsx | 7 ++++ src/main.tsx | 2 +- src/pages/login/index.tsx | 7 ++++ src/pages/third/login/index.tsx | 5 +++ src/routes/index.tsx | 32 +++++++++++++++ src/routes/thirdRoutes.ts | 11 +++++ src/{ => styles}/main.css | 0 src/utils/request.ts | 37 +++++++++++++++++ src/utils/storage.ts | 13 ++++++ tsconfig.json | 30 ++++++++++---- tsconfig.node.json | 6 ++- vite.config.ts | 8 +++- 15 files changed, 213 insertions(+), 20 deletions(-) create mode 100644 src/layout/ContentLayout/index.tsx create mode 100644 src/layout/MainLayout.tsx/index.tsx create mode 100644 src/pages/login/index.tsx create mode 100644 src/pages/third/login/index.tsx create mode 100644 src/routes/index.tsx create mode 100644 src/routes/thirdRoutes.ts rename src/{ => styles}/main.css (100%) create mode 100644 src/utils/request.ts create mode 100644 src/utils/storage.ts diff --git a/package.json b/package.json index 0d52946..c808a06 100644 --- a/package.json +++ b/package.json @@ -37,10 +37,12 @@ } }, "dependencies": { + "axios": "^1.7.2", "i18next": "^23.11.5", "pretty-bytes": "^6.1.1", "react": "^18.2.0", - "react-dom": "^18.2.0" + "react-dom": "^18.2.0", + "react-router-dom": "^6.23.1" }, "devDependencies": { "@commitlint/cli": "^18.4.4", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 347ed2d..61b6129 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -5,6 +5,9 @@ settings: excludeLinksFromLockfile: false dependencies: + axios: + specifier: ^1.7.2 + version: 1.7.2 i18next: specifier: ^23.11.5 version: 23.11.5 @@ -17,6 +20,9 @@ dependencies: react-dom: specifier: ^18.2.0 version: 18.2.0(react@18.2.0) + react-router-dom: + specifier: ^6.23.1 + version: 6.23.1(react-dom@18.2.0)(react@18.2.0) devDependencies: '@commitlint/cli': @@ -1153,6 +1159,11 @@ packages: engines: {node: ^12.20.0 || ^14.18.0 || >=16.0.0} dev: true + /@remix-run/router@1.16.1: + resolution: {integrity: sha512-es2g3dq6Nb07iFxGk5GuHN20RwBZOsuDQN7izWIisUcv9r+d2C5jQxqmgkdebXgReWfiyUabcki6Fg77mSNrig==} + engines: {node: '>=14.0.0'} + dev: false + /@rollup/rollup-android-arm-eabi@4.9.5: resolution: {integrity: sha512-idWaG8xeSRCfRq9KpRysDHJ/rEHBEXcHuJ82XY0yYFIWnLMjZv9vF/7DOq8djQ2n3Lk6+3qfSH8AqlmHlmi1MA==} cpu: [arm] @@ -1818,7 +1829,6 @@ packages: /asynckit@0.4.0: resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==} - dev: true /at-least-node@1.0.0: resolution: {integrity: sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg==} @@ -1848,6 +1858,16 @@ packages: postcss-value-parser: 4.2.0 dev: true + /axios@1.7.2: + resolution: {integrity: sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==} + dependencies: + follow-redirects: 1.15.6 + form-data: 4.0.0 + proxy-from-env: 1.1.0 + transitivePeerDependencies: + - debug + dev: false + /balanced-match@1.0.2: resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==} dev: true @@ -2128,7 +2148,6 @@ packages: engines: {node: '>= 0.8'} dependencies: delayed-stream: 1.0.0 - dev: true /commander@4.1.1: resolution: {integrity: sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==} @@ -2376,7 +2395,6 @@ packages: /delayed-stream@1.0.0: resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==} engines: {node: '>=0.4.0'} - dev: true /detect-node@2.1.0: resolution: {integrity: sha512-T0NIuQpnTvFDATNuHN5roPwSBG83rFsuO+MXXH9/3N1eFbn4wcPjttvjMLEPWJ0RGUYgQE7cGgS3tNxbqCGM7g==} @@ -2968,6 +2986,16 @@ packages: resolution: {integrity: sha512-36yxDn5H7OFZQla0/jFJmbIKTdZAQHngCedGxiMmpNfEZM0sdEeT+WczLQrjK6D7o2aiyLYDnkw0R3JK0Qv1RQ==} dev: true + /follow-redirects@1.15.6: + resolution: {integrity: sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==} + engines: {node: '>=4.0'} + peerDependencies: + debug: '*' + peerDependenciesMeta: + debug: + optional: true + dev: false + /foreground-child@3.1.1: resolution: {integrity: sha512-TMKDUnIte6bfb5nWv7V/caI169OHgvwjb7V4WkeUvbQQdjr5rWKqHFiKWb/fcOwB+CzBT+qbWjvj+DVwRskpIg==} engines: {node: '>=14'} @@ -2983,7 +3011,6 @@ packages: asynckit: 0.4.0 combined-stream: 1.0.8 mime-types: 2.1.35 - dev: true /fraction.js@4.3.7: resolution: {integrity: sha512-ZsDfxO51wGAXREY55a7la9LScWpwv9RxIrYABrlvOFBlH/ShPnrtsXeuUIfXKKOVicNxQ+o8JTbJvjS4M89yew==} @@ -3784,14 +3811,12 @@ packages: /mime-db@1.52.0: resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==} engines: {node: '>= 0.6'} - dev: true /mime-types@2.1.35: resolution: {integrity: sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==} engines: {node: '>= 0.6'} dependencies: mime-db: 1.52.0 - dev: true /mime@2.6.0: resolution: {integrity: sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==} @@ -4310,6 +4335,10 @@ packages: retry: 0.12.0 dev: true + /proxy-from-env@1.1.0: + resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==} + dev: false + /pump@3.0.0: resolution: {integrity: sha512-LwZy+p3SFs1Pytd/jYct4wpv49HiYCqd9Rlc5ZVdk0V+8Yzv6jR5Blk3TRmPL1ft69TxP0IMZGJ+WPFU2BFhww==} dependencies: @@ -4351,6 +4380,29 @@ packages: engines: {node: '>=0.10.0'} dev: true + /react-router-dom@6.23.1(react-dom@18.2.0)(react@18.2.0): + resolution: {integrity: sha512-utP+K+aSTtEdbWpC+4gxhdlPFwuEfDKq8ZrPFU65bbRJY+l706qjR7yaidBpo3MSeA/fzwbXWbKBI6ftOnP3OQ==} + engines: {node: '>=14.0.0'} + peerDependencies: + react: '>=16.8' + react-dom: '>=16.8' + dependencies: + '@remix-run/router': 1.16.1 + react: 18.2.0 + react-dom: 18.2.0(react@18.2.0) + react-router: 6.23.1(react@18.2.0) + dev: false + + /react-router@6.23.1(react@18.2.0): + resolution: {integrity: sha512-fzcOaRF69uvqbbM7OhvQyBTFDVrrGlsFdS3AL+1KfIBtGETibHzi3FkoTRyiDJnWNc2VxrfvR+657ROHjaNjqQ==} + engines: {node: '>=14.0.0'} + peerDependencies: + react: '>=16.8' + dependencies: + '@remix-run/router': 1.16.1 + react: 18.2.0 + dev: false + /react@18.2.0: resolution: {integrity: sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==} engines: {node: '>=0.10.0'} diff --git a/src/layout/ContentLayout/index.tsx b/src/layout/ContentLayout/index.tsx new file mode 100644 index 0000000..328cb15 --- /dev/null +++ b/src/layout/ContentLayout/index.tsx @@ -0,0 +1,7 @@ +import { Outlet } from "react-router-dom"; + +const ContentLayout = () => { + return ; +}; + +export default ContentLayout; diff --git a/src/layout/MainLayout.tsx/index.tsx b/src/layout/MainLayout.tsx/index.tsx new file mode 100644 index 0000000..287823e --- /dev/null +++ b/src/layout/MainLayout.tsx/index.tsx @@ -0,0 +1,7 @@ +import { Outlet } from "react-router-dom"; + +const MainLayout = () => { + return ; +}; + +export default MainLayout; diff --git a/src/main.tsx b/src/main.tsx index 532fe6d..0e7dee7 100644 --- a/src/main.tsx +++ b/src/main.tsx @@ -1,4 +1,4 @@ -import "./main.css"; +import "./styles/main.css"; import React from "react"; import ReactDOM from "react-dom/client"; diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx new file mode 100644 index 0000000..d5e656b --- /dev/null +++ b/src/pages/login/index.tsx @@ -0,0 +1,7 @@ +export const Login = () => { + return ; +}; + +export const LoginContent = () => { + return
LoginContent
; +}; diff --git a/src/pages/third/login/index.tsx b/src/pages/third/login/index.tsx new file mode 100644 index 0000000..4a14b51 --- /dev/null +++ b/src/pages/third/login/index.tsx @@ -0,0 +1,5 @@ +import { LoginContent } from "@/pages/login"; + +export const Login = () => { + return ; +}; diff --git a/src/routes/index.tsx b/src/routes/index.tsx new file mode 100644 index 0000000..e87bb2f --- /dev/null +++ b/src/routes/index.tsx @@ -0,0 +1,32 @@ +import { createHashRouter } from "react-router-dom"; + +import ContentLayout from "../layout/ContentLayout/index.tsx"; +import MainLayout from "../layout/MainLayout.tsx"; +import thirdRoutes from "./thirdRoutes.ts"; + +const router = createHashRouter([ + { + path: "/", + element: , + children: [ + { + path: "/", + element: , + children: [], + }, + { + path: "login", + async lazy() { + const { Login } = await import("@/pages/login"); + return { Component: Login }; + }, + }, + ], + }, + { + path: "third", + children: thirdRoutes, + }, +]); + +export default router; diff --git a/src/routes/thirdRoutes.ts b/src/routes/thirdRoutes.ts new file mode 100644 index 0000000..48cca28 --- /dev/null +++ b/src/routes/thirdRoutes.ts @@ -0,0 +1,11 @@ +const thirdRoutes = [ + { + path: "login", + async lazy() { + const { Login } = await import("@/pages/third/login"); + return { Component: Login }; + }, + }, +]; + +export default thirdRoutes; diff --git a/src/main.css b/src/styles/main.css similarity index 100% rename from src/main.css rename to src/styles/main.css diff --git a/src/utils/request.ts b/src/utils/request.ts new file mode 100644 index 0000000..bb5f26e --- /dev/null +++ b/src/utils/request.ts @@ -0,0 +1,37 @@ +import axios from "axios"; + +const createAxiosInstance = (baseURL: string) => { + const serves = axios.create({ + baseURL, + timeout: 3000, + }); + + serves.interceptors.request.use( + (config) => { + return config; + }, + (err) => Promise.reject(err), + ); + + serves.interceptors.response.use( + (res) => { + if (res.data.errCode !== 0) { + return Promise.reject(res.data); + } + return res.data; + }, + (err) => { + if (err.message.includes("timeout")) { + console.error("error", err); + } + if (err.message.includes("Network Error")) { + console.error("error", err); + } + return Promise.reject(err); + }, + ); + + return serves; +}; + +export default createAxiosInstance; diff --git a/src/utils/storage.ts b/src/utils/storage.ts new file mode 100644 index 0000000..83204cb --- /dev/null +++ b/src/utils/storage.ts @@ -0,0 +1,13 @@ +const TOKEN = "USER_TOKEN"; +const LOCALE = "USER_LOCALE"; + +export type LocaleString = "zh-CN" | "en-US"; + +export const getIMToken = () => localStorage.getItem(TOKEN); +export const getLocale = (): LocaleString => + (localStorage.getItem(LOCALE) as LocaleString) || + window.navigator.language || + "en-US"; + +export const setTMToken = (token: string) => localStorage.setItem(TOKEN, token); +export const setLocale = (locale: string) => localStorage.setItem(LOCALE, locale); diff --git a/tsconfig.json b/tsconfig.json index a7fc6fb..0440b15 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,24 +2,36 @@ "compilerOptions": { "target": "ES2020", "useDefineForClassFields": true, - "lib": ["ES2020", "DOM", "DOM.Iterable"], + "lib": [ + "ES2020", + "DOM", + "DOM.Iterable" + ], "module": "ESNext", "skipLibCheck": true, - - /* Bundler mode */ "moduleResolution": "bundler", "allowImportingTsExtensions": true, "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "react-jsx", - - /* Linting */ "strict": true, "noUnusedLocals": true, "noUnusedParameters": true, - "noFallthroughCasesInSwitch": true + "noFallthroughCasesInSwitch": true, + "baseUrl": "./", + "paths": { + "@/*": [ + "src/*" + ] + }, }, - "include": ["src"], - "references": [{ "path": "./tsconfig.node.json" }] -} + "include": [ + "src" + ], + "references": [ + { + "path": "./tsconfig.node.json" + } + ] +} \ No newline at end of file diff --git a/tsconfig.node.json b/tsconfig.node.json index 42872c5..73dbb0b 100644 --- a/tsconfig.node.json +++ b/tsconfig.node.json @@ -6,5 +6,7 @@ "moduleResolution": "bundler", "allowSyntheticDefaultImports": true }, - "include": ["vite.config.ts"] -} + "include": [ + "vite.config.ts" + ] +} \ No newline at end of file diff --git a/vite.config.ts b/vite.config.ts index cc4ddbf..6926c92 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,13 +1,19 @@ import { rmSync } from "node:fs"; import path from "node:path"; -import { defineConfig } from "vite"; + import react from "@vitejs/plugin-react"; +import { defineConfig } from "vite"; import electron from "vite-electron-plugin"; import { loadViteEnv } from "vite-electron-plugin/plugin"; rmSync(path.join(__dirname, "dist-electron"), { recursive: true, force: true }); export default defineConfig({ + resolve: { + alias: { + "@": path.join(__dirname, "src"), + }, + }, plugins: [ react(), electron({