tianyunperfect 5 år sedan
förälder
incheckning
74377df0ca

+ 3 - 3
src/components/GlobalHeader/AvatarDropdown.tsx

@@ -36,7 +36,7 @@ class AvatarDropdown extends React.Component<GlobalHeaderRightProps> {
     const {
       currentUser = {
         avatar: '',
-        name: '',
+        userName: '',
       },
       menu,
     } = this.props;
@@ -62,11 +62,11 @@ class AvatarDropdown extends React.Component<GlobalHeaderRightProps> {
         </Menu.Item>
       </Menu>
     );
-    return currentUser && currentUser.name ? (
+    return currentUser && currentUser.userName ? (
       <HeaderDropdown overlay={menuHeaderDropdown}>
         <span className={`${styles.action} ${styles.account}`}>
           <Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
-          <span className={styles.name}>{currentUser.name}</span>
+          <span className={styles.name}>{currentUser.userName}</span>
         </span>
       </HeaderDropdown>
     ) : (

+ 2 - 1
src/layouts/SecurityLayout.tsx

@@ -4,6 +4,7 @@ import { Redirect, connect, ConnectProps } from 'umi';
 import { stringify } from 'querystring';
 import { ConnectState } from '@/models/connect';
 import { CurrentUser } from '@/models/user';
+import {isTokenExist} from "@/utils/tokenUtil";
 
 interface SecurityLayoutProps extends ConnectProps {
   loading?: boolean;
@@ -36,7 +37,7 @@ class SecurityLayout extends React.Component<SecurityLayoutProps, SecurityLayout
     const { children, loading, currentUser } = this.props;
     // You can replace it to your authentication rule (such as check token exists)
     // 你可以把它替换成你自己的登录认证规则(比如判断 token 是否存在)
-    const isLogin = currentUser && currentUser.userid;
+    const isLogin = isTokenExist();
     const queryString = stringify({
       redirect: window.location.href,
     });

+ 7 - 14
src/models/user.ts

@@ -1,19 +1,12 @@
-import { Effect, Reducer } from 'umi';
+import {Effect, Reducer} from 'umi';
 
-import { queryCurrent, query as queryUsers } from '@/services/user';
+import {queryCurrent, query as queryUsers} from '@/services/user';
 
 export interface CurrentUser {
   avatar?: string;
-  name?: string;
-  title?: string;
-  group?: string;
-  signature?: string;
-  tags?: {
-    key: string;
-    label: string;
-  }[];
-  userid?: string;
-  unreadCount?: number;
+  userName?: string;
+  id?: number
+  roleId?: number
 }
 
 export interface UserModelState {
@@ -41,14 +34,14 @@ const UserModel: UserModelType = {
   },
 
   effects: {
-    *fetch(_, { call, put }) {
+    * fetch(_, {call, put}) {
       const response = yield call(queryUsers);
       yield put({
         type: 'save',
         payload: response,
       });
     },
-    *fetchCurrent(_, { call, put }) {
+    * fetchCurrent(_, {call, put}) {
       const response = yield call(queryCurrent);
       yield put({
         type: 'saveCurrentUser',

+ 1 - 1
src/pages/Authorized.tsx

@@ -20,7 +20,7 @@ const AuthComponent: React.FC<AuthComponentProps> = ({
 }) => {
   const { currentUser } = user;
   const { routes = [] } = route;
-  const isLogin = currentUser && currentUser.name;
+  const isLogin = currentUser && currentUser.userName;
   return (
     <Authorized
       authority={getRouteAuthority(location.pathname, routes) || ''}

+ 7 - 6
src/utils/request.ts

@@ -2,8 +2,9 @@
  * request 网络请求工具
  * 更详细的 api 文档: https://github.com/umijs/umi-request
  */
-import { extend } from 'umi-request';
-import { notification } from 'antd';
+import {extend} from 'umi-request';
+import {notification} from 'antd';
+import {getToken, setToken} from "@/utils/tokenUtil";
 
 const codeMessage = {
   200: '服务器成功返回请求的数据。',
@@ -27,10 +28,10 @@ const codeMessage = {
  * 异常处理程序
  */
 const errorHandler = (error: { response: Response }): Response => {
-  const { response } = error;
+  const {response} = error;
   if (response && response.status) {
     const errorText = codeMessage[response.status] || response.statusText;
-    const { status, url } = response;
+    const {status, url} = response;
 
     notification.error({
       message: `请求错误 ${status}: ${url}`,
@@ -53,7 +54,7 @@ const request = extend({
   credentials: 'include', // 默认请求是否带上cookie
   // 默认请求头
   headers: {
-    authorization: localStorage.getItem('authorization'), // 携带token
+    authorization: getToken(), // 携带token
   },
 });
 
@@ -62,7 +63,7 @@ const request = extend({
 request.interceptors.response.use((response, options) => {
   let token = response.headers.get("authorization");
   if (token) {
-    localStorage.setItem("authorization", token);
+    setToken(token);
   }
   return response;
 });

+ 13 - 0
src/utils/tokenUtil.ts

@@ -0,0 +1,13 @@
+const tokenKeyStr = 'authorization';
+
+export function getToken() {
+  return localStorage.getItem(tokenKeyStr);
+}
+
+export function setToken(token: string) {
+  localStorage.setItem(tokenKeyStr, token)
+}
+
+export function isTokenExist() {
+  return getToken() !== null;
+}