From 2939228cfa4a7259b6776ffc29cfd62e2ff468aa Mon Sep 17 00:00:00 2001 From: callmeyan Date: Wed, 28 Aug 2024 17:25:11 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=83=E9=99=90=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/permission/index.tsx | 22 +++ src/hooks/useRemoteUserList.ts | 21 ++- src/i18n/translations/en.json | 5 +- src/i18n/translations/sc.json | 5 +- src/i18n/translations/tc.json | 5 +- src/pages/auth/permission.tsx | 176 ++++++++++++------ .../bill/components/import_bill_modal.tsx | 20 +- src/pages/bill/query.tsx | 39 ++-- src/pages/bill/reconciliation.tsx | 6 +- src/routes/layout/dashboard-navigation.tsx | 12 +- src/service/api/bill.ts | 2 +- src/service/api/user.ts | 10 +- src/service/request.ts | 2 +- src/types/auth.d.ts | 27 +-- 14 files changed, 231 insertions(+), 121 deletions(-) create mode 100644 src/components/permission/index.tsx diff --git a/src/components/permission/index.tsx b/src/components/permission/index.tsx new file mode 100644 index 0000000..4838cb9 --- /dev/null +++ b/src/components/permission/index.tsx @@ -0,0 +1,22 @@ +import React from "react"; +import useAuth from "@/hooks/useAuth.ts"; + +type PermissionKeys = + 'apply_button' + | 'apply_page' + | 'bill_page' + | 'complete_button' + | 'confirm_button' + | 'manual_payment' + | 'permission_edit'; + +type PermissionCheckProps = { + permission: PermissionKeys ; + children: React.ReactNode; +} +export const PermissionCheck: React.FC = ({permission, children}) => { + const {user} = useAuth() + return <> + {user && user.permissions && user.permissions[permission] ? children : null} + +} \ No newline at end of file diff --git a/src/hooks/useRemoteUserList.ts b/src/hooks/useRemoteUserList.ts index f135e26..ff3b9dc 100644 --- a/src/hooks/useRemoteUserList.ts +++ b/src/hooks/useRemoteUserList.ts @@ -1,4 +1,4 @@ -import {useEffect, useState} from "react"; +import {useRequest} from "ahooks"; function getRemoteUserNameList() { return new Promise((resolve, reject) => { @@ -23,13 +23,18 @@ function getRemoteUserNameList() { } export function useRemoteUserList() { + const loadUserList = async () => { + const list = await getRemoteUserNameList(); + return list.flat(); + } + const {data:usernameList} = useRequest(loadUserList) + // const [userList, setUserList] = useState([]) + //const [usernameList, setUserList] = useState([]) - const [usernameList, setUserList] = useState([]) - - useEffect(()=>{ - getRemoteUserNameList().then(data=>{ - setUserList(data.flat()) - }) - },[]) + // useEffect(()=>{ + // getRemoteUserNameList().then(data=>{ + // setUserList(data.flat()) + // }) + // },[]) return usernameList } \ No newline at end of file diff --git a/src/i18n/translations/en.json b/src/i18n/translations/en.json index 8ed21bd..5d2c460 100644 --- a/src/i18n/translations/en.json +++ b/src/i18n/translations/en.json @@ -21,6 +21,8 @@ "query_bill": "Failed to query bill:", "remove": "Remove", "save": "Save", + "save_failed": "Save failed", + "save_success": "Saved successfully", "select_excel_file": "Select File", "select_upload_file": "Select File", "student_number": "Student Number", @@ -162,7 +164,8 @@ "permission": { "message": { "empty_tips": "No data, please create new", - "error_require": "Please set your username or role" + "error_require": "Please set your username or role", + "username_exist": "Username already exists" }, "title": { "add": "New", diff --git a/src/i18n/translations/sc.json b/src/i18n/translations/sc.json index f936faa..f60bba2 100644 --- a/src/i18n/translations/sc.json +++ b/src/i18n/translations/sc.json @@ -21,6 +21,8 @@ "query_bill": "查询账单失败:", "remove": "删除", "save": "保存", + "save_failed": "保存失败", + "save_success": "保存成功", "select_excel_file": "选择文件", "select_upload_file": "选择文件", "student_number": "学号", @@ -162,7 +164,8 @@ "permission": { "message": { "empty_tips": "暂无数据,请新增", - "error_require": "请设置用户名或角色" + "error_require": "请设置用户名或角色", + "username_exist": "用户名已经存在了" }, "title": { "add": "新增", diff --git a/src/i18n/translations/tc.json b/src/i18n/translations/tc.json index 0448f8e..a6e578d 100644 --- a/src/i18n/translations/tc.json +++ b/src/i18n/translations/tc.json @@ -21,6 +21,8 @@ "query_bill": "查詢帳單失敗:", "remove": "刪除", "save": "儲存", + "save_failed": "保存失敗", + "save_success": "保存成功", "select_excel_file": "選擇文件", "select_upload_file": "選擇文件", "student_number": "學號", @@ -162,7 +164,8 @@ "permission": { "message": { "empty_tips": "暫無數據,請新增", - "error_require": "請設定使用者名稱或角色" + "error_require": "請設定使用者名稱或角色", + "username_exist": "使用者名稱已經存在了" }, "title": { "add": "新增", diff --git a/src/pages/auth/permission.tsx b/src/pages/auth/permission.tsx index a1f3276..9af0b90 100644 --- a/src/pages/auth/permission.tsx +++ b/src/pages/auth/permission.tsx @@ -3,11 +3,16 @@ import {Button, Checkbox, Empty, Popconfirm, Select, Space, Toast} from "@douyin import {useTranslation} from "react-i18next"; import {useEffect, useMemo} from "react"; -import './permission.less'; - import {Card} from "@/components/card"; +import './permission.less'; import {useRemoteUserList} from "@/hooks/useRemoteUserList.ts"; -import {getUserPermissionList, removeUserPermission} from "@/service/api/user.ts"; +import { + createUserPermission, + getUserPermissionList, + removeUserPermission, + updateUserPermission +} from "@/service/api/user.ts"; +import useAuth from "@/hooks/useAuth.ts"; // const PermissionList = [ @@ -20,28 +25,49 @@ import {getUserPermissionList, removeUserPermission} from "@/service/api/user.ts // 'permission' // ] - -const UserPermissionItem = ({it, onChange, usernameOptionList}: { +type UserPermissionItemProps = { it: UserPermission; - onChange: (action:'remove'|'saved'|'modify',value:UserPermission) => void; + onChange: (action: 'remove' | 'saved' | 'modify', value: UserPermission) => Promise; usernameOptionList: OptionValue[] -}) => { + roleOptionList: OptionValue[] +} + +const UserPermissionItem = ({it, onChange, usernameOptionList,roleOptionList}: UserPermissionItemProps) => { const {t} = useTranslation() + const {user} = useAuth() + const [state,setState] = useSetState({ + loading: false + }) const onValueChange = (value: { - [key:string]:string|boolean - })=>{ - onChange('modify',{ + [key: string]: string | boolean + }) => { + onChange('modify', { ...it, ...value - }) + }).then(); } - const onSave = ()=>{ + const onRemove = () => { + if(it.id > 0){ + setState({loading: true}) + onChange('remove', it).finally(()=>{ + setState({loading: false}) + }) + return; + } + onChange('remove', it).then(); + } + const onSave = () => { console.log(it) - if(!it.role || !it.username){ + if (!it.role || !it.username) { Toast.warning(t('permission.message.error_require')) return; } + setState({loading: true}) + onChange('saved', it).finally(()=>{ + setState({loading: false}) + }) } + // const [values,setValues] = use return (
@@ -60,11 +86,7 @@ const UserPermissionItem = ({it, onChange, usernameOptionList}: {