|
|
|
@@ -1,13 +1,18 @@ |
|
|
|
import { clearSessionToken, setSessionToken } from '@/access'; |
|
|
|
import { getCaptchaImg, login } from '@/services/system/auth'; |
|
|
|
import { loginPasswordKey, loginUserKey, rememberPasswordKey } from '@/utils/localStorage'; |
|
|
|
import { safeInvoke } from '@/utils/functional'; |
|
|
|
import { loginUserKey, rememberPasswordKey } from '@/utils/localStorage'; |
|
|
|
import { to } from '@/utils/promise'; |
|
|
|
import { history, useModel } from '@umijs/max'; |
|
|
|
import { Button, Checkbox, Flex, Form, Image, Input, message, type InputRef } from 'antd'; |
|
|
|
import CryptoJS from 'crypto-js'; |
|
|
|
import { useEffect, useRef, useState } from 'react'; |
|
|
|
import { flushSync } from 'react-dom'; |
|
|
|
import styles from './login.less'; |
|
|
|
|
|
|
|
const VERSION = 1; |
|
|
|
const AESKEY = 'OPENSOURCETECHNOLOGYCENTER'; |
|
|
|
|
|
|
|
const LoginInputPrefix = ({ icon }: { icon: string }) => { |
|
|
|
return ( |
|
|
|
<div className={styles['login-input-prefix']}> |
|
|
|
@@ -28,14 +33,22 @@ const Login = () => { |
|
|
|
getCaptchaCode(); |
|
|
|
const autoLogin = localStorage.getItem(rememberPasswordKey) ?? 'false'; |
|
|
|
if (autoLogin === 'true') { |
|
|
|
const username = localStorage.getItem(loginUserKey); |
|
|
|
const password = localStorage.getItem(loginPasswordKey); |
|
|
|
form.setFieldsValue({ username: username, password: password, autoLogin: true }); |
|
|
|
const userStorage = localStorage.getItem(loginUserKey); |
|
|
|
const userJson = safeInvoke((text: string) => |
|
|
|
CryptoJS.AES.decrypt(text, AESKEY).toString(CryptoJS.enc.Utf8), |
|
|
|
)(userStorage); |
|
|
|
const user = safeInvoke(JSON.parse)(userJson); |
|
|
|
if (user && typeof user === 'object' && user.version === VERSION) { |
|
|
|
const { username, password } = user; |
|
|
|
form.setFieldsValue({ username: username, password: password, autoLogin: true }); |
|
|
|
} else { |
|
|
|
form.setFieldsValue({ username: '', password: '', autoLogin: true }); |
|
|
|
localStorage.removeItem(loginUserKey); |
|
|
|
} |
|
|
|
} else { |
|
|
|
form.setFieldsValue({ username: '', password: '', autoLogin: false }); |
|
|
|
} |
|
|
|
}, []); |
|
|
|
|
|
|
|
const getCaptchaCode = async () => { |
|
|
|
const [res] = await to(getCaptchaImg()); |
|
|
|
if (res) { |
|
|
|
@@ -69,11 +82,15 @@ const Login = () => { |
|
|
|
|
|
|
|
localStorage.setItem(rememberPasswordKey, values.autoLogin ? 'true' : 'false'); |
|
|
|
if (values.autoLogin) { |
|
|
|
localStorage.setItem(loginUserKey, values.username ?? ''); |
|
|
|
localStorage.setItem(loginPasswordKey, values.password ?? ''); |
|
|
|
const user = { |
|
|
|
username: values.username, |
|
|
|
password: values.password, |
|
|
|
version: VERSION, |
|
|
|
}; |
|
|
|
const encrypted = CryptoJS.AES.encrypt(JSON.stringify(user), AESKEY).toString(); |
|
|
|
localStorage.setItem(loginUserKey, encrypted); |
|
|
|
} else { |
|
|
|
localStorage.removeItem(loginUserKey); |
|
|
|
localStorage.removeItem(loginPasswordKey); |
|
|
|
} |
|
|
|
|
|
|
|
await fetchUserInfo(); |
|
|
|
|