|  |  |  | 
|---|
|  |  |  | import { | 
|---|
|  |  |  | useTranslate, | 
|---|
|  |  |  | useLogin, | 
|---|
|  |  |  | localStorageStore, | 
|---|
|  |  |  | useNotify, | 
|---|
|  |  |  | } from 'react-admin'; | 
|---|
|  |  |  | import { getSystemDicts } from "@/api/auth"; | 
|---|
|  |  |  | import { useForm, Controller, useWatch, FormProvider, useFormContext } from "react-hook-form"; | 
|---|
|  |  |  | import ProviderChoices from "./ProviderChoices"; | 
|---|
|  |  |  | import Visibility from '@mui/icons-material/Visibility'; | 
|---|
|  |  |  | 
|---|
|  |  |  | const notify = useNotify(); | 
|---|
|  |  |  | const login = useLogin(); | 
|---|
|  |  |  | const location = useLocation(); | 
|---|
|  |  |  | const { systemInfo, tenantList } = props; | 
|---|
|  |  |  | const { systemInfo: { mode }, tenantList } = props; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const { control, handleSubmit, watch, setValue, getValues, setError, clearErrors } = useForm(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | const username = watch('username'); | 
|---|
|  |  |  | const password = watch('password'); | 
|---|
|  |  |  | const tenantId = watch('tenantId'); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | useEffect(() => { | 
|---|
|  |  |  | if (tenantList.length > 0 && !tenantId) { | 
|---|
|  |  |  | const rememberTenantId = localStorage.getItem('remember_tenantId'); | 
|---|
|  |  |  | 
|---|
|  |  |  | }, [tenantList, setValue]); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const onSubmit = (data) => { | 
|---|
|  |  |  | getSystemDicts().then(data => { | 
|---|
|  |  |  | localStorage.setItem('sys_dicts', JSON.stringify(data)); | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | setLoading(true); | 
|---|
|  |  |  | login( | 
|---|
|  |  |  | data, | 
|---|
|  |  |  | 
|---|
|  |  |  | component="form" onSubmit={handleSubmit(onSubmit)} noValidate | 
|---|
|  |  |  | > | 
|---|
|  |  |  | <Stack spacing={2}> | 
|---|
|  |  |  | {/* <Controller | 
|---|
|  |  |  | name="tenantId" | 
|---|
|  |  |  | control={control} | 
|---|
|  |  |  | rules={{ required: true }} | 
|---|
|  |  |  | defaultValue={tenantList.length > 0 ? tenantList[0].id : ''} | 
|---|
|  |  |  | render={({ field: { onChange, value, ref } }) => { | 
|---|
|  |  |  | const selectedTenant = tenantList.find(tenant => tenant.id === value) || null; | 
|---|
|  |  |  | return ( | 
|---|
|  |  |  | <Autocomplete | 
|---|
|  |  |  | options={tenantList} | 
|---|
|  |  |  | getOptionLabel={(option) => option.name} | 
|---|
|  |  |  | value={selectedTenant} | 
|---|
|  |  |  | onChange={(_, newValue) => { | 
|---|
|  |  |  | const newTenantId = newValue ? newValue.id : ''; | 
|---|
|  |  |  | onChange(newTenantId); | 
|---|
|  |  |  | localStorage.setItem('remember_tenantId', newTenantId); | 
|---|
|  |  |  | }} | 
|---|
|  |  |  | renderInput={(params) => ( | 
|---|
|  |  |  | <TextField | 
|---|
|  |  |  | {...params} | 
|---|
|  |  |  | label={translate("page.login.tenant")} | 
|---|
|  |  |  | variant="standard" | 
|---|
|  |  |  | inputRef={ref} | 
|---|
|  |  |  | /> | 
|---|
|  |  |  | )} | 
|---|
|  |  |  | /> | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | }} | 
|---|
|  |  |  | /> */} | 
|---|
|  |  |  | {mode === 'OFFLINE' && ( | 
|---|
|  |  |  | <Controller | 
|---|
|  |  |  | name="tenantId" | 
|---|
|  |  |  | control={control} | 
|---|
|  |  |  | rules={{ required: true }} | 
|---|
|  |  |  | defaultValue={tenantList.length > 0 ? tenantList[0].id : ''} | 
|---|
|  |  |  | render={({ field: { onChange, value, ref } }) => { | 
|---|
|  |  |  | const selectedTenant = tenantList.find(tenant => tenant.id === value) || null; | 
|---|
|  |  |  | return ( | 
|---|
|  |  |  | <Autocomplete | 
|---|
|  |  |  | options={tenantList} | 
|---|
|  |  |  | getOptionLabel={(option) => option.name} | 
|---|
|  |  |  | value={selectedTenant} | 
|---|
|  |  |  | onChange={(_, newValue) => { | 
|---|
|  |  |  | const newTenantId = newValue ? newValue.id : ''; | 
|---|
|  |  |  | onChange(newTenantId); | 
|---|
|  |  |  | localStorage.setItem('remember_tenantId', newTenantId); | 
|---|
|  |  |  | }} | 
|---|
|  |  |  | renderInput={(params) => ( | 
|---|
|  |  |  | <TextField | 
|---|
|  |  |  | {...params} | 
|---|
|  |  |  | label={translate("page.login.tenant")} | 
|---|
|  |  |  | variant="standard" | 
|---|
|  |  |  | inputRef={ref} | 
|---|
|  |  |  | /> | 
|---|
|  |  |  | )} | 
|---|
|  |  |  | /> | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | }} | 
|---|
|  |  |  | /> | 
|---|
|  |  |  | )} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | <Controller | 
|---|
|  |  |  | name="username" | 
|---|
|  |  |  | 
|---|
|  |  |  | <Button | 
|---|
|  |  |  | type="submit" | 
|---|
|  |  |  | variant="contained" | 
|---|
|  |  |  | disabled={loading || !(true && username && password)} | 
|---|
|  |  |  | sx={{ | 
|---|
|  |  |  | // backgroundColor: "#3D4BA7" | 
|---|
|  |  |  | }} | 
|---|
|  |  |  | disabled={loading || !((mode === 'OFFLINE' ? tenantId : true) && username && password)} | 
|---|
|  |  |  | > | 
|---|
|  |  |  | {loading && <CircularProgress size={25} thickness={2} />} | 
|---|
|  |  |  | {translate('page.login.button.login')} | 
|---|
|  |  |  | </Button> | 
|---|
|  |  |  |  | 
|---|
|  |  |  | </Stack> | 
|---|
|  |  |  | <Box mt={1} mb={1} sx={{ textAlign: 'center' }}>or</Box> | 
|---|
|  |  |  | {/* <Box mt={1} mb={1} sx={{ textAlign: 'center' }}>or</Box> */} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | <ProviderChoices type="LOG IN" /> | 
|---|
|  |  |  | {/* <ProviderChoices type="LOG IN" /> */} | 
|---|
|  |  |  | </Box > | 
|---|
|  |  |  | </> | 
|---|
|  |  |  | ) | 
|---|