Commit 6ed561ee authored by Ylg's avatar Ylg

replace loacalStorage with access

parent 5ac257d2
/** /**
* @see https://umijs.org/zh-CN/plugins/plugin-access * @see https://umijs.org/zh-CN/plugins/plugin-access
* */ * */
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) { export default function access(initialState: { userInfo: { token: '' }; }) {
console.log(initialState) console.log('initialState', initialState)
const { currentUser } = initialState ?? {}; const { token } = initialState.userInfo ?? {};
return { return {
canAdmin: currentUser && currentUser.access === 'admin', token
}; };
} }
...@@ -13,15 +13,17 @@ export const initialStateConfig = { ...@@ -13,15 +13,17 @@ export const initialStateConfig = {
/** /**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state * @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */ * */
export async function getInitialState() { export async function getInitialState() {
const userMessage = JSON.parse(localStorage.getItem('user')); const userInfo = JSON.parse(localStorage.getItem('user') || '{}');
if (isEmpty(userMessage)) { if (isEmpty(userInfo)) {
history.push(loginPath); history.push(loginPath);
return {}; return {};
} }
return { userMessage }; return { userInfo };
} }
// ProLayout 支持的api https://procomponents.ant.design/components/layout // ProLayout 支持的api https://procomponents.ant.design/components/layout
......
...@@ -17,6 +17,7 @@ import LoginModal from '@/components/loginModal'; ...@@ -17,6 +17,7 @@ import LoginModal from '@/components/loginModal';
import NewSession from '@/components/newSession'; import NewSession from '@/components/newSession';
import { useWebSocket, useRequest } from 'ahooks'; import { useWebSocket, useRequest } from 'ahooks';
import { GetChatGLMToken } from '@/services/chatGLM/api'; import { GetChatGLMToken } from '@/services/chatGLM/api';
import { useAccess } from 'umi';
const { useBreakpoint } = Grid; const { useBreakpoint } = Grid;
const { Title } = Typography; const { Title } = Typography;
...@@ -41,6 +42,8 @@ const Chat: React.FC = () => { ...@@ -41,6 +42,8 @@ const Chat: React.FC = () => {
const [open, setOpen] = useState(false); const [open, setOpen] = useState(false);
const tabShow = showSider && !isMobile; const tabShow = showSider && !isMobile;
const access = useAccess();
const messageHistory = useRef<IHistory[]>([]); const messageHistory = useRef<IHistory[]>([]);
const { sendMessage } = useWebSocket('wss://glm-api.baibaomen.com/ws', { const { sendMessage } = useWebSocket('wss://glm-api.baibaomen.com/ws', {
onMessage: (event) => { onMessage: (event) => {
...@@ -83,15 +86,15 @@ const Chat: React.FC = () => { ...@@ -83,15 +86,15 @@ const Chat: React.FC = () => {
messageHistory.current.push({ prompt: chat, answer: '' }); messageHistory.current.push({ prompt: chat, answer: '' });
} }
const user = JSON.parse(JSON.stringify(localStorage.getItem('user') || '')); console.log('access', access);
useEffect(() => { useEffect(() => {
if(user?.token) { if(access?.token) {
setOpen(false); setOpen(false);
} else { } else {
setOpen(true); setOpen(true);
} }
}, [user]); }, [access]);
const onSend = () => { const onSend = () => {
form.setFieldValue('chatInput', ''); form.setFieldValue('chatInput', '');
......
...@@ -9,7 +9,7 @@ interface ILoginParams { ...@@ -9,7 +9,7 @@ interface ILoginParams {
const request = extend({}); const request = extend({});
request.interceptors.request.use((url, options) => { request.interceptors.request.use((url, options) => {
const token = JSON.parse(JSON.stringify(localStorage.getItem('user') || ''))?.token; const token = JSON.parse(localStorage.getItem('user') || '{}')?.token;
const authHeader = { const authHeader = {
Authorization: `Bearer ${token || ''}`, Authorization: `Bearer ${token || ''}`,
'Content-Type': 'application/json', 'Content-Type': 'application/json',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment