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

View File

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

View File

@ -101,12 +101,17 @@ class groupController extends baseController {
let groupInst = yapi.getInst(groupModel);
let result = await groupInst.getGroupById(params.id);
result = result.toObject();
result.role = await this.getProjectRole(params.id, 'group');
if (result.type === 'private') {
result.group_name = '个人空间';
console.log('result', result);
if(result) {
result = result.toObject();
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
* @method GET
* @category project
@ -994,6 +994,8 @@ class projectController extends baseController {
let projectList = await this.Model.search(q);
let groupList = await this.groupModel.search(q);
let interfaceList = await this.interfaceModel.search(q);
let projectRules = [
'_id',
'name',
@ -1013,13 +1015,23 @@ class projectController extends baseController {
{ key: 'add_time', alias: 'addTime' },
{ 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);
groupList = commons.filterRes(groupList, groupRules);
interfaceList = commons.filterRes(interfaceList, interfaceRules);
let queryList = {
project: projectList,
group: groupList
group: groupList,
interface: interfaceList
};
return (ctx.body = yapi.commons.resReturn(queryList, 0, 'ok'));

View File

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