fix: 全局查询接口

This commit is contained in:
gaoxiaolin.gao 2018-06-11 15:17:39 +08:00
parent 14e96783c4
commit b922c345af
5 changed files with 160 additions and 77 deletions

View File

@ -1,115 +1,174 @@
import React, { PureComponent as Component } from 'react' import React, { PureComponent as Component } from 'react';
import PropTypes from 'prop-types' import PropTypes from 'prop-types';
import { connect } from 'react-redux' import { connect } from 'react-redux';
import { Icon, Input, AutoComplete } from 'antd' import { Icon, Input, AutoComplete } from 'antd';
import './Search.scss' import './Search.scss';
import { withRouter } from 'react-router'; import { withRouter } from 'react-router';
import axios from 'axios'; import axios from 'axios';
import { setCurrGroup } from '../../../reducer/modules/group' import { setCurrGroup, fetchGroupMsg } from '../../../reducer/modules/group';
import { changeMenuItem } from '../../../reducer/modules/menu' import { changeMenuItem } from '../../../reducer/modules/menu';
import { setBreadcrumb } from '../../../reducer/modules/user';
const Option = AutoComplete.Option; const Option = AutoComplete.Option;
@connect( @connect(
state => ({ state => ({
groupList: state.group.groupList, groupList: state.group.groupList,
projectList: state.project.projectList currGroup: state.group.currGroup,
}),{ projectList: state.project.projectList,
currProject: state.project.currProject
}),
{
setCurrGroup, setCurrGroup,
changeMenuItem changeMenuItem,
setBreadcrumb,
fetchGroupMsg
} }
) )
@withRouter @withRouter
export default class Srch extends Component{ export default class Srch extends Component {
constructor(props){ constructor(props) {
super(props); super(props);
this.state = { this.state = {
dataSource:[] dataSource: []
}; };
} }
static propTypes = { static propTypes = {
groupList : PropTypes.array, groupList: PropTypes.array,
projectList: PropTypes.array, projectList: PropTypes.array,
router: PropTypes.object, router: PropTypes.object,
history: PropTypes.object, history: PropTypes.object,
location: PropTypes.object, location: PropTypes.object,
setCurrGroup: PropTypes.func, setCurrGroup: PropTypes.func,
changeMenuItem : PropTypes.func changeMenuItem: PropTypes.func,
} setBreadcrumb: PropTypes.func,
fetchGroupMsg: PropTypes.func
};
onSelect = (value,option) => { onSelect = async (value, option) => {
if( option.props.tpye == "分组" ){ console.log('option', option.props);
console.log('value', value);
if (option.props.type == '分组') {
this.props.changeMenuItem('/group'); this.props.changeMenuItem('/group');
this.props.history.push('/group/'+value); this.props.history.push('/group/' + option.props['id']);
this.props.setCurrGroup({"group_name":value,"_id":option.props['id']}); this.props.setCurrGroup({ group_name: value, _id: option.props['id']-0 });
} else { } else if(option.props.type == '项目') {
this.props.history.push('/project/'+option.props['id']); await this.props.fetchGroupMsg(option.props['groupId'])
this.props.history.push('/project/' + option.props['id']);
} else if (option.props.type == '接口') {
// console.log('currProject', this.props.currProject);
this.props.changeMenuItem('/project');
this.props.history.push('/project/' + option.props['projectId']+'/interface/api/'+option.props['id']);
} }
}
handleSearch = (value) => { };
axios.get('/api/project/search?q='+value)
.then((res) => { handleSearch = value => {
if(res.data && res.data.errcode === 0){ axios
.get('/api/project/search?q=' + value)
.then(res => {
if (res.data && res.data.errcode === 0) {
const dataSource = []; const dataSource = [];
for(let title in res.data.data) { for (let title in res.data.data) {
res.data.data[title].map(item => { res.data.data[title].map(item => {
dataSource.push( switch (title) {
title == "group" ? case 'group':
( <Option dataSource.push(
key={`${item._id}`} <Option
tpye="分组" key={`分组${item._id}`}
value={`${item.groupName}`} type="分组"
id={`${item._id}`} value={`${item.groupName}`}
> id={`${item._id}`}
{`分组: ${item.groupName}`} >
</Option>) : {`分组: ${item.groupName}`}
(<Option </Option>
key={`${item._id}`} );
tpye="项目" break;
value={`${item._id}`} case 'project':
id={`${item._id}`} dataSource.push(
> <Option
{`项目: ${item.name}`} key={`项目${item._id}`}
</Option>) type="项目"
) id={`${item._id}`}
}) groupId={`${item.groupId}`}
>
{`项目: ${item.name}`}
</Option>
);
break;
case 'interface':
dataSource.push(
<Option
key={`接口${item._id}`}
type="接口"
id={`${item._id}`}
projectId={`${item.projectId}`}
>
{`接口: ${item.title}`}
</Option>
)
break;
default:
break;
}
// dataSource.push(
// // title == "group" ?
// // ( <Option
// // key={`${item._id}`}
// // tpye="分组"
// // value={`${item.groupName}`}
// // id={`${item._id}`}
// // >
// // {`分组: ${item.groupName}`}
// // </Option>) :
// // (<Option
// // key={`${item._id}`}
// // tpye="项目"
// // value={`${item._id}`}
// // id={`${item._id}`}
// // >
// // {`项目: ${item.name}`}
// // </Option>)
// )
});
} }
this.setState({ this.setState({
dataSource: dataSource dataSource: dataSource
}); });
}else{ } else {
console.log("查询项目或分组失败"); console.log('查询项目或分组失败');
} }
}) })
.catch((err) => { .catch(err => {
console.log(err); console.log(err);
}) });
} };
getDataSource(groupList){ // getDataSource(groupList){
const groupArr =[]; // const groupArr =[];
groupList.forEach(item =>{ // groupList.forEach(item =>{
groupArr.push("group: "+ item["group_name"]); // groupArr.push("group: "+ item["group_name"]);
}) // })
return groupArr; // return groupArr;
} // }
render(){ render() {
const { dataSource } = this.state; const { dataSource } = this.state;
return( return (
<div className="search-wrapper"> <div className="search-wrapper">
<AutoComplete <AutoComplete
className="search-dropdown" className="search-dropdown"
dataSource={dataSource} dataSource={dataSource}
style={{ width: '100%' }} style={{ width: '100%' }}
defaultActiveFirstOption= {false} defaultActiveFirstOption={false}
onSelect={this.onSelect} onSelect={this.onSelect}
onSearch={this.handleSearch} onSearch={this.handleSearch}
filterOption={(inputValue, option) => option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1} filterOption={(inputValue, option) =>
option.props.children.toUpperCase().indexOf(inputValue.toUpperCase()) !== -1
}
> >
<Input <Input
prefix={<Icon type="search" className="srch-icon" />} prefix={<Icon type="search" className="srch-icon" />}
@ -118,6 +177,6 @@ export default class Srch extends Component{
/> />
</AutoComplete> </AutoComplete>
</div> </div>
) );
} }
} }

View File

@ -60,7 +60,7 @@ export default class Project extends Component {
const nextProjectId = nextProps.match.params.id; const nextProjectId = nextProps.match.params.id;
if(currProjectId !== nextProjectId) { if(currProjectId !== nextProjectId) {
await this.props.getProject(nextProjectId); await this.props.getProject(nextProjectId);
await this.props.fetchGroupMsg(this.props.currGroup._id);
this.props.setBreadcrumb([{ this.props.setBreadcrumb([{
name: this.props.currGroup.group_name, name: this.props.currGroup.group_name,
href: '/group/' + this.props.currGroup._id href: '/group/' + this.props.currGroup._id

View File

@ -101,12 +101,17 @@ class groupController extends baseController {
let groupInst = yapi.getInst(groupModel); let groupInst = yapi.getInst(groupModel);
let result = await groupInst.getGroupById(params.id); let result = await groupInst.getGroupById(params.id);
result = result.toObject(); console.log('result', result);
result.role = await this.getProjectRole(params.id, 'group'); if(result) {
if (result.type === 'private') { result = result.toObject();
result.group_name = '个人空间'; let role = await this.getProjectRole(params.id, 'group');
result.role = role;
if (result.type === 'private') {
result.group_name = '个人空间';
}
ctx.body = yapi.commons.resReturn(result);
} }
ctx.body = yapi.commons.resReturn(result);
} }
/** /**

View File

@ -972,7 +972,7 @@ class projectController extends baseController {
} }
/** /**
* 模糊搜索项目名称或者组名称 * 模糊搜索项目名称或者名称或接口名称
* @interface /project/search * @interface /project/search
* @method GET * @method GET
* @category project * @category project
@ -994,6 +994,8 @@ class projectController extends baseController {
let projectList = await this.Model.search(q); let projectList = await this.Model.search(q);
let groupList = await this.groupModel.search(q); let groupList = await this.groupModel.search(q);
let interfaceList = await this.interfaceModel.search(q);
let projectRules = [ let projectRules = [
'_id', '_id',
'name', 'name',
@ -1013,13 +1015,23 @@ class projectController extends baseController {
{ key: 'add_time', alias: 'addTime' }, { key: 'add_time', alias: 'addTime' },
{ key: 'up_time', alias: 'upTime' } { key: 'up_time', alias: 'upTime' }
]; ];
let interfaceRules = [
'_id',
'uid',
{ key: 'title', alias: 'title' },
{ key: 'project_id', alias: 'projectId' },
{ key: 'add_time', alias: 'addTime' },
{ key: 'up_time', alias: 'upTime' }
];
projectList = commons.filterRes(projectList, projectRules); projectList = commons.filterRes(projectList, projectRules);
groupList = commons.filterRes(groupList, groupRules); groupList = commons.filterRes(groupList, groupRules);
interfaceList = commons.filterRes(interfaceList, interfaceRules);
let queryList = { let queryList = {
project: projectList, project: projectList,
group: groupList group: groupList,
interface: interfaceList
}; };
return (ctx.body = yapi.commons.resReturn(queryList, 0, 'ok')); return (ctx.body = yapi.commons.resReturn(queryList, 0, 'ok'));

View File

@ -275,7 +275,14 @@ class interfaceModel extends baseModel {
}, { }, {
index: index index: index
}) })
} }
search(keyword) {
return this.model.find({
title: new RegExp(keyword, 'ig')
})
.limit(10);
}
} }