Commit 872c7822 authored by 前端-许佳敏's avatar 前端-许佳敏

权限接口对接

parent 81852234
import React, { useState } from 'react'
import { history } from 'umi'
import { Form, Row, Col, Input, Button } from 'antd'
import {
UserOutlined,
......@@ -7,6 +8,7 @@ import {
} from '@ant-design/icons';
import styles from '../index.less'
import { PublicApi } from '@/services/api';
import { setAuth } from '@/utils/auth';
const LoginWrap: React.FC = () => {
const [validFrame, setValidFrame] = useState(false)
......@@ -15,7 +17,13 @@ const LoginWrap: React.FC = () => {
const finish = (value:any) => {
console.log(value)
PublicApi.postMemberLogin(value).then(res => {
console.log(res)
const { data } = res
setAuth({
memberId: data.memberId,
userId: data.userId,
token: data.token
})
history.push('/')
})
}
......
export interface AuthInfo {
userId: number,
memberId: number,
token: string
}
export const setAuth = (info: AuthInfo) => {
window.localStorage.setItem('auth', JSON.stringify(info))
}
export const getAuth = () => {
try {
return JSON.parse(window.localStorage.getItem('auth'))
} catch (error) {
return {}
}
}
......@@ -2,6 +2,7 @@ import { extend, ResponseError, OnionOptions, RequestOptionsInit, ResponseInterc
import responseCode from '@/constants/responseCode'
import { IRequestError, IRequestSuccess } from '..';
import { message } from 'antd'
import { getAuth } from './auth';
export type CtlType = 'none' | 'message'
// 根前缀请求路径
......@@ -49,7 +50,9 @@ const errorHandler = (error: ResponseError): IRequestError => {
}
const defaultHeaders = {
'Content-Type': 'Application/json'
'Content-Type': 'Application/json',
// 能力中心特定标识, 不可修改
source: '1'
}
/**
......@@ -64,10 +67,20 @@ const baseRequest = extend({
// 请求拦截器
baseRequest.interceptors.request.use((url: string, options: RequestOptionsInit): { url: string, options: RequestOptionsInit } => {
// 判断是否有权限
const loginAfterHeaders = getAuth()
const headers = {
...options.headers,
...loginAfterHeaders
}
return {
// 前缀如果已经带上api, 跳过自动补前缀
url: url.startsWith('/api') ? url : basePrefix + url,
options,
options: {
...options,
headers
},
};
});
......
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