Skip to content

Commit

Permalink
refactor: 大写常量 startsWith 匹配路由
Browse files Browse the repository at this point in the history
  • Loading branch information
wanpan11 committed Jul 17, 2024
1 parent 948a392 commit f3240f5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const colorPrimary = "#4096ff";
export const defaultPageInfo = { pageNum: 1, pageSize: 10 };
export const splitFlag = "@&@";
export const COLOR_PRIMARY = "#4096ff";
export const DEFAULT_PAGE = { pageNum: 1, pageSize: 10 };
export const SPLIT_FLAG = "@&@";

export const LOCAL_TOKEN = "LOCAL_TOKEN";
export const LOCAL_USER_INFO = "LOCAL_USER_INFO";
Expand Down
16 changes: 11 additions & 5 deletions src/layout/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@ import { observer } from "mobx-react-lite";
import BreadCrumb from "@src/components/BreadCrumb";
import MobxContext from "@src/store/context";
import store from "@src/store/store";
import { colorPrimary, splitFlag } from "@src/config";
import { COLOR_PRIMARY, SPLIT_FLAG } from "@src/config";
import MenuHeader from "./header";
import SiderCom from "./sider";

const { Content } = Layout;

/**
* 处理路由菜单,根据当前路径匹配菜单项,并生成对应的tabId、menuId和侧边菜单。
* @param routerMenu 路由菜单数组,包含所有菜单项。
* @param pathname 当前页面的路径。
* @returns 返回一个包含tabId、menuId和侧边菜单的数组。
*/
function menuHandle(routerMenu: MenuItem[] = [], pathname: string): [string, string, MenuItem[]] {
let menuId = "";
let parentStr = "";
Expand All @@ -19,7 +25,7 @@ function menuHandle(routerMenu: MenuItem[] = [], pathname: string): [string, str
if (arr.length < 0) return;

arr.forEach(e => {
if (pathname.includes(e.path)) {
if (pathname.startsWith(e.path)) {
parentStr = parent ? parent : `${e.key}`;

if (!e.children?.length) {
Expand All @@ -30,7 +36,7 @@ function menuHandle(routerMenu: MenuItem[] = [], pathname: string): [string, str
let newParentStr = "";
if (e.children?.length) {
if (parent) {
newParentStr = `${parent + splitFlag + e.key}`;
newParentStr = `${parent + SPLIT_FLAG + e.key}`;
} else {
newParentStr = `${e.key}`;
}
Expand All @@ -41,7 +47,7 @@ function menuHandle(routerMenu: MenuItem[] = [], pathname: string): [string, str
}
getCurrentPathId(routerMenu);

const tabId = parentStr.split(splitFlag)?.[0] || `${routerMenu[0]?.key}`;
const tabId = parentStr.split(SPLIT_FLAG)?.[0] || `${routerMenu[0]?.key}`;
const sider = routerMenu.filter(e => e.key === tabId)[0]?.children || [];

function getSideMenu(arr: MenuItem[]): MenuItem[] {
Expand Down Expand Up @@ -77,7 +83,7 @@ const AppLayout = observer(({ children }: { children: React.ReactNode }) => {

const currentThem = {
algorithm: darkMode ? theme.darkAlgorithm : theme.defaultAlgorithm,
token: { colorPrimary },
token: { colorPrimary: COLOR_PRIMARY },
};

return (
Expand Down
6 changes: 3 additions & 3 deletions src/pages/system/setting/company.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Card, Button, Table, notification } from "antd";
import { useRequest } from "ahooks";
import { companyService } from "@src/api/setting";
import type { CompanyApi } from "@src/types/api";
import { defaultPageInfo } from "@src/config";
import { DEFAULT_PAGE } from "@src/config";

const filterInfo: FormItem[] = [{ name: "projectName", type: "input", label: "厂商名称" }];
const editInfo: FormItem[] = [
Expand Down Expand Up @@ -51,7 +51,7 @@ const Company = () => {
const [ModalOpen, ModalOpenHandle] = useState(false);

const [searchInfo, searchInfoHandle] = useState<Record<string, any>>({});
const [pageInfo, pageInfoHandle] = useState(defaultPageInfo);
const [pageInfo, pageInfoHandle] = useState(DEFAULT_PAGE);

Check warning on line 54 in src/pages/system/setting/company.tsx

View workflow job for this annotation

GitHub Actions / Build

'pageInfoHandle' is assigned a value but never used
const [editData, editDataHandle] = useState<any>(null);

const { data, error, loading, run } = useRequest(companyService.list, {
Expand All @@ -63,7 +63,7 @@ const Company = () => {
}

const onSearch = (query: Record<string, any>) => {
run({ ...defaultPageInfo, ...query });
run({ ...DEFAULT_PAGE, ...query });
searchInfoHandle(query);
};
const createOrEdit = async (value: CompanyApi.InsertReq) => {
Expand Down

0 comments on commit f3240f5

Please sign in to comment.