feat: uesr management module and fix some server bug

This commit is contained in:
sean 2017-07-18 15:35:32 +08:00
parent 9cd843d3a6
commit 7749672f26
18 changed files with 1120 additions and 186 deletions

View File

@ -3,11 +3,13 @@ import login from './reducer/Login/login.js'
import group from './reducer/group/group.js'
import Interface from './reducer/Interface/InterfaceReducer.js'
import news from './reducer/news/news.js'
import user from './reducer/user/user.js'
export default {
group,
login,
LoginRedux,
Interface,
news
news,
user
}

10
client/actions/user.js Normal file
View File

@ -0,0 +1,10 @@
import {
CHANGE_CUR_UID
} from '../constants/action-types.js'
export function changeCurUid(curUid) {
return {
type: CHANGE_CUR_UID,
data: curUid
}
}

View File

@ -20,3 +20,6 @@ export const LOGIN_TYPE = 'LOGIN_TYPE';
// News
export const FETCH_NEWS_DATA = 'FETCH_NEWS_DATA'
// User
export const CHANGE_CUR_UID = 'CHANGE_CUR_UID'

View File

@ -1,44 +1,59 @@
import React, { Component } from 'react'
import { Link } from 'react-router-dom'
import {Input, Row, Col} from 'antd'
import { Input, Row, Col } from 'antd'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
@connect(
state => {
return {
curUid: state.user.curUid
}
}
)
class LeftMenu extends Component {
static propTypes = {
curUid: PropTypes.string
}
constructor(props) {
super(props)
this.state = {
curitem: 'profile'
}
console.log(this.props)
}
handleCurItem(curitem) {
return () => {
this.setState({
curitem: curitem
})
}
handleActive = () => {
this.setState({
______a: 1 //强制刷新
})
}
render() {
const menus = [{
title: '个人资料',
path: '/user/profile'
path: "/user/profile/107"
}, {
title: '用户管理',
path: '/user/list'
path: '/user/list',
auth: (role) => {
if (role === 'admin') {
return true;
}
return false;
}
}
]
let content = menus.map((menu, index) => {
if (typeof menu.auth === 'function' && menu.auth('admin') === false) {
return '';
}
return (
<li key={index} className={location.hash === '#' + menu.path ? 'active' : ''}>
<li onClick={this.handleActive} key={index} className={location.hash === '#' + menu.path ? 'active' : ''}>
<Link to={menu.path} >{menu.title}</Link>
</li>
)
})
const Search = Input.Search;
return (<div>

View File

@ -4,52 +4,67 @@ import {
Table,
Button
} from 'antd'
import axios from 'axios';
const columns = [{
title: 'UID',
dataIndex: 'uid',
key: 'uid'
}, {
title: '用户名',
dataIndex: 'username',
key: 'username'
}, {
title: 'Email',
dataIndex: 'email',
key: 'email'
}, {
title: '用户角色',
dataIndex: 'role',
key: 'role'
}, {
title: '更新日期',
dataIndex: 'up_time',
key: 'up_time'
}, {
title: '功能',
key: 'action',
render: () => {
return (
<span>
<Button type="primary">查看</Button>
<Button type="danger">删除</Button>
</span>
)
}
}]
class List extends Component {
constructor(props) {
super(props)
this.state = {
data: []
}
}
getUserList() {
axios.get('/user/list').then((res) => {
let data = res.data;
if (res.errno === 0) {
this.setState('data', data.data);
}
})
}
componentDidMount() {
this.getUserList()
}
render() {
const columns = [{
title: 'UID',
dataIndex: 'uid',
key: 'uid'
}, {
title: '用户名',
dataIndex: 'username',
key: 'username'
}, {
title: 'email',
dataIndex: 'email',
key: 'email'
}, {
title: '更新日期',
dataIndex: 'up_time',
key: 'up_time'
}, {
title: '功能',
key: 'action',
render: () => {
return (
<span>
<Button type="primary">编辑</Button>
<Button type="danger">删除</Button>
</span>
)
}
}]
const data = [
{ uid: 1, username: 'admin', email: 'admin@admin.com', up_time: '2017.07.01', key: 1 },
{ uid: 2, username: 'admin2', email: 'admin21113qq3ß@admin311.com', up_time: '2017.07.21', key: 2 }
];
const data = this.state.data;
return (
<section className="user-table">

View File

@ -1,110 +1,221 @@
import React, { Component } from 'react'
import { Row, Col, Icon , Input, Button, Select} from 'antd'
import { Row, Col, Icon, Input, Button, Select, Message } from 'antd'
import axios from 'axios';
import { connect } from 'react-redux'
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
}
constructor(props) {
super(props)
this.state = {
usernameEdit: false,
emailEdit: false,
secureEdit: false,
roleEdit: false
roleEdit: false,
userinfo: {
}
}
this.getUserInfo(101)
}
handleEdit = (key, val) =>{
componentDidMount(){
const uid = this.props.match.params.uid;
this.props.changeCurUid(uid)
this.getUserInfo(uid)
}
handleEdit = (key, val) => {
var s = {};
s[key] = val ;
s[key] = val;
this.setState(s)
}
getUserInfo = (id) => {
axios.get('/user/find', {
id: id
}).then((res) =>{
console.log(res)
var _this = this;
axios.get('/user/find?id=' + id).then((res) => {
_this.setState({
userinfo: res.data.data,
_userinfo: res.data.data
})
})
}
updateUserinfo = (name) =>{
var state = this.state;
let value = this.state._userinfo[name];
let params = {uid: state.userinfo.uid}
params[name] = value;
axios.post('/user/update', params).then( (res)=>{
let data = res.data;
if(data.errcode === 0){
let userinfo = this.state.userinfo;
userinfo[name] = value;
this.setState({
userinfo: userinfo
})
this.handleEdit(name + 'Edit', false)
Message.success('更新用户信息成功');
}else{
Message.error(data.errmsg)
}
}, (err) => {
Message.error(err.message)
} )
}
changeUserinfo = (e) =>{
let dom = e.target;
let name = dom.getAttribute("name");
let value = dom.value;
let userinfo = this.state._userinfo;
userinfo[name] = value;
this.setState({
_userinfo: userinfo
})
}
changeRole = (val) =>{
let userinfo = this.state.userinfo;
userinfo.role = val;
this.setState({
_userinfo: userinfo
})
this.updateUserinfo('role');
}
updatePassword = () =>{
let old_password = document.getElementById('old_password').value;
let password = document.getElementById('password').value;
let verify_pass = document.getElementById('verify_pass').value;
if(password != verify_pass){
return Message.error('两次输入的密码不一样');
}
let params = {
uid: this.state.userinfo.uid,
password: password,
old_password: old_password
}
axios.post('/user/change_password', params).then( (res)=>{
let data = res.data;
if(data.errcode === 0){
this.handleEdit('secureEdit', false)
Message.success('修改密码成功');
}else{
Message.error(data.errmsg)
}
}, (err) => {
Message.error(err.message)
} )
}
render() {
let ButtonGroup = Button.Group;
let userNameEditHtml, emailEditHtml,secureEditHtml, roleEditHtml;
let userNameEditHtml, emailEditHtml, secureEditHtml, roleEditHtml;
const Option = Select.Option;
if(this.state.usernameEdit === false){
let userinfo = this.state.userinfo;
let _userinfo = this.state._userinfo;
let roles = { admin: '管理员', member: '会员' }
if (this.state.usernameEdit === false) {
userNameEditHtml = <div >
<span className="text">xiaoming</span>&nbsp;&nbsp;
<span className="text-button" onClick={() => {this.handleEdit( 'usernameEdit', true)}}><Icon type="edit"/>修改</span>
<span className="text">{userinfo.username}</span>&nbsp;&nbsp;
<span className="text-button" onClick={() => { this.handleEdit('usernameEdit', true) }}><Icon type="edit" />修改</span>
</div>
}else{
} else {
userNameEditHtml = <div>
<Input placeholder="用户名" />
<Input value={_userinfo.username} name="username" onChange={this.changeUserinfo} placeholder="用户名" />
<ButtonGroup className="edit-buttons" >
<Button className="edit-button" onClick={() => {this.handleEdit( 'usernameEdit', false)}} >Cancel</Button>
<Button className="edit-button" type="primary">OK</Button>
<Button className="edit-button" onClick={() => { this.handleEdit('usernameEdit', false) }} >Cancel</Button>
<Button className="edit-button" onClick={ () => { this.updateUserinfo('username')} } type="primary">OK</Button>
</ButtonGroup>
</div>
}
if(this.state.emailEdit === false){
if (this.state.emailEdit === false) {
emailEditHtml = <div >
<span className="text">abc@qq.com</span>&nbsp;&nbsp;
<span className="text-button" onClick={() => {this.handleEdit( 'emailEdit', true)}} ><Icon type="edit"/>修改</span>
<span className="text">{userinfo.email}</span>&nbsp;&nbsp;
<span className="text-button" onClick={() => { this.handleEdit('emailEdit', true) }} ><Icon type="edit" />修改</span>
</div>
}else{
} else {
emailEditHtml = <div>
<Input placeholder="Email" />
<Input placeholder="Email" value={_userinfo.email} name="email" onChange={this.changeUserinfo} />
<ButtonGroup className="edit-buttons" >
<Button className="edit-button" onClick={() => {this.handleEdit( 'emailEdit', false)}} >Cancel</Button>
<Button className="edit-button" type="primary">OK</Button>
<Button className="edit-button" onClick={() => { this.handleEdit('emailEdit', false) }} >Cancel</Button>
<Button className="edit-button" type="primary" onClick={ () => { this.updateUserinfo('email')} }>OK</Button>
</ButtonGroup>
</div>
}
if(this.state.roleEdit === true){
if (this.state.roleEdit === false) {
roleEditHtml = <div>
<span className="text">管理员</span>&nbsp;&nbsp;
<span className="text-button" onClick={() => {this.handleEdit( 'roleEdit', true)}} ><Icon type="edit"/>修改</span>
<span className="text">{roles[userinfo.role]}</span>&nbsp;&nbsp;
<span className="text-button" onClick={() => { this.handleEdit('roleEdit', true) }} ><Icon type="edit" />修改</span>
</div>
}else{
roleEditHtml = <Select defaultValue="admin" style={{ width: 150 }} >
} else {
roleEditHtml = <Select defaultValue={_userinfo.role} onChange={ this.changeRole} style={{ width: 150 }} >
<Option value="admin">管理员</Option>
<Option value="member">会员</Option>
</Select>
}
if(this.state.secureEdit === false){
secureEditHtml = <Button type="primary" onClick={() => {this.handleEdit( 'secureEdit', true)}}>密码修改</Button>
}else{
</Select>
}
if (this.state.secureEdit === false) {
secureEditHtml = <Button type="primary" onClick={() => { this.handleEdit('secureEdit', true) }}>密码修改</Button>
} else {
secureEditHtml = <div>
<Input placeholder="旧的密码" />
<Input placeholder="新的密码" />
<Input style={{display: this.state.userinfo.role === 'admin' ? 'none': ''}} placeholder="旧的密码" type="password" name="old_password" id="old_password" />
<Input placeholder="新的密码" type="password" name="password" id="password" />
<Input placeholder="确认密码" type="password" name="verify_pass" id="verify_pass" />
<ButtonGroup className="edit-buttons" >
<Button className="edit-button" onClick={() => {this.handleEdit( 'secureEdit', false)}}>Cancel</Button>
<Button className="edit-button" type="primary">OK</Button>
<Button className="edit-button" onClick={() => { this.handleEdit('secureEdit', false) }}>Cancel</Button>
<Button className="edit-button" onClick={this.updatePassword} type="primary">OK</Button>
</ButtonGroup>
</div>
}
return <div className="user-profile">
<Row className="user-item" type="flex" justify="start">
<Row className="user-item" type="flex" justify="start">
<Col span={4}>用户名</Col>
<Col span={12}>
{userNameEditHtml}
</Col>
</Row>
<Row className="user-item" type="flex" justify="start">
<Row className="user-item" type="flex" justify="start">
<Col span={4}>Email</Col>
<Col span={12}>
{emailEditHtml}
</Col>
</Row>
<Row className="user-item" type="flex" justify="start">
<Row className="user-item" style={{display: this.state.userinfo.role === 'admin'? '': 'none'}} type="flex" justify="start">
<Col span={4}>角色</Col>
<Col span={12}>
{roleEditHtml}

View File

@ -30,8 +30,8 @@ class User extends Component {
<LeftMenu />
<Route path={this.props.match.path + '/list/:uid'} component={List} />
<Route path={this.props.match.path + '/profile'} component={Profile} />
<Route path={this.props.match.path + '/list'} component={List} />
<Route path={this.props.match.path + '/profile/:uid'} component={Profile} />
</section>
</div>
)

View File

@ -0,0 +1,21 @@
import {
CHANGE_CUR_UID
} from '../../constants/action-types';
const initialState = {
curUid: "0"
};
export default (state = initialState, action) => {
switch (action.type) {
case CHANGE_CUR_UID: {
return {
...state,
curUid: action.data
};
}
default:
return state;
}
};

503
doc/build/api.html vendored
View File

@ -104,6 +104,10 @@
<a href="#-user-logout">/user/logout</a>
</li>
<li >
<a href="#-user-change_password">/user/change_password</a>
</li>
<li >
<a href="#-user-reg">/user/reg</a>
</li>
@ -113,12 +117,20 @@
</li>
<li >
<a href="#-user-list">/user/list</a>
<a href="#-user-find">/user/find</a>
</li>
<li >
<a href="#-user-del">/user/del</a>
</li>
<li >
<a href="#-user-update">/user/update</a>
</li>
<li >
<a href="#-user-search">/user/search</a>
</li>
</ul>
@ -142,6 +154,10 @@
<a href="#-project-del_member">/project/del_member</a>
</li>
<li >
<a href="#-project-get_member_list-json">/project/get_member_list.json</a>
</li>
<li >
<a href="#-project-get">/project/get</a>
</li>
@ -157,6 +173,10 @@
<li >
<a href="#-project-up">/project/up</a>
</li>
<li >
<a href="#-project-search">/project/search</a>
</li>
</ul>
@ -218,7 +238,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/group.js.html#13" target="_blank">./server/controllers/group.js:13</a>
<a href="./static/server/controllers/group.js.html#12" target="_blank">./server/controllers/group.js:12</a>
</p>
@ -301,7 +321,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/group.js.html#52" target="_blank">./server/controllers/group.js:52</a>
<a href="./static/server/controllers/group.js.html#51" target="_blank">./server/controllers/group.js:51</a>
</p>
@ -363,7 +383,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/group.js.html#72" target="_blank">./server/controllers/group.js:72</a>
<a href="./static/server/controllers/group.js.html#71" target="_blank">./server/controllers/group.js:71</a>
</p>
@ -435,7 +455,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/group.js.html#103" target="_blank">./server/controllers/group.js:103</a>
<a href="./static/server/controllers/group.js.html#102" target="_blank">./server/controllers/group.js:102</a>
</p>
@ -535,7 +555,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#11" target="_blank">./server/controllers/user.js:11</a>
<a href="./static/server/controllers/user.js.html#13" target="_blank">./server/controllers/user.js:13</a>
</p>
@ -595,6 +615,7 @@
<span class="token string">"data"</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">"username"</span><span class="token punctuation">:</span> <span class="token string">"admin"</span><span class="token punctuation">,</span>
<span class="token string">"add_time"</span><span class="token punctuation">:</span> <span class="token number">1499762848</span><span class="token punctuation">,</span>
<span class="token string">"up_time"</span><span class="token punctuation">:</span> <span class="token number">1499762848</span>
<span class="token punctuation">}</span>
@ -621,7 +642,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#62" target="_blank">./server/controllers/user.js:62</a>
<a href="./static/server/controllers/user.js.html#57" target="_blank">./server/controllers/user.js:57</a>
</p>
@ -634,6 +655,102 @@
<span class="token punctuation">}</span></code></pre>
</div>
<div class="con-list-item">
<blockquote class="api">
<h3 id="-user-change_password" class="page-header subject">
/user/change_password
<span class="ui-badge">POST</span>
<a class="hashlink" href="#-user-change_password">#</a>
</h3>
</blockquote>
<p>
<small class="text-muted">描述:</small>
修改用户密码
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#150" target="_blank">./server/controllers/user.js:150</a>
</p>
<p>
<small class="text-muted">参数:</small>
</p>
<div class="docs-table">
<table class="yo-table yo-table-border">
<colgroup>
<col class="c1">
<col class="c2">
<col class="c3">
<col class="c4">
</colgroup>
<thead>
<tr class="active">
<th>参数名</th>
<th>类型</th>
<th>描述</th>
<th>必选</th>
<th>支持版本</th>
</tr>
</thead>
<tr>
<td>uid</td>
<td>Number</td>
<td>用户ID</td>
<td>
<i class="yo-ico glyphicon glyphicon-ok text-success"></i>
</td>
<td></td>
</tr>
<tr>
<td>old_password</td>
<td>Number</td>
<td>旧密码, 非admin用户必须传</td>
<td>
</td>
<td></td>
</tr>
<tr>
<td>password</td>
<td>Number</td>
<td>新密码</td>
<td>
<i class="yo-ico glyphicon glyphicon-ok text-success"></i>
</td>
<td></td>
</tr>
</table>
</div>
<div>示例:</div>
<pre class="ydoc-example" ><code><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">"success"</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">"n"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token string">"nModified"</span><span class="token punctuation">:</span> <span class="token number">1</span><span class="token punctuation">,</span>
<span class="token string">"ok"</span><span class="token punctuation">:</span> <span class="token number">1</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
</div>
<div class="con-list-item">
@ -654,7 +771,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#79" target="_blank">./server/controllers/user.js:79</a>
<a href="./static/server/controllers/user.js.html#214" target="_blank">./server/controllers/user.js:214</a>
</p>
@ -724,6 +841,7 @@
<span class="token string">"data"</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">"username"</span><span class="token punctuation">:</span> <span class="token string">"admin"</span><span class="token punctuation">,</span>
<span class="token string">"add_time"</span><span class="token punctuation">:</span> <span class="token number">1499762848</span><span class="token punctuation">,</span>
<span class="token string">"up_time"</span><span class="token punctuation">:</span> <span class="token number">1499762848</span>
<span class="token punctuation">}</span>
@ -750,7 +868,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#136" target="_blank">./server/controllers/user.js:136</a>
<a href="./static/server/controllers/user.js.html#276" target="_blank">./server/controllers/user.js:276</a>
</p>
@ -760,23 +878,23 @@
<div class="con-list-item">
<blockquote class="api">
<h3 id="-user-list" class="page-header subject">
/user/list
<h3 id="-user-find" class="page-header subject">
/user/find
<span class="ui-badge">GET</span>
<a class="hashlink" href="#-user-list">#</a>
<a class="hashlink" href="#-user-find">#</a>
</h3>
</blockquote>
<p>
<small class="text-muted">描述:</small>
获取用户列表
获取用户个人信息
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#159" target="_blank">./server/controllers/user.js:159</a>
<a href="./static/server/controllers/user.js.html#299" target="_blank">./server/controllers/user.js:299</a>
</p>
@ -833,12 +951,12 @@
</blockquote>
<p>
<small class="text-muted">描述:</small>
获取用户列表,只有admin用户才有此权限
删除用户,只有admin用户才有此权限
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#184" target="_blank">./server/controllers/user.js:184</a>
<a href="./static/server/controllers/user.js.html#328" target="_blank">./server/controllers/user.js:328</a>
</p>
@ -880,6 +998,160 @@
</div>
<div class="con-list-item">
<blockquote class="api">
<h3 id="-user-update" class="page-header subject">
/user/update
<span class="ui-badge">POST</span>
<a class="hashlink" href="#-user-update">#</a>
</h3>
</blockquote>
<p>
<small class="text-muted">描述:</small>
更新用户个人信息
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#352" target="_blank">./server/controllers/user.js:352</a>
</p>
<p>
<small class="text-muted">参数:</small>
</p>
<div class="docs-table">
<table class="yo-table yo-table-border">
<colgroup>
<col class="c1">
<col class="c2">
<col class="c3">
<col class="c4">
</colgroup>
<thead>
<tr class="active">
<th>参数名</th>
<th>类型</th>
<th>描述</th>
<th>必选</th>
<th>支持版本</th>
</tr>
</thead>
<tr>
<td>username</td>
<td></td>
<td>String</td>
<td>
<i class="yo-ico glyphicon glyphicon-ok text-success"></i>
</td>
<td></td>
</tr>
<tr>
<td>email</td>
<td></td>
<td>String</td>
<td>
<i class="yo-ico glyphicon glyphicon-ok text-success"></i>
</td>
<td></td>
</tr>
</table>
</div>
</div>
<div class="con-list-item">
<blockquote class="api">
<h3 id="-user-search" class="page-header subject">
/user/search
<span class="ui-badge">GET</span>
<a class="hashlink" href="#-user-search">#</a>
</h3>
</blockquote>
<p>
<small class="text-muted">描述:</small>
模糊搜索用户名或者email
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/user.js.html#388" target="_blank">./server/controllers/user.js:388</a>
</p>
<p>
<small class="text-muted">参数:</small>
</p>
<div class="docs-table">
<table class="yo-table yo-table-border">
<colgroup>
<col class="c1">
<col class="c2">
<col class="c3">
<col class="c4">
</colgroup>
<thead>
<tr class="active">
<th>参数名</th>
<th>类型</th>
<th>描述</th>
<th>必选</th>
<th>支持版本</th>
</tr>
</thead>
<tr>
<td>q</td>
<td>String</td>
<td></td>
<td>
<i class="yo-ico glyphicon glyphicon-ok text-success"></i>
</td>
<td></td>
</tr>
</table>
</div>
<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">"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">"_id"</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">"password"</span><span class="token punctuation">:</span> <span class="token string">"3d078af947521bb4a99c93f5e089fc2ac601fa09"</span><span class="token punctuation">,</span>
<span class="token string">"passsalt"</span><span class="token punctuation">:</span> <span class="token string">"qc8lnjpnbs9z1vodz4ynfjemi"</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">"add_time"</span><span class="token punctuation">:</span> <span class="token number">1499936103</span><span class="token punctuation">,</span>
<span class="token string">"up_time"</span><span class="token punctuation">:</span> <span class="token number">1499936103</span><span class="token punctuation">,</span>
<span class="token string">"__v"</span><span class="token punctuation">:</span> <span class="token number">0</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span></code></pre>
</div>
@ -903,7 +1175,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#12" target="_blank">./server/controllers/project.js:12</a>
<a href="./static/server/controllers/project.js.html#15" target="_blank">./server/controllers/project.js:15</a>
</p>
@ -1027,12 +1299,12 @@
</blockquote>
<p>
<small class="text-muted">描述:</small>
添加项目
添加项目成员
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#74" target="_blank">./server/controllers/project.js:74</a>
<a href="./static/server/controllers/project.js.html#77" target="_blank">./server/controllers/project.js:77</a>
</p>
@ -1070,8 +1342,8 @@
</tr>
<tr>
<td>uid</td>
<td>member_uid</td>
<td>String</td>
<td>项目成员uid,不能为空</td>
<td>
@ -1111,12 +1383,12 @@
</blockquote>
<p>
<small class="text-muted">描述:</small>
添加项目
删除项目成员
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#106" target="_blank">./server/controllers/project.js:106</a>
<a href="./static/server/controllers/project.js.html#109" target="_blank">./server/controllers/project.js:109</a>
</p>
@ -1180,6 +1452,87 @@
<span class="token punctuation">}</span></code></pre>
</div>
<div class="con-list-item">
<blockquote class="api">
<h3 id="-project-get_member_list-json" class="page-header subject">
/project/get_member_list.json
<span class="ui-badge">GET</span>
<a class="hashlink" href="#-project-get_member_list-json">#</a>
</h3>
</blockquote>
<p>
<small class="text-muted">描述:</small>
获取项目成员列表
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#142" target="_blank">./server/controllers/project.js:142</a>
</p>
<p>
<small class="text-muted">参数:</small>
</p>
<div class="docs-table">
<table class="yo-table yo-table-border">
<colgroup>
<col class="c1">
<col class="c2">
<col class="c3">
<col class="c4">
</colgroup>
<thead>
<tr class="active">
<th>参数名</th>
<th>类型</th>
<th>描述</th>
<th>必选</th>
<th>支持版本</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>Number</td>
<td>项目id不能为空</td>
<td>
<i class="yo-ico glyphicon glyphicon-ok text-success"></i>
</td>
<td></td>
</tr>
</table>
</div>
<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">0</span><span class="token punctuation">,</span>
<span class="token string">"errmsg"</span><span class="token punctuation">:</span> <span class="token string">"success"</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">"_id"</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">"password"</span><span class="token punctuation">:</span> <span class="token string">"e00084747ecba89837affe8a048e83c751e44209"</span><span class="token punctuation">,</span>
<span class="token string">"passsalt"</span><span class="token punctuation">:</span> <span class="token string">"fmf3kytyysc4swwuqc15rk9"</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">"add_time"</span><span class="token punctuation">:</span> <span class="token number">1499932673</span><span class="token punctuation">,</span>
<span class="token string">"up_time"</span><span class="token punctuation">:</span> <span class="token number">1499932673</span><span class="token punctuation">,</span>
<span class="token string">"__v"</span><span class="token punctuation">:</span> <span class="token number">0</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre>
</div>
<div class="con-list-item">
@ -1200,7 +1553,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#138" target="_blank">./server/controllers/project.js:138</a>
<a href="./static/server/controllers/project.js.html#176" target="_blank">./server/controllers/project.js:176</a>
</p>
@ -1283,7 +1636,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#162" target="_blank">./server/controllers/project.js:162</a>
<a href="./static/server/controllers/project.js.html#200" target="_blank">./server/controllers/project.js:200</a>
</p>
@ -1383,7 +1736,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#186" target="_blank">./server/controllers/project.js:186</a>
<a href="./static/server/controllers/project.js.html#224" target="_blank">./server/controllers/project.js:224</a>
</p>
@ -1455,7 +1808,7 @@
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#219" target="_blank">./server/controllers/project.js:219</a>
<a href="./static/server/controllers/project.js.html#257" target="_blank">./server/controllers/project.js:257</a>
</p>
@ -1595,6 +1948,104 @@
<span class="token punctuation">}</span></code></pre>
</div>
<div class="con-list-item">
<blockquote class="api">
<h3 id="-project-search" class="page-header subject">
/project/search
<span class="ui-badge">GET</span>
<a class="hashlink" href="#-project-search">#</a>
</h3>
</blockquote>
<p>
<small class="text-muted">描述:</small>
模糊搜索项目名称或者组名称
</p>
<p>
<small class="text-muted">源码位置:</small>
<a href="./static/server/controllers/project.js.html#318" target="_blank">./server/controllers/project.js:318</a>
</p>
<p>
<small class="text-muted">参数:</small>
</p>
<div class="docs-table">
<table class="yo-table yo-table-border">
<colgroup>
<col class="c1">
<col class="c2">
<col class="c3">
<col class="c4">
</colgroup>
<thead>
<tr class="active">
<th>参数名</th>
<th>类型</th>
<th>描述</th>
<th>必选</th>
<th>支持版本</th>
</tr>
</thead>
<tr>
<td>q</td>
<td>String</td>
<td></td>
<td>
<i class="yo-ico glyphicon glyphicon-ok text-success"></i>
</td>
<td></td>
</tr>
</table>
</div>
<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">"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>
<span class="token punctuation">{</span>
<span class="token string">"_id"</span><span class="token punctuation">:</span> <span class="token number">101</span><span class="token punctuation">,</span>
<span class="token string">"name"</span><span class="token punctuation">:</span> <span class="token string">"project yapi"</span><span class="token punctuation">,</span>
<span class="token string">"prd_host"</span><span class="token punctuation">:</span> <span class="token string">"yapi.qunar.com"</span><span class="token punctuation">,</span>
<span class="token string">"basepath"</span><span class="token punctuation">:</span> <span class="token string">"/yapi/"</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">"group_id"</span><span class="token punctuation">:</span> <span class="token number">193</span><span class="token punctuation">,</span>
<span class="token string">"add_time"</span><span class="token punctuation">:</span> <span class="token number">1500013365</span><span class="token punctuation">,</span>
<span class="token string">"up_time"</span><span class="token punctuation">:</span> <span class="token number">1500013365</span><span class="token punctuation">,</span>
<span class="token string">"__v"</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token string">"env"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">"members"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
<span class="token string">"101"</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span><span class="token punctuation">,</span>
<span class="token string">"group"</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>
<span class="token punctuation">{</span>
<span class="token string">"_id"</span><span class="token punctuation">:</span> <span class="token number">193</span><span class="token punctuation">,</span>
<span class="token string">"group_name"</span><span class="token punctuation">:</span> <span class="token string">"yapi"</span><span class="token punctuation">,</span>
<span class="token string">"group_desc"</span><span class="token punctuation">:</span> <span class="token string">"group yapi"</span><span class="token punctuation">,</span>
<span class="token string">"uid"</span><span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
<span class="token string">"add_time"</span><span class="token punctuation">:</span> <span class="token number">1500013066</span><span class="token punctuation">,</span>
<span class="token string">"up_time"</span><span class="token punctuation">:</span> <span class="token number">1500013066</span><span class="token punctuation">,</span>
<span class="token string">"__v"</span><span class="token punctuation">:</span> <span class="token number">0</span>
<span class="token punctuation">}</span>
<span class="token punctuation">]</span>
<span class="token punctuation">}</span>
<span class="token punctuation">}</span></code></pre>
</div>

View File

@ -33,7 +33,7 @@ const jwt = require('jsonwebtoken');
class baseController{
constructor(ctx){
this.ctx = ctx;
//网站上线后role对象key是不能修改的value可以修改
this.roles = {
admin: 'Admin',
@ -43,7 +43,14 @@ class baseController{
async init(ctx){
this.$user = null;
if(ctx.path === '/user/login' || ctx.path === '/user/reg' || ctx.path === '/user/status' || ctx.path === '/user/logout'){
let ignoreRouter = [
'/user/login_by_token',
'/user/login',
'/user/reg',
'/user/status',
'/user/logout'
]
if(ignoreRouter.indexOf(ctx.path) > -1){
this.$auth = true;
}else{
await this.checkLogin(ctx)
@ -77,7 +84,7 @@ class baseController{
}
async getLoginStatus(ctx){
if(await this.checkLogin(ctx) === true){
if(await this.checkLogin(ctx) === true){
return ctx.body = yapi.commons.resReturn(yapi.commons.fieldSelect(this.$user,['_id','username','email', 'up_time', 'add_time']));
}
return ctx.body = yapi.commons.resReturn(null, 300 , 'Please login.');

View File

@ -34,7 +34,6 @@ import projectModel from '../models/project.js'
class groupController extends baseController{
constructor(ctx){
super(ctx)
console.log('constructor....')
}

View File

@ -25,16 +25,19 @@
<div class="ydoc-container-content">
<div class="static-code-content" role="main">
<pre class="brush: js;">
import projectModel from '../models/project.js'
import projectModel from '../models/project.js'
import yapi from '../yapi.js'
import baseController from './base.js'
import interfaceModel from '../models/interface.js'
import userModel from '../models/user.js'
import groupModel from '../models/group'
class projectController extends baseController {
constructor(ctx){
super(ctx)
this.Model = yapi.getInst(projectModel);
this.groupModel = yapi.getInst(groupModel);
}
/**
@ -100,13 +103,13 @@ class projectController extends baseController {
}
/**
* 添加项目
* 添加项目成员
* @interface /project/add_member
* @method POST
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @param {member_uid} uid 项目成员uid,不能为空
* @param {String} member_uid 项目成员uid,不能为空
* @returns {Object}
* @example ./api/project/add_member.json
*/
@ -132,7 +135,7 @@ class projectController extends baseController {
}
/**
* 添加项目
* 删除项目成员
* @interface /project/del_member
* @method POST
* @category project
@ -163,6 +166,41 @@ class projectController extends baseController {
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
/**
* 获取项目成员列表
* @interface /project/get_member_list.json
* @method GET
* @category project
* @foldnumber 10
* @param {Number} id 项目id不能为空
* @return {Object}
* @example ./api/project/get_member_list.json
*/
async getMemberList(ctx) {
let params = ctx.request.query;
if(!params.id) {
return ctx.body = yapi.commons.resReturn(null, 400, '项目id不能为空');
}
try {
let project = await this.Model.get(params.id);
let userInst = yapi.getInst(userModel);
let result = [];
for(let i of project.members) {
let user = await userInst.findById(i);
result.push(user);
}
ctx.body = yapi.commons.resReturn(result);
} catch(e) {
ctx.body = yapi.commons.resReturn(null, 402, e.message);
}
}
/**
* 添加项目
* @interface /project/get
@ -304,6 +342,35 @@ class projectController extends baseController {
ctx.body = yapi.commons.resReturn(null, 402, e.message)
}
}
/**
* 模糊搜索项目名称或者组名称
* @interface /project/search
* @method GET
* @category project
* @foldnumber 10
* @param {String} q
* @return {Object}
* @example ./api/project/search.json
*/
async search(ctx) {
const { q } = ctx.request.query;
if (!q) {
return ctx.body = yapi.commons.resReturn(void 0, 400, 'No keyword.')
}
if (!yapi.commons.validateSearchKeyword(q)) {
return ctx.body = yapi.commons.resReturn(void 0, 400, 'Bad query.')
}
let queryList = {
project: await this.Model.search(q),
group: await this.groupModel.search(q)
}
return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok')
}
}
module.exports = projectController;

View File

@ -29,12 +29,14 @@
import yapi from '../yapi.js'
import baseController from './base.js'
import mongoose from 'mongoose'
import request from 'request'
const jwt = require('jsonwebtoken');
class userController extends baseController{
constructor(ctx){
super(ctx)
this.Model = yapi.getInst(userModel);
}
/**
* 用户登录接口
@ -65,17 +67,10 @@ class userController extends baseController{
if(!result){
return ctx.body = yapi.commons.resReturn(null,404,'该用户不存在');
}else if(yapi.commons.generatePassword(password, result.passsalt) === result.password){
let token = jwt.sign({uid: result._id},result.passsalt,{expiresIn: '7 days'});
ctx.cookies.set('_yapi_token', token, {
expires: yapi.commons.expireDate(7),
httpOnly: true
})
ctx.cookies.set('_yapi_uid', result._id, {
expires: yapi.commons.expireDate(7),
httpOnly: true
})
this.setLoginCookie(result._id, result.passsalt)
return ctx.body = yapi.commons.resReturn({
username: result.username,
uid: result._id,
email: result.email,
add_time: result.add_time,
@ -104,6 +99,146 @@ class userController extends baseController{
}
/**
* 第三方登录需要提供一个request方法和 token字段暂时只支持qunar第三方
* @return {email: String, username: String}
*/
thirdQunarLogin(){
return {
request: (token) => {
return new Promise((resolve, reject) =>{
request('http://qsso.corp.qunar.com/api/verifytoken.php?token=' + token ,function (error, response, body) {
if (!error && response.statusCode == 200) {
let result = JSON.parse(body);
if(result && result.ret === true){
let ret = {
email: result.userId + '@qunar.com',
username: result.data.userInfo.name
}
resolve(ret)
}else{
reject(result)
}
}
reject(error)
})
})
},
tokenField: 'token',
}
}
async loginByToken(ctx){
let config = this.thirdQunarLogin();
let token = ctx.request.body[config.tokenField] || ctx.request.query[config.tokenField];
try{
let ret = await config.request(token);
let login = await this.handleThirdLogin(ret.email, ret.username);
if(login === true){
yapi.commons.log('login success');
ctx.redirect('/')
}
}catch(e){
yapi.commons.log(e.message, 'error')
ctx.redirect('/')
}
}
async handleThirdLogin(email, username){
let user, data, passsalt;
var userInst = yapi.getInst(userModel);
try{
user = await userInst.findByEmail(email);
if(!user || !user._id){
passsalt = yapi.commons.randStr();
data = {
username: username,
password: yapi.commons.generatePassword(passsalt, passsalt),
email: email,
passsalt: passsalt,
role: 'member',
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
}
user = await userInst.save(data);
}
this.setLoginCookie(user._id, user.passsalt)
return true;
}catch(e){
console.error(e.message)
return false;
}
}
/**
* 修改用户密码
* @interface /user/change_password
* @method POST
* @category user
* @param {Number} uid 用户ID
* @param {Number} [old_password] 旧密码, 非admin用户必须传
* @param {Number} password 新密码
* @return {Object}
* @example ./api/user/change_password.json
*/
async changePassword(ctx){
let params = ctx.request.body;
let userInst = yapi.getInst(userModel);
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') {
if(!params.old_password) {
return ctx.body = yapi.commons.resReturn(null, 400, '旧密码不能为空');
}
let user = await userInst.findById(params.uid);
if(yapi.commons.generatePassword(params.old_password, user.passsalt) !== user.password) {
return ctx.body = yapi.commons.resReturn(null, 402, '旧密码错误');
}
}
let passsalt = yapi.commons.randStr();
let data = {
up_time: yapi.commons.time(),
password: yapi.commons.generatePassword(params.password, passsalt),
passsalt: passsalt
};
try{
let result = await userInst.update(params.uid, data);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null, 401, e.message);
}
}
async forgetPassword(ctx){
}
async resetPassword(ctx){
}
setLoginCookie(uid, passsalt){
let token = jwt.sign({uid: uid},passsalt,{expiresIn: '7 days'});
this.ctx.cookies.set('_yapi_token', token, {
expires: yapi.commons.expireDate(7),
httpOnly: true
})
this.ctx.cookies.set('_yapi_uid', uid, {
expires: yapi.commons.expireDate(7),
httpOnly: true
})
}
/**
* 用户注册接口
* @interface /user/reg
@ -141,15 +276,20 @@ class userController extends baseController{
add_time: yapi.commons.time(),
up_time: yapi.commons.time()
}
if(!data.username){
data.username = data.email.substr(0, data.email.indexOf('@'));
}
try{
let user = await userInst.save(data);
this.setLoginCookie(user._id, user.passsalt)
ctx.body = yapi.commons.resReturn({
uid: user._id,
email: user.email,
username: user.username,
add_time: user.add_time,
up_time: user.up_time,
role: 'member',
role: 'member'
});
yapi.commons.sendMail({
to: params.email,
@ -173,8 +313,8 @@ class userController extends baseController{
async list(ctx){
if(this.getRole() !== 'admin'){
return ctx.body = yapi.commons.resReturn(null, 402, 'Without permission.');
}
return ctx.body = yapi.commons.resReturn(null, 402, '没有权限');
}
var userInst = yapi.getInst(userModel);
try{
let user = await userInst.list();
@ -185,8 +325,8 @@ class userController extends baseController{
}
/**
* 获取用户列表
* @interface /user/list
* 获取用户个人信息
* @interface /user/find
* @method GET
* @param id 用户uid
* @category user
@ -196,21 +336,25 @@ class userController extends baseController{
*/
async findById(ctx){ //根据id获取用户信息
try{
try{
var userInst = yapi.getInst(userModel);
let id = ctx.request.body.id;
if(this.getUid() != id){
return ctx.body = yapi.commons.resReturn(null, 402, 'Without permission.');
}
let id = ctx.request.query.id;
let result = await userInst.findById(id);
return ctx.body = yapi.commons.resReturn(result);
return ctx.body = yapi.commons.resReturn({
uid: result._id,
username: result.username,
email: result.email,
role: result.role,
add_time: result.add_time,
up_time: result.up_time
});
}catch(e){
return ctx.body = yapi.commons.resReturn(null,402,e.message);
}
}
/**
* 获取用户列表,只有admin用户才有此权限
* 删除用户,只有admin用户才有此权限
* @interface /user/del
* @method POST
* @param id 用户uid
@ -233,20 +377,66 @@ class userController extends baseController{
}
}
/**
* 更新用户个人信息
* @interface /user/update
* @method POST
* @param username String
* @param email String
* @category user
* @foldnumber 10
* @returns {Object}
* @example
*/
async update(ctx){ //更新用户信息
try{
var userInst = yapi.getInst(userModel);
let id = this.getUid();
let data ={};
let data ={
up_time: yapi.commons.time()
};
ctx.request.body.username && (data.username = ctx.request.body.username)
ctx.request.body.email && (data.email = ctx.request.body.email)
let result = await userInst.update(id,data);
if(data.email){
var checkRepeat = await userInst.checkRepeat(data.email);//然后检查是否已经存在该用户
if(checkRepeat>0){
return ctx.body = yapi.commons.resReturn(null,401,'该email已经注册');
}
}
let result = await userInst.update(id, data);
ctx.body = yapi.commons.resReturn(result);
}catch(e){
ctx.body = yapi.commons.resReturn(null,402,e.message);
}
}
/**
* 模糊搜索用户名或者email
* @interface /user/search
* @method GET
* @category user
* @foldnumber 10
* @param {String} q
* @return {Object}
* @example ./api/user/search.json
*/
async search(ctx) {
const { q } = ctx.request.query;
if (!q) {
return ctx.body = yapi.commons.resReturn(void 0, 400, 'No keyword.')
}
if (!yapi.commons.validateSearchKeyword(q)) {
return ctx.body = yapi.commons.resReturn(void 0, 400, 'Bad query.')
}
let queryList = await this.Model.search(q);
return ctx.body = yapi.commons.resReturn(queryList, 200, 'ok')
}
}
module.exports = userController

View File

@ -157,7 +157,7 @@ class userController extends baseController{
* @param {Number} [old_password] 旧密码, 非admin用户必须传
* @param {Number} password 新密码
* @return {Object}
* @example ./api/user/change_password
* @example ./api/user/change_password.json
*/
async changePassword(ctx){
let params = ctx.request.body;
@ -311,9 +311,16 @@ class userController extends baseController{
async findById(ctx){ //根据id获取用户信息
try{
var userInst = yapi.getInst(userModel);
let id = ctx.request.body.id;
let id = ctx.request.query.id;
let result = await userInst.findById(id);
return ctx.body = yapi.commons.resReturn(result);
return ctx.body = yapi.commons.resReturn({
uid: result._id,
username: result.username,
email: result.email,
role: result.role,
add_time: result.add_time,
up_time: result.up_time
});
}catch(e){
return ctx.body = yapi.commons.resReturn(null,402,e.message);
}
@ -347,8 +354,10 @@ class userController extends baseController{
* 更新用户个人信息
* @interface /user/update
* @method POST
* @param username String
* @param email String
* @param uid 用户uid
* @param [role] 用户角色,只有管理员有权限修改
* @param [username] String
* @param [email] String
* @category user
* @foldnumber 10
* @returns {Object}
@ -356,13 +365,20 @@ class userController extends baseController{
*/
async update(ctx){ //更新用户信息
try{
let params = ctx.request.body;
if(this.getRole() !== 'admin' && params.uid != this.getUid()){
return ctx.body = yapi.commons.resReturn(null,401,'没有权限');
}
var userInst = yapi.getInst(userModel);
let id = this.getUid();
let id = params.uid;
let data ={
up_time: yapi.commons.time()
};
ctx.request.body.username && (data.username = ctx.request.body.username)
ctx.request.body.email && (data.email = ctx.request.body.email)
if(this.getRole() === 'admin'){
params.role && (data.role = params.role)
}
params.username && (data.username = params.username)
params.email && (data.email = params.email)
if(data.email){
var checkRepeat = await userInst.checkRepeat(data.email);//然后检查是否已经存在该用户

View File

@ -9,7 +9,10 @@ class userModel extends baseModel{
getSchema(){
return{
username: String,
username: {
type: String,
required: true
},
password:{
type:String,
required: true

View File

@ -353,7 +353,7 @@ var userController = function (_baseController) {
* @param {Number} [old_password] 旧密码, 非admin用户必须传
* @param {Number} password 新密码
* @return {Object}
* @example ./api/user/change_password
* @example ./api/user/change_password.json
*/
}, {
@ -690,13 +690,20 @@ var userController = function (_baseController) {
case 0:
_context10.prev = 0;
userInst = _yapi2.default.getInst(_user2.default);
id = ctx.request.body.id;
id = ctx.request.query.id;
_context10.next = 5;
return userInst.findById(id);
case 5:
result = _context10.sent;
return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(result));
return _context10.abrupt('return', ctx.body = _yapi2.default.commons.resReturn({
uid: result._id,
username: result.username,
email: result.email,
role: result.role,
add_time: result.add_time,
up_time: result.up_time
}));
case 9:
_context10.prev = 9;
@ -785,8 +792,10 @@ var userController = function (_baseController) {
* 更新用户个人信息
* @interface /user/update
* @method POST
* @param username String
* @param email String
* @param uid 用户uid
* @param [role] 用户角色,只有管理员有权限修改
* @param [username] String
* @param [email] String
* @category user
* @foldnumber 10
* @returns {Object}
@ -797,62 +806,75 @@ var userController = function (_baseController) {
key: 'update',
value: function () {
var _ref12 = (0, _asyncToGenerator3.default)(_regenerator2.default.mark(function _callee12(ctx) {
var userInst, id, data, checkRepeat, result;
var params, userInst, id, data, checkRepeat, result;
return _regenerator2.default.wrap(function _callee12$(_context12) {
while (1) {
switch (_context12.prev = _context12.next) {
case 0:
_context12.prev = 0;
params = ctx.request.body;
if (!(this.getRole() !== 'admin' && params.uid != this.getUid())) {
_context12.next = 4;
break;
}
return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '没有权限'));
case 4:
userInst = _yapi2.default.getInst(_user2.default);
id = this.getUid();
id = params.uid;
data = {
up_time: _yapi2.default.commons.time()
};
ctx.request.body.username && (data.username = ctx.request.body.username);
ctx.request.body.email && (data.email = ctx.request.body.email);
if (this.getRole() === 'admin') {
params.role && (data.role = params.role);
}
params.username && (data.username = params.username);
params.email && (data.email = params.email);
if (!data.email) {
_context12.next = 12;
_context12.next = 16;
break;
}
_context12.next = 9;
_context12.next = 13;
return userInst.checkRepeat(data.email);
case 9:
case 13:
checkRepeat = _context12.sent;
if (!(checkRepeat > 0)) {
_context12.next = 12;
_context12.next = 16;
break;
}
return _context12.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 401, '该email已经注册'));
case 12:
_context12.next = 14;
case 16:
_context12.next = 18;
return userInst.update(id, data);
case 14:
case 18:
result = _context12.sent;
ctx.body = _yapi2.default.commons.resReturn(result);
_context12.next = 21;
_context12.next = 25;
break;
case 18:
_context12.prev = 18;
case 22:
_context12.prev = 22;
_context12.t0 = _context12['catch'](0);
ctx.body = _yapi2.default.commons.resReturn(null, 402, _context12.t0.message);
case 21:
case 25:
case 'end':
return _context12.stop();
}
}
}, _callee12, this, [[0, 18]]);
}, _callee12, this, [[0, 22]]);
}));
function update(_x13) {

View File

@ -51,7 +51,10 @@ var userModel = function (_baseModel) {
key: 'getSchema',
value: function getSchema() {
return {
username: String,
username: {
type: String,
required: true
},
password: {
type: String,
required: true

View File

@ -1 +0,0 @@
[]