- 添加菜单权限控制

- url 权限控制
This commit is contained in:
sin 2019-03-04 11:32:26 +08:00
parent 17e1a017c9
commit 8a804d654e
5 changed files with 41 additions and 29 deletions

View File

@ -1,4 +1,6 @@
// 代码中会兼容本地 service mock 以及部署站点的静态数据 // 代码中会兼容本地 service mock 以及部署站点的静态数据
// import { stringify } from 'qs';
export default { export default {
// 支持值为 Object 和 Array // 支持值为 Object 和 Array
'GET /api/currentUser': { 'GET /api/currentUser': {
@ -72,28 +74,24 @@ export default {
address: 'Sidney No. 1 Lake Park', address: 'Sidney No. 1 Lake Park',
}, },
], ],
'POST /api/login/account': (req, res) => { 'POST /admin-api/admin/passport/login': (req, res) => {
const { password, userName, type } = req.body; const { password, username } = req.body;
if (password === 'ant.design' && userName === 'admin') { if (password === 'admin' && username === 'admin') {
res.send({ res.send({
status: 'ok', code: 0,
type, data: {
currentAuthority: 'admin', accessToken: '2e3d7635c15e47e997611707a237859f',
}); expiresIn: 2879,
return; refreshToken: 'd091e7c35bbb4313b0f557a6ef23d033',
} },
if (password === 'ant.design' && userName === 'user') { message: 'string',
res.send({
status: 'ok',
type,
currentAuthority: 'user',
}); });
return; return;
} }
res.send({ res.send({
status: 'error', code: 1000,
type, data: {},
currentAuthority: 'guest', message: '账号或密码错误!',
}); });
}, },
'POST /api/register': (req, res) => { 'POST /api/register': (req, res) => {

View File

@ -4,6 +4,7 @@ import { fakeAccountLogin, getFakeCaptcha } from '@/services/api';
import { setAuthority } from '@/utils/authority'; import { setAuthority } from '@/utils/authority';
import { getPageQuery } from '@/utils/utils'; import { getPageQuery } from '@/utils/utils';
import { reloadAuthorized } from '@/utils/Authorized'; import { reloadAuthorized } from '@/utils/Authorized';
import { setLoginToken } from '../utils/cache';
export default { export default {
namespace: 'login', namespace: 'login',
@ -19,8 +20,15 @@ export default {
type: 'changeLoginStatus', type: 'changeLoginStatus',
payload: response, payload: response,
}); });
yield put(routerRedux.replace('/'));
// Login successfully // Login successfully
if (response.status === 'ok') { if (response.code === 0) {
// 保存 token 到 localStorage发送请求的时候会自动取 token 放到 header
setLoginToken(response.data.accessToken, response.data.refreshToken);
// 此处直接设置为 admin、和 user 角色,因为暂时不做服务控制前段 角色
setAuthority(['admin', 'user']);
reloadAuthorized(); reloadAuthorized();
const urlParams = new URL(window.location.href); const urlParams = new URL(window.location.href);
const params = getPageQuery(); const params = getPageQuery();

View File

@ -4,10 +4,16 @@ import { getAuthority } from '@/utils/authority';
import Redirect from 'umi/redirect'; import Redirect from 'umi/redirect';
const Authority = getAuthority(); const Authority = getAuthority();
// TODO RenderAuthorized 暂时写死为 admin次组件集成于 antd-pro 后期有时间处理,(可能有用,可能没用)
// TODO 可大致分为两种角色admin 管理员角色user 代表其他非授权页面,可以公开的
// const Authorized = RenderAuthorized(['admin', 'user']);
const Authorized = RenderAuthorized(Authority); const Authorized = RenderAuthorized(Authority);
export default ({ children }) => ( export default ({ children }) => {
return (
<Authorized authority={children.props.route.authority} noMatch={<Redirect to="/user/login" />}> <Authorized authority={children.props.route.authority} noMatch={<Redirect to="/user/login" />}>
{children} {children}
</Authorized> </Authorized>
); );
};

View File

@ -24,7 +24,7 @@ class LoginPage extends Component {
onGetCaptcha = () => onGetCaptcha = () =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
this.loginForm.validateFields(['mobile'], {}, (err, values) => { this.loginForm.validateFields(['username'], {}, (err, values) => {
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
@ -82,7 +82,7 @@ class LoginPage extends Component {
!submitting && !submitting &&
this.renderMessage(formatMessage({ id: 'app.login.message-invalid-credentials' }))} this.renderMessage(formatMessage({ id: 'app.login.message-invalid-credentials' }))}
<UserName <UserName
name="userName" name="username"
placeholder={`${formatMessage({ id: 'app.login.userName' })}: admin or user`} placeholder={`${formatMessage({ id: 'app.login.userName' })}: admin or user`}
rules={[ rules={[
{ {
@ -93,7 +93,7 @@ class LoginPage extends Component {
/> />
<Password <Password
name="password" name="password"
placeholder={`${formatMessage({ id: 'app.login.password' })}: ant.design`} placeholder={`${formatMessage({ id: 'app.login.password' })}: admin`}
rules={[ rules={[
{ {
required: true, required: true,
@ -114,7 +114,7 @@ class LoginPage extends Component {
formatMessage({ id: 'app.login.message-invalid-verification-code' }) formatMessage({ id: 'app.login.message-invalid-verification-code' })
)} )}
<Mobile <Mobile
name="mobile" name="username"
placeholder={formatMessage({ id: 'form.phone-number.placeholder' })} placeholder={formatMessage({ id: 'form.phone-number.placeholder' })}
rules={[ rules={[
{ {
@ -128,7 +128,7 @@ class LoginPage extends Component {
]} ]}
/> />
<Captcha <Captcha
name="captcha" name="password"
placeholder={formatMessage({ id: 'form.verification-code.placeholder' })} placeholder={formatMessage({ id: 'form.verification-code.placeholder' })}
countDown={120} countDown={120}
onGetCaptcha={this.onGetCaptcha} onGetCaptcha={this.onGetCaptcha}

View File

@ -104,7 +104,7 @@ export async function updateFakeList(params) {
} }
export async function fakeAccountLogin(params) { export async function fakeAccountLogin(params) {
return request('/api/login/account', { return request(`/admin-api/admin/passport/login/?${stringify(params)}`, {
method: 'POST', method: 'POST',
body: params, body: params,
}); });