Commit 45b2a018 authored by superman's avatar superman

ok

parent 5a303517
This diff is collapsed.
...@@ -2,6 +2,6 @@ ...@@ -2,6 +2,6 @@
// - https://github.com/dora-js/dora-plugin-proxy#规则定义 // - https://github.com/dora-js/dora-plugin-proxy#规则定义
module.exports = { module.exports = {
// '/api/*': 'http://react.yanky.cn/', '/api/*': 'http://react.yanky.cn/',
'/api/*': 'http://192.168.1.126:8080/' // '/api/*': 'http://192.168.1.126:8080/'
}; };
...@@ -21,6 +21,17 @@ export default class App extends Component { ...@@ -21,6 +21,17 @@ export default class App extends Component {
store: PropTypes.object.isRequired store: PropTypes.object.isRequired
}; };
componentDidMount(){
this.fetchAuthorityList();
}
fetchAuthorityList() {
this.props.dispatch({
type: 'FETCH_AUTHORITY_LIST',
query: {s: 100}
});
};
render() { render() {
const styles = require('./App.less'); const styles = require('./App.less');
const menu = [ const menu = [
......
...@@ -43,6 +43,9 @@ import { ...@@ -43,6 +43,9 @@ import {
export default class EditItem extends Component { export default class EditItem extends Component {
constructor() { constructor() {
super(...arguments); super(...arguments);
this.state = {
authoritiesCheckedValues: []
}
} }
componentDidMount() { componentDidMount() {
...@@ -50,27 +53,14 @@ export default class EditItem extends Component { ...@@ -50,27 +53,14 @@ export default class EditItem extends Component {
this.fetchAuthorityList(); this.fetchAuthorityList();
}; };
// componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
// if (nextProps.item && nextProps.authorities) { if ((nextProps.isEdit != this.props.isEdit) && nextProps.item && nextProps.item.authorities) {
// this.analysisCheckedKeys(nextProps); this.setState({
// } authoritiesCheckedValues: nextProps.item.authorities.split(',')
// }; });
// }
// analysisCheckedKeys(props) {
// const {item, authorities, isEdit} = props; }
// //对权限资源分组
// const authoritiesMap = {};
// const checkedKeys = [];
// authorities && authorities.forEach(authority=> {
// authoritiesMap[authority.name] = authority;
//
// });
//
// this.setState({
// authoritiesMap,
// checkedKeys
// });
// }
fetchItem(id) { fetchItem(id) {
this.props.dispatch({ this.props.dispatch({
...@@ -98,10 +88,11 @@ export default class EditItem extends Component { ...@@ -98,10 +88,11 @@ export default class EditItem extends Component {
} }
}) })
} }
data.authorities = this.state.authoritiesCheckedValues.join();
console.log(data); console.log(data);
this.props.dispatch({ this.props.dispatch({
type: 'UPDATE_USER_ITEM', type: 'UPDATE_USER_ITEM',
data item: data
}); });
} }
...@@ -143,21 +134,20 @@ export default class EditItem extends Component { ...@@ -143,21 +134,20 @@ export default class EditItem extends Component {
/> />
); );
const authoritiesMap = authorities ? authorities.reduce((map, authority)=> { const authoritiesMap = {};
map[authority.name] = authority; const userAuthorities = [];
return map; if (item && authorities) {
}, {}) : {}; authorities.forEach(authority=> {
authority.label = authority.description + '(' + authority.name + ')';
if (item) { authority.value = authority.name;
item.authorities = item.authorities.split(','); authoritiesMap[authority.name] = authority.label;
});
item.authoritiesDescList = []; if (item.authorities) {
const uas = item.authorities.split(',');
// item.authoritiesMap = item.authorities.reduce((map, name)=> { uas.forEach(name=> {
// const authority = authoritiesMap[name]; userAuthorities.push(authoritiesMap[name]);
// map[name] = authority; });
// item.authoritiesDescList.push(authority.description); }
// }, {});
} }
return ( return (
...@@ -166,8 +156,7 @@ export default class EditItem extends Component { ...@@ -166,8 +156,7 @@ export default class EditItem extends Component {
{ {
item && item &&
<Form className="main-form" horizontal> <Form className="main-form" horizontal>
<Row>
<Col span="12">
<Form.Item label="用户名" {...smallFormItemLayout}> <Form.Item label="用户名" {...smallFormItemLayout}>
{ item.username } { item.username }
</Form.Item> </Form.Item>
...@@ -184,7 +173,17 @@ export default class EditItem extends Component { ...@@ -184,7 +173,17 @@ export default class EditItem extends Component {
} }
</Form.Item> </Form.Item>
<Form.Item label="角色" {...smallFormItemLayout}> <Form.Item label="角色" {...smallFormItemLayout}>
{item.authoritiesDescList.join()} { isEdit ?
<Checkbox.Group options={authorities}
value={this.state.authoritiesCheckedValues}
onChange={(checkedValues)=>{
this.setState({
authoritiesCheckedValues: checkedValues
})
}}/>
:
(userAuthorities.length ? userAuthorities.join(',') : NULL)
}
</Form.Item> </Form.Item>
<Form.Item label="昵称" {...smallFormItemLayout}> <Form.Item label="昵称" {...smallFormItemLayout}>
{ {
...@@ -244,11 +243,7 @@ export default class EditItem extends Component { ...@@ -244,11 +243,7 @@ export default class EditItem extends Component {
{isEdit ? '取消' : '返回'} {isEdit ? '取消' : '返回'}
</Button> </Button>
</Form.Item> </Form.Item>
</Col>
<Col span="12">
</Col>
</Row>
</Form> </Form>
} }
</Spin> </Spin>
......
...@@ -31,10 +31,13 @@ const columns = [ ...@@ -31,10 +31,13 @@ const columns = [
) )
}, { }, {
title: '角色', title: '角色',
dataIndex: 'buyerName', dataIndex: 'authorities',
key: 'buyerName', key: 'authorities',
width: 200, width: 200,
className: 'tac', className: 'tac',
render:(authorities)=>(
<span>{authorities}</span>
)
}, { }, {
title: 'E-mail', title: 'E-mail',
dataIndex: 'email', dataIndex: 'email',
...@@ -57,7 +60,7 @@ const columns = [ ...@@ -57,7 +60,7 @@ const columns = [
]; ];
@connect(state=>({ @connect(state=>({
items: state.user.items, items: state.user.list,
loading: state.user.loading, loading: state.user.loading,
total: state.user.total, total: state.user.total,
})) }))
......
...@@ -37,17 +37,17 @@ const user = handleActions({ ...@@ -37,17 +37,17 @@ const user = handleActions({
return {...state, loading: true}; return {...state, loading: true};
}, },
['FETCH_USER_LIST_SUCCESS'](state, action){ ['FETCH_USER_LIST_SUCCESS'](state, action){
return {...state, loading: false, items: action.items, total: action.total}; return {...state, loading: false, ...action.result};
}, },
['FETCH_USER_LIST_FAILED'](state, action){ ['FETCH_USER_LIST_FAILED'](state, action){
return {...state, loading: false, err: action.err}; return {...state, loading: false, err: action.err};
}, },
['FETCH_USER_ITEM'](state){ ['FETCH_USER_ITEM'](state){
return {...state, loading:true}; return {...state, loading: true};
}, },
['FETCH_USER_ITEM_SUCCESS'](state, action){ ['FETCH_USER_ITEM_SUCCESS'](state, action){
return {...state, loading: false, item: action.item}; return {...state, loading: false, item: action.result};
}, },
['FETCH_USER_ITEM_FAILED'](state, action){ ['FETCH_USER_ITEM_FAILED'](state, action){
return {...state, loading: false, err: action.err}; return {...state, loading: false, err: action.err};
...@@ -60,13 +60,13 @@ const user = handleActions({ ...@@ -60,13 +60,13 @@ const user = handleActions({
return {...state, isEdit: false} return {...state, isEdit: false}
}, },
['UPDATE_USER_ITEM'](state){ ['UPDATE_USER_ITEM'](state){
return {...state, loading:true}; return {...state, loading: true};
}, },
['UPDATE_USER_ITEM_SUCCESS'](state, action){ ['UPDATE_USER_ITEM_SUCCESS'](state, action){
return {...state, loading: false, item: {...state.item, ...action.item}, isEdit: false}; return {...state, loading: false, item: {...state.item, ...action.item}, isEdit: false};
}, },
['UPDATE_USER_ITEM_FAILED'](state, action){ ['UPDATE_USER_ITEM_FAILED'](state, action){
return {...state, loading: false, err: action.err, isEdit:false}; return {...state, loading: false, err: action.err, isEdit: false};
}, },
}, { }, {
......
/*import {takeLatest} from 'redux-saga'; import {takeLatest} from 'redux-saga';
import {take, call, put, fork, cancel, select, race} from 'redux-saga/effects'; import {take, call, put, fork, cancel, select, race} from 'redux-saga/effects';
import {message} from 'antd'; import {message} from 'antd';
export default class RESTful { export function index(id, api){
constructor(id) { return function* (query) {
this.id = id;
}
index() {
const {api, id} = this;
return function*(query) {
try { try {
const result = yield call(api.index, query); const result = yield call(api, query);
yield put({ yield put({
type: 'FETCH_' + id + '_LIST_SUCCESS', type: 'FETCH_' + id + '_LIST_SUCCESS',
result result
...@@ -25,50 +19,82 @@ export default class RESTful { ...@@ -25,50 +19,82 @@ export default class RESTful {
}); });
} }
} }
} }
watchIndex() { export function watchIndex(id, api){
const self = this;
return function*() { return function*() {
while (true) { while (true) {
const {query} = yield take('FETCH_' + self.id + '_LIST'); const {query} = yield take('FETCH_' + id + '_LIST');
yield fork(self.index(), query); yield fork(index(id, api), query);
} }
}; };
} }
show() {
const self = this;
const {api} = this;
export function show(rid, api) {
return function*(id) { return function*(id) {
try { try {
const result = yield call(api.show, id); const result = yield call(api, id);
yield put({ yield put({
type: 'FETCH_' + self.id + '_ITEM_SUCCESS', type: 'FETCH_' + rid + '_ITEM_SUCCESS',
result result
}); });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
message.error(err); message.error(err);
yield put({ yield put({
type: 'FETCH_' + self.id + '_ITEM_FAILED', type: 'FETCH_' + rid + '_ITEM_FAILED',
err err
}); });
} }
} }
}
export function watchShow(rid, api) {
return function*() {
while (true) {
const {id} = yield take('FETCH_' + rid + '_ITEM');
yield fork(show(rid, api), id);
}
} }
}
watchShow() { export function create(id, api) {
const self = this; return function*(data) {
try {
const result = yield call(api, data);
yield put({
type: 'CREATE_' + id + '_ITEM_SUCCESS',
result
});
} catch (err) {
console.log(err);
message.error(err);
yield put({
type: 'CREATE_' + id + '_ITEM_FAILED',
err
});
}
}
}
export function watchCreate(id, api) {
return function*() { return function*() {
while (true) { while (true) {
const {id} = yield take('FETCH_' + self.id + '_ITEM'); const {data} = yield take('CREATE_' + id + '_ITEM');
yield fork(self.show(), id); yield fork(create(id, api), data);
} }
} }
}
/*
export default class RESTful {
constructor(id) {
this.id = id;
} }
create() { create() {
const {api, id} = this; const {api, id} = this;
return function*(data) { return function*(data) {
......
...@@ -2,6 +2,8 @@ import {takeLatest} from 'redux-saga'; ...@@ -2,6 +2,8 @@ import {takeLatest} from 'redux-saga';
import {take, call, put, fork, cancel, race } from 'redux-saga/effects'; import {take, call, put, fork, cancel, race } from 'redux-saga/effects';
import {fetch, clear, save, create, fetchList, fetchItem, updateItem} from '../services/user'; import {fetch, clear, save, create, fetchList, fetchItem, updateItem} from '../services/user';
import {message} from 'antd'; import {message} from 'antd';
import {watchIndex, watchShow, watchCreate} from './RESTful';
function* authorize(username, password, push) { function* authorize(username, password, push) {
try { try {
...@@ -43,58 +45,13 @@ function* watchAddItem() { ...@@ -43,58 +45,13 @@ function* watchAddItem() {
} }
} }
function* getList(query) {
try {
const {list, total} = yield call(fetchList, query);
yield put({
type: 'FETCH_USER_LIST_SUCCESS',
items: list,
total
});
} catch (err) {
console.log(err);
message.error(err);
yield put({type: 'FETCH_USER_LIST_FAILED', err});
}
}
function* watchList() {
while (true) {
const {query} = yield take('FETCH_USER_LIST');
yield fork(getList, query);
}
}
function* getItem(id) {
try {
const item = yield call(fetchItem, id);
yield put({
type: 'FETCH_USER_ITEM_SUCCESS',
item: item
});
} catch (err) {
console.log(err);
message.error(err);
yield put({
type: 'FETCH_USER_ITEM_FAILED',
err
});
}
}
function* watchItem() {
while (true) {
const {id} = yield take('FETCH_USER_ITEM');
yield fork(getItem, id);
}
}
function* modifyItem(data) { function* modifyItem(item) {
try { try {
yield call(updateItem, data); yield call(updateItem, item);
yield put({ yield put({
type: 'UPDATE_USER_ITEM_SUCCESS', type: 'UPDATE_USER_ITEM_SUCCESS',
item: data item
}); });
} catch (err) { } catch (err) {
console.log(err); console.log(err);
...@@ -130,9 +87,9 @@ function* watchMidfiyItem(){ ...@@ -130,9 +87,9 @@ function* watchMidfiyItem(){
export default function*() { export default function*() {
yield fork(loginFlow); yield fork(loginFlow);
yield fork(watchAddItem); yield fork(watchCreate('USER', create));
yield fork(watchList); yield fork(watchIndex('USER', fetchList));
yield fork(watchItem); yield fork(watchShow('USER', fetchItem));
yield fork(watchMidfiyItem); yield fork(watchMidfiyItem);
} }
import xFetch from './xFetch';
const storage = sessionStorage;
const PREFIX = 'shuniu-cache-';
export default (url, options, time = 30 * 60 * 1000)=> {
if (!options) {
let cache = storage[PREFIX + url];
if (cache) {
try {
cache = JSON.parse(cache);
if (cache.time - Date.now() < time) {
return Promise.resolve(cache.data);
}
} catch (err) {
console.error(err);
clearCache(url);
}
}
} else {
let index = url.indexOf('?');
if (index != -1) {
clearCache(url.substr(0, index));
} else {
index = url.lastIndexOf('/');
clearCache(url.substr(0, index));
}
}
return xFetch(url).then(result=> {
for (let i = 0; i < 3; i++) {
try {
storage[PREFIX + url] = JSON.stringify({
time: Date.now(),
data: result
});
break;
} catch (err) {
console.error(err);
clearCache();
}
}
return result;
});
}
function clearCache(id) {
console.log('clear cache ' + id);
Object.keys(storage).forEach(key=> {
if (key.indexOf(PREFIX + id) === 0) {
storage.removeItem(key);
}
});
}
import xFetch from '../xFetch'; import xFetch from '../xFetch';
import {serialize} from '../../utils'; import {serialize} from '../../utils';
import storage from '../storage';
export async function fetchList(query) { export async function fetchList(query) {
return xFetch('/api/admin/authorities' + '?' + serialize({s:30, ...query})); return xFetch('/api/admin/authorities' + '?' + serialize({s: 30, ...query}));
} }
export async function fetchItem(id) { export async function fetchItem(id) {
...@@ -10,8 +11,9 @@ export async function fetchItem(id) { ...@@ -10,8 +11,9 @@ export async function fetchItem(id) {
} }
export async function updateItem(item) { export async function updateItem(item) {
return xFetch('/api/admin/authorities/'+item.id, { return xFetch('/api/admin/authorities/' + item.id, {
method: 'PUT', method: 'PUT',
body: serialize(item) body: serialize(item)
}); });
} }
//sessionStorage
import xFetch from '../xFetch'; import xFetch from '../xFetch';
import {serialize} from '../../utils'; import {serialize} from '../../utils';
import storage from '../storage';
export async function fetchList(query) { export async function fetchList(query) {
return xFetch('/api/admin/resources' + '?' + serialize({s:30, ...query})); return xFetch('/api/admin/resources' + '?' + serialize({s:30, ...query}));
......
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