feat: user module in the client

This commit is contained in:
suxiaoxin 2017-07-19 13:58:12 +08:00
parent fa7f90bb3b
commit deeccfa44e
13 changed files with 112 additions and 113 deletions

View File

@ -1,68 +1,94 @@
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import { Input, Row, Col } from 'antd'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { Row, Col, Menu, AutoComplete, Input, Icon } from 'antd'
import axios from 'axios'
@connect(
state => {
return {
curUid: state.user.curUid
}
}
)
const Option = AutoComplete.Option;
class LeftMenu extends Component {
static propTypes = {
curUid: PropTypes.string
}
constructor(props) {
super(props)
this.state = {
dataSource: []
}
this.searchSign = 0;
this._searchSign = 0;
this.interval = null;
}
handleActive = () => {
this.setState({
______a: 1 //强制刷新
})
//延迟搜索
handleSearch = (value) => {
if(!value || value.length < 1) return ;
this.searchSign = this.searchSign + 1;
this.interval && clearInterval(this.interval)
this.interval = setInterval(() => {
if (this.searchSign === this._searchSign) {
this.interval = clearInterval(this.interval)
axios.get('/user/search?q=' + value).then((res) => {
if (res.data.errcode === 0) {
this.setState({
dataSource: res.data.data
})
}
})
} else {
this._searchSign = this.searchSign;
}
}, 600)
}
renderOption = (item) => {
return (
<Option key={item.uid} text={item.username} >
<Link to={"/user/profile/" + item.uid} > {item.username} </Link>
</Option>
)
}
render() {
const menus = [{
title: '个人资料',
path: "/user/profile/" + this.props.curUid
path: "/user/profile/" + 107
}, {
title: '用户管理',
path: '/user/list'
}
]
let content = menus.map((menu, index) => {
if (typeof menu.auth === 'function' && menu.auth('admin') === false) {
return '';
}
let content = menus.map((menu) => {
return (
<li onClick={this.handleActive} key={index} className={location.hash === '#' + menu.path ? 'active' : ''}>
<Menu.Item key={'#' + menu.path} >
<Link to={menu.path} >{menu.title}</Link>
</li>
</Menu.Item>
)
})
const Search = Input.Search;
return (<div>
const { dataSource } = this.state;
return (<div className="user-list">
<Row type="flex" justify="start" className="search">
<Col span="24">
<Search
placeholder="搜索用户"
onSearch={value => console.log(value)}
/>
<div className="certain-category-search-wrapper" style={{ width: "100%" }}>
<AutoComplete
className="certain-category-search"
dropdownClassName="certain-category-search-dropdown"
size="large"
style={{ width: '100%' }}
dataSource={dataSource.map(this.renderOption)}
onSearch={this.handleSearch}
placeholder="input here"
optionLabelProp="text"
>
<Input suffix={<Icon type="search" className="certain-category-icon" />} />
</AutoComplete>
</div>
</Col>
</Row>
<ul className="user-list">
<Menu defaultSelectedKeys={[location.hash]}>
{content}
</ul>
</Menu>
</div>
)
}

View File

@ -91,7 +91,7 @@ class List extends Component {
return (
<span>
<Button type="primary"><Link to={"/user/profile/" + item._id} > 查看 </Link></Button>
<Popconfirm placement="leftTop" title="Are you sure delete this task?" onConfirm={() => {this.confirm(item._id)}} okText="Yes" cancelText="No">
<Popconfirm placement="leftTop" title="确认删除此用户?" onConfirm={() => {this.confirm(item._id)}} okText="Yes" cancelText="No">
<Button type="danger">删除</Button>
</Popconfirm>
</span>

View File

@ -1,27 +1,13 @@
import React, { Component } from 'react'
import { Row, Col, Icon, Input, Button, Select, message } from 'antd'
import axios from 'axios';
import { connect } from 'react-redux'
import {formatTime} from '../../common.js'
import {
changeCurUid
} from '../../actions/user.js'
import PropTypes from 'prop-types'
@connect(
state => ({
curUid: state.user.curUid
}),
{
changeCurUid
}
)
class Profile extends Component {
static propTypes = {
curUid: PropTypes.string,
changeCurUid: PropTypes.func,
match: PropTypes.object
}
@ -41,7 +27,6 @@ class Profile extends Component {
componentDidMount(){
const uid = this.props.match.params.uid;
this.props.changeCurUid(uid)
this.getUserInfo(uid)
}

View File

@ -7,55 +7,21 @@
font-size: 0.14rem;
background: #FFF;
min-height:500px;
margin-top: 64px;
margin-top: 84px;
.search{
height: 40px;
padding: 5px;
background: #f9f9f9;
input{
background: #f9f9f9;
line-height: 40px;
height: 40px;
border: 0;
font-size: 14px;
}
input:focus{
outline:0;
border-color:0;
box-shadow: none
}
}
ul{border:none}
.user-list {
width: 216px;
line-height: 45px;
background: #f9fafe;
li {
padding: 0 0 0 30px;
a{
color: #344562;
}
&.active,&.active a {
background: #657289;
color: #FFF;
text-decoration: none;
}
.search{
margin: 5px;
}
}
.user-table {
-webkit-box-flex: 1;
padding-right: 15px;
margin-left: 15px;
.ant-table-wrapper table {
font-size: .14rem;

17
doc/build/api.html vendored
View File

@ -771,7 +771,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#215" target="_blank">./server/controllers/user.js:215</a>
<a href="./static/server/controllers/user.js.html#224" target="_blank">./server/controllers/user.js:224</a>
</p>
@ -868,7 +868,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#277" target="_blank">./server/controllers/user.js:277</a>
<a href="./static/server/controllers/user.js.html#286" target="_blank">./server/controllers/user.js:286</a>
</p>
@ -938,7 +938,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#309" target="_blank">./server/controllers/user.js:309</a>
<a href="./static/server/controllers/user.js.html#315" target="_blank">./server/controllers/user.js:315</a>
</p>
@ -1000,7 +1000,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#338" target="_blank">./server/controllers/user.js:338</a>
<a href="./static/server/controllers/user.js.html#350" target="_blank">./server/controllers/user.js:350</a>
</p>
@ -1062,7 +1062,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#362" target="_blank">./server/controllers/user.js:362</a>
<a href="./static/server/controllers/user.js.html#377" target="_blank">./server/controllers/user.js:377</a>
</p>
@ -1154,7 +1154,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#408" target="_blank">./server/controllers/user.js:408</a>
<a href="./static/server/controllers/user.js.html#426" target="_blank">./server/controllers/user.js:426</a>
</p>
@ -1197,13 +1197,14 @@
<div>示例:</div>
<pre class="ydoc-example" data-foldnumber=10><code><span class="token punctuation">{</span>
<span class="token string">"errcode"</span><span class="token punctuation">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
<span class="token string">"errcode"</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token string">"errmsg"</span><span class="token punctuation">:</span> <span class="token string">"ok"</span><span class="token punctuation">,</span>
<span class="token string">"data"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token string">"uid"</span><span class="token punctuation">:</span> <span class="token number">101</span><span class="token punctuation">,</span>
<span class="token string">"email"</span><span class="token punctuation">:</span> <span class="token string">"admin@admin.com"</span><span class="token punctuation">,</span>
<span class="token string">"role"</span><span class="token punctuation">:</span> <span class="token string">"admin"</span><span class="token punctuation">,</span>
<span class="token string">"username"</span><span class="token punctuation">:</span> <span class="token string">"admin"</span><span class="token punctuation">,</span>
<span class="token string">"addTime"</span><span class="token punctuation">:</span> <span class="token number">1499936103</span><span class="token punctuation">,</span>
<span class="token string">"upTime"</span><span class="token punctuation">:</span> <span class="token number">1499936103</span>
<span class="token punctuation">}</span>
@ -2086,7 +2087,7 @@
<div>示例:</div>
<pre class="ydoc-example" data-foldnumber=10><code><span class="token punctuation">{</span>
<span class="token string">"errcode"</span><span class="token punctuation">:</span> <span class="token number">200</span><span class="token punctuation">,</span>
<span class="token string">"errcode"</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token string">"errmsg"</span><span class="token punctuation">:</span> <span class="token string">"ok"</span><span class="token punctuation">,</span>
<span class="token string">"data"</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
<span class="token string">"project"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>

View File

@ -410,7 +410,7 @@ class projectController extends baseController {
group: groupList
};
return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok')
return ctx.body = yapi.commons.resReturn(queryList, 0, 'ok')
}
}

View File

@ -190,8 +190,17 @@ class userController extends baseController {
async changePassword(ctx) {
let params = ctx.request.body;
let userInst = yapi.getInst(userModel);
if (!params.uid) {
return ctx.body = yapi.commons.resReturn(null, 400, 'uid不能为空');
}
if (!params.password) {
return ctx.body = yapi.commons.resReturn(null, 400, '密码不能为空');
}
if (this.getRole() !== 'admin' && params.uid != this.getUid()) {
console.log(this.getRole(), this.getUid());
return ctx.body = yapi.commons.resReturn(null, 402, '没有权限');
}
if (this.getRole() !== 'admin') {
@ -317,10 +326,7 @@ class userController extends baseController {
async list(ctx){
let page = ctx.request.query.page || 1,
limit = ctx.request.query.limit || 10;
if(this.getRole() !== 'admin'){
return ctx.body = yapi.commons.resReturn(null, 402, '没有权限');
}
const userInst = yapi.getInst(userModel);
try {
let user = await userInst.listWithPaging(page, limit);
@ -349,7 +355,13 @@ class userController extends baseController {
try {
var userInst = yapi.getInst(userModel);
let id = ctx.request.query.id;
if (!id) {
return ctx.body = yapi.commons.resReturn(null, 400, 'uid不能为空');
}
let result = await userInst.findById(id);
if(!result){
return ctx.body = yapi.commons.resReturn(null,402,"不存在的用户");
}
return ctx.body = yapi.commons.resReturn({
uid: result._id,
username: result.username,
@ -374,12 +386,15 @@ class userController extends baseController {
* @example
*/
async del(ctx) { //根据id删除一个用户
try {
try {
if (this.getRole() !== 'admin') {
return ctx.body = yapi.commons.resReturn(null, 402, 'Without permission.');
}
var userInst = yapi.getInst(userModel);
let id = ctx.request.body.id;
if (!id) {
return ctx.body = yapi.commons.resReturn(null, 400, 'uid不能为空');
}
let result = await userInst.del(id);
ctx.body = yapi.commons.resReturn(result);
} catch (e) {
@ -408,6 +423,9 @@ class userController extends baseController {
}
var userInst = yapi.getInst(userModel);
let id = params.uid;
if (!id) {
return ctx.body = yapi.commons.resReturn(null, 400, 'uid不能为空');
}
let data ={
up_time: yapi.commons.time()
@ -460,6 +478,7 @@ class userController extends baseController {
key: '_id',
alias: 'uid'
},
'username',
'email',
'role',
{
@ -475,7 +494,7 @@ class userController extends baseController {
let filteredRes = common.filterRes(queryList, rules);
console.log(queryList)
return ctx.body = yapi.commons.resReturn(filteredRes, 200, 'ok');
return ctx.body = yapi.commons.resReturn(filteredRes, 0, 'ok');
}
}

View File

@ -1,5 +1,5 @@
{
"errcode": 200,
"errcode": 0,
"errmsg": "ok",
"data": {
"project": [

View File

@ -1,11 +1,12 @@
{
"errcode": 200,
"errcode": 0,
"errmsg": "ok",
"data": [
{
"uid": 101,
"email": "admin@admin.com",
"role": "admin",
"username": "admin",
"addTime": 1499936103,
"upTime": 1499936103
}

View File

@ -383,7 +383,7 @@ class projectController extends baseController {
group: groupList
};
return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok')
return ctx.body = yapi.commons.resReturn(queryList, 0, 'ok')
}
}

View File

@ -451,6 +451,7 @@ class userController extends baseController {
key: '_id',
alias: 'uid'
},
'username',
'email',
'role',
{
@ -466,7 +467,7 @@ class userController extends baseController {
let filteredRes = common.filterRes(queryList, rules);
console.log(queryList)
return ctx.body = yapi.commons.resReturn(filteredRes, 200, 'ok');
return ctx.body = yapi.commons.resReturn(filteredRes, 0, 'ok');
}
}

View File

@ -912,7 +912,7 @@ var projectController = function (_baseController) {
project: projectList,
group: groupList
};
return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 200, 'ok'));
return _context9.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(queryList, 0, 'ok'));
case 17:
case 'end':

View File

@ -989,7 +989,7 @@ var userController = function (_baseController) {
rules = [{
key: '_id',
alias: 'uid'
}, 'email', 'role', {
}, 'username', 'email', 'role', {
key: 'add_time',
alias: 'addTime'
}, {
@ -1000,7 +1000,7 @@ var userController = function (_baseController) {
console.log(queryList);
return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(filteredRes, 200, 'ok'));
return _context13.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(filteredRes, 0, 'ok'));
case 12:
case 'end':