From 0d804354610af2b6beb0631459671a150632db02 Mon Sep 17 00:00:00 2001 From: callmeyan Date: Wed, 23 Aug 2023 13:59:16 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BC=96=E8=AF=91=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 34 ++++++++++++++++++ front/App.tsx | 1 + front/components/Result/index.tsx | 2 +- front/components/icons/NotFound.tsx | 54 ++++++++++++++--------------- front/pages/Router.tsx | 47 +++++++++++++++++-------- front/pages/dashboard/index.tsx | 7 ++-- front/pages/index/index.tsx | 4 ++- front/service/api.app.ts | 2 +- front/service/request.ts | 11 +++--- front/service/user.ts | 2 +- front/store/userinfoStore.ts | 25 +++++++++++-- package.json | 4 +-- service.tsconfig.json | 19 ++++++++++ service/core/server.ts | 6 ++-- service/routes/index.ts | 3 +- service/routes/user.ts | 6 ++-- tsconfig.json | 2 +- vite.config.ts | 4 +-- 18 files changed, 166 insertions(+), 67 deletions(-) create mode 100644 README.md create mode 100644 service.tsconfig.json diff --git a/README.md b/README.md new file mode 100644 index 0000000..6a12749 --- /dev/null +++ b/README.md @@ -0,0 +1,34 @@ +## 注意事项: + +项目编译时需要修改tsconfig.json中的如下配置 +```json +{ + "compilerOptions": { + // 将如下commonjs改为esnext + // "module": "commonjs", + "module": "esnext" + // 省略其他配置... + }, +} + + +``` + +## 相关命令 +1. 请使用 `npm install` 安装依赖 +2. 请使用 `npm run build` 打包 +3. 请使用 `npm run dev` 启动 +4. 请使用 `npm run test` 测试 +5. 请使用 `npm run lint` 检查代码 +6. 请使用 `npm run doc` 生成文档 +7. 请使用 `npm run deploy` 部署 +8. 请使用 `npm run deploy:dev` 部署开发环境 +9. 请使用 `npm run deploy:test` 部署测试环境 +10. 请使用 `npm run deploy:prod` 部署生产环境 +11. 请使用 `npm run deploy:pre` 部署预发布环境 +12. 请使用 `npm run deploy:sit` 部署体验环境 +13. 请使用 `npm run deploy:uat` 部署UAT环境 +14. 请使用 `npm run deploy:pro` 部署生产环境 +15. 请使用 `npm run deploy:pre` 部署预发布环境 +16. 请使用 `npm run deploy:sit` 部署体验环境 +17. 请使用 `npm run deploy:uat` 部署UAT环境 diff --git a/front/App.tsx b/front/App.tsx index 5920d3b..187e098 100644 --- a/front/App.tsx +++ b/front/App.tsx @@ -2,6 +2,7 @@ import React, {useEffect} from 'react' import './assets/global.scss' import {AppRouter} from "./pages/Router.tsx"; import {useUserinfoStore} from "./store/userinfoStore.ts"; +import {useLocation, useNavigation} from "react-router-dom"; export function App() { diff --git a/front/components/Result/index.tsx b/front/components/Result/index.tsx index f7e2aa0..589b8d9 100644 --- a/front/components/Result/index.tsx +++ b/front/components/Result/index.tsx @@ -27,7 +27,7 @@ export const IconMap: { // '500': serverError, // '403': unauthorized, }; -export const Index: React.FC = (props) => { +export const Result: React.FC = (props) => { const icon = props.icon || (props.status ? IconMap[props.status] : <>) return
{icon}
diff --git a/front/components/icons/NotFound.tsx b/front/components/icons/NotFound.tsx index ac36d7a..5188d19 100644 --- a/front/components/icons/NotFound.tsx +++ b/front/components/icons/NotFound.tsx @@ -5,7 +5,7 @@ export const NotFound: React.FC = () => { - + { fill="#E4EBF7" mask="url(#b)"> - + - + - + { d="M99.66 278.514l13.378.092s1.298-54.52 1.853-64.403c.554-9.882 3.776-43.364 1.002-63.128l-12.547-.644-22.849.78s-.434 3.966-1.195 9.976c-.063.496-.682.843-.749 1.365-.075.585.423 1.354.32 1.966-2.364 14.08-6.377 33.104-8.744 46.677-.116.666-1.234 1.009-1.458 2.691-.04.302.211 1.525.112 1.795-6.873 18.744-10.949 47.842-14.277 61.885l14.607-.014s2.197-8.57 4.03-16.97c2.811-12.886 23.111-85.01 23.111-85.01l3.016-.521 1.043 46.35s-.224 1.234.337 2.02c.56.785-.56 1.123-.392 2.244l.392 1.794s-.449 7.178-.898 11.89c-.448 4.71-.092 39.165-.092 39.165" fill="#7BB2F9"> - + stroke="#648BD8" strokeWidth="1.051" strokeLinecap="round" strokeLinejoin="round"> + + stroke="#648BD8" strokeWidth="1.051" strokeLinecap="round" strokeLinejoin="round"> @@ -85,7 +85,7 @@ export const NotFound: React.FC = () => { fill="#192064"> + stroke="#648BD8" strokeWidth="1.051" strokeLinecap="round" strokeLinejoin="round"> @@ -98,23 +98,23 @@ export const NotFound: React.FC = () => { - - + - + stroke="#DB836E" strokeWidth="1.118" strokeLinecap="round" strokeLinejoin="round"> + + stroke="#DB836E" strokeWidth="1.118" strokeLinecap="round" strokeLinejoin="round"> - - + strokeWidth="1.101" strokeLinecap="round" strokeLinejoin="round"> + + @@ -135,12 +135,12 @@ export const NotFound: React.FC = () => { fill="#FFC6A0"> + stroke="#DB836E" strokeWidth="1.051" strokeLinecap="round" strokeLinejoin="round"> + strokeWidth="1.051" strokeLinecap="round" strokeLinejoin="round"> + stroke="#DB836E" strokeWidth="1.051" strokeLinecap="round" strokeLinejoin="round"> @@ -150,8 +150,8 @@ export const NotFound: React.FC = () => { - + ) } diff --git a/front/pages/Router.tsx b/front/pages/Router.tsx index cc8f103..48cf6e0 100644 --- a/front/pages/Router.tsx +++ b/front/pages/Router.tsx @@ -1,10 +1,11 @@ -import React from "react"; -import {BrowserRouter, HashRouter, Navigate, Route, Routes, useNavigate} from "react-router-dom"; -import {APP_CONFIG} from "../config.ts"; +import React, {useEffect} from "react"; +import {BrowserRouter, HashRouter, Route, Routes, useLocation, useNavigate} from "react-router-dom"; +import {APP_CONFIG} from "../config"; import {Button} from "@douyinfe/semi-ui"; import DefaultPage from "./index"; -import {Index} from "../components/Result"; +import {Result} from "../components/Result"; import {DashboardIndex} from "./dashboard"; +import {useUserinfoStore} from "../store/userinfoStore.ts"; const routerMode: 'browser' | 'hash' | string = APP_CONFIG.ROUTER_MODE; @@ -16,18 +17,36 @@ const WebRouter: React.FC<{ const NotFound: React.FC = () => { const navigate = useNavigate(); - return navigate('/')} type="primary">返回首页}/> } -export const AppRouter = () => ( -
- - }/> - }/> - }/> - -
-
) +const AppRoutes = () => ( + }/> + }/> + }/> +) +const RouteView = () => { + const navigate = useNavigate(); + const loc = useLocation(); + const {userinfo, setUid} = useUserinfoStore(); + + useEffect(() => { + if (userinfo.id == -1 && loc.pathname !== '/') { + setUid(0) + navigate('/') + } + }, [userinfo]) + return ( +
+

{loc.pathname}

+ +
) +} +export const AppRouter = () => { + return ( + + ) +} diff --git a/front/pages/dashboard/index.tsx b/front/pages/dashboard/index.tsx index cf7c617..1880e50 100644 --- a/front/pages/dashboard/index.tsx +++ b/front/pages/dashboard/index.tsx @@ -2,10 +2,10 @@ import React from "react"; import {Panel} from "../../components/panel"; import {AppList} from "../../components/AppList"; import {AppModel, EventDataModel, EventModel} from "../../../model"; -import {appList, appEventList, appEventDataList} from "../../service/api.app.ts"; +import {appList, appEventList, appEventDataList} from "../../service/api.app"; import './style.scss' import {Space, Table} from "@douyinfe/semi-ui"; -import {formatDate} from "../../../utils/date.ts"; +import {formatDate} from "../../../utils/date"; import {useInterval} from "ahooks"; export const DashboardIndex: React.FC = () => { @@ -62,6 +62,7 @@ export const DashboardIndex: React.FC = () => { // 加载引用所有事件上报的数据 const loadAppEventsAndDataList = (page = 1) => { + if (currentAppID < 1) return; appEventDataList({ appId: currentAppID, pageSize: 10, @@ -72,7 +73,7 @@ export const DashboardIndex: React.FC = () => { }) } - const getAppEventAndData = ()=>{ + const getAppEventAndData = () => { if (currentAppID > 0) { getAppEvents() loadAppEventsAndDataList() diff --git a/front/pages/index/index.tsx b/front/pages/index/index.tsx index 496716c..92bc95e 100644 --- a/front/pages/index/index.tsx +++ b/front/pages/index/index.tsx @@ -23,7 +23,9 @@ const DefaultPage: React.FC = () => { console.log(err) // 登录失败 Notification.error({ - content: '登录失败,请检查用户名和密码' + content: '登录失败,请检查用户名和密码', + position: 'top', + // duration: 2, }) } setLoading(false); diff --git a/front/service/api.app.ts b/front/service/api.app.ts index d506048..69fa951 100644 --- a/front/service/api.app.ts +++ b/front/service/api.app.ts @@ -1,4 +1,4 @@ -import {get, post} from "./request.ts"; +import {get, post} from "./request"; import {AppModel, EventDataModel, EventModel, UserModel} from "../../model"; // 获取用户归属应用列表 diff --git a/front/service/request.ts b/front/service/request.ts index d5a52df..89272ba 100644 --- a/front/service/request.ts +++ b/front/service/request.ts @@ -1,8 +1,9 @@ import axios from 'axios'; -import {BizError} from "./types.ts"; -import Storage from "./storage.ts"; -import {useUserinfoStore} from "../store/userinfoStore.ts"; -import {APP_CONFIG} from "../config.ts"; +import {BizError} from "./types"; +import Storage from "./storage"; +import {useUserinfoStore} from "../store/userinfoStore"; +import {APP_CONFIG} from "../config"; +import {stat} from "fs"; export interface APIResponse { /** @@ -95,7 +96,7 @@ export function request(url: string, method: RequestMethod, data: any = null, if (code == 403) { const state = useUserinfoStore.getState() // 未登录 显示登录modal - // state.showLogin(true); + state.showLogin(); } reject(new BizError(msg, code)) } diff --git a/front/service/user.ts b/front/service/user.ts index 6ca1d61..174fa90 100644 --- a/front/service/user.ts +++ b/front/service/user.ts @@ -1,4 +1,4 @@ -import {get, post} from "./request.ts"; +import {get, post} from "./request"; import {UserModel} from "../../model"; diff --git a/front/store/userinfoStore.ts b/front/store/userinfoStore.ts index 54a85e1..dc84f17 100644 --- a/front/store/userinfoStore.ts +++ b/front/store/userinfoStore.ts @@ -1,7 +1,7 @@ import {create} from "zustand"; -import Storage from "../service/storage.ts"; -import {getInfo, login} from './../service/user.ts' -import {APP_CONFIG} from "../config.ts"; +import Storage from "../service/storage"; +import {getInfo, login} from './../service/user' +import {APP_CONFIG} from "../config"; import {UserModel} from "../../model"; type LoginDataType = { account: string; code?: string; password: string } @@ -34,8 +34,27 @@ export const useUserinfoStore = create<{ * 初始化用户数据 */ init: () => void; + showLogin: () => void; + setUid: (id: number) => void; }>((set, _, _state) => { return { + showLogin() { + // const navigate = useNavigate() + // navigate('/') + + set({ + userinfo: { + id: -1 + }, + token: undefined + }) + Storage.remove(LOGIN_TOKEN_KEY) + }, + setUid(id) { + set({ + userinfo: {id} + }) + }, userinfo: { id: 0 }, diff --git a/package.json b/package.json index 5d30b1f..0fe52b6 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { "name": "app-report-all", "version": "1.0.0", - "main": "index.js", + "main": "service/index.ts", "license": "MIT", "scripts": { "dev": "nodemon service/index.ts", "start": "ts-node service/index.ts", + "build": "tsc -p service.tsconfig.json", "preview": "vite preview", "dev-front": "vite --mode development --host 0.0.0.0", "build-front": "tsc && vite build --mode production", - "build": "tsc", "test": "mocha -r ts-node/register test/**/**.test.ts", "test-db": "mocha -r ts-node/register test/db.test.ts" }, diff --git a/service.tsconfig.json b/service.tsconfig.json new file mode 100644 index 0000000..bdc50eb --- /dev/null +++ b/service.tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "target": "ES2020", + "lib": ["DOM", "DOM.Iterable", "ES2020"], + "module": "commonjs", + "skipLibCheck": true, + "moduleResolution": "node", + "outDir": "dist" + }, + "include": [ + "service/**/*", + "utils/**/*", + "model/**/*" + ], + "exclude": [ + "node_modules" + ] +} + diff --git a/service/core/server.ts b/service/core/server.ts index 4ac7b7c..a7a32e2 100644 --- a/service/core/server.ts +++ b/service/core/server.ts @@ -1,14 +1,16 @@ import {InitServerOption} from "./types"; -// import { createServer as createServerOrigin } from "http"; -// import express = require("express"); import * as express from "express"; +import {dirname,resolve} from "path"; +const WEB_ROOT = dirname(dirname(__dirname)) export function createServer(options: Partial, callback?: () => void) { // 创建express服务器 const app = express(); // 将请求体转换为JSON格式 app.use(express.json()) app.use(express.urlencoded({extended: true})) + // 指定静态资源目录 + app.use('/',express.static(resolve(WEB_ROOT, "static"))); // 监听端口 app.listen(options.port, callback); // 添加允许跨域中间件 diff --git a/service/routes/index.ts b/service/routes/index.ts index 3bb2a0c..f8b892b 100644 --- a/service/routes/index.ts +++ b/service/routes/index.ts @@ -2,11 +2,12 @@ import {Application, Request, Response} from "express"; import {RouteHandleFunction, RouteHandleFunctionParam} from "../core/types"; import {home} from "./home"; import {appList, reportToServer, appEvent, eventData} from "./reportor"; -import {decodeUserToken, getUserInfo, loginHandler} from "./user.ts"; +import {decodeUserToken, getUserInfo, loginHandler} from "./user"; const excludes = [ '/home', '/api/report', + '/api/user/login', ] // diff --git a/service/routes/user.ts b/service/routes/user.ts index 240fdc6..bedf832 100644 --- a/service/routes/user.ts +++ b/service/routes/user.ts @@ -1,7 +1,7 @@ -import {RouteHandleFunction} from "../core/types.ts"; -import {login} from "../service/app.ts"; +import {RouteHandleFunction} from "../core/types"; +import {login} from "../service/app"; import {UserModel} from "../../model"; -import {getFromRedis, setToRedis} from "../service/redis.ts"; +import {getFromRedis, setToRedis} from "../service/redis"; export function encodeUserToken(user: UserModel) { if (user == null) throw new Error('user is null') diff --git a/tsconfig.json b/tsconfig.json index b636812..8556718 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,7 +3,7 @@ /* ESNext */ "target": "ESNext", "lib": ["DOM", "DOM.Iterable", "ESNext"], - "module": "commonjs", + "module": "ESNext", "skipLibCheck": true, /* Bundler mode */ diff --git a/vite.config.ts b/vite.config.ts index 1d5acce..7ae97e1 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -17,7 +17,7 @@ export default defineConfig(({mode}) => { // mode:JSON.stringify(mode), // }, build: { - outDir:'static', + outDir:'./dist/static', // 小于10kb直接base64 assetsInlineLimit: 10240, rollupOptions: { @@ -33,7 +33,7 @@ export default defineConfig(({mode}) => { } } }, - base: "/", + base: "./", server: { proxy: { '/api': {