Skip to content

Commit

Permalink
fix: 搜索机器人客户 插件
Browse files Browse the repository at this point in the history
  • Loading branch information
ldq1220 committed Aug 26, 2024
1 parent 9e0be30 commit 730a315
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 54 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "@nocobase-sample/plugin-bind-bot-relation",
"version": "1.3.1-alpha",
"version": "1.3.4-alpha",
"main": "dist/server/index.js",
"displayName": "Bind the robot crowd/friend relationship",
"displayName.zh-CN": "绑定机器人群/好友关系",
"description": "Call the enterprise brain to search the robot crowd/friend relationship data, select and bind customers.",
"description.zh-CN": "调用企业大脑搜索机器人群/好友关系数据,选择并绑定客户。124",
"description.zh-CN": "调用企业大脑搜索机器人群/好友关系数据,选择并绑定客户。",
"dependencies": {},
"peerDependencies": {
"@nocobase/client": "1.x",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ declare module '*.module.sss' {
}

// CSS
declare module '*.css' { }
declare module '*.scss' { }
declare module '*.sass' { }
declare module '*.less' { }
declare module '*.styl' { }
declare module '*.stylus' { }
declare module '*.pcss' { }
declare module '*.sss' { }
declare module '*.css' {}
declare module '*.scss' {}
declare module '*.sass' {}
declare module '*.less' {}
declare module '*.styl' {}
declare module '*.stylus' {}
declare module '*.pcss' {}
declare module '*.sss' {}

// Built-in asset types
// see `src/node/constants.ts`
Expand Down Expand Up @@ -197,14 +197,14 @@ declare module '*.wasm?init' {
// web worker
declare module '*?worker' {
const workerConstructor: {
new(options?: { name?: string }): Worker;
new (options?: { name?: string }): Worker;
};
export default workerConstructor;
}

declare module '*?worker&inline' {
const workerConstructor: {
new(options?: { name?: string }): Worker;
new (options?: { name?: string }): Worker;
};
export default workerConstructor;
}
Expand All @@ -216,14 +216,14 @@ declare module '*?worker&url' {

declare module '*?sharedworker' {
const sharedWorkerConstructor: {
new(options?: { name?: string }): SharedWorker;
new (options?: { name?: string }): SharedWorker;
};
export default sharedWorkerConstructor;
}

declare module '*?sharedworker&inline' {
const sharedWorkerConstructor: {
new(options?: { name?: string }): SharedWorker;
new (options?: { name?: string }): SharedWorker;
};
export default sharedWorkerConstructor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,44 @@ import { TableView } from './TableView';
import axios from 'axios';

export interface BotDetailsProps {
BotField?: string;
BotField: string;
SearchScope: number;
}

// 查个人信息 获取所属协会信息
const useSearchAssicontionInfo = (associationUrl: string) => {
let associationId = null;
const { data } = useRequest<{ data: any }>({ url: '/auth:check' });
associationId = data?.data.f_business_association_id || data?.data.f_belong_association_id;
// 查商会表 获取该协会信息
const { data: associationData } = useRequest<{ data: any }>(
{ url: associationUrl + associationId },
{
ready: !!associationId, // 只有在 associationId 有值时才触发请求
},
);
const apiKey = associationData?.data['api-key'] || associationData?.data['api_key'];
return { apiKey };
};

export const BotDetails: FC<BotDetailsProps> = observer(
({ BotField }) => {
({ BotField, SearchScope }) => {
const form = useForm();
const value = BotField ? form.values[BotField] : undefined;
if (!BotField) return <div style={{ padding: 20 }}>请选择绑定字段</div>;

let xAppApiKey = null;
let associationId = null;
const baseUrl = 'http://192.168.11.124:4000/api/';
const associationUrl = '/association:get/';
// const baseUrl = 'https://api.gemelai.com/api/';
// const associationUrl = '/business_association:get/';
const searchGroup = [1, 3].includes(SearchScope);
const searchFriend = [2, 3].includes(SearchScope);

// 查个人信息 获取所属协会id
const { data } = useRequest<{ data: any }>({ url: '/auth:check' });
associationId = data?.data.f_business_association_id || data?.data.f_belong_association_id;
// 查商会表 获取该协会信息
const { data: associationData } = useRequest<{ data: any }>(
{ url: associationUrl + associationId },
{
ready: !!associationId, // 只有在 associationId 有值时才触发请求
},
);
xAppApiKey = associationData?.data['api-key'] || associationData?.data['api_key'];
// const baseUrl = 'http://192.168.11.124:4000/api/';
// const baseUrl = 'https://brain-api.we17.fun/api/';
// const associationUrl = '/association:get/';
const baseUrl = 'https://api.gemelai.com/api/';
const associationUrl = '/business_association:get/';

const { apiKey } = useSearchAssicontionInfo(associationUrl); // 获取apiKey
xAppApiKey = apiKey;

localStorage.setItem(
'PLUGIN-BIND-BOT-RELATION',
Expand All @@ -65,7 +76,7 @@ export const BotDetails: FC<BotDetailsProps> = observer(
`${baseUrl}open/bot/bind-groups`,
{
keyword: value,
limit: 2,
limit: 3,
},
{
headers: {
Expand All @@ -75,7 +86,7 @@ export const BotDetails: FC<BotDetailsProps> = observer(
},
),
{
ready: !!value && !!xAppApiKey,
ready: !!value && !!xAppApiKey && searchGroup,
refreshDeps: [BotField, value],
debounceWait: 300,
},
Expand All @@ -86,7 +97,7 @@ export const BotDetails: FC<BotDetailsProps> = observer(
`${baseUrl}open/bot/bind-friends`,
{
keyword: value,
limit: 2,
limit: 3,
},
{
headers: {
Expand All @@ -96,22 +107,16 @@ export const BotDetails: FC<BotDetailsProps> = observer(
},
),
{
ready: !!value && !!xAppApiKey,
ready: !!value && !!xAppApiKey && searchFriend,
refreshDeps: [BotField, value],
debounceWait: 300,
},
);
const Error: any = error;

if (Error || !xAppApiKey) {
return !value ? (
<Empty description="暂无数据" />
) : !xAppApiKey ? (
<Empty description="API秘钥缺失,请完善信息。" />
) : (
<Empty description={<Typography.Text>{Error?.response?.data?.message}</Typography.Text>} />
);
}
if (!value) return <Empty description="暂无数据" />;
if (!xAppApiKey) return <Empty description="API秘钥缺失,请完善信息。" />;
if (Error) return <Empty description={<Typography.Text>{Error?.response?.data?.message}</Typography.Text>} />;

if (groupLoading || friendLoading) {
return (
Expand All @@ -121,8 +126,8 @@ export const BotDetails: FC<BotDetailsProps> = observer(
);
}

const groupDataResult = groupData ? groupData.data?.result : [];
const friendDataResult = friendData ? friendData.data?.result : [];
const groupDataResult = groupData && searchGroup ? groupData.data?.result : [];
const friendDataResult = friendData && searchFriend ? friendData.data?.result : [];
if (groupDataResult.length === 0 && friendDataResult.length === 0) return <Empty description="暂无数据" />;

return <TableView data={[...groupDataResult, ...friendDataResult]} />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
useCollection,
useCompile,
useSchemaInitializer,
SchemaSettingsDefaultValue,
} from '@nocobase/client';
import { SearchOutlined } from '@ant-design/icons';
import { FieldNameLowercase } from '../constants';
Expand All @@ -32,6 +31,15 @@ export function useFieldOptions(): SelectProps['options'] {
.map((field) => ({ label: field.uiSchema?.title ? compile(field.uiSchema.title) : field.name, value: field.name }));
}

export function useSearchScopeOptions(): SelectProps['options'] {
const searchScopeOptions: any = [
{ label: '群', value: 1 },
{ label: '好友', value: 2 },
{ label: '群 + 好友', value: 3 },
];
return searchScopeOptions.map((field: any) => ({ label: field.label, value: field.value }));
}

const BotDetailsSchemaInitializer = () => {
const t = useT();
const { insert } = useSchemaInitializer();
Expand All @@ -48,8 +56,8 @@ const BotDetailsSchemaInitializer = () => {
title={t('Select bind Field')}
icon={<SearchOutlined />}
isItem
onSubmit={({ BotField }) => {
insert(getBotDetailsSchema(BotField));
onSubmit={({ BotField, SearchScope }) => {
insert(getBotDetailsSchema(BotField, SearchScope));
}}
schema={{
BotField: {
Expand All @@ -63,6 +71,17 @@ const BotDetailsSchemaInitializer = () => {
'x-decorator': 'FormItem',
enum: options,
},
SearchScope: {
type: 'number',
title: t('Search scope'),
required: true,
'x-component': 'Select',
'x-component-props': {
placeholder: t('Please select search scope'),
},
'x-decorator': 'FormItem',
enum: useSearchScopeOptions(),
},
// ApiKey: {
// type: 'string',
// title: 'API Key',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import { ISchema } from '@nocobase/client';
import { FieldComponentName } from '../constants';
import { botDetailsSettings } from '../settings';

export const getBotDetailsSchema = (BotField: string): ISchema => ({
export const getBotDetailsSchema = (BotField: string, SearchScope: number): ISchema => ({
type: 'void',
'x-decorator': 'FormItem',
'x-toolbar': 'FormItemSchemaToolbar',
'x-component': FieldComponentName,
'x-component-props': {
BotField: BotField,
BotField,
SearchScope,
},
'x-settings': botDetailsSettings.name,
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@

import { SchemaSettings } from '@nocobase/client';
import { FieldNameLowercase } from '../constants';
import { botFieldSchemaSettingsItem } from './items/botField';
import { botFieldSchemaSettingsItem, changeSearchFieldSchemaSettingsItem } from './items/botField';

export const botDetailsSettings = new SchemaSettings({
name: `blockSettings:${FieldNameLowercase}`,
items: [
botFieldSchemaSettingsItem,
changeSearchFieldSchemaSettingsItem,
{
name: 'remove',
type: 'remove',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@

import { createSelectSchemaSettingsItem } from '@nocobase/client';
import { tStr } from '../../locale';
import { useFieldOptions } from '../../initializer';
import { useFieldOptions, useSearchScopeOptions } from '../../initializer';

export const botFieldSchemaSettingsItem = createSelectSchemaSettingsItem({
name: 'BotField',
title: tStr('Bind field'),
useOptions: useFieldOptions,
schemaKey: `x-component-props.BotField`,
});

export const changeSearchFieldSchemaSettingsItem = createSelectSchemaSettingsItem({
name: 'ChangeSearchField',
title: tStr('Search scope'),
useOptions: useSearchScopeOptions,
schemaKey: `x-component-props.SearchScope`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"Bot Details": "Bot Details",
"Select bind Field": "Select bind Field",
"Please select a field": "Please select a field",
"Search scope": "Search scope",
"Please select search scope": "Please select search scope",
"Bind field": "Bind field"
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"Bot Details": "搜索机器人客户",
"Select bind Field": "选择绑定字段",
"Please select a field": "请选择绑定字段",
"Search scope": "搜索范围",
"Please select search scope": "请选择搜索范围",
"Bind field": "绑定字段"
}

0 comments on commit 730a315

Please sign in to comment.