mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
调整搜索实现-从前端过滤改为后端查询
This commit is contained in:
@@ -271,4 +271,15 @@ public class AgentController {
|
||||
.body(audioData);
|
||||
}
|
||||
|
||||
@GetMapping("/search")
|
||||
@Operation(summary = "搜索智能体")
|
||||
@RequiresPermissions("sys:role:normal")
|
||||
public Result<List<AgentDTO>> searchAgent(
|
||||
@RequestParam("keyword") String keyword,
|
||||
@RequestParam(value = "searchType", defaultValue = "name") String searchType) {
|
||||
UserDetail user = SecurityUser.getUser();
|
||||
List<AgentDTO> agents = agentService.searchAgent(keyword, searchType, user.getId());
|
||||
return new Result<List<AgentDTO>>().ok(agents);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -98,4 +98,14 @@ public interface AgentService extends BaseService<AgentEntity> {
|
||||
* @return 创建的智能体ID
|
||||
*/
|
||||
String createAgent(AgentCreateDTO dto);
|
||||
|
||||
/**
|
||||
* 搜索智能体
|
||||
*
|
||||
* @param keyword 搜索关键词
|
||||
* @param searchType 搜索类型:name(按名称搜索)或mac(按MAC地址搜索)
|
||||
* @param userId 用户ID
|
||||
* @return 智能体列表
|
||||
*/
|
||||
List<AgentDTO> searchAgent(String keyword, String searchType, Long userId);
|
||||
}
|
||||
|
||||
+39
@@ -478,4 +478,43 @@ public class AgentServiceImpl extends BaseServiceImpl<AgentDao, AgentEntity> imp
|
||||
agentPluginMappingService.saveBatch(toInsert);
|
||||
return entity.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AgentDTO> searchAgent(String keyword, String searchType, Long userId) {
|
||||
if (StringUtils.isBlank(keyword)) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
QueryWrapper<AgentEntity> queryWrapper = new QueryWrapper<>();
|
||||
queryWrapper.eq("user_id", userId);
|
||||
|
||||
if ("mac".equals(searchType)) {
|
||||
// 按MAC地址搜索:先搜索设备,再获取对应的智能体
|
||||
List<DeviceEntity> devices = deviceService.searchDevicesByMacAddress(keyword, userId);
|
||||
|
||||
if (devices.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 获取设备对应的智能体ID列表
|
||||
List<String> agentIds = devices.stream()
|
||||
.map(DeviceEntity::getAgentId)
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
if (agentIds.isEmpty()) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
// 查询智能体
|
||||
queryWrapper.in("id", agentIds);
|
||||
} else {
|
||||
// 按名称搜索
|
||||
queryWrapper.like("agent_name", keyword);
|
||||
}
|
||||
|
||||
// 执行查询
|
||||
List<AgentEntity> agentEntities = baseDao.selectList(queryWrapper);
|
||||
return ConvertUtils.sourceToTarget(agentEntities, AgentDTO.class);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,4 +108,13 @@ public interface DeviceService extends BaseService<DeviceEntity> {
|
||||
*/
|
||||
String generateWebSocketToken(String clientId, String username) throws Exception;
|
||||
|
||||
/**
|
||||
* 根据MAC地址搜索设备
|
||||
*
|
||||
* @param macAddress MAC地址关键词
|
||||
* @param userId 用户ID
|
||||
* @return 设备列表
|
||||
*/
|
||||
List<DeviceEntity> searchDevicesByMacAddress(String macAddress, Long userId);
|
||||
|
||||
}
|
||||
+8
@@ -496,6 +496,14 @@ public class DeviceServiceImpl extends BaseServiceImpl<DeviceDao, DeviceEntity>
|
||||
redisUtils.delete(RedisKeys.getAgentDeviceCountById(dto.getAgentId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<DeviceEntity> searchDevicesByMacAddress(String macAddress, Long userId) {
|
||||
QueryWrapper<DeviceEntity> wrapper = new QueryWrapper<>();
|
||||
wrapper.like("mac_address", macAddress);
|
||||
wrapper.eq("user_id", userId);
|
||||
return deviceDao.selectList(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成MQTT密码签名
|
||||
*
|
||||
|
||||
@@ -382,4 +382,24 @@ export default {
|
||||
});
|
||||
}).send();
|
||||
},
|
||||
|
||||
// 搜索智能体
|
||||
searchAgent(keyword, searchType, callback) {
|
||||
RequestService.sendRequest()
|
||||
.url(`${getServiceUrl()}/agent/search`)
|
||||
.method('GET')
|
||||
.data({
|
||||
keyword: keyword,
|
||||
searchType: searchType // searchType: 'name' 或 'mac'
|
||||
})
|
||||
.success((res) => {
|
||||
RequestService.clearRequestTime();
|
||||
callback(res);
|
||||
})
|
||||
.networkFail(() => {
|
||||
RequestService.reAjaxFun(() => {
|
||||
this.searchAgent(keyword, searchType, callback);
|
||||
});
|
||||
}).send();
|
||||
},
|
||||
}
|
||||
|
||||
@@ -408,18 +408,8 @@ export default {
|
||||
// 保存搜索历史
|
||||
this.saveSearchHistory(searchValue);
|
||||
|
||||
try {
|
||||
// 创建不区分大小写的正则表达式
|
||||
const regex = new RegExp(searchValue, "i");
|
||||
// 触发搜索事件,将正则表达式传递给父组件
|
||||
this.$emit("search", regex);
|
||||
} catch (error) {
|
||||
console.error("正则表达式创建失败:", error);
|
||||
this.$message.error({
|
||||
message: this.$t("message.error"),
|
||||
showClose: true,
|
||||
});
|
||||
}
|
||||
// 触发搜索事件,将搜索关键词传递给父组件
|
||||
this.$emit("search", searchValue);
|
||||
|
||||
// 搜索完成后让输入框失去焦点,从而触发blur事件隐藏搜索历史
|
||||
if (this.$refs.searchInput) {
|
||||
|
||||
@@ -119,34 +119,31 @@ export default {
|
||||
handleDeviceManage() {
|
||||
this.$router.push('/device-management');
|
||||
},
|
||||
handleSearch(regex) {
|
||||
handleSearch(keyword) {
|
||||
this.isSearching = true;
|
||||
this.searchRegex = regex;
|
||||
this.applySearchFilter();
|
||||
this.isLoading = true;
|
||||
// 检测MAC地址格式:包含4个冒号
|
||||
const isMac = /(:.*?){4}/.test(keyword);
|
||||
const searchType = isMac ? 'mac' : 'name';
|
||||
Api.agent.searchAgent(keyword, searchType, ({ data }) => {
|
||||
if (data?.data) {
|
||||
this.devices = data.data.map(item => ({
|
||||
...item,
|
||||
agentId: item.id
|
||||
}));
|
||||
}
|
||||
this.isLoading = false;
|
||||
}, (error) => {
|
||||
console.error('搜索智能体失败:', error);
|
||||
this.isLoading = false;
|
||||
this.$message.error(this.$t('message.searchFailed'));
|
||||
});
|
||||
},
|
||||
handleSearchReset() {
|
||||
this.isSearching = false;
|
||||
this.searchRegex = null;
|
||||
this.devices = [...this.originalDevices];
|
||||
this.fetchAgentList();
|
||||
},
|
||||
applySearchFilter() {
|
||||
if (!this.isSearching || !this.searchRegex) {
|
||||
this.devices = [...this.originalDevices];
|
||||
return;
|
||||
}
|
||||
|
||||
this.devices = this.originalDevices.filter(device => {
|
||||
// 搜索智能体名称
|
||||
if (this.searchRegex.test(device.agentName)) {
|
||||
return true;
|
||||
}
|
||||
// 搜索关联设备的MAC地址
|
||||
if (device.macAddresses && device.macAddresses.length > 0) {
|
||||
return device.macAddresses.some(macAddress => this.searchRegex.test(macAddress));
|
||||
}
|
||||
return false;
|
||||
});
|
||||
},
|
||||
// 搜索更新智能体列表
|
||||
handleSearchResult(filteredList) {
|
||||
this.devices = filteredList; // 更新设备列表
|
||||
|
||||
Reference in New Issue
Block a user