Commit 6ed561ee authored by Ylg's avatar Ylg

replace loacalStorage with access

parent 5ac257d2
/**
* @see https://umijs.org/zh-CN/plugins/plugin-access
* */
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
console.log(initialState)
const { currentUser } = initialState ?? {};
export default function access(initialState: { userInfo: { token: '' }; }) {
console.log('initialState', initialState)
const { token } = initialState.userInfo ?? {};
return {
canAdmin: currentUser && currentUser.access === 'admin',
token
};
}
......@@ -13,15 +13,17 @@ export const initialStateConfig = {
/**
* @see https://umijs.org/zh-CN/plugins/plugin-initial-state
* */
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);
return {};
}
return { userMessage };
return { userInfo };
}
// ProLayout 支持的api https://procomponents.ant.design/components/layout
......
......@@ -17,6 +17,7 @@ import LoginModal from '@/components/loginModal';
import NewSession from '@/components/newSession';
import { useWebSocket, useRequest } from 'ahooks';
import { GetChatGLMToken } from '@/services/chatGLM/api';
import { useAccess } from 'umi';
const { useBreakpoint } = Grid;
const { Title } = Typography;
......@@ -41,6 +42,8 @@ const Chat: React.FC = () => {
const [open, setOpen] = useState(false);
const tabShow = showSider && !isMobile;
const access = useAccess();
const messageHistory = useRef<IHistory[]>([]);
const { sendMessage } = useWebSocket('wss://glm-api.baibaomen.com/ws', {
onMessage: (event) => {
......@@ -83,15 +86,15 @@ const Chat: React.FC = () => {
messageHistory.current.push({ prompt: chat, answer: '' });
}
const user = JSON.parse(JSON.stringify(localStorage.getItem('user') || ''));
console.log('access', access);
useEffect(() => {
if(user?.token) {
if(access?.token) {
setOpen(false);
} else {
setOpen(true);
}
}, [user]);
}, [access]);
const onSend = () => {
form.setFieldValue('chatInput', '');
......
......@@ -9,7 +9,7 @@ interface ILoginParams {
const request = extend({});
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 = {
Authorization: `Bearer ${token || ''}`,
'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