增加用户令牌保存到 Session

release
Jimmy Liow 2 years ago
parent e76504a581
commit 3ab6efc5d2

@ -3,11 +3,14 @@ import { fetchJSON, postForm } from '@/utils/request';
import { HT_HOST } from "@/config"; import { HT_HOST } from "@/config";
import { prepareUrl } from '@/utils/commons'; import { prepareUrl } from '@/utils/commons';
const KEY_LOGIN_TOKEN = 'KEY_LOGIN_TOKEN';
class Auth { class Auth {
constructor(root) { constructor(root) {
makeAutoObservable(this, { rootStore: false }); makeAutoObservable(this, { rootStore: false });
this.root = root; this.root = root;
this.login.token = root.getSession(KEY_LOGIN_TOKEN);
} }
valdateUserPassword(usr, pwd) { valdateUserPassword(usr, pwd) {
@ -20,6 +23,7 @@ class Auth {
.then(json => { .then(json => {
if (json.errcode == 0) { if (json.errcode == 0) {
this.login.token = json.Result.token; this.login.token = json.Result.token;
this.root.putSession(KEY_LOGIN_TOKEN, json.Result.token);
return json.Result.WU_LMI_SN; return json.Result.WU_LMI_SN;
} else { } else {
throw new Error(json.errmsg + ': ' + json.errcode); throw new Error(json.errmsg + ': ' + json.errcode);
@ -71,7 +75,7 @@ class Auth {
} }
login = { login = {
token: '249FC25C949B4BB182431F89762AE5E8', token: '',//'249FC25C949B4BB182431F89762AE5E8',
userId: 1, // LMI_SN userId: 1, // LMI_SN
username: 'Vu Xuan Giang', username: 'Vu Xuan Giang',
travelAgencyId: 32531, // VEI_SN travelAgencyId: 32531, // VEI_SN

@ -14,6 +14,25 @@ class Root {
this.invoiceStore = new Invoice(this); this.invoiceStore = new Invoice(this);
makeAutoObservable(this); makeAutoObservable(this);
} }
getSession(key) {
if (window.sessionStorage) {
const sessionStorage = window.sessionStorage;
return sessionStorage.getItem(key);
} else {
console.error('browser not support sessionStorage!');
return null;
}
}
putSession(key, value) {
if (window.sessionStorage) {
const sessionStorage = window.sessionStorage;
return sessionStorage.setItem(key, value);
} else {
console.error('browser not support sessionStorage!');
}
}
} }
export default Root; export default Root;

@ -1,10 +1,12 @@
import { Outlet, Link, useHref, useLocation, NavLink } from "react-router-dom"; import { Outlet, Link, useHref, useNavigate, NavLink } from "react-router-dom";
import { useEffect } from "react"; import { useEffect } from "react";
import { observer } from "mobx-react"; import { observer } from "mobx-react";
import { toJS } from "mobx";
import { Layout, Menu, ConfigProvider, theme, Dropdown, Space, Row, Col, Badge, Typography, Divider, App as AntApp } from "antd"; import { Layout, Menu, ConfigProvider, theme, Dropdown, Space, Row, Col, Badge, Typography, Divider, App as AntApp } from "antd";
import { DownOutlined } from "@ant-design/icons"; import { DownOutlined } from "@ant-design/icons";
import "antd/dist/reset.css"; import "antd/dist/reset.css";
import AppLogo from "@/assets/logo-gh.png"; import AppLogo from "@/assets/logo-gh.png";
import { isEmpty } from "@/utils/commons";
import { useStore } from "@/stores/StoreContext.js"; import { useStore } from "@/stores/StoreContext.js";
const { Header, Content, Footer } = Layout; const { Header, Content, Footer } = Layout;
@ -27,13 +29,20 @@ const items = [
key: "3", key: "3",
}, },
]; ];
function App() { function App() {
const { authStore, noticeStore } = useStore(); const { authStore, noticeStore } = useStore();
const { login } = authStore;
const { noticeUnRead } = noticeStore; const { noticeUnRead } = noticeStore;
const href = useHref(); const href = useHref();
const loginToken = toJS(login).token;
const navigate = useNavigate();
useEffect(() => { useEffect(() => {
// Check location // Check location
console.info("href: " + href); console.info("href: " + href + '; login.token: ' + loginToken);
if (href !== '/login' && isEmpty(loginToken)) {
// navigate('/login');
}
}, [href]); }, [href]);
const splitPath = href.split("/"); const splitPath = href.split("/");

Loading…
Cancel
Save