From 1abfdb00a4f748b6e710c451e5459695b43956d1 Mon Sep 17 00:00:00 2001 From: dark Date: Wed, 1 May 2024 15:36:31 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E5=AE=8C=E5=96=84=E9=83=A8=E9=97=A8?= =?UTF-8?q?=E9=A1=B5=E9=9D=A2=202=E3=80=81=E4=BC=98=E5=8C=96=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E7=BA=A7store=E7=9A=84=E7=9B=AE=E5=BD=95=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E4=BD=BF=E7=BB=84=E4=BB=B6=E4=B8=AD=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E6=9B=B4=E5=8A=A0=E7=AE=80=E6=B4=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.tsx | 2 +- src/Auth.tsx | 2 +- src/context.ts | 2 +- src/global.d.ts | 94 ++++++++ src/hooks/useFetch.ts | 2 +- src/i18n.ts | 2 +- src/layout/RootLayout.tsx | 2 +- .../departments/components/DepartmentTree.tsx | 8 +- .../departments/components/TreeNodeRender.tsx | 8 +- src/pages/system/departments/index.tsx | 255 +++++++++++---------- src/pages/system/menus/components/BatchButton.tsx | 6 +- src/pages/system/menus/components/ButtonTable.tsx | 2 +- src/pages/system/menus/components/MenuTree.tsx | 10 +- .../system/menus/components/TreeNodeRender.tsx | 8 +- src/pages/system/menus/index.tsx | 10 +- src/pages/system/roles/index.tsx | 16 +- src/request.ts | 2 +- src/routes.tsx | 2 +- src/service/base.ts | 2 +- src/service/system.ts | 8 +- src/store/department.ts | 2 +- src/store/menu.ts | 2 +- src/store/role.ts | 2 +- src/store/route.ts | 2 +- src/store/system.ts | 2 +- src/store/user.ts | 86 ++++--- src/types.d.ts | 94 -------- src/types/index.ts | 5 + src/types/menus.d.ts | 3 + src/utils/index.ts | 2 +- src/utils/uuid.ts | 2 +- 31 files changed, 337 insertions(+), 308 deletions(-) create mode 100644 src/global.d.ts delete mode 100644 src/types.d.ts create mode 100644 src/types/index.ts diff --git a/src/App.tsx b/src/App.tsx index 7a9ff3b..bc068d7 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -2,7 +2,7 @@ import { AppContextProvider } from '@/context.ts' import { initI18n } from '@/i18n.ts' import { appAtom, appStore, changeLanguage } from '@/store/system.ts' import { userMenuDataAtom } from '@/store/user.ts' -import { IAppData } from '@/types' +import { IAppData } from '@/global' import { ConfigProvider } from '@/components/config-provider' import { Provider, useAtom, useAtomValue } from 'jotai' import './App.css' diff --git a/src/Auth.tsx b/src/Auth.tsx index 9250ab6..a21f5cc 100644 --- a/src/Auth.tsx +++ b/src/Auth.tsx @@ -1,5 +1,5 @@ import React from 'react' -import { Props } from './types' +import { Props } from './global' import { useAtomValue } from 'jotai' import { authAtom } from './store/user.ts' diff --git a/src/context.ts b/src/context.ts index 7dcb15f..7bade70 100644 --- a/src/context.ts +++ b/src/context.ts @@ -1,4 +1,4 @@ -import { IAppData } from '@/types' +import { IAppData } from '@/global' import React, { createContext, ProviderProps, useContext } from 'react' import { t } from 'i18next' diff --git a/src/global.d.ts b/src/global.d.ts new file mode 100644 index 0000000..a21d1db --- /dev/null +++ b/src/global.d.ts @@ -0,0 +1,94 @@ +import { IMenu } from '@/types/menus' +import { QueryClient } from '@tanstack/react-query' +import { Router } from '@tanstack/react-router' +import { RouteOptions } from '@tanstack/react-router/src/route.ts' +import { Attributes, ReactNode } from 'react' + +export type LayoutType = 'list' | 'form' | 'tree' | 'normal' + +export type IAppData = { + name: string; + version: string; + language: string; + baseUrl: string; + token: string; + device: string; +} + +export type TRouter = { + router: Router & { + context: IRootContext + } +} + +export type IPage = { + page?: number + pageSize?: number +} + +export type IPageResult = IPage & { + rows: T[] + total: number +} + +export type IApiResult = { + code: number; + data: T; + message: string; +} + +export type TreeItem = { + [key: keyof T]: T[keyof T]; + children?: TreeItem[]; +} + +export type FlattenData = TreeItem & { + key: string, + title?: string, + label?: string, + level?: number, +} + +export type FiledNames = { + key?: string; + title?: string; + children?: string +} + + +export type IDataProps = { + value?: T; + onChange?: (value: T) => void; +} + + +export type Props = Attributes & { + children?: ReactNode +}; + +export interface IRootContext { + menuData: MenuItem[]; + queryClient: QueryClient; +} + +interface MenuItem extends IMenu { + routes?: MenuItem[]; +} + +interface IAuth { + isLogin: boolean; + authKey?: string[]; +} + +declare module '@tanstack/react-router' { + interface Register { + router: TRouter + } + + interface AnyRoute { + options: RouteOptions & { + menu: MenuItem + } + } + +} diff --git a/src/hooks/useFetch.ts b/src/hooks/useFetch.ts index da248fd..3a01867 100644 --- a/src/hooks/useFetch.ts +++ b/src/hooks/useFetch.ts @@ -1,5 +1,5 @@ import { atomFamily } from 'jotai/utils' -import { IDataProps } from '@/types' +import { IDataProps } from '@/global' import { useMemo, useRef } from 'react' import { generateUUID } from '@/utils/uuid' diff --git a/src/i18n.ts b/src/i18n.ts index 93e2c61..2f95850 100644 --- a/src/i18n.ts +++ b/src/i18n.ts @@ -35,7 +35,7 @@ export const initI18n = (options?: InitOptions) => { }, }, fallbackLng: 'zh', - debug: true, + debug: false, detection: detectionOptions, interpolation: { escapeValue: false, diff --git a/src/layout/RootLayout.tsx b/src/layout/RootLayout.tsx index fc164cc..9b6f639 100644 --- a/src/layout/RootLayout.tsx +++ b/src/layout/RootLayout.tsx @@ -4,7 +4,7 @@ import ErrorPage from '@/components/error/error.tsx' import SelectLang from '@/components/select-lang' import { appAtom } from '@/store/system.ts' import { userMenuDataAtom } from '@/store/user.ts' -import { MenuItem } from '@/types' +import { MenuItem } from '@/global' import { ProConfigProvider, ProLayout, } from '@ant-design/pro-components' import { zhCNIntl, enUSIntl } from '@ant-design/pro-provider/es/intl' import { CatchBoundary, Link, Outlet } from '@tanstack/react-router' diff --git a/src/pages/system/departments/components/DepartmentTree.tsx b/src/pages/system/departments/components/DepartmentTree.tsx index b51f828..ef8b87f 100644 --- a/src/pages/system/departments/components/DepartmentTree.tsx +++ b/src/pages/system/departments/components/DepartmentTree.tsx @@ -1,4 +1,4 @@ -import { usePageStoreOptions } from '@/store' + import { Empty, Spin, Tree } from 'antd' import { useStyle } from '../style.ts' import { useTranslation } from '@/i18n.ts' @@ -16,9 +16,9 @@ export const DepartmentTree = ({ form }: { form: FormInstance }) => { const { styles } = useStyle() const { t } = useTranslation() - const setIds = useSetAtom(batchIdsAtom, usePageStoreOptions()) - const setCurrent = useSetAtom(selectedDepartAtom, usePageStoreOptions()) - const { data = [], isLoading } = useAtomValue(departTreeAtom, usePageStoreOptions()) + const setIds = useSetAtom(batchIdsAtom) + const setCurrent = useSetAtom(selectedDepartAtom) + const { data = [], isLoading } = useAtomValue(departTreeAtom) const flattenMenusRef = useRef([]) useDeepCompareEffect(() => { diff --git a/src/pages/system/departments/components/TreeNodeRender.tsx b/src/pages/system/departments/components/TreeNodeRender.tsx index 89192c0..0365b53 100644 --- a/src/pages/system/departments/components/TreeNodeRender.tsx +++ b/src/pages/system/departments/components/TreeNodeRender.tsx @@ -1,6 +1,6 @@ -import { usePageStoreOptions } from '@/store' + import { memo } from 'react' -import { MenuItem } from '@/types' +import { MenuItem } from '@/global' import { Popconfirm, Space, TreeDataNode } from 'antd' import { FormInstance } from 'antd/lib' import { useTranslation } from '@/i18n.ts' @@ -14,9 +14,9 @@ export const TreeNodeRender = memo(({ node, form }: { node: MenuItem & TreeDataN const { name } = node const { t } = useTranslation() const { styles } = useStyle() - const { mutate } = useAtomValue(deleteDepartAtom, usePageStoreOptions()) + const { mutate } = useAtomValue(deleteDepartAtom) - const setCurrent = useSetAtom(selectedDepartAtom, usePageStoreOptions()) + const setCurrent = useSetAtom(selectedDepartAtom) return (
diff --git a/src/pages/system/departments/index.tsx b/src/pages/system/departments/index.tsx index 07016e7..d4dbb12 100644 --- a/src/pages/system/departments/index.tsx +++ b/src/pages/system/departments/index.tsx @@ -1,4 +1,3 @@ -import { usePageStoreOptions } from '@/store' import { PageContainer, ProCard } from '@ant-design/pro-components' import { Flexbox } from 'react-layout-kit' import { DraggablePanel } from '@/components/draggable-panel' @@ -11,145 +10,147 @@ import { defaultDepart, selectedDepartAtom, departTreeAtom, saveOrUpdateDepartAt import { useAtom, useAtomValue, } from 'jotai' import Glass from '@/components/glass' import { useEffect, useRef } from 'react' +import UserPicker from '@/components/user-picker/UserPicker.tsx' const Departments = () => { - const { t } = useTranslation() - const { styles, cx } = useStyle() - const [ form ] = Form.useForm() - const inputRef = useRef() - const { data } = useAtomValue(departTreeAtom, usePageStoreOptions()) - const { mutate, isPending, isError, error } = useAtomValue(saveOrUpdateDepartAtom, usePageStoreOptions()) - const [ current, setCurrent ] = useAtom(selectedDepartAtom, usePageStoreOptions()) + const { t } = useTranslation() + const { styles, cx } = useStyle() + const [ form ] = Form.useForm() + const inputRef = useRef() + const { data } = useAtomValue(departTreeAtom) + const { mutate, isPending, isError, error } = useAtomValue(saveOrUpdateDepartAtom) + const [ current, setCurrent ] = useAtom(selectedDepartAtom) - useEffect(() => { + useEffect(() => { - if (isError) { - notification.error({ - message: t('message.error', '错误'), - description: (error as any).message ?? t('message.saveFail', '保存失败'), - }) - } - }, [ isError ]) + if (isError) { + notification.error({ + message: t('message.error', '错误'), + description: (error as any).message ?? t('message.saveFail', '保存失败'), + }) + } + }, [ isError ]) - useEffect(() => { - if (current.id === 0 && inputRef.current) { - inputRef.current.focus() - } - }, [ current ]) + useEffect(() => { + if (current.id === 0 && inputRef.current) { + inputRef.current.focus() + } + }, [ current ]) - return ( - - - - - - -
- - -
-
- - - - } - > + return ( + + + + + + +
+ + +
+
+ + + + } + > - -
- - - - - - - - - - - - - + + + + + + + + + - - - - - - + + + + + + - -
+ + + + + + -
-
-
-
- ) + + + +
+
+
+
+ ) } diff --git a/src/pages/system/menus/components/BatchButton.tsx b/src/pages/system/menus/components/BatchButton.tsx index b6e938d..4d31e46 100644 --- a/src/pages/system/menus/components/BatchButton.tsx +++ b/src/pages/system/menus/components/BatchButton.tsx @@ -1,4 +1,4 @@ -import { usePageStoreOptions } from '@/store' + import { Button, Popconfirm } from 'antd' import { useAtomValue } from 'jotai' import { batchIdsAtom, deleteMenuAtom } from '@/store/menu.ts' @@ -7,8 +7,8 @@ import { useTranslation } from '@/i18n.ts' const BatchButton = () => { const { t } = useTranslation() - const { isPending, mutate, } = useAtomValue(deleteMenuAtom, usePageStoreOptions()) - const ids = useAtomValue(batchIdsAtom, usePageStoreOptions()) + const { isPending, mutate, } = useAtomValue(deleteMenuAtom) + const ids = useAtomValue(batchIdsAtom) if (ids.length === 0) { diff --git a/src/pages/system/menus/components/ButtonTable.tsx b/src/pages/system/menus/components/ButtonTable.tsx index 52d4124..8c224ea 100644 --- a/src/pages/system/menus/components/ButtonTable.tsx +++ b/src/pages/system/menus/components/ButtonTable.tsx @@ -1,7 +1,7 @@ import { useTranslation } from '@/i18n.ts' import { EditableFormInstance, EditableProTable } from '@ant-design/pro-components' import { useMemo, useRef, useState } from 'react' -import { IDataProps } from '@/types' +import { IDataProps } from '@/global' import { FormInstance } from 'antd/lib' type DataSourceType = { diff --git a/src/pages/system/menus/components/MenuTree.tsx b/src/pages/system/menus/components/MenuTree.tsx index 5f95081..c23adb2 100644 --- a/src/pages/system/menus/components/MenuTree.tsx +++ b/src/pages/system/menus/components/MenuTree.tsx @@ -1,6 +1,6 @@ -import { usePageStoreOptions } from '@/store' + import { Empty, Spin, Tree } from 'antd' -import { MenuItem } from '@/types' +import { MenuItem } from '@/global' import { useStyle } from '../style.ts' import { useTranslation } from '@/i18n.ts' import { useSetAtom } from 'jotai' @@ -17,9 +17,9 @@ const MenuTree = ({ form }: { form: FormInstance }) => { const { styles } = useStyle() const { t } = useTranslation() - const setCurrentMenu = useSetAtom(selectedMenuAtom, usePageStoreOptions()) - const setIds = useSetAtom(batchIdsAtom, usePageStoreOptions()) - const { data = [], isLoading } = useAtomValue(menuDataAtom, usePageStoreOptions()) + const setCurrentMenu = useSetAtom(selectedMenuAtom) + const setIds = useSetAtom(batchIdsAtom) + const { data = [], isLoading } = useAtomValue(menuDataAtom) const flattenMenusRef = useRef([]) useDeepCompareEffect(() => { diff --git a/src/pages/system/menus/components/TreeNodeRender.tsx b/src/pages/system/menus/components/TreeNodeRender.tsx index c0193f7..1525d21 100644 --- a/src/pages/system/menus/components/TreeNodeRender.tsx +++ b/src/pages/system/menus/components/TreeNodeRender.tsx @@ -1,6 +1,6 @@ -import { usePageStoreOptions } from '@/store' + import { memo } from 'react' -import { MenuItem } from '@/types' +import { MenuItem } from '@/global' import { Popconfirm, Space, TreeDataNode } from 'antd' import { FormInstance } from 'antd/lib' import { useTranslation } from '@/i18n.ts' @@ -14,9 +14,9 @@ export const TreeNodeRender = memo(({ node, form }: { node: MenuItem & TreeDataN const { title } = node const { t } = useTranslation() const { styles } = useStyle() - const { mutate } = useAtomValue(deleteMenuAtom, usePageStoreOptions()) + const { mutate } = useAtomValue(deleteMenuAtom) - const setMenuData = useSetAtom(selectedMenuAtom, usePageStoreOptions()) + const setMenuData = useSetAtom(selectedMenuAtom) return (
diff --git a/src/pages/system/menus/index.tsx b/src/pages/system/menus/index.tsx index 846f229..d45fba3 100644 --- a/src/pages/system/menus/index.tsx +++ b/src/pages/system/menus/index.tsx @@ -1,6 +1,6 @@ import Glass from '@/components/glass' import { useTranslation } from '@/i18n.ts' -import { usePageStoreOptions } from '@/store' + import { PlusOutlined } from '@ant-design/icons' import { PageContainer, ProCard } from '@ant-design/pro-components' import { Button, Form, Input, Radio, TreeSelect, InputNumber, notification, Alert, InputRef, Divider } from 'antd' @@ -11,7 +11,7 @@ import ButtonTable from './components/ButtonTable.tsx' import { Flexbox } from 'react-layout-kit' import { DraggablePanel } from '@/components/draggable-panel' import { useStyle } from './style.ts' -import { MenuItem } from '@/types' +import { MenuItem } from '@/global' import MenuTree from './components/MenuTree.tsx' import BatchButton from '@/pages/system/menus/components/BatchButton.tsx' import { useEffect, useRef } from 'react' @@ -21,9 +21,9 @@ const Menus = () => { const { styles, cx } = useStyle() const { t } = useTranslation() const [ form ] = Form.useForm() - const { mutate, isPending, error, isError } = useAtomValue(saveOrUpdateMenuAtom, usePageStoreOptions()) - const { data = [] } = useAtomValue(menuDataAtom, usePageStoreOptions()) - const [ currentMenu, setMenuData ] = useAtom(selectedMenuAtom, usePageStoreOptions()) ?? {} + const { mutate, isPending, error, isError } = useAtomValue(saveOrUpdateMenuAtom) + const { data = [] } = useAtomValue(menuDataAtom) + const [ currentMenu, setMenuData ] = useAtom(selectedMenuAtom) ?? {} const menuInputRef = useRef(undefined) useEffect(() => { diff --git a/src/pages/system/roles/index.tsx b/src/pages/system/roles/index.tsx index e1508f1..9960264 100644 --- a/src/pages/system/roles/index.tsx +++ b/src/pages/system/roles/index.tsx @@ -1,5 +1,5 @@ import Switch from '@/components/switch' -import { usePageStoreOptions } from '@/store' + import { IMenu } from '@/types/menus' import { ActionType, @@ -54,13 +54,13 @@ const Roles = memo(() => { const { styles } = useStyle() const [ form ] = Form.useForm() const actionRef = useRef() - const [ page, setPage ] = useAtom(pageAtom, usePageStoreOptions()) - const [ search, setSearch ] = useAtom(searchAtom, usePageStoreOptions()) - const [ roleIds, setRoleIds ] = useAtom(roleIdsAtom, usePageStoreOptions()) - const { data, isLoading, isFetching, refetch } = useAtomValue(rolesAtom, usePageStoreOptions()) - const { isPending, mutate, isSuccess } = useAtomValue(saveOrUpdateRoleAtom, usePageStoreOptions()) - const { mutate: deleteRole, isPending: isDeleting } = useAtomValue(deleteRoleAtom, usePageStoreOptions()) - const [ , setRole ] = useAtom(roleAtom, usePageStoreOptions()) + const [ page, setPage ] = useAtom(pageAtom) + const [ search, setSearch ] = useAtom(searchAtom) + const [ roleIds, setRoleIds ] = useAtom(roleIdsAtom) + const { data, isLoading, isFetching, refetch } = useAtomValue(rolesAtom) + const { isPending, mutate, isSuccess } = useAtomValue(saveOrUpdateRoleAtom) + const { mutate: deleteRole, isPending: isDeleting } = useAtomValue(deleteRoleAtom) + const [ , setRole ] = useAtom(roleAtom) const [ open, setOpen ] = useState(false) const columns = useMemo(() => { diff --git a/src/request.ts b/src/request.ts index f8fbfea..896bf29 100644 --- a/src/request.ts +++ b/src/request.ts @@ -1,5 +1,5 @@ import { getToken, setToken } from '@/store/system.ts' -import { IApiResult } from '@/types' +import { IApiResult } from '@/global' import { Record } from '@icon-park/react' import { message } from 'antd' import axios, { diff --git a/src/routes.tsx b/src/routes.tsx index cec987d..82ee837 100644 --- a/src/routes.tsx +++ b/src/routes.tsx @@ -24,7 +24,7 @@ import { import { TanStackRouterDevtools } from '@tanstack/router-devtools' import { memo } from 'react' import RootLayout from './layout/RootLayout' -import { IRootContext, MenuItem } from './types' +import { IRootContext, MenuItem } from './global' import { DevTools } from 'jotai-devtools' const PageRootLayout = () => { diff --git a/src/service/base.ts b/src/service/base.ts index 116b14f..ea27b76 100644 --- a/src/service/base.ts +++ b/src/service/base.ts @@ -1,5 +1,5 @@ import { request, AxiosRequestConfig } from '@/request.ts' -import { IPage, IPageResult } from '@/types' +import { IPage, IPageResult } from '@/global' export const createCURD = (api: string, options?: AxiosRequestConfig) => { diff --git a/src/service/system.ts b/src/service/system.ts index 04118e7..7c3bfa0 100644 --- a/src/service/system.ts +++ b/src/service/system.ts @@ -1,5 +1,5 @@ -import { IPageResult } from '@/types' -import { IUserInfo } from '@/types/user' +import { IPageResult } from '@/global' +import { IUser, IUserInfo } from '@/types/user' import request from '../request.ts' import { LoginRequest, LoginResponse } from '@/types/login' import { createCURD } from '@/service/base.ts' @@ -21,12 +21,14 @@ const systemServ = { return request.post('/sys/login', data) }, user: { + ...createCURD('/sys/user'), current: () => { return request.get('/sys/user/info') }, menus: () => { return request.get>('/sys/user/menus') - } + }, + }, role: { diff --git a/src/store/department.ts b/src/store/department.ts index a210495..48479d1 100644 --- a/src/store/department.ts +++ b/src/store/department.ts @@ -1,6 +1,6 @@ import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query' import systemServ from '@/service/system.ts' -import { IApiResult, IPage } from '@/types' +import { IApiResult, IPage } from '@/global' import { IDepartment } from '@/types/department' import { atom, createStore } from 'jotai' import { t } from 'i18next' diff --git a/src/store/menu.ts b/src/store/menu.ts index f04e0b1..27bf08a 100644 --- a/src/store/menu.ts +++ b/src/store/menu.ts @@ -1,5 +1,5 @@ import systemServ from '@/service/system.ts' -import { IApiResult, IPage, MenuItem } from '@/types' +import { IApiResult, IPage, MenuItem } from '@/global' import { IMenu } from '@/types/menus' import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query' import { atom, createStore } from 'jotai' diff --git a/src/store/role.ts b/src/store/role.ts index 25443dd..a6b7952 100644 --- a/src/store/role.ts +++ b/src/store/role.ts @@ -2,7 +2,7 @@ import { convertToBool } from '@/utils' import { atom } from 'jotai/index' import { IRole } from '@/types/roles' import { atomWithMutation, atomWithQuery, queryClientAtom } from 'jotai-tanstack-query' -import { IApiResult, IPage } from '@/types' +import { IApiResult, IPage } from '@/global' import systemServ from '@/service/system.ts' import { message } from 'antd' import { t } from '@/i18n.ts' diff --git a/src/store/route.ts b/src/store/route.ts index d4fc954..9bada25 100644 --- a/src/store/route.ts +++ b/src/store/route.ts @@ -1,4 +1,4 @@ -import { IRootContext } from '@/types' +import { IRootContext } from '@/global' import { atom } from 'jotai' export const routeContextAtom = atom({}) diff --git a/src/store/system.ts b/src/store/system.ts index 7767f13..676bb43 100644 --- a/src/store/system.ts +++ b/src/store/system.ts @@ -1,4 +1,4 @@ -import { IAppData } from '@/types' +import { IAppData } from '@/global' import { createStore } from 'jotai' import { atomWithStorage } from 'jotai/utils' import { changeLanguage as setLang } from 'i18next' diff --git a/src/store/user.ts b/src/store/user.ts index 10b726e..c7eefd7 100644 --- a/src/store/user.ts +++ b/src/store/user.ts @@ -1,59 +1,77 @@ import { appAtom } from '@/store/system.ts' import { IMenu } from '@/types/menus' -import { IUserInfo } from '@/types/user' +import { IUserInfo } from '@/types/user' import { atom } from 'jotai/index' -import { IApiResult, IAuth, IPageResult, MenuItem } from '@/types' +import { IApiResult, IAuth, IPageResult, MenuItem } from '@/global' import { LoginRequest } from '@/types/login' import { atomWithMutation, atomWithQuery } from 'jotai-tanstack-query' import systemServ from '@/service/system.ts' import { formatMenuData, isDev } from '@/utils' export const authAtom = atom({ - isLogin: false, - authKey: [] + isLogin: false, + authKey: [] }) const devLogin = { - username: 'SupperAdmin', - password: 'kk123456', - code: '123456' + username: 'SupperAdmin', + password: 'kk123456', + code: '123456' } export const loginFormAtom = atom({ - ...(isDev ? devLogin : {}) + ...(isDev ? devLogin : {}) } as LoginRequest) export const loginAtom = atomWithMutation(() => ({ - mutationKey: [ 'login' ], - mutationFn: async (params) => { - return await systemServ.login(params) - }, - onSuccess: () => { - // console.log('login success', data) - }, - retry: false, + mutationKey: [ 'login' ], + mutationFn: async (params) => { + return await systemServ.login(params) + }, + onSuccess: () => { + // console.log('login success', data) + }, + retry: false, })) export const currentUserAtom = atomWithQuery, any, IUserInfo>((get) => { - return { - queryKey: [ 'user_info', get(appAtom).token ], - queryFn: async () => { - return await systemServ.user.current() - }, - select: (data) => { - return data.data + return { + queryKey: [ 'user_info', get(appAtom).token ], + queryFn: async () => { + return await systemServ.user.current() + }, + select: (data) => { + return data.data + } } - } }) export const userMenuDataAtom = atomWithQuery>, any, MenuItem[]>((get) => ({ - enabled: false, - queryKey: [ 'user_menus', get(appAtom).token ], - queryFn: async () => { - return await systemServ.user.menus() - }, - select: (data) => { - return formatMenuData(data.data.rows as any ?? []) - }, - cacheTime: 1000 * 60, - retry: false, + enabled: false, + queryKey: [ 'user_menus', get(appAtom).token ], + queryFn: async () => { + return await systemServ.user.menus() + }, + select: (data) => { + return formatMenuData(data.data.rows as any ?? []) + }, + cacheTime: 1000 * 60, + retry: false, })) + +export const userSearchAtom = atom<{ + dept_id: number, + key: string +} | unknown>({}) + +//user list +export const userListAtom = atomWithQuery((get) => { + return { + queryKey: [ 'user_list', get(userSearchAtom) ], + queryFn: async ({ queryKey: [ , params ] }) => { + return await systemServ.user.list(params) + }, + select: (data) => { + return data.data + }, + } +}) diff --git a/src/types.d.ts b/src/types.d.ts deleted file mode 100644 index a21d1db..0000000 --- a/src/types.d.ts +++ /dev/null @@ -1,94 +0,0 @@ -import { IMenu } from '@/types/menus' -import { QueryClient } from '@tanstack/react-query' -import { Router } from '@tanstack/react-router' -import { RouteOptions } from '@tanstack/react-router/src/route.ts' -import { Attributes, ReactNode } from 'react' - -export type LayoutType = 'list' | 'form' | 'tree' | 'normal' - -export type IAppData = { - name: string; - version: string; - language: string; - baseUrl: string; - token: string; - device: string; -} - -export type TRouter = { - router: Router & { - context: IRootContext - } -} - -export type IPage = { - page?: number - pageSize?: number -} - -export type IPageResult = IPage & { - rows: T[] - total: number -} - -export type IApiResult = { - code: number; - data: T; - message: string; -} - -export type TreeItem = { - [key: keyof T]: T[keyof T]; - children?: TreeItem[]; -} - -export type FlattenData = TreeItem & { - key: string, - title?: string, - label?: string, - level?: number, -} - -export type FiledNames = { - key?: string; - title?: string; - children?: string -} - - -export type IDataProps = { - value?: T; - onChange?: (value: T) => void; -} - - -export type Props = Attributes & { - children?: ReactNode -}; - -export interface IRootContext { - menuData: MenuItem[]; - queryClient: QueryClient; -} - -interface MenuItem extends IMenu { - routes?: MenuItem[]; -} - -interface IAuth { - isLogin: boolean; - authKey?: string[]; -} - -declare module '@tanstack/react-router' { - interface Register { - router: TRouter - } - - interface AnyRoute { - options: RouteOptions & { - menu: MenuItem - } - } - -} diff --git a/src/types/index.ts b/src/types/index.ts new file mode 100644 index 0000000..ebe95fa --- /dev/null +++ b/src/types/index.ts @@ -0,0 +1,5 @@ +export * from './department' +export * from './user' +export * from './login' +export * from './roles' +export * from './menus' diff --git a/src/types/menus.d.ts b/src/types/menus.d.ts index c87e0b7..fa746ab 100644 --- a/src/types/menus.d.ts +++ b/src/types/menus.d.ts @@ -6,6 +6,9 @@ export interface MenuButton { } +interface Meta { +} + export interface IMenu { id: number, key: string, diff --git a/src/utils/index.ts b/src/utils/index.ts index 0d8a09b..69dd807 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -1,5 +1,5 @@ import { IMenu } from '@/types/menus' -import { FiledNames, FlattenData, MenuItem, TreeItem } from '@/types' +import { FiledNames, FlattenData, MenuItem, TreeItem } from '@/global' import { getIcon } from '@/components/icon' import { TreeDataNode } from 'antd' diff --git a/src/utils/uuid.ts b/src/utils/uuid.ts index 1b4a783..6f32f94 100644 --- a/src/utils/uuid.ts +++ b/src/utils/uuid.ts @@ -1,4 +1,4 @@ -import { MenuItem } from '@/types' +import { MenuItem } from '@/global' import { TreeDataNode } from 'antd' export function generateUUID() {