diff --git a/config.ts b/config.ts new file mode 100644 index 0000000..339b6a8 --- /dev/null +++ b/config.ts @@ -0,0 +1,15 @@ +export const AppConfig: { + [key:string]: { + ldapApiUrl: string, + ldapApiKey: string + } +} = { + default:{ + ldapApiUrl: 'https://test-api.hkchc.team', + ldapApiKey: 'MPCbsNa6l2RJ7D1Zo6D03qtVF1P93st3' + }, + production:{ + ldapApiUrl: 'https://test-api.hkchc.team', + ldapApiKey: 'MPCbsNa6l2RJ7D1Zo6D03qtVF1P93st3' + } +} \ No newline at end of file diff --git a/src/hooks/useRemoteUserList.ts b/src/hooks/useRemoteUserList.ts new file mode 100644 index 0000000..63acaf9 --- /dev/null +++ b/src/hooks/useRemoteUserList.ts @@ -0,0 +1,35 @@ +import {useEffect, useState} from "react"; + +function getRemoteUserNameList() { + return new Promise((resolve, reject) => { + fetch(`${AppConfig.ldapApiUrl}/api/v1/hkchc/user/ldap/get_staff_list`, { + method: 'GET', + headers: { + Apikey: AppConfig.ldapApiKey + }, + redirect: 'follow' + }) + .then(response => response.json()) + .then(ret => { + const result = ret as APIResponse; + if (result.code === 0) { + resolve(result.data!) + } else { + reject(result.message) + } + }) + .catch(reject); + }) +} + +export function useRemoteUserList() { + + const [usernameList, setUserList] = useState([]) + + useEffect(()=>{ + getRemoteUserNameList().then(data=>{ + setUserList(data.flat()) + }) + },[]) + return usernameList +} \ No newline at end of file diff --git a/src/pages/auth/permission.tsx b/src/pages/auth/permission.tsx index 40766ce..6e1c9fb 100644 --- a/src/pages/auth/permission.tsx +++ b/src/pages/auth/permission.tsx @@ -1,86 +1,96 @@ import {Card} from "@/components/card"; import {useSetState} from "ahooks"; -import {Button, Space, TagInput, Toast} from "@douyinfe/semi-ui"; +import {Button, Select, Space, Toast} from "@douyinfe/semi-ui"; import {useTranslation} from "react-i18next"; -import {useEffect} from "react"; +import {useEffect, useMemo} from "react"; import {getPermissionList, savePermissionList} from "@/service/api/user.ts"; +import {useRemoteUserList} from "@/hooks/useRemoteUserList.ts"; const DEFAULT_ROLES = [ - 'root','ro','fo' + 'root', 'ro', 'fo' ] -const Permission = ()=>{ +const Permission = () => { const {t} = useTranslation() - const [state,setState] = useSetState<{ - list:PermissionUserList[]; - loading?:boolean; + const usernameList = useRemoteUserList(); + const [state, setState] = useSetState<{ + list: PermissionUserList[]; + loading?: boolean; }>({ - list:[] + list: [] }) - const onUsernameChange = (role_name:string,username_list: string[])=>{ - const index = state.list.findIndex(it=>it.role_name == role_name) - if(index != -1){ + const onUsernameChange = (role_name: string, username_list: string[]) => { + const index = state.list.findIndex(it => it.role_name == role_name) + if (index != -1) { state.list[index] = { - role_name,username_list + role_name, username_list }; setState({ - list:state.list + list: state.list }) } } - const saveRoles = ()=>{ + const saveRoles = () => { setState({ - loading:true + loading: true }) - savePermissionList(state.list).then(()=>{ - Toast.success({content:`Save Success`,duration: 3,}) - }).catch(e=>{ + savePermissionList(state.list).then(() => { + Toast.success({content: `Save Success`, duration: 3,}) + }).catch(e => { Toast.error({ - content:`Save Error:${e.message}`, + content: `Save Error:${e.message}`, duration: 3, }) - }).finally(()=>{ - setState({loading:false}) + }).finally(() => { + setState({loading: false}) }) } - const loadAllPermission = ()=>{ + const loadAllPermission = () => { setState({ - loading:true + loading: true }) - getPermissionList().then(list=>{ - const roles:{ - [key:string]:string[] + getPermissionList().then(list => { + const roles: { + [key: string]: string[] } = {} // array to object - list.forEach(it=>{ + list.forEach(it => { roles[it.role_name] = it.username_list }) - const permissionList:PermissionUserList[] = []; - DEFAULT_ROLES.forEach(role_name=>{ + const permissionList: PermissionUserList[] = []; + DEFAULT_ROLES.forEach(role_name => { permissionList.push({ - role_name,username_list: roles[role_name] + role_name, username_list: roles[role_name] }) }) - setState({list:permissionList}) - }).finally(()=>{ - setState({loading:false}) + setState({list: permissionList}) + }).finally(() => { + setState({loading: false}) }) } - useEffect(loadAllPermission,[]) + useEffect(loadAllPermission, []) + const optionList = useMemo(() => (usernameList.map(name => ({label: name, value: name}))), [usernameList]) return ( - {state.list.map(it=>(
-
{it.role_name.toUpperCase()}
- (
+
{it.role_name.toUpperCase()}
+