Compare commits

...

272 Commits

Author SHA1 Message Date
kuaifan
055cf53738 build 2026-01-14 22:31:27 +08:00
kuaifan
cb414b48f6 refactor: 优化窗口关闭拦截机制,采用声明式注册
- 将 onBeforeUnload 从 utils.js 移至 web-tab-manager.js
- 新增声明式拦截注册机制,前端通过 registerCloseInterceptor 声明需要拦截
- 仅对已声明拦截的页面执行 JS 检查,未声明的直接关闭
- 添加 5 秒超时保护,防止网页卡死导致无法关闭窗口
- 修复 command+w 快捷键关闭整个窗口而非当前 tab 的问题
2026-01-14 22:29:36 +08:00
kuaifan
1c27719ac4 no message 2026-01-14 20:15:48 +08:00
kuaifan
ec33327408 fix: 修复文件夹上传时数据库死锁问题
使用 Redis 分布式锁对同一用户往相同父目录的上传请求进行排队,
  避免并发上传导致的 MySQL 死锁错误 (SQLSTATE[40001])
2026-01-14 11:44:47 +00:00
kuaifan
c2c27a684b feat: 复制/周期任务时复制子任务并重置状态
- 复制任务时同时复制子任务,子任务状态重置为未完成
  - 周期任务生成时,子任务状态重置为未完成并映射到 start 工作流
  - 新增 getProjectFlowItems 方法获取项目工作流状态
  - 新增 formatFlowItemName 方法格式化工作流状态名称
  - 新增 copySubTasks 方法复制子任务到新父任务
  - 新增 moveSubTasks 方法移动子任务,重构 moveTask 复用代码
2026-01-14 11:31:28 +00:00
kuaifan
224703a6d0 feat: 支持输入法组合状态,优化输入框键盘事件处理 2026-01-14 10:11:28 +00:00
kuaifan
dd20711c04 refactor: 移除冗余日志记录,优化代码可读性 2026-01-14 09:41:06 +00:00
kuaifan
3a2b7b1400 feat: 新增 AI 提示词占位符与用户上下文注入
- 新增 PromptPlaceholder 模块,负责构建用户上下文和条件性提示块
  - 用户上下文包含:基础信息、部门、同事印象、场景角色、任务列表
  - 前端使用 {{SYSTEM_OPTIONAL_PROMPTS}} 占位符,后端统一替换为实际内容
  - 重构 BotReceiveMsgTask 和 ai.js,复用 PromptPlaceholder 逻辑
  - 任务列表支持智能排序:逾期优先 → 最近活跃 → 负责人优先
2026-01-14 09:33:20 +00:00
kuaifan
792989a504 refactor: 统一 webTab 事件分发逻辑
新增 dispatchToTabBar() 函数,封装 window 模式检查逻辑:
  - window 模式无标签栏,跳过 executeJavaScript 调用
  - 避免 did-stop-loading 监听器累积导致 MaxListenersExceededWarning
  - 统一 14 处调用点,提升代码一致性和可维护性
2026-01-14 13:41:28 +08:00
kuaifan
c0183e62fb style: 统一 webTab 主题配色风格
- 深色模式:背景 #202124,活跃Tab #323639,文字 #D6D6D7
  - 浅色模式:背景 #F1F3F4,活跃Tab #FFFFFF,文字 #5F6368
  - 同步更新 WebView 默认背景色和加载页背景色
  - 更新 earth 图标选中态颜色适配新主题
  - 删除未使用的 link 图标资源
  - 语言切换时重建预加载池
2026-01-14 11:50:15 +08:00
kuaifan
ce5bb5f187 refactor: 统一 webTab 背景色设置逻辑
- 移除 createWebTabView 中冗余的深色/浅色主题背景色判断分支
  - 统一使用 utils.getDefaultBackgroundColor() 获取默认背景色
  - 移除 did-stop-loading 事件中不必要的背景色重置逻辑
2026-01-14 10:14:31 +08:00
kuaifan
a34b0c88d5 refactor: 优化 webTab 管理和状态同步
- 封装 safeCloseWebTab 方法,复用标签关闭时的未保存数据检查逻辑
  - 添加 recreatePreloadPool,支持主题切换后重建预加载池
  - broadcastCommand 扩展到 webTab views,确保子窗口收到同步消息
  - 修复 synchTheme 和 saveDialogDraft 的跨窗口参数传递
  - IDBDel 返回 Promise 并正确 await
2026-01-14 10:11:41 +08:00
kuaifan
9c7ec58bb6 no message 2026-01-14 09:14:35 +08:00
kuaifan
067a736b57 fix: 恢复窗口/标签关闭时的未保存数据检查
恢复 onBeforeUnload 功能,防止关闭窗口或标签时丢失未保存的数据:
  - 快捷键关闭:检查当前激活标签的 onBeforeUnload
  - 点击窗口关闭按钮:依次检查所有标签,遇到拦截时激活对应标签
  - 点击 tab 关闭按钮:检查对应标签的 onBeforeUnload
  - 重构 close 事件处理,使用 early return 简化代码结构
2026-01-13 14:49:59 +00:00
kuaifan
f8f08c9d0d no message 2026-01-13 14:48:05 +00:00
kuaifan
4f2d382fd6 fix: 移除 Markdown 消息中的工具使用标签 2026-01-13 12:57:54 +00:00
kuaifan
42e4ddbd17 fix: 修复权限级联同步缺口
修复 Manticore 搜索索引在特定场景下 allowed_users 权限未能正确同步的问题:

  Observer.updated 补充:
  - ProjectUserObserver: 处理项目成员移交时的权限级联
  - ProjectTaskUserObserver: 处理任务成员移交时的权限更新

  批量操作绕过 Observer 修复(delete → remove):
  - FileUser: deleteFileAll/deleteFileUser 方法
  - ProjectTask: 可见性设置时的批量删除
  - ProjectController: 子任务升级和任务复制时的批量删除

  文件批量更新封装:
  - File 新增 updateChildFilesUserid() 方法,统一处理子文件 userid
    更新及 Manticore 同步
2026-01-13 11:55:45 +00:00
kuaifan
3026cd698f feat: 添加文本换行样式以改善审批详情的可读性 2026-01-13 10:54:13 +00:00
kuaifan
47c53a18fa fix: 修复跨项目移动任务时子任务工作流状态未更新的问题
跨项目移动任务时,子任务的 flow_item_id 和 flow_item_name 没有被正确更新,
  导致子任务在新项目中显示的工作流状态与新项目的工作流不匹配。
2026-01-13 10:50:42 +00:00
kuaifan
22926e19cd refactor: 统一 dootask:// 链接处理与资源格式指南
- 将 dootask:// 协议链接处理逻辑从 AIAssistant 迁移到 DialogMarkdown 组件
  - 新增 beforeNavigate prop 支持导航前回调(如关闭弹窗)
  - 后端 BotReceiveMsgTask 添加条件性资源格式指南提示词
  - 前端 ai.js 新增 SEARCH_AI_SYSTEM_PROMPT 和 DOOTASK_RESOURCE_FORMAT_GUIDE
  - SearchBox 改用统一的 SEARCH_AI_SYSTEM_PROMPT 常量
  - 重构 ai.js 代码组织,添加注释说明各常量用途
2026-01-13 10:31:31 +00:00
kuaifan
495b25e2b1 feat: 增强 MCP 配置助手,支持多种 AI 工具
- 新增 Tabs 组件展示多种 AI 工具的配置方式
  - 支持 Claude Code、Cursor、VS Code、Windsurf、Claude Desktop、
    Codex、Kiro、Trae、Antigravity、Opencode 等工具
  - 丰富使用示例,按任务管理、项目查询、工作汇报、团队协作、
    文件查找等分类展示
  - 优化国际化支持,使用 t() 函数替代 $L() 实现中英双语
2026-01-13 08:56:20 +00:00
kuaifan
01908b7c48 no message 2026-01-13 03:56:56 +00:00
kuaifan
b138dc580d refactor: 重构 MCP 工具并新增搜索功能
主要变更:
  - 新增 search_dialogs 工具,支持按名称搜索群聊或联系人
  - 新增 intelligent_search 统一搜索工具,支持任务/项目/文件/联系人/消息
  - 重构 send_message 工具,支持 dialog_id 或 userid 两种方式
  - 重构 get_message_list 工具,支持 dialog_id 或 userid
  - 优化 get_project 并行获取项目详情和列信息
  - 统一返回字段命名 (id -> task_id/project_id/file_id/report_id)
  - 修正 HTTP 方法 (POST 用于 add/remove 操作)
  - 精简工具描述文案
2026-01-13 02:23:14 +00:00
kuaifan
78b14f4aad feat: 添加 dialog_only 参数支持仅搜索对话
在 dialog/search 接口中增加 dialog_only 可选参数,
  启用后仅搜索会话和联系人,跳过消息内容搜索。
2026-01-12 15:03:04 +00:00
kuaifan
60387aa521 refactor: 优化注释 2026-01-12 09:09:24 +00:00
kuaifan
633826cb89 refactor: 迁移到 navigationHistory API
将已废弃的 webContents 导航方法迁移到新的 navigationHistory API:
  - canGoBack() → navigationHistory.canGoBack()
  - canGoForward() → navigationHistory.canGoForward()
  - goBack() → navigationHistory.goBack()
  - goForward() → navigationHistory.goForward()
2026-01-12 07:27:18 +00:00
kuaifan
cf6d180fc5 feat: 添加 webTab 预加载池机制
引入预加载池以优化 webTab 首屏加载性能:
  - 应用启动后延迟创建预加载 view,避免影响主窗口
  - 新建 tab 时优先复用已预加载的 view
  - 取走后自动延迟补充,保持池容量
  - 应用退出前清理预加载资源
2026-01-12 06:55:57 +00:00
kuaifan
0d85174250 feat: 添加 favicon 双层缓存机制
实现仿 Chrome 的 favicon 缓存系统:
  - 第一层:域名缓存 - 导航开始时立即查询,快速显示 favicon
  - 第二层:URL 缓存 - favicon URL 精确匹配
  - 支持内存缓存 + 文件持久化,应用启动时自动清理 30 天过期缓存
2026-01-12 05:40:57 +00:00
kuaifan
925449c66a refactor: 抽离 webTab 窗口管理为独立模块
将 electron.js 中 1000+ 行的 webTab 窗口管理逻辑抽离到
  electron/lib/web-tab-manager.js,提升代码可维护性
2026-01-12 05:15:32 +00:00
kuaifan
cd58b418af refactor: 新增 updateWindow 接口并移除废弃的预加载窗口调用
- 新增 updateWindow IPC handler,支持窗口/标签页内部导航时更新 URL 和名称
  - 将前端 updateChildWindow 调用替换为 updateWindow
  - 移除 reloadPreloadWindow 调用(预加载窗口已删除)
2026-01-12 01:44:34 +00:00
kuaifan
4cfc5e6024 refactor: 移除 userAgent 相关代码以简化窗口管理逻辑 2026-01-12 09:07:10 +08:00
kuaifan
7321ab06f0 refactor: 优化窗口尺寸和位置管理逻辑 2026-01-12 09:02:58 +08:00
kuaifan
790f5d4838 refactor: 统一 Electron 子窗口与标签页窗口管理
将原有独立子窗口 (childWindow) 和标签页窗口 (webTabWindow) 合并为统一的
  窗口管理系统,通过 mode 参数区分窗口类型:
  - mode='tab': 标签页模式(有导航栏,默认)
  - mode='window': 独立窗口模式(无导航栏)

  主要变更:
  - 移除 createChildWindow、preCreateChildWindow 等独立窗口相关代码
  - 扩展 createWebTabWindow 支持 mode 参数
  - 简化前端 openWindow 调用,将 config 对象扁平化为顶层参数
  - 更新所有调用点使用新的统一接口
2026-01-11 21:13:55 +00:00
kuaifan
731dbc5507 feat: 标签页新增更多菜单功能
- 新增更多菜单按钮替代原浏览器打开按钮
  - 实现重新加载、复制链接地址、默认浏览器打开功能
  - 实现将标签页移至新窗口功能
  - 实现打印功能
  - 菜单支持根据当前 URL 类型动态启用/禁用选项
  - 添加相关国际化文案
2026-01-10 16:35:19 +00:00
kuaifan
3b1dce6d67 feat: 标签页新增更多菜单按钮
- 将原浏览器打开按钮替换为更多菜单按钮
  - 添加 more.svg 图标并调整样式
  - 实现 webTabShowMenu 通信接口及菜单框架
2026-01-10 15:47:43 +00:00
kuaifan
4929d44ce7 refactor: 优化标签页加载状态管理与 URL 加载逻辑
- 新增 loadContentUrl 方法统一处理完整 URL 和相对路径的加载
  - 优化标签页加载状态,忽略 SPA 路由切换(isSameDocument),避免频繁闪烁
  - 添加定时检查器确保加载状态正确停止
  - windowClose/windowDestroy 支持识别 tab 页面发送者,仅关闭对应标签
  - 子窗口重启过程中不再意外销毁窗口
  - 微应用打开标签页时传递标题信息
  - isLocalHost 对空 URL 和相对路径返回 true
2026-01-10 15:44:58 +00:00
kuaifan
ce42c2a660 refactor(frontend): 统一域名获取与比较逻辑
- 新增 mainDomain() 函数,简化 mainUrl 域名获取
  - 新增 removeMainUrlPrefix() 函数,用于移除 URL 的服务器域名前缀
  - getDomain() 返回值统一转为小写,确保域名比较不受大小写影响
  - 将多处 getDomain(mainUrl()) 调用替换为 mainDomain(),提升代码可读性
2026-01-10 05:48:25 +00:00
kuaifan
16d5ffd4f9 refactor: 统一客户端窗口打开接口并支持标签页名称复用
- 合并 openChildWindow 和 openWebTabWindow 为统一的 openWindow 接口
  - 新增 webTabNameMap 映射,支持按名称查找和复用已存在的标签页
  - 标签页增加 name、titleFixed 元数据支持
  - 窗口间转移标签时同步更新名称映射
  - 重构前端 actions,统一使用 openWindow 方法,通过 mode 参数区分窗口/标签模式
  - 更新所有调用点使用新的统一接口
2026-01-10 02:08:36 +00:00
kuaifan
fc74e0d952 feat: 标签页拖拽合并时支持插入到鼠标所在位置
- getAllWebTabWindowsInfo 增加返回 tabCount 用于计算标签位置
  - attachToWindow 根据鼠标 screenX 和目标窗口标签信息计算插入位置
  - 拖拽标签合并到其他窗口时插入到鼠标位置而非总在末尾
2026-01-09 15:17:21 +00:00
kuaifan
089f219280 feat: 标签页拖拽创建新窗口时窗口定位优化及 favicon 验证
- 优化拖拽标签创建新窗口时的位置计算,使用 setPosition 确保窗口出现在鼠标位置
  - 重构 createWebTabWindowInstance 函数,仅在明确指定 x/y 时设置窗口坐标
  - 新增 fetchFaviconAsBase64 工具函数,在主进程验证 favicon 并转为 base64
  - favicon 验证后再保存和传递给前端,确保拖拽后 icon 状态与原窗口一致
  - 简化前端 favicon 处理逻辑,移除重复的图片验证代码
2026-01-09 13:58:22 +00:00
kuaifan
9d62ec1ec1 feat: 添加标签页拖拽排序功能
- 引入 Sortable.js 库以支持标签页的拖拽排序
- 实现标签页的动态插入和顺序重排
- 更新样式以适应拖拽效果
- 增加 IPC 通信以同步标签页顺序变化
- 优化标签页创建和关闭逻辑,提升用户体验
2026-01-09 15:46:02 +08:00
kuaifan
5a4e51d1e0 no message 2026-01-08 14:18:45 +00:00
kuaifan
f0982d7d9a efactor: 拆分 electron 主进程代码为独立模块
将 electron.js 中的 PDF 导出、渲染器辅助函数和工具函数拆分为独立模块:
  - electron/lib/pdf-export.js: PDF 导出相关功能
  - electron/lib/renderer.js: 渲染器辅助函数
  - electron/lib/other.js: 平台检测和 URL 验证常量

  此重构提高了代码可维护性,减少了主文件的复杂度。
2026-01-08 13:54:55 +00:00
kuaifan
1ac3a4cc96 feat: 添加 user_update hook 事件并重构用户生命周期 hook
- 新增 user_update 事件,当用户基本信息变更时触发
  - 扩展 dispatchUserHook payload 包含完整用户信息(tel、profession、birthday、address、introduction、departments)
  - 将 user_onboard/user_offboard/user_update hook 触发逻辑集中到 UserObserver
  - 区分 profile_update(用户自己修改)和 admin_update(管理员修改)事件类型
  - 修复 User::reg() 中 Manticore 索引同步遗漏问题
  - 排除机器人账号的 hook 触发
2026-01-08 11:31:16 +00:00
kuaifan
7f9c42d3d8 no message 2026-01-07 04:11:42 +00:00
kuaifan
4e99e398d6 feat: 添加动态时间提示和自动校正功能
- 在"最早可提前"下方动态显示最早可签到时间
  - 在"最晚可延后"下方动态显示最晚可签到时间(跨天显示"次日"前缀)
  - 输入值变化时自动校正到临界值,防止时间重叠
  - 调整表单布局支持换行显示提示信息
2026-01-07 04:11:32 +00:00
kuaifan
395fc155ce feat: 使用用户头像作为封面背景
在用户详情弹窗的顶部封面区域,使用用户头像作为模糊背景,
提升视觉效果和个性化体验。

- 将用户头像通过 CSS 变量传递给封面区域
- 添加背景模糊滤镜和缩放效果
- 修复容器溢出问题
2026-01-07 03:11:34 +00:00
kuaifan
6bdefc4f03 feat: 支持跨天打卡和时间重叠验证
- 允许签到"最晚可延后"时间超过 23:59:59,支持员工凌晨下班打卡
  - 凌晨打卡记录自动归属前一天
  - 前后端新增提前/延后时间重叠验证,防止产生歧义时间窗口
  - 优化导出逻辑以正确处理跨天打卡记录
  - 打卡消息提示归属日期信息
2026-01-06 12:31:41 +00:00
kuaifan
d4547cbe97 refactor: 移除语言偏好部分,简化文档内容 2026-01-06 08:57:38 +00:00
kuaifan
c9a0b7481a feat: 统一用户编辑入口为独立弹窗组件
- 新增 UserEditModal 组件,整合昵称、电话、职位、邮箱、密码、部门、个人简介、个性标签编辑
  - 签到模式下支持编辑人脸图片和 MAC 地址,并高亮显示相关字段
  - TeamManagement 移除分散的编辑入口(快捷修改、修改邮箱/密码/部门/人脸/MAC 等菜单)
  - 简化 operationUser 方法,移除冗余的 data/watch/methods
2026-01-06 08:55:04 +00:00
kuaifan
f496bc5fca feat: Optimize search functionality and AI module integration
- Refactor Manticore search classes for better performance
- Update AI module with enhanced processing capabilities
- Improve Apps module functionality
- Enhance SearchBox Vue component with new features
2026-01-06 07:25:23 +00:00
kuaifan
4ba02b9dce feat: 优化 remove_by_network 函数以批量删除容器并处理空容器情况 2026-01-06 02:13:15 +00:00
kuaifan
f821e5ad28 refactor: 移除缓存写入逻辑并简化未获取向量填充过程 2026-01-05 12:10:17 +00:00
kuaifan
425f7b6f79 fix: 修复多标签窗口关闭后事件回调导致的崩溃 2026-01-05 09:36:22 +00:00
kuaifan
61d7970b6a feat: 更新 remove_by_network 函数以删除所有状态的容器并等待网络清空 2026-01-05 09:35:39 +00:00
kuaifan
1aa9984535 fix: 会话列表待办完成消息显示最后完成者 2026-01-05 06:31:14 +00:00
kuaifan
8ab810c670 feat: 将 Manticore 相关检查更新为使用 "search" 应用 2026-01-05 05:51:48 +00:00
kuaifan
5cc3d60e15 feat: 添加交互规范,建议在提问时附带具体选项以帮助用户决策 2026-01-05 02:27:18 +00:00
kuaifan
42a2eb56c7 feat: 升级语音识别模型并优化转写逻辑
- 语音识别模型从 whisper-1 升级到 gpt-4o-mini-transcribe
   - 根据用户语言设置自动添加简繁体中文提示词
   - 录音转文字新增 dialog_id 参数,支持获取对话上下文提高识别准确率
   - 移除前端语言手动选择功能,简化用户操作
   - 添加参数空值保护
   - 优化 reasoning_effort 参数逻辑,区分 gpt-5 和 gpt-5.1+ 版本
2026-01-05 02:26:36 +00:00
kuaifan
4b0f4e388c feat: 优化 Manticore 相关描述 2026-01-04 13:30:03 +00:00
kuaifan
31045b3808 feat: 更新 Manticore 数据库插入逻辑,添加 userid 和 tags 字段;在 WebSocket 消息删除时同步 Manticore 2026-01-04 07:48:53 +00:00
kuaifan
a95f22bf42 feat: 添加 ManticoreSyncTask 的去重功能,优化任务投递逻辑 2026-01-04 07:48:32 +00:00
kuaifan
fa84f92577 feat: 添加 ProjectTaskContentObserver 以处理任务内容的创建、更新和删除事件 2026-01-04 07:24:36 +00:00
kuaifan
90a5624877 feat: 添加用户标签功能,更新用户索引以支持标签创建、更新和删除事件 2026-01-04 07:13:13 +00:00
kuaifan
f42250b8b7 feat: 重构文件管理界面,优化文件操作区域布局和样式 2026-01-04 06:13:44 +00:00
kuaifan
b9809d207d feat: 添加同步 responseSeed 方法,避免与已有响应 localId 冲突 2026-01-04 01:40:25 +00:00
kuaifan
0d8e10b60e feat: 优化 IDBClear 方法,支持保留指定键的缓存项 2026-01-04 01:40:13 +00:00
kuaifan
501ff21e55 feat: 添加数值类型转换功能,确保查询结果中的数值类型一致性 2026-01-04 00:29:29 +00:00
kuaifan
4759e28a56 feat: 在 DialogWrapper 组件中添加 search_type 属性以支持文本搜索 2026-01-03 23:20:56 +00:00
kuaifan
bd7841ac05 feat: 添加 TTY 参数检测,优化 Docker 命令执行 2026-01-03 23:09:59 +00:00
kuaifan
ea0d27fdea feat: 添加 Manticore 同步命令通用锁机制,优化信号处理与锁管理 2026-01-03 23:09:50 +00:00
kuaifan
610979f30b feat: Enhance Manticore sync commands with incremental processing and sleep options
- Updated sync commands (SyncFileToManticore, SyncMsgToManticore, SyncProjectToManticore, SyncTaskToManticore, SyncUserToManticore) to support continuous incremental updates until completion.
- Added --sleep option to allow a pause between batches in incremental mode.
- Improved signal handling to allow graceful shutdown during processing.
- Adjusted lock duration to 30 minutes for long-running processes.
- Enhanced logging for better visibility of sync progress and completion.
- Updated ManticoreSyncTask to ensure commands run continuously and check for new data every 2 minutes.
2026-01-03 22:41:49 +00:00
kuaifan
9a8304d595 feat: 增强 Manticore 向量更新逻辑,记录更新失败的 ID 2026-01-03 21:59:44 +00:00
kuaifan
e020a80020 feat: Add batch embedding retrieval and vector update methods for Manticore integration
- Implemented `getBatchEmbeddings` method in AI module for retrieving embeddings for multiple texts.
- Added vector update methods for messages, files, tasks, projects, and users in ManticoreBase.
- Enhanced ManticoreFile, ManticoreMsg, ManticoreProject, ManticoreTask, and ManticoreUser to support vector generation during sync operations.
- Introduced `generateVectorsBatch` methods for batch processing of vector generation in Manticore modules.
- Updated ManticoreSyncTask to handle incremental updates and vector generation asynchronously.
2026-01-03 15:19:23 +00:00
kuaifan
7a21a2d800 refactor: 统一搜索接口,移除 dialog/msg/search
- 前端 DialogWrapper.vue 改用 search/message 接口
  - 删除 DialogController::msg__search 方法
  - search/message 已完全覆盖原接口功能
2026-01-03 13:04:40 +00:00
kuaifan
ec0db3a76c refactor: 提取搜索逻辑到 Model Scope
- User: 新增 scopeSearchByKeyword
  - Project: 新增 scopeSearchByKeyword
  - ProjectTask: 新增 scopeSearchByKeyword
  - File: 新增 scopeSearchByKeyword, scopeSharedToUser
  - WebSocketDialogMsg: 新增 scopeSearchByKeyword, scopeAccessibleByUser
  - SearchController: 使用新的 Model Scope 简化 MySQL 回退逻辑
2026-01-03 07:58:11 +00:00
kuaifan
67fc0781e5 feat: 添加 Claude Code 配置文件
- 创建 CLAUDE.md 项目指南
  - 添加 .claude/rules/graphiti.md Graphiti 长期记忆集成规则
2026-01-03 07:33:35 +00:00
kuaifan
79c2ba140c feat: 更新搜索功能,统一搜索接口,优化请求参数 2026-01-03 04:42:15 +00:00
kuaifan
908171a977 feat: 新增对话ID参数支持,优化搜索功能以支持对话过滤 2026-01-03 03:59:51 +00:00
kuaifan
a52dc14369 feat: Enhance AIAssistant and SearchBox components with improved link handling and search functionality
- Updated AIAssistant to support parsing of additional message links in the format dootask://message/id1/id2.
- Modified search methods in SearchBox to streamline API calls and remove AI search logic, improving performance and clarity.
- Cleaned up unused AI search code and adjusted search result handling for better data presentation.
- Updated documentation to reflect new link formats for tasks, projects, files, and messages.
2026-01-02 09:48:52 +00:00
kuaifan
1e94ce501e refactor: 移除 ZincSearch,统一使用 Manticore Search
- 删除 ZincSearch 模块、任务、命令
- 对话消息搜索改用 ManticoreMsg::searchDialogs
- 移除 Observer 中的 ZincSearch 同步
- 移除定时任务中的 ZincSearch 同步
- 更新项目文档
2026-01-02 07:25:14 +00:00
kuaifan
7a5ef3a491 feat: 新增消息搜索功能
- 新增 msg_vectors 表,支持消息全文/向量/混合搜索
- 采用 MVA 权限方案,allowed_users 内联存储
- 新增 /api/search/message API
- 新增 manticore:sync-msgs 同步命令
- Observer 触发消息创建/更新/删除同步
- Observer 触发对话成员变更时更新 allowed_users
2026-01-02 06:46:18 +00:00
kuaifan
c08323e1ea feat: 迁移至 MVA 权限方案
- 表结构:为 file/project/task_vectors 添加 allowed_users MULTI 字段
- 删除关系表:file_users, project_users, task_users
- 搜索:使用 allowed_users = userid 进行权限过滤
- 同步:sync 时自动计算并写入 allowed_users
- 级联:项目成员变更异步级联 v=1 任务,任务成员变更递归更新子任务
- 覆盖场景:visibility/parent_id/project_id 变更、子任务升级主任务等
2026-01-02 02:03:21 +00:00
kuaifan
fdf5ceeaab feat: Enhance Manticore integration and AI model support
- Added support for specifying vector dimensions in AI payloads for compatible vendors.
- Updated default AI model from 'text-embedding-ada-002' to 'text-embedding-3-small'.
- Refactored ManticoreBase to bind parameters explicitly for PDO statements, improving type handling.
- Adjusted SQL queries across Manticore modules to remove content previews and ensure inline vector values.
- Updated content preview handling in ManticoreFile, ManticoreProject, ManticoreTask, and ManticoreUser to use substrings for better data management.
2026-01-01 08:59:54 +00:00
kuaifan
48ef4cfdef refactor: 使用 Manticore Search 替换 SeekDB 2026-01-01 03:17:27 +00:00
kuaifan
10c6177a9f no message 2025-12-31 16:55:33 +00:00
kuaifan
0362c83e77 feat: 支持 AI 助手输入框回车快捷操作
- 新增 onInputKeydown 方法:支持回车发送、Shift+Enter 换行,提升输入体验。
- 更新输入框组件,绑定键盘事件,实现更流畅的交互。
- 自动聚焦输入框,提升用户体验。
2025-12-31 09:57:34 +00:00
kuaifan
1af29837e2 feat: 增加增量同步功能以优化 SeekDB 用户关系同步
- 在 SyncFileToSeekDB、SyncProjectToSeekDB 和 SyncTaskToSeekDB 中实现增量同步逻辑,支持只同步新增的用户关系。
- 新增 syncFileUsersIncremental、syncProjectUsersIncremental 和 syncTaskUsersIncremental 方法,提升数据同步效率。
- 更新相关命令行输出信息,以清晰指示同步状态和进度。
2025-12-31 09:28:10 +00:00
kuaifan
986c4871df feat: Enhance AI Assistant with session management and improved UI
- Added session management capabilities to the AI Assistant, allowing users to create, load, and delete sessions.
- Improved modal UI with a new header for session actions and a footer for model selection.
- Updated input handling to support dynamic loading of session data and improved response formatting.
- Enhanced search functionality in various components to utilize the AI Assistant for generating content based on user input.
2025-12-31 08:47:03 +00:00
kuaifan
fe7a2a0e73 feat: 扩展 SeekDB 支持联系人、项目、任务的 AI 搜索
- 合并 SeekDBFileSyncTask 到 SeekDBSyncTask
- 统一 AI 搜索 API 入口
2025-12-30 07:48:00 +00:00
kuaifan
23faf28f7f feat: 集成 SeekDB AI 搜索引擎实现文件内容搜索 2025-12-30 05:49:26 +00:00
kuaifan
a8d4f261a4 no message 2025-12-30 05:49:18 +00:00
kuaifan
a336fd4a1a feat: omit content from report list APIs 2025-12-30 01:58:03 +00:00
kuaifan
8759e6fd7e build 2025-12-30 09:20:59 +08:00
kuaifan
92d23014a7 fix: avoid opening blank dialog window when dialogId is 0 2025-12-29 16:22:06 +00:00
kuaifan
7c3f33ea0d fix: avoid mutating task getter arrays in mention list 2025-12-29 16:01:37 +00:00
kuaifan
16a55de6f1 feat: 增强搜索功能,支持通过 ID、名称和其他字段搜索任务、文件和报告 2025-12-29 15:43:50 +00:00
kuaifan
869ac7d316 feat: 更新 appstore 镜像版本至 0.3.8 2025-12-27 10:29:51 +00:00
kuaifan
55303689ea feat: support configurable default priority 2025-12-26 02:42:47 +00:00
kuaifan
c69123ac92 no message 2025-12-24 09:49:21 +00:00
kuaifan
7bce5f1c1f feat: 添加迁移脚本以为相关表添加索引 2025-12-24 09:18:48 +00:00
kuaifan
989660969c feat: 添加迁移脚本以反转待办消息中的用户ID顺序 2025-12-24 07:11:01 +00:00
kuaifan
862acd0776 fix: 修复行前缀检测逻辑,确保正确判断空行 2025-12-24 06:30:43 +00:00
kuaifan
3b3ffd494f feat: 规范以斜杠开头的命令 2025-12-24 06:10:39 +00:00
kuaifan
6cf8290565 feat: 增强斜杠命令支持,添加机器人命令和行首检测功能 2025-12-24 05:58:48 +00:00
kuaifan
230ebbcfb9 feat: support slash trigger for mention/task/file/report 2025-12-24 00:59:31 +00:00
kuaifan
dc77f1cda1 build 2025-12-23 09:51:18 +08:00
kuaifan
1f791b528a fix: 更新对话ID和场景信息的描述,增加字段标识 2025-12-23 01:40:53 +00:00
kuaifan
1459d953ed feat: 更新获取消息列表MCP工具的描述,增强功能说明 2025-12-22 03:44:33 +00:00
kuaifan
719a36b275 chore: update mobile subproject commit reference 2025-12-19 22:35:57 +08:00
kuaifan
0b7a3046fe fix: align parent task subtask progress with task detail (include archived, exclude deleted) 2025-12-19 21:36:00 +08:00
kuaifan
203d107d68 fix: skip loading related tasks for subtasks to prevent request spam 2025-12-19 19:37:07 +08:00
kuaifan
17fd7f02a6 build 2025-12-19 09:13:49 +08:00
kuaifan
57ea4f2b6f feat: 自定义应用菜单新增 immersive 沉浸式开关 2025-12-19 01:07:02 +00:00
kuaifan
df431eea46 no message 2025-12-18 23:12:53 +00:00
kuaifan
ad9dd6330f feat: merge todo done notices and render done_userids 2025-12-18 23:03:11 +00:00
kuaifan
df9d291f98 feat: 优化群组资料修改逻辑,增加权限判断和名称修改提示 2025-12-18 21:53:04 +00:00
kuaifan
0cf7fc2ed2 feat: replace group name quick edit with modify trigger 2025-12-18 21:42:15 +00:00
kuaifan
e8f82baa99 feat: 添加 urlType 字段以兼容旧版本微应用配置 2025-12-18 21:06:49 +00:00
kuaifan
353a05f344 feat: 优化 openMicroApp 方法,增强参数校验和微应用 ID 解析逻辑 2025-12-18 20:59:44 +00:00
kuaifan
d94ebfe04c feat: 添加解析类型的方法,优化微应用配置逻辑 2025-12-18 08:26:42 +00:00
kuaifan
52913abb4f feat: 更新 appstore 镜像版本至 0.3.7 2025-12-18 02:47:39 +00:00
kuaifan
d77406951d feat: 更新微应用菜单配置,统一使用类型字段替代URL类型字段 2025-12-18 02:44:37 +00:00
kuaifan
8c23192eeb build 2025-12-17 09:30:53 +08:00
kuaifan
078c9c198d feat: 更新 appstore 镜像版本至 0.3.6 2025-12-16 11:32:33 +00:00
kuaifan
6cfe2d226a feat: 增加获取胶囊可见性的方法,优化胶囊显示逻辑 2025-12-16 11:31:50 +00:00
kuaifan
fee1c12357 feat: 添加导航功能,支持快捷键和鼠标手势操作 2025-12-16 18:36:11 +08:00
kuaifan
a6385b699e fix: 修复在某些情况下无法打开微应用的问题 2025-12-14 22:36:14 +00:00
kuaifan
718ed8953f no message 2025-12-14 00:23:04 +00:00
kuaifan
a1eea77b9e feat: 更新 appstore 镜像版本至 0.3.5 2025-12-12 07:12:07 +00:00
kuaifan
6eb08ac09b build 2025-12-11 10:28:18 +08:00
kuaifan
20fc2b073b no message 2025-12-11 02:09:59 +00:00
kuaifan
8c4b9e8d12 feat: 优化项目/报告控制器及任务模型 2025-12-11 02:06:13 +00:00
kuaifan
8d187f5cfc feat: 优化周报/日报模板的已完成与未完成任务规则 2025-12-11 01:35:10 +00:00
kuaifan
db07a96e97 fix: 修复任务导出状态判断及状态高亮列错位问题 2025-12-11 01:13:03 +00:00
kuaifan
7acc9227ff fix: 修复任务统计导出漏掉无计划时间已完成任务的问题 2025-12-11 00:43:54 +00:00
kuaifan
c3a71e5b07 feat: 更新 appstore 镜像版本至 0.3.4 2025-12-10 02:01:43 +00:00
kuaifan
ac9e1e5e67 feat: call appstore user lifecycle hooks from main app 2025-12-09 10:30:23 +00:00
kuaifan
c668340661 feat: 优化消息推送逻辑 2025-12-05 02:10:37 +00:00
kuaifan
ee9b6248bb fix(electron): cleanup child windows by instance instead of name 2025-12-04 11:18:47 +00:00
kuaifan
01c7f7250b fix: 修复关闭应用时加载状态未正确更新的问题 2025-12-03 12:48:33 +00:00
kuaifan
2abc5976f9 fix: 更新 iframe 的 sandbox 属性以增强安全性 2025-12-02 12:03:54 +00:00
kuaifan
3e468c74e4 fix: 修改微模态框的最小高度设置 2025-12-02 11:46:46 +00:00
kuaifan
4ef78d2c81 feat: 添加点击消息打开微应用功能 2025-12-02 06:29:45 +00:00
kuaifan
4621222fa3 build 2025-11-30 12:18:18 +08:00
kuaifan
be860f9968 fix: load fastmcp via dynamic import in electron MCP 2025-11-30 12:13:31 +08:00
kuaifan
fe0b8aed20 no message 2025-11-28 22:09:55 +00:00
kuaifan
f0e844c308 feat: 添加个人任务上限设置,限制负责人或协助人的未完成任务数量 2025-11-28 11:05:08 +00:00
kuaifan
6a7cc95b23 feat: 添加颜色工具函数,支持颜色反转和解析 2025-11-28 09:35:01 +00:00
kuaifan
7fd90b9ceb feat: 添加对话框顶部消息样式 2025-11-28 08:58:14 +00:00
kuaifan
43577073e6 fix: 调整各组件最大高度计算,考虑状态栏和导航栏高度 2025-11-28 02:27:03 +00:00
kuaifan
faeeb09a4a fix: 修复微模态组件的样式,调整为固定定位以适应全屏显示 2025-11-28 01:33:49 +00:00
kuaifan
d88349b6f7 feat: 使用 CSS 变量动态调整窗口高度,优化各组件的最大高度设置 2025-11-28 01:33:35 +00:00
kuaifan
ff53e1fac3 fix: enforce positive rounded size in normalizeSize 2025-11-27 10:40:45 +08:00
kuaifan
cf4894b7c3 no message 2025-11-27 02:24:40 +00:00
kuaifan
678dfd2d5c feat: 更新 appstore 镜像版本 2025-11-27 02:24:34 +00:00
kuaifan
bf4a62ae04 feat: 更新文档,添加前端弹窗文案处理说明 2025-11-24 01:23:39 +00:00
kuaifan
7e6f3f92cf feat: 添加 URL 输入提示,优化 iframe 测试功能的用户体验 2025-11-24 01:23:22 +00:00
kuaifan
df382dafb4 no message 2025-11-24 00:38:16 +00:00
kuaifan
10925d3a47 no message 2025-11-20 06:19:29 +00:00
kuaifan
66252072c7 feat: 添加 iframe 测试功能,支持通过 URL 加载外部内容 2025-11-20 06:18:56 +00:00
kuaifan
29918882bd no message 2025-11-19 07:54:56 +00:00
kuaifan
4983fe8feb feat: 添加自定义微应用菜单功能,支持管理员配置和保存菜单项 2025-11-19 07:54:47 +00:00
kuaifan
f65da118d7 feat: 更新 appstore 镜像版本至 0.3.2 2025-11-15 09:17:38 +00:00
kuaifan
a86bd9a05e fix: 修复桌面端部分机器新窗口任务报错的情况 2025-11-14 09:48:10 +00:00
kuaifan
f2719eb742 feat: 更新助手默认模型为 gpt-5.1-mini 2025-11-14 01:20:41 +00:00
kuaifan
4f9ee1dfa9 no message 2025-11-14 01:17:48 +00:00
kuaifan
e6ad1218bc feat: 添加一键归档列表中已完成任务 2025-11-14 01:15:19 +00:00
kuaifan
dd2cd1df9a feat: 更新 OnlyOffice 组件的主题名称;优化文件管理页面的列表渲染;调整抽屉和文件内容的圆角样式 2025-11-13 06:20:21 +08:00
kuaifan
6dcbe8ba38 build 2025-11-12 16:46:33 +08:00
kuaifan
360d4dbbe2 no message 2025-11-12 07:18:54 +00:00
kuaifan
2f32b53d19 feat: 修改 getDomain 函数以支持可选的小写转换参数;更新 getObject 函数的默认值 2025-11-12 07:07:00 +00:00
kuaifan
6a3e3c3753 feat: AI 助手增加最大响应数至50,并添加上下文窗口大小设置 2025-11-12 01:23:34 +00:00
kuaifan
5ad08d8d36 no message 2025-11-12 01:06:36 +00:00
kuaifan
b892d92614 build 2025-11-12 07:11:38 +08:00
kuaifan
b259f083d4 no message 2025-11-12 07:05:46 +08:00
kuaifan
38aa9fe2fb build 2025-11-12 00:30:39 +08:00
kuaifan
863dd3a53e no message 2025-11-11 22:42:45 +08:00
kuaifan
bea5058df8 feat: 优化错误处理逻辑,简化错误消息输出 2025-11-11 21:49:09 +08:00
kuaifan
31c157f58f no message 2025-11-11 21:40:34 +08:00
kuaifan
8af6887daa feat: 优化WebSocketDialogMsg和BotReceiveMsgTask中的消息格式,统一中文标点,增强可读性 2025-11-11 13:05:04 +00:00
kuaifan
eb9b7b4f86 feat: 更新MCP工具描述 2025-11-11 07:16:04 +00:00
kuaifan
cf78766a37 feat: 移除未使用的消息处理函数和Markdown插件任务创建功能,优化代码结构 2025-11-11 05:42:02 +00:00
kuaifan
944824b552 feat: 移除未使用的函数和代码,优化BotReceiveMsgTask和WebSocketDialogMsg的消息处理逻辑 2025-11-11 05:31:59 +00:00
kuaifan
477bb1ac8f feat: MCP增加文件管理功能,支持获取文件访问URL、文件列表和文件搜索 2025-11-11 05:23:00 +00:00
kuaifan
29df864ecb feat: MCP增加工作报告相关功能,包括获取汇报列表、获取汇报详情、生成汇报模板、创建汇报及标记已读/未读状态 2025-11-11 02:24:35 +00:00
kuaifan
bcf897b7e0 no message 2025-11-10 23:03:42 +00:00
kuaifan
e63890c755 feat: 重构隐私政策页面,优化结构和样式,增强可读性 2025-11-10 23:01:39 +00:00
kuaifan
f3725215bd feat: 简化长按指令的参数配置 2025-11-10 22:43:25 +00:00
kuaifan
c43e305ea7 feat: 优化AI输出语言策略提示词 2025-11-10 22:36:37 +00:00
kuaifan
b9215e2410 feat: 添加语言偏好提示功能到AI系统提示 2025-11-10 16:46:29 +00:00
kuaifan
19d79ab055 feat: 优化触摸设备交互
- 触摸设备取消拖动选中文件
2025-11-10 16:14:01 +00:00
kuaifan
64d4492806 feat: 优化AI助手响应构建
- 增加剔除推理块功能
2025-11-10 16:13:05 +00:00
kuaifan
0790eae8c6 no message 2025-11-10 15:20:31 +00:00
kuaifan
e10e2c27c1 feat: 优化导出菜单交互 2025-11-10 07:59:52 +00:00
kuaifan
d30b38d4b9 feat: 添加应用排序功能 2025-11-10 07:47:00 +00:00
kuaifan
f6e4ed7c60 no message
- 添加AI助手流式会话凭证生成方法
- 优化AI助手模型获取逻辑
- 更新相关接口调用
2025-11-09 22:20:38 +00:00
kuaifan
7a6bbfac75 feat: 更新AI模块的transcriptions方法,增加扩展请求头参数,优化语音识别功能 2025-11-09 04:43:17 +00:00
kuaifan
425d6f9a06 feat: 移除冗余的AI助手设置方法,优化AI模块的模型配置逻辑 2025-11-09 04:28:51 +00:00
kuaifan
58c760bb77 no message 2025-11-09 02:14:27 +00:00
kuaifan
3ffdce5e7a no message 2025-11-08 23:54:18 +00:00
kuaifan
8e518a044a feat: 优化AI助手输出界面,简化状态显示逻辑,增强用户交互体验 2025-11-08 23:43:06 +00:00
kuaifan
a5adbf80a9 feat: 重构报告分析功能,更新API接口,移除冗余代码,优化分析逻辑 2025-11-08 22:18:59 +00:00
kuaifan
0b6c478b4f feat: 优化报告AI整理功能,优化报告编辑逻辑,移除冗余代码 2025-11-08 21:53:02 +00:00
kuaifan
0434bde16f feat: 移除冗余的AI任务和项目生成逻辑,优化代码结构 2025-11-08 21:52:26 +00:00
kuaifan
0deb3113b5 feat: 引入文本提取功能,优化AI内容解析逻辑,移除冗余代码 2025-11-08 20:42:21 +00:00
kuaifan
ecb52c76b9 feat: 完善AI助手功能 2025-11-08 08:57:22 +00:00
kuaifan
69c66053b7 feat: 完善AI助手功能,新增消息提示词整理接口,优化流式消息处理逻辑,移除冗余数据表和相关代码 2025-11-07 22:25:45 +00:00
kuaifan
892ad395a7 feat: 添加额外数据处理,优化AI助手消息生成与发送逻辑 2025-11-07 20:38:06 +00:00
kuaifan
e801c09c0f feat: 增强AI助手响应处理,支持流式输出和模型缓存 2025-11-07 08:13:51 +00:00
kuaifan
ad560a8555 feat: 增强流消息处理,支持回应和会话ID 2025-11-07 08:13:41 +00:00
kuaifan
e75aa5c2b9 feat: 创建新 AI 会话时将旧会话消息批量标记已读 2025-11-07 07:54:04 +00:00
kuaifan
e83fd7af1b feat: 优化 AI 助手,支持自定义模型 2025-11-07 07:01:15 +00:00
kuaifan
eaec8ef994 no message 2025-11-07 01:00:30 +00:00
kuaifan
3339e6b442 feat: 添加文件列表滚动事件处理,优化右键菜单显示逻辑 2025-11-07 01:00:22 +00:00
kuaifan
4c2425c758 feat: 优化链接获取逻辑 2025-11-06 14:53:16 +00:00
kuaifan
80d1e6469e no message 2025-11-06 14:23:39 +00:00
kuaifan
2fad6394ee no message 2025-11-06 14:03:58 +00:00
kuaifan
4bfe33a37f feat: 优化打开会话事件接口,优化机器人webhook逻辑
- 新增 `open__event` 方法用于处理打开会话事件
- 移除旧的 `open__webhook` 方法
- 更新前端调用逻辑,使用新的事件接口
- 优化 webhook 事件推送逻辑,简化参数传递
2025-11-06 13:59:10 +00:00
kuaifan
130c8bf3b1 Merge pull request #289 from nightcp/dev
feat: 调整机器人webhook事件
2025-11-06 15:24:06 +08:00
kuaifan
b9df277104 no message 2025-11-06 07:16:29 +00:00
kuaifan
97e1f321ca feat: 优化长文本预览组件 2025-11-06 07:00:11 +00:00
王昱
4933930afd feat: 调整机器人webhook事件
- 可取消接收消息事件
- 打开机器人会话窗口时推送webhook消息,相同机器人消息缓存1分钟
2025-11-06 04:08:39 +00:00
kuaifan
ab4640382d feat: 添加会员扩展信息接口,优化用户详情和个人设置页面 2025-11-06 02:01:15 +00:00
kuaifan
e4cfa4b405 feat: 优化个性标签 2025-11-05 22:19:45 +00:00
kuaifan
789062e85e Merge pull request #288 from xxyijixx/dev-profile
Dev profile
2025-11-05 17:11:46 +08:00
kuaifan
5370bee369 Merge branch 'dev' into pro
# Conflicts:
#	CHANGELOG.md
#	cmd
#	package.json
#	public/js/build/404.5645cb91.js
#	public/js/build/404.9598cd97.js
#	public/js/build/404.a5736629.js
#	public/js/build/AceEditor.8747edb1.js
#	public/js/build/AceEditor.af35593f.js
#	public/js/build/AceEditor.e7f5b602.js
#	public/js/build/DialogWrapper.0c7cd033.js
#	public/js/build/DialogWrapper.64072671.js
#	public/js/build/DialogWrapper.7fcb5b27.js
#	public/js/build/Drawio.2ca59c31.js
#	public/js/build/Drawio.6691a6ef.js
#	public/js/build/Drawio.e3576e4e.js
#	public/js/build/FileContent.3a899bcc.js
#	public/js/build/FileContent.c311c89c.js
#	public/js/build/FileContent.d8e600e1.js
#	public/js/build/FilePreview.87ca99d9.js
#	public/js/build/FilePreview.f8134ee5.js
#	public/js/build/FilePreview.f9f90ff4.js
#	public/js/build/IFrame.02598edc.js
#	public/js/build/IFrame.2a7489ee.js
#	public/js/build/IFrame.be9780e1.js
#	public/js/build/ImgUpload.29e2d88d.js
#	public/js/build/ImgUpload.a4eff264.js
#	public/js/build/ImgUpload.e96999cf.js
#	public/js/build/Minder.2bce6c16.js
#	public/js/build/Minder.b1d1145f.js
#	public/js/build/Minder.f5bc5aca.js
#	public/js/build/OnlyOffice.31e7af4f.js
#	public/js/build/OnlyOffice.574ad560.js
#	public/js/build/OnlyOffice.9ce921ed.js
#	public/js/build/ReportEdit.5eb3a319.js
#	public/js/build/ReportEdit.9141bb93.js
#	public/js/build/ReportEdit.e3369e09.js
#	public/js/build/SearchButton.906cea81.js
#	public/js/build/SearchButton.cf201525.js
#	public/js/build/SearchButton.d41addb6.js
#	public/js/build/TEditor.7b9a9d91.js
#	public/js/build/TEditor.971af80f.js
#	public/js/build/TEditor.cc94d929.js
#	public/js/build/TaskDetail.38815236.js
#	public/js/build/TaskDetail.d1a9952e.js
#	public/js/build/TaskDetail.dfd78b4a.js
#	public/js/build/add.0cfbdd9e.js
#	public/js/build/add.3673f91c.js
#	public/js/build/add.423bc480.js
#	public/js/build/application.005cc174.js
#	public/js/build/application.5587ac3b.js
#	public/js/build/application.5b8f123b.js
#	public/js/build/apps.4e0bf65b.js
#	public/js/build/apps.b0a3d4f5.js
#	public/js/build/apps.f77a8c4e.js
#	public/js/build/calendar.31470aa0.js
#	public/js/build/calendar.ad5d85d5.js
#	public/js/build/calendar.e08e7575.js
#	public/js/build/checkin.5d4c364e.js
#	public/js/build/checkin.ab08f01e.js
#	public/js/build/checkin.c05284a9.js
#	public/js/build/dashboard.7cced7be.js
#	public/js/build/dashboard.c82415db.js
#	public/js/build/dashboard.f6ed8299.js
#	public/js/build/dayjs.495f600d.js
#	public/js/build/dayjs.71653272.js
#	public/js/build/dayjs.cf033d87.js
#	public/js/build/delete.4072c68f.js
#	public/js/build/delete.5f06c51d.js
#	public/js/build/delete.b26aa3fd.js
#	public/js/build/device.4cff22ad.js
#	public/js/build/device.66a7e05a.js
#	public/js/build/device.a13f3ef0.js
#	public/js/build/dialog.97b951ce.js
#	public/js/build/dialog.e9f6d55f.js
#	public/js/build/dialog.eb7b795a.js
#	public/js/build/editor.18a511b5.js
#	public/js/build/editor.2cca497c.js
#	public/js/build/editor.e034df4e.js
#	public/js/build/email.0643f86b.js
#	public/js/build/email.1d00cb0c.js
#	public/js/build/email.d95a35c0.js
#	public/js/build/file.4fe82c29.js
#	public/js/build/file.684a63df.js
#	public/js/build/file.9dceb82f.js
#	public/js/build/fileMsg.0a0029c2.js
#	public/js/build/fileMsg.1f4ecb0f.js
#	public/js/build/fileMsg.f99b6f61.js
#	public/js/build/fileTask.72914205.js
#	public/js/build/fileTask.bf35fb6b.js
#	public/js/build/fileTask.f4356f14.js
#	public/js/build/index.236af26f.js
#	public/js/build/index.299c9f99.js
#	public/js/build/index.2ffa8f9e.js
#	public/js/build/index.7d6e1bbe.js
#	public/js/build/index.94a5d2da.css
#	public/js/build/index.af34aeb9.js
#	public/js/build/index.b0ae9460.js
#	public/js/build/index.b69b5f25.js
#	public/js/build/index.b71c2859.js
#	public/js/build/index.c3968cad.js
#	public/js/build/index.d1ae44be.js
#	public/js/build/index.e07db7f9.css
#	public/js/build/index.edee4b6e.css
#	public/js/build/index.ef9e1e57.js
#	public/js/build/index.fe32159a.js
#	public/js/build/jquery.0909250e.js
#	public/js/build/jquery.16b446fd.js
#	public/js/build/jquery.27f590f5.js
#	public/js/build/keyboard.3f5b3ac6.js
#	public/js/build/keyboard.5de3dd2c.js
#	public/js/build/keyboard.c3ef7d49.js
#	public/js/build/language.1fadd54c.js
#	public/js/build/language.8bb72294.js
#	public/js/build/language.f3d03ece.js
#	public/js/build/license.21482fde.js
#	public/js/build/license.60871496.js
#	public/js/build/license.add318a7.js
#	public/js/build/localforage.65ac7a2a.js
#	public/js/build/localforage.be4775a0.js
#	public/js/build/localforage.dd58f5ac.js
#	public/js/build/login.7560afa5.js
#	public/js/build/login.75b3978c.js
#	public/js/build/login.aa163163.js
#	public/js/build/meeting.a60d7e8d.js
#	public/js/build/meeting.aa5510c7.js
#	public/js/build/meeting.fdb9793b.js
#	public/js/build/password.267357fd.js
#	public/js/build/password.749ce44d.js
#	public/js/build/password.e6d81eb1.js
#	public/js/build/personal.69279937.js
#	public/js/build/personal.a27cef8e.js
#	public/js/build/personal.c613af3c.js
#	public/js/build/preload.5827bd38.js
#	public/js/build/preload.8ec61a5b.js
#	public/js/build/preload.c6189d87.js
#	public/js/build/preview.29e49902.js
#	public/js/build/preview.7329f0f4.js
#	public/js/build/preview.b452b0ee.js
#	public/js/build/preview.c64402ed.js
#	public/js/build/preview.ec796a92.js
#	public/js/build/preview.ec85a43c.js
#	public/js/build/pro.2128a514.js
#	public/js/build/pro.213d8da6.js
#	public/js/build/pro.9fb60d27.js
#	public/js/build/projectInvite.0b3bf524.js
#	public/js/build/projectInvite.393920f8.js
#	public/js/build/projectInvite.e9cee390.js
#	public/js/build/reportDetail.2db50632.js
#	public/js/build/reportDetail.90aaf973.js
#	public/js/build/reportDetail.d93cc650.js
#	public/js/build/reportEdit.84a81076.js
#	public/js/build/reportEdit.8baf23d4.js
#	public/js/build/reportEdit.d008dd34.js
#	public/js/build/swipe.0c72cce1.js
#	public/js/build/swipe.4567bb5d.js
#	public/js/build/swipe.92aebd0c.js
#	public/js/build/system.67c1b700.js
#	public/js/build/system.c45c70de.js
#	public/js/build/system.f3384133.js
#	public/js/build/task.1b9e0e77.js
#	public/js/build/task.a445c89e.js
#	public/js/build/task.d43091db.js
#	public/js/build/taskContent.20b80714.js
#	public/js/build/taskContent.3ebbd2f9.js
#	public/js/build/taskContent.9dc7a121.js
#	public/js/build/theme.72d103d1.js
#	public/js/build/theme.7f1b2ffd.js
#	public/js/build/theme.df79fe8f.js
#	public/js/build/token.0ecffef5.js
#	public/js/build/token.a7f5ccf5.js
#	public/js/build/token.ece75257.js
#	public/js/build/validEmail.1462dd30.js
#	public/js/build/validEmail.17a3e0d2.js
#	public/js/build/validEmail.ee19c1f3.js
#	public/js/build/version.137935c7.js
#	public/js/build/version.1441c1fd.js
#	public/js/build/version.b0154505.js
#	public/js/build/video.03b62c93.js
#	public/js/build/video.2dc7f3c6.js
#	public/js/build/video.531c68e2.js
#	public/js/build/view.18713f1b.js
#	public/js/build/view.7770155e.js
#	public/js/build/view.8c6a0cc1.js
#	public/manifest.json
2025-11-05 16:55:17 +08:00
kuaifan
2f972488a1 Merge pull request #287 from nightcp/dev
feat: 优化用户机器人 webhook 逻辑
2025-11-05 16:30:37 +08:00
kuaifan
6f7656802f no message 2025-11-05 06:20:04 +00:00
kuaifan
7d98c5493e feat: 添加AI整理工作汇报功能 2025-11-05 04:02:29 +00:00
kuaifan
e0443aa336 feat: 添加AI分析工作汇报功能 2025-11-05 04:02:06 +00:00
kuaifan
39ff0d1516 feat: 将AI助手从gpt-5-nano更改为gpt-5-mini 2025-11-05 01:58:24 +00:00
kuaifan
1b9c0ee4b8 feat: 优化AI助手入口 2025-11-05 01:55:59 +00:00
kuaifan
d48287f93a feat: 添加判断是否为iPad的功能,并在预加载时处理安全区域 2025-11-04 13:08:23 +08:00
kuaifan
717e87cfa9 feat: 更新抽屉样式以支持横屏模式下的最大宽度设置 2025-11-04 13:06:19 +08:00
kuaifan
708b488af8 fix: 修复android分享页面元素重叠的情况 2025-11-03 16:56:20 +08:00
kuaifan
d60d3f374b feat: 调整对话框尺寸计算,避免发送消息失败的情况 2025-11-03 14:46:46 +08:00
kuaifan
8b87a2bc40 feat: 添加聊天输入历史记录功能 2025-11-03 02:12:05 +00:00
kuaifan
d0da517503 no message 2025-11-03 00:43:28 +00:00
kuaifan
754036c472 build 2025-11-03 08:05:35 +08:00
kuaifan
720438fd91 Merge commit '96106498d8c480c3ea7ec493bfb063450e11b7b5' into pro 2025-11-03 08:00:22 +08:00
kuaifan
ba76df1b00 no message 2025-11-03 08:00:15 +08:00
kuaifan
44d85c2864 feat: 增加对应用平台的 overscroll-behavior 设置
- 优化iOS15滚动超限的情况
2025-11-03 07:51:44 +08:00
kuaifan
1c8b73a381 feat: 重构胶囊缓存逻辑,增加设置和移除缓存的方法 2025-11-03 01:29:34 +08:00
kuaifan
b445af932c feat: 更新消息推送逻辑 2025-11-03 00:45:34 +08:00
kuaifan
5121739fe4 feat: 优化应用激活逻辑,增加 IndexedDB 测试失败时的提前返回处理 2025-11-03 00:34:32 +08:00
kuaifan
96106498d8 feat: 添加Umeng日志模型及数据库迁移 2025-11-01 16:15:32 +00:00
kuaifan
0116d92021 feat: 给支持角标的Android设备推送添加角标 2025-11-01 16:15:25 +00:00
kuaifan
43746634a5 no message 2025-10-31 08:27:44 +00:00
kuaifan
5183786fb0 no message 2025-10-30 20:04:41 +00:00
kuaifan
5ba0eed721 no message 2025-10-29 00:15:45 +00:00
kuaifan
7d08c735ef no message 2025-10-28 11:35:36 +00:00
kuaifan
e3067b685c no message 2025-10-28 09:23:41 +00:00
kuaifan
b219ca4c1c no message 2025-10-27 20:57:42 +00:00
kuaifan
9e5d16ff16 feat: 添加 MCP 服务器类型为 streamable-http 2025-10-27 02:49:53 +00:00
kuaifan
da630458e1 fix: 修复任务操作无法点击确定 2025-10-27 02:45:29 +00:00
王昱
66002ff401 Merge branch 'kuaifan:dev' into dev 2025-10-22 17:30:34 +08:00
nightcp
bdfc8bdd0c feat: 添加机器人消息推送参数文档,增强 webhook 事件说明 2025-10-22 17:29:32 +08:00
nightcp
98e4668969 feat: 优化用户机器人 webhook 逻辑 2025-10-21 13:53:16 +08:00
yatgei
c92b9bf0fb feat: 在用户详情组件中添加创建群组按钮功能 2025-10-14 18:29:21 +08:00
yatgei
b4cbfd2ae9 feat: 更新用户详情组件样式,调整布局和颜色 2025-10-14 14:01:03 +08:00
yatgei
dd7eee277e feat: 添加共同群组对话框组件并在用户详情中集成 2025-10-13 18:22:25 +08:00
kuaifan
ab76185434 feat: 优化个人资料卡片 2025-10-13 06:56:44 +00:00
kuaifan
6d97bf1e88 feat: 添加个性标签管理功能 2025-10-12 23:02:34 +00:00
kuaifan
49701fcd09 feat: 会员资料窗口添加创建群组按钮 2025-10-12 15:15:34 +00:00
kuaifan
40f04d9860 feat: 添加用户生日、地址和个人简介 2025-10-12 15:07:10 +00:00
kuaifan
d58dd25dbb feat: 添加用户生日、地址和个人简介 2025-10-12 15:05:05 +00:00
kuaifan
9b2731607b feat: 优化开发环境配置 2025-10-11 10:42:49 +00:00
kuaifan
a8d2d6f13f feat: 优化开发环境配置 2025-10-11 02:53:17 +00:00
kuaifan
7c21782ab5 no message 2025-10-08 04:34:31 +00:00
kuaifan
f59bdaf5e0 feat: 添加用户机器人 webhook 事件配置,优化相关逻辑 2025-09-30 04:25:50 +00:00
kuaifan
6ffd169784 build 2025-09-28 06:54:05 +08:00
435 changed files with 32606 additions and 14659 deletions

55
.claude/rules/graphiti.md Normal file
View File

@@ -0,0 +1,55 @@
# Graphiti 长期记忆集成
本项目使用 Graphiti 作为「长期记忆层」,用于持久化用户偏好、工作流程、重要约束和关键事实。
**统一 group_id**: `dootask-main`
## 任务开始前(读取记忆)
在进行实质性工作(写代码、设计方案、做大改动)前,应先通过 Graphiti 查询已有记忆:
- 使用节点搜索(如 `search_nodes`)在 `group_id = "dootask-main"` 下查找与当前任务相关的 Preference / Procedure / Requirement
- 使用事实搜索(如 `search_facts`)查找相关事实与实体关系
- 查询语句中可包含任务类型Bug 修复 / 重构 / 新功能等、涉及模块任务、项目、对话、WebSocket、报表等以及关键字 `dootask`
发现与当前任务高度相关的偏好 / 流程 / 约束时,应优先遵守;如存在冲突,应在回答中说明并做合理选择。
## 什么时候写入 Graphiti
| 类型 | 说明 | 示例 |
|------|------|------|
| **偏好 (Preferences)** | 用户表达持续性偏好时 | 语言、输出格式、技术选型 |
| **流程 (Procedures)** | 形成稳定的开发/发布/调试流程时 | 可复用步骤 |
| **约束 (Requirements)** | 项目长期有效的决策 | 不再支持某版本、架构约定 |
| **事实 (Facts)** | 模块边界、服务调用关系、外部集成方式 | AgoraIO、Manticore Search |
### 写入建议
- 默认使用 `source: "text"`,在 `episode_body` 中用简洁结构化自然语言描述背景、类型、范围、具体内容
- 需要结构化数据时可用 `source: "json"`,保证 `episode_body` 是合法 JSON 字符串
- 所有写入默认使用 `group_id: "dootask-main"`
## 更新与更正
- 偏好 / 流程发生变化时,新增一条 episode 说明新约定,并标明这是对旧习惯的更新
- 用户要求「忘记」某些记忆时,可通过删除或更正相关 episode / 关系的方式处理
- 尽量通过新增 episode 记录「更正 / 废弃说明」,而不是直接改写历史事实
## 使用原则
- **尊重已存偏好**:编码风格、回答结构、工具选择等应对齐已知偏好
- **遵循已有流程**:若图谱中已有与当前任务匹配的 Procedure应尽量按步骤执行
- **利用事实**:理解系统行为、模块边界、历史决策时优先查已存 Facts
- **代码优先**:如 Graphiti 与当前代码实际冲突,应以代码实际为准,并视情况新增 episode 更新事实
## 不要写入的内容
- 敏感信息(密钥、密码、隐私数据)
- 只与当前一次任务相关、未来不会复用的临时信息
- 体量巨大的原始数据(完整日志、长脚本全文),应只存摘要和关键结论
## 最佳实践
1. **先查再做**:在提出方案或改动架构前,优先查阅 Graphiti 中已有的设计、偏好和约束
2. **能复用就沉淀**:只要发现某个偏好 / 流程 / 约束未来会反复用到,就尽快写入 Graphiti
3. **保持一致**:确保 Graphiti 中的记忆与实际代码长期保持一致,避免「记忆漂移」

4
.gitignore vendored
View File

@@ -23,6 +23,9 @@
vars.yaml
# IDE and editor files
.cursor/*
!.cursor/rules/
!.cursor/rules/**
.idea
.vscode
.windsurfrules
@@ -57,5 +60,4 @@ laravels.pid
.DS_Store
# Documentation
AGENTS.md
README_LOCAL.md

File diff suppressed because it is too large Load Diff

125
CLAUDE.md Normal file
View File

@@ -0,0 +1,125 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## 项目概述
DooTask 是一套开源的任务/项目管理系统,支持看板、任务、子任务、评论、对话、文件、报表等协作能力。
- **后端**Laravel 8运行在 LaravelS/Swoole 常驻进程上
- **前端**Vue 2 + Vite
- **桌面端**Electron 壳,核心逻辑复用 Web 前端
## 开发命令
所有命令通过 `./cmd` 脚本执行,确保与 Docker/容器环境一致:
```bash
# 服务管理
./cmd up # 启动容器
./cmd down # 停止容器
./cmd restart # 重启容器
./cmd reup # 重新构建并启动
# 开发构建
./cmd dev # 启动前端开发服务器(需要 Node.js 20+
./cmd serve # dev 别名
./cmd prod # 构建前端生产版本
./cmd build # prod 别名
# Laravel/PHP
./cmd artisan ... # 运行 Laravel Artisan 命令
./cmd composer ... # 运行 Composer 命令
./cmd php ... # 运行 PHP 命令
# Electron
./cmd electron # 构建桌面应用
# 配置管理
./cmd port <端口> # 修改服务端口
./cmd url <地址> # 修改访问地址
./cmd env <键> <值> # 设置环境变量
./cmd debug [true|false] # 切换调试模式
# 数据库
./cmd mysql backup # 备份数据库
./cmd mysql recovery # 恢复数据库
# 其他
./cmd install # 一键安装
./cmd update # 升级项目
./cmd repassword # 重置管理员密码
./cmd doc # 生成 API 文档
./cmd https # 配置 HTTPS
```
## 代码架构
### 后端 (`app/`)
**Controller (`app/Http/Controllers/Api/`)**API 控制器,负责路由入口、参数校验、编排调用模型/模块、组装响应。保持控制器「薄」,业务异常通过 `App\Exceptions\ApiException` 抛出。
**Model (`app/Models/`)**Eloquent 模型,负责表结构映射、关系、访问器/修改器、查询 Scope。避免在模型中堆积复杂业务逻辑。
**Module (`app/Module/`)**:跨控制器/跨模型的业务逻辑与独立功能子域:
- 外部集成:`AgoraIO/``Manticore/`
- 通用工具:`Lock.php``TextExtractor.php``Image.php``AI.php`
- 复杂业务逻辑:`Base.php`(核心业务)、`Doo.php``Timer.php`
**Tasks (`app/Tasks/`)**Swoole 异步任务,用于后台处理:
- WebSocket 消息推送:`WebSocketDialogMsgTask.php``PushTask.php`
- 定时任务:`LoopTask.php``AutoArchivedTask.php`
- 搜索同步:`ManticoreSyncTask.php`
**Observers (`app/Observers/`)**Eloquent 观察者监听模型事件created/updated/deleted自动触发相关逻辑。
**Services (`app/Services/`)**:服务类,如 `WebSocketService.php``RequestContext.php`
### 前端 (`resources/assets/js/`)
```
├── app.js, App.vue # 应用入口与根组件
├── components/ # 通用与业务组件(看板、文件预览、聊天)
├── pages/ # 页面级组件(登录、项目、任务、消息、报表)
├── store/ # Vuex 状态管理
│ ├── state.js # 状态定义
│ ├── mutations.js # 同步修改
│ ├── actions.js # 异步操作(含 API 调用封装)
│ └── getters.js # 计算属性
├── routes.js # 前端路由
├── functions/ # 业务函数
├── utils/ # 工具函数
├── directives/ # Vue 自定义指令
├── mixins/ # Vue 混入
└── language/ # 国际化翻译
```
API 调用应使用 `store/actions.js` 中已有的封装,避免在组件中散落 axios/fetch。
### LaravelS/Swoole 注意事项
- **避免在静态属性、单例、全局变量中存储请求级状态**——防止请求间数据串联和内存泄漏
- 构造函数、服务提供者、`boot()` 方法不会在每个请求重新执行
- 配置/路由变更需要 `./cmd php restart` 或容器重启才能生效
- 长生命周期逻辑WebSocket、定时器应复用现有模式避免阻塞协程/事件循环
## 数据库
- 所有表结构变更必须通过 Laravel migration禁止直接改库
- 使用 Eloquent 模型访问数据库
## 前端弹窗文案
调用 `$A.modalXXX``$A.messageXXX``$A.noticeXXX` 时,内部会自动处理 `$L` 翻译,调用方不要额外包 `$L`。仅当显式传入 `language: false` 时,才由调用方自行处理翻译。
## 交互规范
- **提问时附带建议**:当需要向用户提问或请求澄清时,应同时提供具体的建议选项或推荐方案,帮助用户快速决策,而非仅抛出开放式问题
## 语言偏好
- 技术总结和关键结论优先使用简体中文,除非用户明确要求其他语言
## 扩展规则
详见 @.claude/rules/graphiti.md 了解 Graphiti 长期记忆集成。

View File

@@ -0,0 +1,205 @@
<?php
namespace App\Console\Commands;
use App\Console\Commands\Traits\ManticoreSyncLock;
use App\Models\File;
use App\Models\Project;
use App\Models\ProjectTask;
use App\Models\User;
use App\Models\WebSocketDialogMsg;
use App\Module\Apps;
use App\Module\Manticore\ManticoreFile;
use App\Module\Manticore\ManticoreKeyValue;
use App\Module\Manticore\ManticoreMsg;
use App\Module\Manticore\ManticoreProject;
use App\Module\Manticore\ManticoreTask;
use App\Module\Manticore\ManticoreUser;
use Illuminate\Console\Command;
/**
* 异步向量生成命令
*
* 用于后台批量生成已索引数据的向量,与全文索引解耦
* 使用双指针追踪sync:xxxLastId全文已同步和 vector:xxxLastId向量已生成
*
* 运行模式:
* - 持续处理直到所有待处理数据完成
* - 每批处理完成后休眠几秒,避免 API 过载
* - 定时器只作为兜底触发机制
*/
class GenerateManticoreVectors extends Command
{
use ManticoreSyncLock;
protected $signature = 'manticore:generate-vectors
{--type=all : 类型 (msg/file/task/project/user/all)}
{--batch=50 : 每批 embedding 数量}
{--sleep=3 : 每批处理后休眠秒数}
{--reset : 重置向量进度指针}';
protected $description = '批量生成 Manticore 已索引数据的向量';
/**
* 类型配置
*/
private const TYPE_CONFIG = [
'msg' => [
'syncKey' => 'sync:manticoreMsgLastId',
'vectorKey' => 'vector:manticoreMsgLastId',
'class' => ManticoreMsg::class,
'model' => WebSocketDialogMsg::class,
'idField' => 'id',
],
'file' => [
'syncKey' => 'sync:manticoreFileLastId',
'vectorKey' => 'vector:manticoreFileLastId',
'class' => ManticoreFile::class,
'model' => File::class,
'idField' => 'id',
],
'task' => [
'syncKey' => 'sync:manticoreTaskLastId',
'vectorKey' => 'vector:manticoreTaskLastId',
'class' => ManticoreTask::class,
'model' => ProjectTask::class,
'idField' => 'id',
],
'project' => [
'syncKey' => 'sync:manticoreProjectLastId',
'vectorKey' => 'vector:manticoreProjectLastId',
'class' => ManticoreProject::class,
'model' => Project::class,
'idField' => 'id',
],
'user' => [
'syncKey' => 'sync:manticoreUserLastId',
'vectorKey' => 'vector:manticoreUserLastId',
'class' => ManticoreUser::class,
'model' => User::class,
'idField' => 'userid',
],
];
public function handle(): int
{
if (!Apps::isInstalled("search")) {
$this->error("应用「Manticore Search」未安装");
return 1;
}
if (!Apps::isInstalled("ai")) {
$this->error("应用「AI」未安装无法生成向量");
return 1;
}
$this->registerSignalHandlers();
if (!$this->acquireLock()) {
return 1;
}
$type = $this->option('type');
$batchSize = intval($this->option('batch'));
$sleepSeconds = intval($this->option('sleep'));
$reset = $this->option('reset');
if ($type === 'all') {
$types = array_keys(self::TYPE_CONFIG);
} else {
if (!isset(self::TYPE_CONFIG[$type])) {
$this->error("未知类型: {$type}。可用类型: msg, file, task, project, user, all");
$this->releaseLock();
return 1;
}
$types = [$type];
}
// 持续处理直到所有类型都没有待处理数据
$round = 0;
do {
$round++;
$totalPending = 0;
foreach ($types as $t) {
if ($this->shouldStop) {
break;
}
$pending = $this->processType($t, $batchSize, $reset && $round === 1);
$totalPending += $pending;
}
// 如果还有待处理数据,休眠后继续
if ($totalPending > 0 && !$this->shouldStop) {
$this->info("\n--- 第 {$round} 轮完成,剩余 {$totalPending} 条待处理,{$sleepSeconds} 秒后继续 ---\n");
sleep($sleepSeconds);
$this->setLock(); // 刷新锁
}
} while ($totalPending > 0 && !$this->shouldStop);
$this->info("\n向量生成完成(共 {$round} 轮)");
$this->releaseLock();
return 0;
}
/**
* 处理单个类型的向量生成(每次处理一批)
*
* @param string $type 类型
* @param int $batchSize 每批数量
* @param bool $reset 是否重置进度
* @return int 剩余待处理数量
*/
private function processType(string $type, int $batchSize, bool $reset): int
{
$config = self::TYPE_CONFIG[$type];
// 获取进度指针
$syncLastId = intval(ManticoreKeyValue::get($config['syncKey'], 0));
$vectorLastId = $reset ? 0 : intval(ManticoreKeyValue::get($config['vectorKey'], 0));
if ($reset) {
ManticoreKeyValue::set($config['vectorKey'], 0);
$this->info("[{$type}] 已重置向量进度指针");
}
// 计算待处理范围
$pendingCount = $syncLastId - $vectorLastId;
if ($pendingCount <= 0) {
return 0;
}
// 获取待处理的 ID 列表(每次处理 batchSize * 5 条,让 generateVectorsBatch 内部再分批调用 API
$modelClass = $config['model'];
$idField = $config['idField'];
$fetchCount = $batchSize * 5;
$ids = $modelClass::where($idField, '>', $vectorLastId)
->where($idField, '<=', $syncLastId)
->orderBy($idField)
->limit($fetchCount)
->pluck($idField)
->toArray();
if (empty($ids)) {
return 0;
}
// 批量生成向量
$manticoreClass = $config['class'];
$successCount = $manticoreClass::generateVectorsBatch($ids, $batchSize);
$currentLastId = end($ids);
// 更新向量进度指针
ManticoreKeyValue::set($config['vectorKey'], $currentLastId);
$remaining = $pendingCount - count($ids);
$this->info("[{$type}] 处理 " . count($ids) . " 条,成功 {$successCount}ID: {$vectorLastId} -> {$currentLastId},剩余 {$remaining}");
// 刷新锁
$this->setLock();
return max(0, $remaining);
}
}

View File

@@ -0,0 +1,155 @@
<?php
namespace App\Console\Commands;
use App\Console\Commands\Traits\ManticoreSyncLock;
use App\Models\File;
use App\Module\Apps;
use App\Module\Manticore\ManticoreFile;
use App\Module\Manticore\ManticoreKeyValue;
use Illuminate\Console\Command;
class SyncFileToManticore extends Command
{
use ManticoreSyncLock;
/**
* 更新数据
* --f: 全量更新 (默认)
* --i: 增量更新
*
* 清理数据
* --c: 清除索引
*
* 其他选项
* --sleep: 每批处理完成后休眠秒数
*/
protected $signature = 'manticore:sync-files {--f} {--i} {--c} {--batch=100} {--sleep=3}';
protected $description = '同步文件数据到 Manticore Search';
public function handle(): int
{
if (!Apps::isInstalled("search")) {
$this->error("应用「Manticore Search」未安装");
return 1;
}
$this->registerSignalHandlers();
if (!$this->acquireLock()) {
return 1;
}
// 清除索引
if ($this->option('c')) {
$this->info('清除索引...');
ManticoreKeyValue::clear();
ManticoreFile::clear();
$this->info("索引删除成功");
$this->releaseLock();
return 0;
}
$this->info('开始同步文件数据...');
$this->syncFiles();
$this->info("\n同步完成");
$this->releaseLock();
return 0;
}
/**
* 同步文件数据
*/
private function syncFiles(): void
{
$lastKey = "sync:manticoreFileLastId";
$isIncremental = $this->option('i');
$sleepSeconds = intval($this->option('sleep'));
$batchSize = $this->option('batch');
$maxFileSize = ManticoreFile::getMaxFileSize();
$round = 0;
do {
$round++;
$lastId = $isIncremental ? intval(ManticoreKeyValue::get($lastKey, 0)) : 0;
if ($round === 1) {
if ($lastId > 0) {
$this->info("\n增量同步文件数据从ID {$lastId} 开始)...");
} else {
$this->info("\n全量同步文件数据...");
}
}
$count = File::where('id', '>', $lastId)
->where('type', '!=', 'folder')
->where('size', '<=', $maxFileSize)
->count();
if ($count === 0) {
if ($round === 1) {
$this->info("无待同步数据");
}
break;
}
$this->info("[第 {$round} 轮] 待同步 {$count} 个文件");
$num = 0;
$total = 0;
do {
if ($this->shouldStop) {
break;
}
$files = File::where('id', '>', $lastId)
->where('type', '!=', 'folder')
->where('size', '<=', $maxFileSize)
->orderBy('id')
->limit($batchSize)
->get();
if ($files->isEmpty()) {
break;
}
$num += count($files);
$progress = $count > 0 ? round($num / $count * 100, 2) : 100;
$this->info("{$num}/{$count} ({$progress}%) 文件ID {$files->first()->id} ~ {$files->last()->id}");
$this->setLock();
$syncCount = ManticoreFile::batchSync($files);
$total += $syncCount;
$lastId = $files->last()->id;
ManticoreKeyValue::set($lastKey, $lastId);
} while (count($files) == $batchSize && !$this->shouldStop);
$this->info("[第 {$round} 轮] 完成,同步 {$total}最后ID {$lastId}");
if ($isIncremental && !$this->shouldStop) {
$newCount = File::where('id', '>', $lastId)
->where('type', '!=', 'folder')
->where('size', '<=', $maxFileSize)
->count();
if ($newCount > 0) {
$this->info("发现 {$newCount} 个新文件,{$sleepSeconds} 秒后继续...");
sleep($sleepSeconds);
continue;
}
}
break;
} while (!$this->shouldStop);
$this->info("同步文件结束(共 {$round} 轮)- 最后ID: " . ManticoreKeyValue::get($lastKey, 0));
$this->info("已索引文件数量: " . ManticoreFile::getIndexedCount());
}
}

View File

@@ -0,0 +1,232 @@
<?php
namespace App\Console\Commands;
use App\Console\Commands\Traits\ManticoreSyncLock;
use App\Models\WebSocketDialogMsg;
use App\Module\Apps;
use App\Module\Manticore\ManticoreMsg;
use App\Module\Manticore\ManticoreKeyValue;
use Illuminate\Console\Command;
class SyncMsgToManticore extends Command
{
use ManticoreSyncLock;
/**
* 更新数据
* --f: 全量更新 (默认)
* --i: 增量更新
*
* 清理数据
* --c: 清除索引
*
* 其他选项
* --dialog: 指定对话ID
* --sleep: 每批处理完成后休眠秒数
*/
protected $signature = 'manticore:sync-msgs {--f} {--i} {--c} {--batch=100} {--dialog=} {--sleep=3}';
protected $description = '同步消息数据到 Manticore Search';
public function handle(): int
{
if (!Apps::isInstalled("search")) {
$this->error("应用「Manticore Search」未安装");
return 1;
}
$this->registerSignalHandlers();
if (!$this->acquireLock()) {
return 1;
}
// 清除索引
if ($this->option('c')) {
$this->info('清除索引...');
ManticoreMsg::clear();
$this->info("索引删除成功");
$this->releaseLock();
return 0;
}
$dialogId = $this->option('dialog') ? intval($this->option('dialog')) : 0;
if ($dialogId > 0) {
$this->info("开始同步对话 {$dialogId} 的消息数据...");
$this->syncDialogMsgs($dialogId);
} else {
$this->info('开始同步消息数据...');
$this->syncMsgs();
}
$this->info("\n同步完成");
$this->releaseLock();
return 0;
}
/**
* 同步所有消息
*/
private function syncMsgs(): void
{
$lastKey = "sync:manticoreMsgLastId";
$isIncremental = $this->option('i');
$sleepSeconds = intval($this->option('sleep'));
$batchSize = $this->option('batch');
$round = 0;
// 持续处理循环(增量模式下)
do {
$round++;
$lastId = $isIncremental ? intval(ManticoreKeyValue::get($lastKey, 0)) : 0;
if ($round === 1) {
if ($lastId > 0) {
$this->info("\n增量同步消息数据从ID {$lastId} 开始)...");
} else {
$this->info("\n全量同步消息数据...");
}
}
// 构建基础查询条件
$count = WebSocketDialogMsg::where('id', '>', $lastId)
->whereNull('deleted_at')
->where('bot', '!=', 1)
->whereNotNull('key')
->where('key', '!=', '')
->whereIn('type', ManticoreMsg::INDEXABLE_TYPES)
->count();
if ($count === 0) {
if ($round === 1) {
$this->info("无待同步数据");
}
break;
}
$this->info("[第 {$round} 轮] 待同步 {$count} 条消息");
$num = 0;
$total = 0;
do {
if ($this->shouldStop) {
break;
}
$msgs = WebSocketDialogMsg::where('id', '>', $lastId)
->whereNull('deleted_at')
->where('bot', '!=', 1)
->whereNotNull('key')
->where('key', '!=', '')
->whereIn('type', ManticoreMsg::INDEXABLE_TYPES)
->orderBy('id')
->limit($batchSize)
->get();
if ($msgs->isEmpty()) {
break;
}
$num += count($msgs);
$progress = $count > 0 ? round($num / $count * 100, 2) : 100;
$this->info("{$num}/{$count} ({$progress}%) 消息ID {$msgs->first()->id} ~ {$msgs->last()->id}");
$this->setLock();
$syncCount = ManticoreMsg::batchSync($msgs);
$total += $syncCount;
$lastId = $msgs->last()->id;
ManticoreKeyValue::set($lastKey, $lastId);
} while (count($msgs) == $batchSize && !$this->shouldStop);
$this->info("[第 {$round} 轮] 完成,同步 {$total}最后ID {$lastId}");
// 增量模式下,检查是否有新数据,有则继续
if ($isIncremental && !$this->shouldStop) {
$newCount = WebSocketDialogMsg::where('id', '>', $lastId)
->whereNull('deleted_at')
->where('bot', '!=', 1)
->whereNotNull('key')
->where('key', '!=', '')
->whereIn('type', ManticoreMsg::INDEXABLE_TYPES)
->count();
if ($newCount > 0) {
$this->info("发现 {$newCount} 条新数据,{$sleepSeconds} 秒后继续...");
sleep($sleepSeconds);
continue;
}
}
break; // 非增量模式或无新数据,退出循环
} while (!$this->shouldStop);
$this->info("同步消息结束(共 {$round} 轮)- 最后ID: " . ManticoreKeyValue::get($lastKey, 0));
$this->info("已索引消息数量: " . ManticoreMsg::getIndexedCount());
}
/**
* 同步指定对话的消息
*
* @param int $dialogId 对话ID
*/
private function syncDialogMsgs(int $dialogId): void
{
$this->info("\n同步对话 {$dialogId} 的消息数据...");
$baseQuery = WebSocketDialogMsg::where('dialog_id', $dialogId)
->whereNull('deleted_at')
->where('bot', '!=', 1)
->whereNotNull('key')
->where('key', '!=', '')
->whereIn('type', ManticoreMsg::INDEXABLE_TYPES);
$num = 0;
$count = $baseQuery->count();
$batchSize = $this->option('batch');
$lastId = 0;
$total = 0;
$lastNum = 0;
do {
$msgs = WebSocketDialogMsg::where('dialog_id', $dialogId)
->where('id', '>', $lastId)
->whereNull('deleted_at')
->where('bot', '!=', 1)
->whereNotNull('key')
->where('key', '!=', '')
->whereIn('type', ManticoreMsg::INDEXABLE_TYPES)
->orderBy('id')
->limit($batchSize)
->get();
if ($msgs->isEmpty()) {
break;
}
$num += count($msgs);
$progress = $count > 0 ? round($num / $count * 100, 2) : 100;
if ($progress < 100) {
$progress = number_format($progress, 2);
}
$this->info("{$num}/{$count} ({$progress}%) 正在同步消息ID {$msgs->first()->id} ~ {$msgs->last()->id} ({$total}|{$lastNum})");
$this->setLock();
$lastNum = ManticoreMsg::batchSync($msgs);
$total += $lastNum;
$lastId = $msgs->last()->id;
} while (count($msgs) == $batchSize);
$this->info("同步对话 {$dialogId} 消息结束");
$this->info("该对话已索引消息数量: " . \App\Module\Manticore\ManticoreBase::getDialogIndexedMsgCount($dialogId));
}
}

View File

@@ -0,0 +1,146 @@
<?php
namespace App\Console\Commands;
use App\Console\Commands\Traits\ManticoreSyncLock;
use App\Models\Project;
use App\Module\Apps;
use App\Module\Manticore\ManticoreProject;
use App\Module\Manticore\ManticoreKeyValue;
use Illuminate\Console\Command;
class SyncProjectToManticore extends Command
{
use ManticoreSyncLock;
/**
* 更新数据
* --f: 全量更新 (默认)
* --i: 增量更新
*
* 清理数据
* --c: 清除索引
*
* 其他选项
* --sleep: 每批处理完成后休眠秒数
*/
protected $signature = 'manticore:sync-projects {--f} {--i} {--c} {--batch=100} {--sleep=3}';
protected $description = '同步项目数据到 Manticore Search';
public function handle(): int
{
if (!Apps::isInstalled("search")) {
$this->error("应用「Manticore Search」未安装");
return 1;
}
$this->registerSignalHandlers();
if (!$this->acquireLock()) {
return 1;
}
if ($this->option('c')) {
$this->info('清除索引...');
ManticoreProject::clear();
$this->info("索引删除成功");
$this->releaseLock();
return 0;
}
$this->info('开始同步项目数据...');
$this->syncProjects();
$this->info("\n同步完成");
$this->releaseLock();
return 0;
}
private function syncProjects(): void
{
$lastKey = "sync:manticoreProjectLastId";
$isIncremental = $this->option('i');
$sleepSeconds = intval($this->option('sleep'));
$batchSize = $this->option('batch');
$round = 0;
do {
$round++;
$lastId = $isIncremental ? intval(ManticoreKeyValue::get($lastKey, 0)) : 0;
if ($round === 1) {
if ($lastId > 0) {
$this->info("\n增量同步项目数据从ID {$lastId} 开始)...");
} else {
$this->info("\n全量同步项目数据...");
}
}
$count = Project::where('id', '>', $lastId)
->whereNull('archived_at')
->count();
if ($count === 0) {
if ($round === 1) {
$this->info("无待同步数据");
}
break;
}
$this->info("[第 {$round} 轮] 待同步 {$count} 个项目");
$num = 0;
$total = 0;
do {
if ($this->shouldStop) {
break;
}
$projects = Project::where('id', '>', $lastId)
->whereNull('archived_at')
->orderBy('id')
->limit($batchSize)
->get();
if ($projects->isEmpty()) {
break;
}
$num += count($projects);
$progress = $count > 0 ? round($num / $count * 100, 2) : 100;
$this->info("{$num}/{$count} ({$progress}%) 项目ID {$projects->first()->id} ~ {$projects->last()->id}");
$this->setLock();
$syncCount = ManticoreProject::batchSync($projects);
$total += $syncCount;
$lastId = $projects->last()->id;
ManticoreKeyValue::set($lastKey, $lastId);
} while (count($projects) == $batchSize && !$this->shouldStop);
$this->info("[第 {$round} 轮] 完成,同步 {$total}最后ID {$lastId}");
if ($isIncremental && !$this->shouldStop) {
$newCount = Project::where('id', '>', $lastId)
->whereNull('archived_at')
->count();
if ($newCount > 0) {
$this->info("发现 {$newCount} 个新项目,{$sleepSeconds} 秒后继续...");
sleep($sleepSeconds);
continue;
}
}
break;
} while (!$this->shouldStop);
$this->info("同步项目结束(共 {$round} 轮)- 最后ID: " . ManticoreKeyValue::get($lastKey, 0));
$this->info("已索引项目数量: " . ManticoreProject::getIndexedCount());
}
}

View File

@@ -0,0 +1,149 @@
<?php
namespace App\Console\Commands;
use App\Console\Commands\Traits\ManticoreSyncLock;
use App\Models\ProjectTask;
use App\Module\Apps;
use App\Module\Manticore\ManticoreTask;
use App\Module\Manticore\ManticoreKeyValue;
use Illuminate\Console\Command;
class SyncTaskToManticore extends Command
{
use ManticoreSyncLock;
/**
* 更新数据
* --f: 全量更新 (默认)
* --i: 增量更新
*
* 清理数据
* --c: 清除索引
*
* 其他选项
* --sleep: 每批处理完成后休眠秒数
*/
protected $signature = 'manticore:sync-tasks {--f} {--i} {--c} {--batch=100} {--sleep=3}';
protected $description = '同步任务数据到 Manticore Search';
public function handle(): int
{
if (!Apps::isInstalled("search")) {
$this->error("应用「Manticore Search」未安装");
return 1;
}
$this->registerSignalHandlers();
if (!$this->acquireLock()) {
return 1;
}
if ($this->option('c')) {
$this->info('清除索引...');
ManticoreTask::clear();
$this->info("索引删除成功");
$this->releaseLock();
return 0;
}
$this->info('开始同步任务数据...');
$this->syncTasks();
$this->info("\n同步完成");
$this->releaseLock();
return 0;
}
private function syncTasks(): void
{
$lastKey = "sync:manticoreTaskLastId";
$isIncremental = $this->option('i');
$sleepSeconds = intval($this->option('sleep'));
$batchSize = $this->option('batch');
$round = 0;
do {
$round++;
$lastId = $isIncremental ? intval(ManticoreKeyValue::get($lastKey, 0)) : 0;
if ($round === 1) {
if ($lastId > 0) {
$this->info("\n增量同步任务数据从ID {$lastId} 开始)...");
} else {
$this->info("\n全量同步任务数据...");
}
}
$count = ProjectTask::where('id', '>', $lastId)
->whereNull('archived_at')
->whereNull('deleted_at')
->count();
if ($count === 0) {
if ($round === 1) {
$this->info("无待同步数据");
}
break;
}
$this->info("[第 {$round} 轮] 待同步 {$count} 个任务");
$num = 0;
$total = 0;
do {
if ($this->shouldStop) {
break;
}
$tasks = ProjectTask::where('id', '>', $lastId)
->whereNull('archived_at')
->whereNull('deleted_at')
->orderBy('id')
->limit($batchSize)
->get();
if ($tasks->isEmpty()) {
break;
}
$num += count($tasks);
$progress = $count > 0 ? round($num / $count * 100, 2) : 100;
$this->info("{$num}/{$count} ({$progress}%) 任务ID {$tasks->first()->id} ~ {$tasks->last()->id}");
$this->setLock();
$syncCount = ManticoreTask::batchSync($tasks);
$total += $syncCount;
$lastId = $tasks->last()->id;
ManticoreKeyValue::set($lastKey, $lastId);
} while (count($tasks) == $batchSize && !$this->shouldStop);
$this->info("[第 {$round} 轮] 完成,同步 {$total}最后ID {$lastId}");
if ($isIncremental && !$this->shouldStop) {
$newCount = ProjectTask::where('id', '>', $lastId)
->whereNull('archived_at')
->whereNull('deleted_at')
->count();
if ($newCount > 0) {
$this->info("发现 {$newCount} 个新任务,{$sleepSeconds} 秒后继续...");
sleep($sleepSeconds);
continue;
}
}
break;
} while (!$this->shouldStop);
$this->info("同步任务结束(共 {$round} 轮)- 最后ID: " . ManticoreKeyValue::get($lastKey, 0));
$this->info("已索引任务数量: " . ManticoreTask::getIndexedCount());
}
}

View File

@@ -1,175 +0,0 @@
<?php
namespace App\Console\Commands;
use App\Models\WebSocketDialogMsg;
use App\Module\Apps;
use App\Module\ZincSearch\ZincSearchKeyValue;
use App\Module\ZincSearch\ZincSearchDialogMsg;
use Cache;
use Illuminate\Console\Command;
class SyncUserMsgToZincSearch extends Command
{
/**
* 更新数据
* --f: 全量更新 (默认)
* --i: 增量更新从上次更新的最后一个ID接上
*
* 清理数据
* --c: 清除索引
*/
protected $signature = 'zinc:sync-user-msg {--f} {--i} {--c} {--batch=1000}';
protected $description = '同步聊天会话用户和消息到 ZincSearch';
/**
* @return int
*/
public function handle(): int
{
if (!Apps::isInstalled("search")) {
$this->error("应用「ZincSearch」未安装");
return 1;
}
// 注册信号处理器仅在支持pcntl扩展的环境下
if (extension_loaded('pcntl')) {
pcntl_async_signals(true); // 启用异步信号处理
pcntl_signal(SIGINT, [$this, 'handleSignal']); // Ctrl+C
pcntl_signal(SIGTERM, [$this, 'handleSignal']); // kill
}
// 检查锁,如果已被占用则退出
$lockInfo = $this->getLock();
if ($lockInfo) {
$this->error("命令已在运行中,开始时间: {$lockInfo['started_at']}");
return 1;
}
// 设置锁
$this->setLock();
// 清除索引
if ($this->option('c')) {
$this->info('清除索引...');
ZincSearchKeyValue::clear();
ZincSearchDialogMsg::clear();
$this->info("索引删除成功");
$this->releaseLock();
return 0;
}
$this->info('开始同步聊天数据...');
// 同步消息数据
$this->syncDialogMsgs();
// 完成
$this->info("\n同步完成");
$this->releaseLock();
return 0;
}
/**
* 获取锁信息
*
* @return array|null 如果锁存在返回锁信息否则返回null
*/
private function getLock(): ?array
{
$lockKey = md5($this->signature);
return Cache::has($lockKey) ? Cache::get($lockKey) : null;
}
/**
* 设置锁
*/
private function setLock(): void
{
$lockKey = md5($this->signature);
$lockInfo = [
'started_at' => date('Y-m-d H:i:s')
];
Cache::put($lockKey, $lockInfo, 300); // 5分钟
}
/**
* 释放锁
*/
private function releaseLock(): void
{
$lockKey = md5($this->signature);
Cache::forget($lockKey);
}
/**
* 处理终端信号
*
* @param int $signal
* @return void
*/
public function handleSignal(int $signal): void
{
// 释放锁
$this->releaseLock();
exit(0);
}
/**
* 同步消息数据
*
* @return void
*/
private function syncDialogMsgs(): void
{
// 获取上次同步的最后ID
$lastKey = "sync:dialogUserMsgLastId";
$lastId = $this->option('i') ? intval(ZincSearchKeyValue::get($lastKey, 0)) : 0;
if ($lastId > 0) {
$this->info("\n同步消息数据({$lastId}...");
} else {
$this->info("\n同步消息数据...");
}
$num = 0;
$count = WebSocketDialogMsg::where('id', '>', $lastId)->count();
$batchSize = $this->option('batch');
$total = 0;
$lastNum = 0;
do {
// 获取一批
$dialogMsgs = WebSocketDialogMsg::where('id', '>', $lastId)
->orderBy('id')
->limit($batchSize)
->get();
if ($dialogMsgs->isEmpty()) {
break;
}
$num += count($dialogMsgs);
$progress = round($num / $count * 100, 2);
if ($progress < 100) {
$progress = number_format($progress, 2);
}
$this->info("{$num}/{$count} ({$progress}%) 正在同步消息ID {$dialogMsgs->first()->id} ~ {$dialogMsgs->last()->id} ({$total}|{$lastNum})");
// 刷新锁
$this->setLock();
// 同步数据
$lastNum = ZincSearchDialogMsg::batchSync($dialogMsgs);
$total += $lastNum;
// 更新最后ID
$lastId = $dialogMsgs->last()->id;
ZincSearchKeyValue::set($lastKey, $lastId);
} while (count($dialogMsgs) == $batchSize);
$this->info("同步消息结束 - 最后ID {$lastId}");
}
}

View File

@@ -0,0 +1,149 @@
<?php
namespace App\Console\Commands;
use App\Console\Commands\Traits\ManticoreSyncLock;
use App\Models\User;
use App\Module\Apps;
use App\Module\Manticore\ManticoreUser;
use App\Module\Manticore\ManticoreKeyValue;
use Illuminate\Console\Command;
class SyncUserToManticore extends Command
{
use ManticoreSyncLock;
/**
* 更新数据
* --f: 全量更新 (默认)
* --i: 增量更新
*
* 清理数据
* --c: 清除索引
*
* 其他选项
* --sleep: 每批处理完成后休眠秒数
*/
protected $signature = 'manticore:sync-users {--f} {--i} {--c} {--batch=100} {--sleep=3}';
protected $description = '同步用户数据到 Manticore Search';
public function handle(): int
{
if (!Apps::isInstalled("search")) {
$this->error("应用「Manticore Search」未安装");
return 1;
}
$this->registerSignalHandlers();
if (!$this->acquireLock()) {
return 1;
}
if ($this->option('c')) {
$this->info('清除索引...');
ManticoreUser::clear();
$this->info("索引删除成功");
$this->releaseLock();
return 0;
}
$this->info('开始同步用户数据...');
$this->syncUsers();
$this->info("\n同步完成");
$this->releaseLock();
return 0;
}
private function syncUsers(): void
{
$lastKey = "sync:manticoreUserLastId";
$isIncremental = $this->option('i');
$sleepSeconds = intval($this->option('sleep'));
$batchSize = $this->option('batch');
$round = 0;
do {
$round++;
$lastId = $isIncremental ? intval(ManticoreKeyValue::get($lastKey, 0)) : 0;
if ($round === 1) {
if ($lastId > 0) {
$this->info("\n增量同步用户数据从ID {$lastId} 开始)...");
} else {
$this->info("\n全量同步用户数据...");
}
}
$count = User::where('userid', '>', $lastId)
->where('bot', 0)
->whereNull('disable_at')
->count();
if ($count === 0) {
if ($round === 1) {
$this->info("无待同步数据");
}
break;
}
$this->info("[第 {$round} 轮] 待同步 {$count} 个用户");
$num = 0;
$total = 0;
do {
if ($this->shouldStop) {
break;
}
$users = User::where('userid', '>', $lastId)
->where('bot', 0)
->whereNull('disable_at')
->orderBy('userid')
->limit($batchSize)
->get();
if ($users->isEmpty()) {
break;
}
$num += count($users);
$progress = $count > 0 ? round($num / $count * 100, 2) : 100;
$this->info("{$num}/{$count} ({$progress}%) 用户ID {$users->first()->userid} ~ {$users->last()->userid}");
$this->setLock();
$syncCount = ManticoreUser::batchSync($users);
$total += $syncCount;
$lastId = $users->last()->userid;
ManticoreKeyValue::set($lastKey, $lastId);
} while (count($users) == $batchSize && !$this->shouldStop);
$this->info("[第 {$round} 轮] 完成,同步 {$total}最后ID {$lastId}");
if ($isIncremental && !$this->shouldStop) {
$newCount = User::where('userid', '>', $lastId)
->where('bot', 0)
->whereNull('disable_at')
->count();
if ($newCount > 0) {
$this->info("发现 {$newCount} 个新用户,{$sleepSeconds} 秒后继续...");
sleep($sleepSeconds);
continue;
}
}
break;
} while (!$this->shouldStop);
$this->info("同步用户结束(共 {$round} 轮)- 最后ID: " . ManticoreKeyValue::get($lastKey, 0));
$this->info("已索引用户数量: " . ManticoreUser::getIndexedCount());
}
}

View File

@@ -0,0 +1,90 @@
<?php
namespace App\Console\Commands\Traits;
use Cache;
/**
* Manticore 同步命令通用锁机制
*
* 提供:
* - 锁的获取、设置、释放
* - 信号处理(优雅退出)
* - 通用的命令初始化检查
*/
trait ManticoreSyncLock
{
private bool $shouldStop = false;
/**
* 获取锁信息
*/
private function getLock(): ?array
{
$lockKey = $this->getLockKey();
return Cache::has($lockKey) ? Cache::get($lockKey) : null;
}
/**
* 设置锁30分钟有效期持续处理时需不断刷新
*/
private function setLock(): void
{
$lockKey = $this->getLockKey();
Cache::put($lockKey, ['started_at' => date('Y-m-d H:i:s')], 1800);
}
/**
* 释放锁
*/
private function releaseLock(): void
{
$lockKey = $this->getLockKey();
Cache::forget($lockKey);
}
/**
* 获取锁的缓存键
*/
private function getLockKey(): string
{
return md5($this->signature);
}
/**
* 信号处理器SIGINT/SIGTERM
*/
public function handleSignal(int $signal): void
{
$this->info("\n收到信号,将在当前批次完成后退出...");
$this->shouldStop = true;
}
/**
* 注册信号处理器
*/
private function registerSignalHandlers(): void
{
if (extension_loaded('pcntl')) {
pcntl_async_signals(true);
pcntl_signal(SIGINT, [$this, 'handleSignal']);
pcntl_signal(SIGTERM, [$this, 'handleSignal']);
}
}
/**
* 检查命令是否可以启动(锁检查)
*
* @return bool 返回 true 表示可以启动false 表示已被占用
*/
private function acquireLock(): bool
{
$lockInfo = $this->getLock();
if ($lockInfo) {
$this->error("命令已在运行中,开始时间: {$lockInfo['started_at']}");
return false;
}
$this->setLock();
return true;
}
}

View File

@@ -41,7 +41,7 @@ class ApproveController extends AbstractController
}
/**
* @api {get} api/approve/verifyToken 01. 验证APi登录
* @api {get} api/approve/verifyToken 验证APi登录
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -63,7 +63,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/procdef/all 02. 查询流程定义
* @api {post} api/approve/procdef/all 查询流程定义
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -90,7 +90,7 @@ class ApproveController extends AbstractController
}
/**
* @api {get} api/approve/procdef/del 03. 删除流程定义
* @api {get} api/approve/procdef/del 删除流程定义
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -116,7 +116,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/process/start 04. 启动流程(审批中)
* @api {post} api/approve/process/start 启动流程(审批中)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -179,7 +179,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/process/addGlobalComment 05. 添加全局评论
* @api {post} api/approve/process/addGlobalComment 添加全局评论
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -224,7 +224,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/task/complete 06. 审批
* @api {post} api/approve/task/complete 审批
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -304,7 +304,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/task/withdraw 07. 撤回
* @api {post} api/approve/task/withdraw 撤回
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -349,7 +349,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/process/findTask 08. 查询需要我审批的流程(审批中)
* @api {post} api/approve/process/findTask 查询需要我审批的流程(审批中)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -392,7 +392,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/process/startByMyselfAll 09. 查询我启动的流程(全部)
* @api {post} api/approve/process/startByMyselfAll 查询我启动的流程(全部)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -435,7 +435,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/process/startByMyself 10. 查询我启动的流程(审批中)
* @api {post} api/approve/process/startByMyself 查询我启动的流程(审批中)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -473,7 +473,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/process/findProcNotify 11. 查询抄送我的流程(审批中)
* @api {post} api/approve/process/findProcNotify 查询抄送我的流程(审批中)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -517,7 +517,7 @@ class ApproveController extends AbstractController
}
/**
* @api {get} api/approve/identitylink/findParticipant 12. 查询流程实例的参与者(审批中)
* @api {get} api/approve/identitylink/findParticipant 查询流程实例的参与者(审批中)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -552,7 +552,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/procHistory/findTask 13. 查询需要我审批的流程(已结束)
* @api {post} api/approve/procHistory/findTask 查询需要我审批的流程(已结束)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -595,7 +595,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/procHistory/startByMyself 14. 查询我启动的流程(已结束)
* @api {post} api/approve/procHistory/startByMyself 查询我启动的流程(已结束)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -633,7 +633,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/procHistory/findProcNotify 15. 查询抄送我的流程(已结束)
* @api {post} api/approve/procHistory/findProcNotify 查询抄送我的流程(已结束)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -677,7 +677,7 @@ class ApproveController extends AbstractController
}
/**
* @api {get} api/approve/identitylinkHistory/findParticipant 16. 查询流程实例的参与者(已结束)
* @api {get} api/approve/identitylinkHistory/findParticipant 查询流程实例的参与者(已结束)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -712,7 +712,7 @@ class ApproveController extends AbstractController
}
/**
* @api {get} api/approve/process/detail 17. 根据流程ID查询流程详情
* @api {get} api/approve/process/detail 根据流程ID查询流程详情
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -734,7 +734,7 @@ class ApproveController extends AbstractController
}
/**
* @api {post} api/approve/export 18. 导出数据
* @api {post} api/approve/export 导出数据
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -970,7 +970,7 @@ class ApproveController extends AbstractController
}
/**
* @api {get} api/approve/down 19. 下载导出的审批数据
* @api {get} api/approve/down 下载导出的审批数据
*
* @apiVersion 1.0.0
* @apiGroup approve
@@ -1192,7 +1192,7 @@ class ApproveController extends AbstractController
/**
* @api {get} api/approve/user/status 20. 获取用户审批状态
* @api {get} api/approve/user/status 获取用户审批状态
*
* @apiVersion 1.0.0
* @apiGroup approve
@@ -1212,7 +1212,7 @@ class ApproveController extends AbstractController
}
/**
* @api {get} api/approve/process/doto 21. 查询需要我审批的流程数量
* @api {get} api/approve/process/doto 查询需要我审批的流程数量
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0

View File

@@ -0,0 +1,73 @@
<?php
namespace App\Http\Controllers\Api;
use App\Models\User;
use App\Module\AI;
use App\Module\Apps;
use App\Module\Base;
use Request;
/**
* @apiDefine assistant
*
* 助手
*/
class AssistantController extends AbstractController
{
public function __construct()
{
Apps::isInstalledThrow('ai');
}
/**
* @api {post} api/assistant/auth 生成授权码
*
* @apiDescription 需要token身份生成 AI 流式会话的 stream_key
* @apiVersion 1.0.0
* @apiGroup assistant
* @apiName auth
*
* @apiParam {String} model_type 模型类型
* @apiParam {String} model_name 模型名称
* @apiParam {JSON} context 上下文数组
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @apiSuccess {String} data.stream_key 流式会话凭证
*/
public function auth()
{
$user = User::auth();
$user->checkChatInformation();
$modelType = trim(Request::input('model_type', ''));
$modelName = trim(Request::input('model_name', ''));
$contextInput = Request::input('context', []);
return AI::createStreamKey($modelType, $modelName, $contextInput);
}
/**
* @api {get} api/assistant/models 获取AI模型
*
* @apiDescription 获取所有AI机器人模型设置
* @apiVersion 1.0.0
* @apiGroup assistant
* @apiName models
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function models()
{
$setting = Base::setting('aibotSetting');
$setting = array_filter($setting, function ($value, $key) {
return str_ends_with($key, '_models') || str_ends_with($key, '_model');
}, ARRAY_FILTER_USE_BOTH);
return Base::retSuccess('success', $setting ?: json_decode('{}'));
}
}

View File

@@ -10,18 +10,18 @@ use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg;
/**
* @apiDefine dialog
* @apiDefine complaint
*
* 投诉
*/
class ComplaintController extends AbstractController
{
/**
* @api {get} api/complaint/lists 01. 获取举报投诉列表
* @api {get} api/complaint/lists 获取举报投诉列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiGroup complaint
* @apiName lists
*
* @apiParam {Number} [type] 类型
@@ -33,6 +33,34 @@ class ComplaintController extends AbstractController
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*
* @apiSuccessExample {json} Success-Response-Data:
* {
* "current_page": 1,
* "data": [
* {
* "id": 1,
* "dialog_id": 100,
* "userid": 1,
* "type": 1,
* "reason": "举报原因",
* "imgs": [],
* "status": 0,
* "created_at": "2025-01-01 00:00:00",
* "updated_at": "2025-01-01 00:00:00"
* }
* ],
* "first_page_url": "http://example.com/api/complaint/lists?page=1",
* "from": 1,
* "last_page": 1,
* "last_page_url": "http://example.com/api/complaint/lists?page=1",
* "next_page_url": null,
* "path": "http://example.com/api/complaint/lists",
* "per_page": 50,
* "prev_page_url": null,
* "to": 1,
* "total": 1
* }
*/
public function lists()
{
@@ -56,21 +84,25 @@ class ComplaintController extends AbstractController
}
/**
* @api {get} api/complaint/submit 02. 举报投诉
* @api {post} api/complaint/submit 举报投诉
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiGroup complaint
* @apiName submit
*
* @apiParam {Number} dialog_id 对话ID
* @apiParam {Number} type 类型
* @apiParam {String} reason 原因
* @apiParam {String} imgs 图片
* @apiBody {Number} dialog_id 对话ID
* @apiBody {Number} type 类型
* @apiBody {String} reason 原因
* @apiBody {Object[]} [imgs] 图片数组(可选)
* @apiBody {String} imgs.path 图片路径
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*
* @apiSuccessExample {json} Success-Response-Data:
* []
*/
public function submit()
{
@@ -125,19 +157,22 @@ class ComplaintController extends AbstractController
}
/**
* @api {get} api/complaint/action 03. 举报投诉 - 操作
* @api {post} api/complaint/action 举报投诉 - 操作
*
* @apiDescription 需要token身份
* @apiDescription 需要token身份(管理员权限)
* @apiVersion 1.0.0
* @apiGroup dialog
* @apiGroup complaint
* @apiName action
*
* @apiParam {Number} id ID
* @apiParam {Number} type 类型
* @apiBody {Number} id 投诉ID
* @apiBody {String} type 操作类型handle=已处理delete=删除
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*
* @apiSuccessExample {json} Success-Response-Data:
* []
*/
public function action()
{

File diff suppressed because it is too large Load Diff

View File

@@ -14,6 +14,7 @@ use App\Models\User;
use App\Models\UserRecentItem;
use App\Module\Base;
use App\Module\Down;
use App\Module\Lock;
use App\Module\Timer;
use App\Module\Ihttp;
use Response;
@@ -31,7 +32,7 @@ use ZipArchive;
class FileController extends AbstractController
{
/**
* @api {get} api/file/lists 01. 获取文件列表
* @api {get} api/file/lists 获取文件列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -54,7 +55,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/one 02. 获取单条数据
* @api {get} api/file/one 获取单条数据
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -64,6 +65,9 @@ class FileController extends AbstractController
* @apiParam {Number|String} id
* - Number 文件ID需要登录
* - String 链接码(不需要登录,用于预览)
* @apiParam {String} [with_url] 是否返回文件访问URL
* - no: 不返回(默认)
* - yes: 返回content_url字段
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
@@ -72,11 +76,12 @@ class FileController extends AbstractController
public function one()
{
$id = Request::input('id');
$with_url = Request::input('with_url', 'no');
//
$permission = 0;
if (Base::isNumber($id)) {
$user = User::auth();
$file = File::permissionFind(intval($id), $user, 0, $permission);
$file = File::permissionFind(intval($id), $user, $with_url === 'yes' ? 1 : 0, $permission);
} elseif ($id) {
$fileLink = FileLink::whereCode($id)->first();
$file = $fileLink?->file;
@@ -88,12 +93,12 @@ class FileController extends AbstractController
}
return Base::retError($msg, $data);
}
// 如果文件不允许游客访问,则需要登录
if (!$file->guest_access) {
User::auth();
}
$fileLink->increment("num");
} else {
return Base::retError('参数错误');
@@ -101,20 +106,26 @@ class FileController extends AbstractController
//
$array = $file->toArray();
$array['permission'] = $permission;
// 如果请求返回文件URL
if ($with_url === 'yes') {
$array['content_url'] = FileContent::getFileUrl($file->id);
}
return Base::retSuccess('success', $array);
}
/**
* @api {get} api/file/search 03. 搜索文件列表
* @api {get} api/file/search 搜索文件列表
*
* @apiDescription 需要token身份
* @apiDescription 需要token身份仅搜索文件名AI 内容搜索请使用 api/search/file
* @apiVersion 1.0.0
* @apiGroup file
* @apiName search
*
* @apiParam {String} [link] 通过分享地址搜索https://t.hitosea.com/single/file/ODcwOCwzOSxpa0JBS2lmVQ==
* @apiParam {String} [key] 关键词
* @apiParam {Number} [take] 获取数量默认50最大100
* @apiParam {String} [link] 通过分享地址搜索https://t.hitosea.com/single/file/ODcwOCwzOSxpa0JBS2lmVQ==
* @apiParam {String} [key] 关键词
* @apiParam {Number} [take] 获取数量默认50最大100
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
@@ -135,6 +146,7 @@ class FileController extends AbstractController
return Base::retSuccess('success', []);
}
}
// 搜索自己的
$builder = File::whereUserid($user->userid);
if ($id) {
@@ -142,7 +154,9 @@ class FileController extends AbstractController
}
if ($key) {
if (!$id && Base::isNumber($key)) {
$builder->where("id", $key);
$builder->where(function ($query) use ($key) {
$query->where("id", $key)->orWhere("name", "like", "%{$key}%");
});
} else {
$builder->where("name", "like", "%{$key}%");
}
@@ -164,7 +178,13 @@ class FileController extends AbstractController
$builder->where("id", $id);
}
if ($key) {
$builder->where("name", "like", "%{$key}%");
if (Base::isNumber($key)) {
$builder->where(function ($query) use ($key) {
$query->where("id", $key)->orWhere("name", "like", "%{$key}%");
});
} else {
$builder->where("name", "like", "%{$key}%");
}
}
$list = $builder->take($take)->get();
if ($list->isNotEmpty()) {
@@ -182,7 +202,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/add 04. 添加、修改文件(夹)
* @api {get} api/file/add 添加、修改文件(夹)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -291,7 +311,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/copy 05. 复制文件(夹)
* @api {get} api/file/copy 复制文件(夹)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -352,7 +372,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/move 06. 移动文件(夹)
* @api {get} api/file/move 移动文件(夹)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -399,7 +419,7 @@ class FileController extends AbstractController
throw new ApiException("{$file->name} 内含有共享文件,无法移动到另一个共享文件夹内");
}
$file->userid = $toShareFile->userid;
File::where('pids', 'LIKE', "%,{$file->id},%")->update(['userid' => $toShareFile->userid]);
$file->updateChildFilesUserid($toShareFile->userid);
}
//
$tmpId = $pid;
@@ -411,7 +431,7 @@ class FileController extends AbstractController
}
} else {
$file->userid = $user->userid;
File::where('pids', 'LIKE', "%,{$file->id},%")->update(['userid' => $user->userid]);
$file->updateChildFilesUserid($user->userid);
}
//
$file->pid = $pid;
@@ -427,7 +447,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/remove 07. 删除文件(夹)
* @api {get} api/file/remove 删除文件(夹)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -466,7 +486,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/content 08. 获取文件内容
* @api {get} api/file/content 获取文件内容
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -547,7 +567,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/content/save 09. 保存文件内容
* @api {get} api/file/content/save 保存文件内容
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -642,7 +662,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/office/token 10. 获取token
* @api {get} api/file/office/token 获取token
*
* @apiDescription 用于生成office在线编辑的token
* @apiVersion 1.0.0
@@ -667,7 +687,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/content/office 11. 保存文件内容office
* @api {get} api/file/content/office 保存文件内容office
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -723,7 +743,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/content/upload 12. 保存文件内容(上传文件)
* @api {get} api/file/content/upload 保存文件内容(上传文件)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -744,14 +764,24 @@ class FileController extends AbstractController
{
$user = User::auth();
$pid = intval(Request::input('pid'));
$overwrite = intval(Request::input('cover'));
$webkitRelativePath = Request::input('webkitRelativePath');
$data = (new File)->contentUpload($user, $pid, $webkitRelativePath, $overwrite);
return Base::retSuccess($data['data']['name'] . ' 上传成功', $data['addItem']);
// 同一用户往相同父目录上传时排队,避免并发导致数据库死锁
try {
return Lock::withLock("file:upload:{$user->userid}:{$pid}", function () use ($user, $pid) {
$overwrite = intval(Request::input('cover'));
$webkitRelativePath = Request::input('webkitRelativePath');
$data = (new File)->contentUpload($user, $pid, $webkitRelativePath, $overwrite);
return Base::retSuccess($data['data']['name'] . ' 上传成功', $data['addItem']);
}, 120000, 120000);
} catch (\Exception $e) {
if (str_contains($e->getMessage(), 'Failed to acquire lock')) {
throw new ApiException('上传繁忙,请稍后再试');
}
throw $e;
}
}
/**
* @api {get} api/file/content/history 13. 获取内容历史
* @api {get} api/file/content/history 获取内容历史
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -783,7 +813,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/content/restore 14. 恢复文件历史
* @api {get} api/file/content/restore 恢复文件历史
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -825,7 +855,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/share 15. 获取共享信息
* @api {get} api/file/share 获取共享信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -861,7 +891,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/share/update 16. 设置共享
* @api {get} api/file/share/update 设置共享
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -951,7 +981,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/share/out 17. 退出共享
* @api {get} api/file/share/out 退出共享
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -985,7 +1015,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/link 18. 获取链接
* @api {get} api/file/link 获取链接
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1025,7 +1055,7 @@ class FileController extends AbstractController
}
/**
* @api {get} api/file/download/pack 19. 打包文件
* @api {get} api/file/download/pack 打包文件
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0

View File

@@ -55,7 +55,7 @@ use App\Observers\ProjectTaskObserver;
class ProjectController extends AbstractController
{
/**
* @api {get} api/project/lists 01. 获取项目列表
* @api {get} api/project/lists 获取项目列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -199,7 +199,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/one 02. 获取一个项目信息
* @api {get} api/project/one 获取一个项目信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -260,7 +260,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/add 03. 添加项目
* @api {get} api/project/add 添加项目
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -287,7 +287,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/update 04. 修改项目
* @api {get} api/project/update 修改项目
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -362,7 +362,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/user 05. 修改项目成员
* @api {get} api/project/user 修改项目成员
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -415,7 +415,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/invite 06. 获取邀请链接
* @api {get} api/project/invite 获取邀请链接
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -465,7 +465,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/invite/info 07. 通过邀请链接code获取项目信息
* @api {get} api/project/invite/info 通过邀请链接code获取项目信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -497,7 +497,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/invite/join 08. 通过邀请链接code加入项目
* @api {get} api/project/invite/join 通过邀请链接code加入项目
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -543,7 +543,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/transfer 09. 移交项目
* @api {get} api/project/transfer 移交项目
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -587,7 +587,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/sort 10. 排序任务
* @api {post} api/project/sort 排序任务
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -656,7 +656,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/user/sort 11. 项目列表排序
* @api {post} api/project/user/sort 项目列表排序
*
* @apiDescription 需要token身份按当前用户对项目进行拖动排序仅影响本人
* @apiVersion 1.0.0
@@ -689,7 +689,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/exit 12. 退出项目
* @api {get} api/project/exit 退出项目
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -721,7 +721,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/archived 13. 归档项目
* @api {get} api/project/archived 归档项目
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -755,7 +755,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/remove 14. 删除项目
* @api {get} api/project/remove 删除项目
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -781,7 +781,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/column/lists 15. 获取任务列表
* @api {get} api/project/column/lists 获取任务列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -814,7 +814,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/column/add 16. 添加任务列表
* @api {get} api/project/column/add 添加任务列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -861,7 +861,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/column/update 17. 修改任务列表
* @api {get} api/project/column/update 修改任务列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -906,7 +906,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/column/remove 18. 删除任务列表
* @api {get} api/project/column/remove 删除任务列表
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -939,7 +939,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/column/one 19. 获取任务列详细
* @api {get} api/project/column/one 获取任务列详细
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -979,7 +979,7 @@ class ProjectController extends AbstractController
/**
* @api {get} api/project/task/lists 20. 任务列表
* @api {get} api/project/task/lists 任务列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -991,10 +991,12 @@ class ProjectController extends AbstractController
* - keys.tag: 标签名称
* - keys.status: 任务状态 (completed: 已完成、uncompleted: 未完成、flow-xx: 流程状态ID)
*
* @apiParam {Number} [project_id] 项目ID
* @apiParam {Number} [parent_id] 主任务IDproject_id && parent_id ≤ 0 时 仅查询自己参与的任务
* - 大于0指定主任务下的子任务
* - 等于-1表示仅主任务
* @apiParam {Number} [project_id] 项目ID(传入后只查询该项目内任务)
* @apiParam {Number} [parent_id] 主任务ID查询优先级最高
* - 大于0只查该主任务下的子任务(此时 archived 强制 all忽略 project_id/scope
* - 等于-1仅主任务(可与 project_id 组合)
* @apiParam {String} [scope] 查询范围(仅在未指定 project_id 且 parent_id ≤ 0 时生效)
* - all_project查询“我参与的项目”下的所有任务仍受可见性限制
*
* @apiParam {String} [time] 指定时间范围today, week, month, year, 2020-12-12,2020-12-30
* - today: 今天
@@ -1038,6 +1040,7 @@ class ProjectController extends AbstractController
$deleted = Request::input('deleted', 'no');
$keys = Request::input('keys');
$sorts = Request::input('sorts');
$scope = Request::input('scope');
$keys = is_array($keys) ? $keys : [];
$sorts = is_array($sorts) ? $sorts : [];
@@ -1045,7 +1048,11 @@ class ProjectController extends AbstractController
//
if ($keys['name']) {
if (Base::isNumber($keys['name'])) {
$builder->where("project_tasks.id", intval($keys['name']));
$builder->where(function ($query) use ($keys) {
$query->where("project_tasks.id", intval($keys['name']))
->orWhere("project_tasks.name", "like", "%{$keys['name']}%")
->orWhere("project_tasks.desc", "like", "%{$keys['name']}%");
});
} else {
$builder->where(function ($query) use ($keys) {
$query->where("project_tasks.name", "like", "%{$keys['name']}%");
@@ -1089,6 +1096,14 @@ class ProjectController extends AbstractController
$scopeAll = true;
$builder->where('project_tasks.project_id', $project_id);
}
if (!$scopeAll && $scope === 'all_project') {
$scopeAll = true;
$builder->whereIn('project_tasks.project_id', function ($query) use ($userid) {
$query->select('project_id')
->from('project_users')
->where('userid', $userid);
});
}
if ($scopeAll) {
$builder->allData();
} else {
@@ -1184,6 +1199,7 @@ class ProjectController extends AbstractController
$builder->leftJoinSub(function ($query) {
$query->select('parent_id', DB::raw('count(*) as sub_num, sum(CASE WHEN complete_at IS NOT NULL THEN 1 ELSE 0 END) sub_complete') )
->from('project_tasks')
->whereNull('deleted_at')
->groupBy('parent_id');
}, 'sub_task', 'sub_task.parent_id', '=', 'project_tasks.id');
// 给前缀“_”是为了不触发获取器
@@ -1230,7 +1246,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/easylists 21. 任务列表-简单的
* @api {get} api/project/task/easylists 任务列表-简单的
*
* @apiDescription 需要token身份主要用于判断是否有时间冲突的任务
* @apiVersion 1.0.0
@@ -1288,7 +1304,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/export 22. 导出任务(限管理员)
* @api {get} api/project/task/export 导出任务(限管理员)
*
* @apiDescription 导出指定范围任务已完成、未完成、已归档返回下载地址需要token身份
* @apiVersion 1.0.0
@@ -1359,11 +1375,30 @@ class ProjectController extends AbstractController
'style' => 'font-weight: bold;padding-bottom: 4px;',
];
//
$startTime = Carbon::parse($time[0])->startOfDay();
$endTime = Carbon::parse($time[1])->endOfDay();
$builder = ProjectTask::with(['taskTag'])->select(['project_tasks.*', 'project_task_users.userid as ownerid'])
->join('project_task_users', 'project_tasks.id', '=', 'project_task_users.task_id')
->where('project_task_users.owner', 1)
->whereIn('project_task_users.userid', $userid)
->betweenTime(Carbon::parse($time[0])->startOfDay(), Carbon::parse($time[1])->endOfDay(), $type);
->whereIn('project_task_users.userid', $userid);
// 按导出时间类型筛选:
// - createdTime仅按创建时间范围筛选
// - 任务时间(默认):优先使用任务计划时间筛选,但对“无计划时间”的任务,
// 若在考核期内已完成,则按完成时间 complete_at 兜底纳入导出,避免漏掉考核期内完成的任务。
if ($type === 'createdTime') {
$builder->betweenTime($startTime, $endTime, $type);
} else {
$builder->where(function ($query) use ($startTime, $endTime) {
$query->betweenTime($startTime, $endTime, 'taskTime')
->orWhere(function ($q2) use ($startTime, $endTime) {
$q2->where(function ($q3) {
$q3->whereNull('project_tasks.start_at')
->orWhereNull('project_tasks.end_at');
})->whereNotNull('project_tasks.complete_at')
->whereBetween('project_tasks.complete_at', [$startTime, $endTime]);
});
});
}
$builder->orderByDesc('project_tasks.id')->chunk(100, function ($tasks) use ($doo, &$datas) {
/** @var ProjectTask $task */
foreach ($tasks as $task) {
@@ -1409,15 +1444,17 @@ class ProjectController extends AbstractController
}
$actualTime = $task->complete_at ? $totalTime : 0; // 实际完成用时
$statusText = '未完成';
// 状态判定规则:
// - flow_item_name 以 end| 开头:视为结束态,区分“已取消”和“已完成”
// - 非 end|,但 complete_at 有值:视为已完成(兼容无流程或历史数据)
if (str_starts_with($task->flow_item_name, 'end')) {
if (preg_match('/已取消|Cancelled|취소됨|キャンセル済み|Abgebrochen|Annulé|Dibatalkan|Отменено/', $task->flow_item_name)) {
$statusText = '已完成';
if (ProjectTask::isCanceledFlowName($task->flow_item_name)) {
$statusText = '已取消';
$actualTime = 0;
$testTime = 0;
$developTime = 0;
$overTime = '-';
} elseif (str_contains($task->flow_item_name, '已完成')) {
$statusText = '已完成';
}
} elseif ($task->complete_at) {
$statusText = '已完成';
@@ -1426,15 +1463,15 @@ class ProjectController extends AbstractController
$datas[$task->ownerid] = [
'index' => 1,
'nickname' => Base::filterEmoji(User::userid2nickname($task->ownerid)),
'styles' => ["A1:P1" => ["font" => ["bold" => true]]],
'styles' => ["A1:Q1" => ["font" => ["bold" => true]]],
'data' => [],
];
}
$datas[$task->ownerid]['index']++;
if ($statusText === '未完成') {
$datas[$task->ownerid]['styles']["P{$datas[$task->ownerid]['index']}"] = ["font" => ["color" => ["rgb" => "ff0000"]]]; // 未完成
$datas[$task->ownerid]['styles']["Q{$datas[$task->ownerid]['index']}"] = ["font" => ["color" => ["rgb" => "ff0000"]]]; // 未完成
} elseif ($statusText === '已完成' && $task->end_at && Carbon::parse($task->complete_at)->gt($task->end_at)) {
$datas[$task->ownerid]['styles']["P{$datas[$task->ownerid]['index']}"] = ["font" => ["color" => ["rgb" => "436FF6"]]]; // 已完成超期
$datas[$task->ownerid]['styles']["Q{$datas[$task->ownerid]['index']}"] = ["font" => ["color" => ["rgb" => "436FF6"]]]; // 已完成超期
}
$datas[$task->ownerid]['data'][] = [
$task->id,
@@ -1476,7 +1513,7 @@ class ProjectController extends AbstractController
foreach ($userid as $ownerid) {
$data = $datas[$ownerid] ?? [
'nickname' => Base::filterEmoji(User::userid2nickname($ownerid)),
'styles' => ["A1:P1" => ["font" => ["bold" => true]]],
'styles' => ["A1:Q1" => ["font" => ["bold" => true]]],
'data' => [],
];
$title = (count($sheets) + 1) . "." . ($data['nickname'] ?: $ownerid);
@@ -1551,7 +1588,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/exportoverdue 23. 导出超期任务(限管理员)
* @api {get} api/project/task/exportoverdue 导出超期任务(限管理员)
*
* @apiDescription 导出指定范围任务已完成、未完成、已归档返回下载地址需要token身份
* @apiVersion 1.0.0
@@ -1719,7 +1756,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/down 24. 下载导出的任务
* @api {get} api/project/task/down 下载导出的任务
*
* @apiVersion 1.0.0
* @apiGroup project
@@ -1740,7 +1777,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/one 25. 获取单个任务信息
* @api {get} api/project/task/one 获取单个任务信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1786,7 +1823,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/subdata 25. 获取子任务数据
* @api {get} api/project/task/subdata 获取子任务数据
*
* @apiDescription 需要token身份相对one接口这个只获取主任务的子任务数据
* @apiVersion 1.0.0
@@ -1818,7 +1855,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/related 26. 获取任务关联任务列表
* @api {get} api/project/task/related 获取任务关联任务列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1924,7 +1961,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/content 27. 获取任务详细描述
* @api {get} api/project/task/content 获取任务详细描述
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1963,7 +2000,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/content_history 28. 获取任务详细历史描述
* @api {get} api/project/task/content_history 获取任务详细历史描述
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1995,7 +2032,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/files 29. 获取任务文件列表
* @api {get} api/project/task/files 获取任务文件列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2020,7 +2057,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/filedelete 30. 删除任务文件
* @api {get} api/project/task/filedelete 删除任务文件
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2064,7 +2101,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/filedetail 31. 获取任务文件详情
* @api {get} api/project/task/filedetail 获取任务文件详情
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2117,7 +2154,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/filedown 32. 下载任务文件
* @api {get} api/project/task/filedown 下载任务文件
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2162,7 +2199,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/task/add 33. 添加任务
* @api {post} api/project/task/add 添加任务
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2248,7 +2285,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/addsub 34. 添加子任务
* @api {get} api/project/task/addsub 添加子任务
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2295,7 +2332,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/upgrade 36. 子任务升级为主任务
* @api {get} api/project/task/upgrade 子任务升级为主任务
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2342,7 +2379,7 @@ class ProjectController extends AbstractController
$task->save();
ProjectTaskUser::whereTaskId($task->id)->update(['task_pid' => $task->id]);
if ($task->visibility == 3 && !empty($visibilityUserids)) {
ProjectTaskVisibilityUser::whereTaskId($task->id)->delete();
ProjectTaskVisibilityUser::whereTaskId($task->id)->remove();
foreach (array_unique($visibilityUserids) as $userid) {
if (!$userid) {
continue;
@@ -2420,7 +2457,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/task/update 35. 修改任务、子任务
* @api {post} api/project/task/update 修改任务、子任务
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2533,7 +2570,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/dialog 36. 创建/获取聊天室
* @api {get} api/project/task/dialog 创建/获取聊天室
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2582,7 +2619,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/archived 37. 归档任务
* @api {get} api/project/task/archived 归档任务
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2627,7 +2664,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/remove 38. 删除任务
* @api {get} api/project/task/remove 删除任务
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2665,7 +2702,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/resetfromlog 39. 根据日志重置任务
* @api {get} api/project/task/resetfromlog 根据日志重置任务
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2724,7 +2761,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/flow 40. 任务工作流信息
* @api {get} api/project/task/flow 任务工作流信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2813,7 +2850,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/move 41. 任务移动
* @api {get} api/project/task/move 任务移动
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2899,7 +2936,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/task/copy 42. 复制任务
* @api {post} api/project/task/copy 复制任务
*
* @apiDescription 需要token身份项目、任务负责人
* @apiVersion 1.0.0
@@ -2994,7 +3031,7 @@ class ProjectController extends AbstractController
$taskTag->project_id = $project->id;
$taskTag->save();
}
ProjectTaskUser::whereTaskId($copy->id)->delete();
ProjectTaskUser::whereTaskId($copy->id)->remove();
$copy->setRelation('taskUser', collect());
$copy->setRelation('project', $project);
$updateData = [
@@ -3014,6 +3051,11 @@ class ProjectController extends AbstractController
$copy->addLog('复制{任务}', [
'copy_from' => $task->id,
]);
// 复制子任务
$task->copySubTasks($copy, [
'reset_complete' => true,
'update_project' => true,
]);
return $copy;
});
//
@@ -3025,116 +3067,27 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/task/ai_generate 43. 使用 AI 助手生成任务
* 使用 AI 助手生成任务
*
* @apiDescription 需要token身份使用AI根据用户输入和上下文信息生成任务标题和详细描述
* @apiVersion 1.0.0
* @apiGroup project
* @apiName task__ai_generate
*
* @apiParam {String} content 用户输入的任务描述(必填)
* @apiParam {String} [current_title] 当前已有的任务标题(用于优化改进)
* @apiParam {String} [current_content] 当前已有的任务内容HTML格式用于优化改进
* @apiParam {String} [template_name] 选中的任务模板名称
* @apiParam {String} [template_content] 选中的任务模板内容HTML格式
* @apiParam {Boolean} [has_owner] 是否已设置负责人
* @apiParam {Boolean} [has_time_plan] 是否已设置计划时间
* @apiParam {String} [priority_level] 任务优先级等级名称
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @apiSuccess {String} data.title AI 生成的任务标题
* @apiSuccess {String} data.content AI 生成的任务内容HTML 格式)
* @apiSuccess {Array} data.subtasks 当任务较复杂时生成的子任务名称列表
* @deprecated 已废弃方法,仅保留路由占位,后续版本中移除
*/
public function task__ai_generate()
{
User::auth();
// 获取用户输入的任务描述
$content = Request::input('content');
if (empty($content)) {
return Base::retError('任务描述不能为空');
}
// 获取上下文信息
$context = [
'current_title' => Request::input('current_title', ''),
'current_content' => Request::input('current_content', ''),
'template_name' => Request::input('template_name', ''),
'template_content' => Request::input('template_content', ''),
'has_owner' => boolval(Request::input('has_owner', false)),
'has_time_plan' => boolval(Request::input('has_time_plan', false)),
'priority_level' => Request::input('priority_level', ''),
];
// 如果当前内容是HTML格式转换为markdown
if (!empty($context['current_content'])) {
$context['current_content'] = Base::html2markdown($context['current_content']);
}
if (!empty($context['template_content'])) {
$context['template_content'] = Base::html2markdown($context['template_content']);
}
$result = AI::generateTask($content, $context);
if (Base::isError($result)) {
return Base::retError('生成任务失败', $result);
}
return Base::retSuccess('生成任务成功', $result['data']);
Base::checkClientVersion('1.4.35');
}
/**
* @api {post} api/project/ai/generate 44. 使用 AI 助手生成项目
* 使用 AI 助手生成项目
*
* @apiDescription 需要token身份根据需求说明自动生成项目名称及任务列表
* @apiVersion 1.0.0
* @apiGroup project
* @apiName ai__generate
*
* @apiParam {String} content 项目需求或背景描述(必填)
* @apiParam {String} [current_name] 当前草拟的项目名称
* @apiParam {Array|String} [current_columns] 已有任务列表(数组或以逗号/换行分隔的字符串)
* @apiParam {Array} [template_examples] 可参考的模板示例,格式:[ {name: 模板名, columns: [列表...] }, ... ]
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @apiSuccess {String} data.name AI 生成的项目名称
* @apiSuccess {Array} data.columns AI 生成的任务列表名称数组
* @deprecated 已废弃方法,仅保留路由占位,后续版本中移除
*/
public function ai__generate()
{
User::auth();
$content = trim((string)Request::input('content', ''));
if ($content === '') {
return Base::retError('项目需求描述不能为空');
}
$templateExamples = Request::input('template_examples', []);
if (!is_array($templateExamples)) {
$templateExamples = [];
} else {
$templateExamples = array_slice($templateExamples, 0, 6);
}
$context = [
'current_name' => Request::input('current_name', ''),
'current_columns' => Request::input('current_columns', []),
'template_examples' => $templateExamples,
];
$result = AI::generateProject($content, $context);
if (Base::isError($result)) {
return Base::retError('生成项目失败', $result);
}
return Base::retSuccess('生成项目成功', $result['data']);
Base::checkClientVersion('1.4.35');
}
/**
* @api {get} api/project/flow/list 45. 工作流列表
* @api {get} api/project/flow/list 工作流列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3160,7 +3113,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/flow/save 46. 保存工作流
* @api {post} api/project/flow/save 保存工作流
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -3194,7 +3147,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/flow/delete 47. 删除工作流
* @api {get} api/project/flow/delete 删除工作流
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -3226,7 +3179,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/log/lists 48. 获取项目、任务日志
* @api {get} api/project/log/lists 获取项目、任务日志
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3294,7 +3247,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/top 49. 项目置顶
* @api {get} api/project/top 项目置顶
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3328,7 +3281,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/permission 50. 获取项目权限设置
* @api {get} api/project/permission 获取项目权限设置
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3354,7 +3307,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/permission/update 51. 项目权限设置
* @api {get} api/project/permission/update 项目权限设置
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3399,7 +3352,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/template_list 52. 任务模板列表
* @api {get} api/project/task/template_list 任务模板列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3428,7 +3381,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/task/template_save 53. 保存任务模板
* @api {post} api/project/task/template_save 保存任务模板
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -3494,7 +3447,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/task/template_sort 54. 排序任务模板
* @api {post} api/project/task/template_sort 排序任务模板
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -3546,7 +3499,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/template_delete 55. 删除任务模板
* @api {get} api/project/task/template_delete 删除任务模板
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -3577,7 +3530,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/task/template_default 56. 设置(取消)任务模板为默认
* @api {get} api/project/task/template_default 设置(取消)任务模板为默认
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -3619,7 +3572,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/tag/save 57. 保存标签
* @api {post} api/project/tag/save 保存标签
*
* @apiDescription 需要token身份修改项目负责人、标签创建者添加项目所有成员
* @apiVersion 1.0.0
@@ -3732,7 +3685,7 @@ class ProjectController extends AbstractController
}
/**
* @api {post} api/project/tag/sort 58. 标签排序
* @api {post} api/project/tag/sort 标签排序
*
* @apiDescription 需要token身份项目负责人
* @apiVersion 1.0.0
@@ -3784,7 +3737,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/tag/delete 59. 删除标签
* @api {get} api/project/tag/delete 删除标签
*
* @apiDescription 需要token身份项目负责人、标签创建者
* @apiVersion 1.0.0
@@ -3841,7 +3794,7 @@ class ProjectController extends AbstractController
}
/**
* @api {get} api/project/tag/list 60. 标签列表
* @api {get} api/project/tag/list 标签列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0

View File

@@ -6,6 +6,7 @@ use App\Exceptions\ApiException;
use App\Models\AbstractModel;
use App\Models\ProjectTask;
use App\Models\Report;
use App\Models\ReportAnalysis;
use App\Models\ReportLink;
use App\Models\ReportReceive;
use App\Models\User;
@@ -28,7 +29,7 @@ use Illuminate\Support\Facades\Validator;
class ReportController extends AbstractController
{
/**
* @api {get} api/report/my 01. 我发送的汇报
* @api {get} api/report/my 我发送的汇报
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -50,7 +51,9 @@ class ReportController extends AbstractController
{
$user = User::auth();
//
$builder = Report::with(['receivesUser'])->whereUserid($user->userid);
$builder = Report::with(['receivesUser'])
->select(Report::LIST_FIELDS)
->whereUserid($user->userid);
$keys = Request::input('keys');
if (is_array($keys)) {
if ($keys['key']) {
@@ -58,6 +61,11 @@ class ReportController extends AbstractController
$builder->whereHas('sendUser', function ($q2) use ($keys) {
$q2->where("users.email", "LIKE", "%{$keys['key']}%");
});
} elseif (Base::isNumber($keys['key'])) {
$builder->where(function ($query) use ($keys) {
$query->where("id", intval($keys['key']))
->orWhere("title", "LIKE", "%{$keys['key']}%");
});
} else {
$builder->where("title", "LIKE", "%{$keys['key']}%");
}
@@ -75,7 +83,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/receive 02. 我接收的汇报
* @api {get} api/report/receive 我接收的汇报
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -98,7 +106,8 @@ class ReportController extends AbstractController
public function receive(): array
{
$user = User::auth();
$builder = Report::with(['receivesUser']);
$builder = Report::with(['receivesUser'])
->select(Report::LIST_FIELDS);
$builder->whereHas("receivesUser", function ($query) use ($user) {
$query->where("report_receives.userid", $user->userid);
});
@@ -110,7 +119,11 @@ class ReportController extends AbstractController
$q2->where("users.email", "LIKE", "%{$keys['key']}%");
});
} elseif (Base::isNumber($keys['key'])) {
$builder->where("userid", intval($keys['key']));
$builder->where(function ($query) use ($keys) {
$query->where("userid", intval($keys['key']))
->orWhere("id", intval($keys['key']))
->orWhere("title", "LIKE", "%{$keys['key']}%");
});
} else {
$builder->where("title", "LIKE", "%{$keys['key']}%");
}
@@ -143,7 +156,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/store 03. 保存并发送工作汇报
* @api {get} api/report/store 保存并发送工作汇报
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -282,7 +295,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/template 04. 生成汇报模板
* @api {get} api/report/template 生成汇报模板
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -326,6 +339,13 @@ class ReportController extends AbstractController
$start_time->startOfWeek();
$end_time = Carbon::instance($start_time)->endOfWeek();
}
// 周报时预计算下一周期时间范围(下周)
$next_start_time = null;
$next_end_time = null;
if ($type === Report::WEEKLY) {
$next_start_time = Carbon::instance($start_time)->copy()->addWeek();
$next_end_time = Carbon::instance($end_time)->copy()->addWeek();
}
// 生成唯一标识
$sign = Report::generateSign($type, 0, Carbon::instance($start_time));
@@ -361,6 +381,10 @@ class ReportController extends AbstractController
->get();
if ($complete_task->isNotEmpty()) {
foreach ($complete_task as $task) {
// 排除取消态任务:不将已取消任务计入“已完成工作”
if (ProjectTask::isCanceledFlowName($task->flow_item_name)) {
continue;
}
$complete_at = Carbon::parse($task->complete_at);
$remark = $type == Report::WEEKLY ? ('<div style="text-align:center">[' . Doo::translate('周' . ['日', '一', '二', '三', '四', '五', '六'][$complete_at->dayOfWeek]) . ']</div>') : '&nbsp;';
$completeDatas[] = [
@@ -376,18 +400,7 @@ class ReportController extends AbstractController
// 未完成的任务
$unfinishedDatas = [];
$unfinished_task = ProjectTask::query()
->join("projects", "projects.id", "=", "project_tasks.project_id")
->whereNull("projects.archived_at")
->whereNull("project_tasks.complete_at")
->whereNotNull("project_tasks.start_at")
->where("project_tasks.end_at", "<", $end_time->toDateTimeString())
->whereHas("taskUser", function ($query) use ($user) {
$query->where("userid", $user->userid);
})
->select("project_tasks.*")
->orderByDesc("project_tasks.id")
->get();
$unfinished_task = ProjectTask::buildUnfinishedTaskQuery($user->userid, $start_time, $end_time, true)->get();
if ($unfinished_task->isNotEmpty()) {
foreach ($unfinished_task as $task) {
empty($task->end_at) || $end_at = Carbon::parse($task->end_at);
@@ -407,8 +420,10 @@ class ReportController extends AbstractController
if ($type === Report::WEEKLY) {
$title = $user->nickname . "的周报[" . $start_time->format("m/d") . "-" . $end_time->format("m/d") . "]";
$title .= "[" . $start_time->month . "月第" . $start_time->weekOfMonth . "周]";
$unfinishedTitle = '本周未完成的工作';
} else {
$title = $user->nickname . "的日报[" . $start_time->format("Y/m/d") . "]";
$unfinishedTitle = '今日未完成的工作';
}
$title = Doo::translate($title);
@@ -421,22 +436,44 @@ class ReportController extends AbstractController
])->render();
$contents[] = '<p>&nbsp;</p>';
$contents[] = '<h2>' . Doo::translate('未完成的工作') . '</h2>';
$contents[] = '<h2>' . Doo::translate($unfinishedTitle) . '</h2>';
$contents[] = view('report', [
'labels' => $labels,
'datas' => $unfinishedDatas,
])->render();
if ($type === Report::WEEKLY) {
// 下周拟定计划:基于下周时间范围预生成候选任务
$nextPlanDatas = [];
if ($next_start_time && $next_end_time) {
$next_tasks = ProjectTask::buildUnfinishedTaskQuery($user->userid, $next_start_time, $next_end_time, false)->get();
if ($next_tasks->isNotEmpty()) {
foreach ($next_tasks as $task) {
$planTime = '-';
if ($task->start_at || $task->end_at) {
$startText = $task->start_at ? Carbon::parse($task->start_at)->format('Y-m-d H:i') : '';
$endText = $task->end_at ? Carbon::parse($task->end_at)->format('Y-m-d H:i') : '';
$planTime = trim($startText . ($endText ? (' ~ ' . $endText) : ''));
}
$nextPlanDatas[] = [
'[' . $task->project->name . '] ' . $task->name,
$planTime,
$task->taskUser->where("owner", 1)->map(function ($item) {
return User::userid2nickname($item->userid);
})->implode(", "),
];
}
}
}
$contents[] = '<p>&nbsp;</p>';
$contents[] = "<h2>" . Doo::translate("下周拟定计划") . "[" . $start_time->addWeek()->format("m/d") . "-" . $end_time->addWeek()->format("m/d") . "]</h2>";
$contents[] = "<h2>" . Doo::translate("下周拟定计划") . "[" . $next_start_time->format("m/d") . "-" . $next_end_time->format("m/d") . "]</h2>";
$contents[] = view('report', [
'labels' => [
Doo::translate('计划描述'),
Doo::translate('计划时间'),
Doo::translate('负责人'),
],
'datas' => [],
'datas' => $nextPlanDatas,
])->render();
}
@@ -454,7 +491,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/detail 05. 报告详情
* @api {get} api/report/detail 报告详情
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -501,11 +538,113 @@ class ReportController extends AbstractController
$one->report_link = $link;
$link->increment("num");
}
$analysis = ReportAnalysis::query()
->whereRid($one->id)
->whereUserid($user->userid)
->first();
if ($analysis) {
$updatedAt = $analysis->updated_at ? $analysis->updated_at->toDateTimeString() : null;
$one->setAttribute('ai_analysis', [
'id' => $analysis->id,
'text' => $analysis->analysis_text,
'model' => $analysis->model,
'updated_at' => $updatedAt,
]);
} else {
$one->setAttribute('ai_analysis', null);
}
return Base::retSuccess("success", $one);
}
/**
* @api {get} api/report/mark 06. 标记已读/未读
* @api {post} api/report/analysave 保存工作汇报 AI 分析
*
* @apiDescription 需要token身份仅支持报告提交人或接收人保存分析
* @apiVersion 1.0.0
* @apiGroup report
* @apiName analysave
*
* @apiParam {Number} id 报告ID
* @apiParam {String} text 分析内容Markdown
* @apiParam {String} [model] 分析使用的模型标识(可选)
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @apiSuccess {Number} data.id 分析记录ID
* @apiSuccess {String} data.text 分析内容Markdown
* @apiSuccess {String} data.updated_at 最近更新时间
*/
public function analysave(): array
{
$user = User::auth();
$id = intval(Request::input("id"));
if ($id <= 0) {
return Base::retError("缺少ID参数");
}
$text = trim((string)Request::input('text', ''));
if ($text === '') {
return Base::retError("分析内容不能为空");
}
$model = trim((string)Request::input('model', ''));
$report = Report::getOne($id);
if (!$this->userCanAccessReport($report, $user)) {
return Base::retError("无权访问该工作汇报");
}
$analysis = ReportAnalysis::query()
->whereRid($report->id)
->whereUserid($user->userid)
->first();
if (!$analysis) {
$analysis = ReportAnalysis::fillInstance([
'rid' => $report->id,
'userid' => $user->userid,
]);
}
$viewerRole = $user->profession ?: (is_array($user->identity) && !empty($user->identity) ? implode('/', $user->identity) : null);
$focusMeta = null;
$focus = Request::input('focus');
if (is_array($focus)) {
$focusMeta = array_filter(array_map('trim', $focus));
} elseif (is_string($focus) && trim($focus) !== '') {
$focusMeta = [trim($focus)];
}
$meta = array_filter([
'viewer_role' => $viewerRole,
'viewer_name' => $user->nickname ?? null,
'focus' => $focusMeta,
], function ($value) {
if (is_array($value)) {
return !empty($value);
}
return $value !== null && $value !== '';
});
$analysis->updateInstance([
'model' => $model,
'analysis_text' => $text,
'meta' => $meta,
]);
$analysis->save();
$analysis->refresh();
return Base::retSuccess("success", [
'id' => $analysis->id,
'text' => $analysis->analysis_text,
'model' => $analysis->model,
'updated_at' => $analysis->updated_at ? $analysis->updated_at->toDateTimeString() : null,
]);
}
/**
* @api {get} api/report/mark 标记已读/未读
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -548,7 +687,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/share 07. 分享报告到消息
* @api {get} api/report/share 分享报告到消息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -610,7 +749,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/last_submitter 08. 获取最后一次提交的接收人
* @api {get} api/report/last_submitter 获取最后一次提交的接收人
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -628,7 +767,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/unread 09. 获取未读
* @api {get} api/report/unread 获取未读
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -653,7 +792,7 @@ class ReportController extends AbstractController
}
/**
* @api {get} api/report/read 10. 标记汇报已读,可批量
* @api {get} api/report/read 标记汇报已读,可批量
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -691,4 +830,22 @@ class ReportController extends AbstractController
}
return Base::retSuccess("success", $data);
}
/**
* 判断当前用户是否有权限查看/分析指定工作汇报
* @param Report $report
* @param User $user
* @return bool
*/
protected function userCanAccessReport(Report $report, User $user): bool
{
if ($report->userid === $user->userid) {
return true;
}
return ReportReceive::query()
->whereRid($report->id)
->whereUserid($user->userid)
->exists();
}
}

View File

@@ -0,0 +1,619 @@
<?php
namespace App\Http\Controllers\Api;
use Request;
use App\Models\File;
use App\Models\Project;
use App\Models\ProjectTask;
use App\Models\User;
use App\Models\UserTag;
use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg;
use App\Module\Base;
use App\Module\Apps;
use App\Module\Manticore\ManticoreFile;
use App\Module\Manticore\ManticoreUser;
use App\Module\Manticore\ManticoreProject;
use App\Module\Manticore\ManticoreTask;
use App\Module\Manticore\ManticoreMsg;
/**
* @apiDefine search
*
* 智能搜索
*/
class SearchController extends AbstractController
{
/**
* @api {get} api/search/contact 搜索联系人
*
* @apiDescription 需要token身份优先使用 Manticore Search未安装则使用 MySQL 搜索
* @apiVersion 1.0.0
* @apiGroup search
* @apiName contact
*
* @apiParam {String} key 搜索关键词
* @apiParam {String} [search_type] 搜索类型text/vector/hybrid默认hybrid仅 Manticore 有效)
* @apiParam {Number} [take] 获取数量默认20最大50
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function contact()
{
User::auth();
$key = trim(Request::input('key'));
$searchType = Request::input('search_type', 'hybrid');
$take = Base::getPaginate(50, 20, 'take');
if (empty($key)) {
return Base::retSuccess('success', []);
}
// 优先使用 Manticore 搜索
if (Apps::isInstalled('search')) {
$results = ManticoreUser::search($key, $searchType, $take);
// 补充用户完整信息
$userids = array_column($results, 'userid');
if (!empty($userids)) {
$users = User::whereIn('userid', $userids)
->select(User::$basicField)
->get()
->keyBy('userid');
foreach ($results as &$item) {
$userData = $users->get($item['userid']);
if ($userData) {
// 标签直接从 Manticore 搜索结果获取(空格分隔的字符串转数组)
$tagsStr = $item['tags'] ?? '';
$searchTags = !empty($tagsStr) ? preg_split('/\s+/', trim($tagsStr)) : [];
$item = array_merge($userData->toArray(), [
'relevance' => $item['relevance'] ?? 0,
'introduction_preview' => $item['introduction_preview'] ?? null,
'search_tags' => $searchTags,
]);
}
}
}
} else {
// MySQL 回退搜索
$results = $this->searchContactByMysql($key, $take);
}
return Base::retSuccess('success', $results);
}
/**
* MySQL 回退搜索联系人
*
* @param string $key 搜索关键词
* @param int $take 获取数量
* @return array
*/
private function searchContactByMysql(string $key, int $take): array
{
$users = User::select(User::$basicField)
->where('bot', 0)
->whereNull('disable_at')
->searchByKeyword($key)
->orderByDesc('line_at')
->take($take)
->get();
// 获取用户标签
$userids = $users->pluck('userid')->toArray();
$userTags = $this->getUserTagsMap($userids);
return $users->map(function ($user) use ($userTags) {
return array_merge($user->toArray(), [
'relevance' => 0,
'introduction_preview' => null,
'search_tags' => $userTags[$user->userid] ?? [],
]);
})->toArray();
}
/**
* @api {get} api/search/project 搜索项目
*
* @apiDescription 需要token身份优先使用 Manticore Search未安装则使用 MySQL 搜索
* @apiVersion 1.0.0
* @apiGroup search
* @apiName project
*
* @apiParam {String} key 搜索关键词
* @apiParam {String} [search_type] 搜索类型text/vector/hybrid默认hybrid仅 Manticore 有效)
* @apiParam {Number} [take] 获取数量默认20最大50
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function project()
{
$user = User::auth();
$key = trim(Request::input('key'));
$searchType = Request::input('search_type', 'hybrid');
$take = Base::getPaginate(50, 20, 'take');
if (empty($key)) {
return Base::retSuccess('success', []);
}
// 优先使用 Manticore 搜索
if (Apps::isInstalled('search')) {
$results = ManticoreProject::search($user->userid, $key, $searchType, $take);
// 补充项目完整信息
$projectIds = array_column($results, 'project_id');
if (!empty($projectIds)) {
$projects = Project::whereIn('id', $projectIds)
->get()
->keyBy('id');
foreach ($results as &$item) {
$projectData = $projects->get($item['project_id']);
if ($projectData) {
$item = array_merge($projectData->toArray(), [
'relevance' => $item['relevance'] ?? 0,
'desc_preview' => $item['desc_preview'] ?? null,
]);
}
}
}
} else {
// MySQL 回退搜索
$results = $this->searchProjectByMysql($user->userid, $key, $take);
}
return Base::retSuccess('success', $results);
}
/**
* MySQL 回退搜索项目
*
* @param int $userid 用户ID
* @param string $key 搜索关键词
* @param int $take 获取数量
* @return array
*/
private function searchProjectByMysql(int $userid, string $key, int $take): array
{
$projects = Project::authData()
->whereNull('projects.archived_at')
->searchByKeyword($key)
->orderByDesc('projects.id')
->take($take)
->get();
return $projects->map(function ($project) {
$array = $project->toArray();
$array['relevance'] = 0;
$array['desc_preview'] = null;
return $array;
})->toArray();
}
/**
* @api {get} api/search/task 搜索任务
*
* @apiDescription 需要token身份优先使用 Manticore Search未安装则使用 MySQL 搜索
* @apiVersion 1.0.0
* @apiGroup search
* @apiName task
*
* @apiParam {String} key 搜索关键词
* @apiParam {String} [search_type] 搜索类型text/vector/hybrid默认hybrid仅 Manticore 有效)
* @apiParam {Number} [take] 获取数量默认20最大50
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function task()
{
$user = User::auth();
$key = trim(Request::input('key'));
$searchType = Request::input('search_type', 'hybrid');
$take = Base::getPaginate(50, 20, 'take');
if (empty($key)) {
return Base::retSuccess('success', []);
}
// 优先使用 Manticore 搜索
if (Apps::isInstalled('search')) {
$results = ManticoreTask::search($user->userid, $key, $searchType, $take);
// 补充任务完整信息
$taskIds = array_column($results, 'task_id');
if (!empty($taskIds)) {
$tasks = ProjectTask::with(['taskUser', 'taskTag'])
->whereIn('id', $taskIds)
->get()
->keyBy('id');
foreach ($results as &$item) {
$taskData = $tasks->get($item['task_id']);
if ($taskData) {
$item = array_merge($taskData->toArray(), [
'relevance' => $item['relevance'] ?? 0,
'desc_preview' => $item['desc_preview'] ?? null,
'content_preview' => $item['content_preview'] ?? null,
]);
}
}
}
} else {
// MySQL 回退搜索
$results = $this->searchTaskByMysql($user->userid, $key, $take);
}
return Base::retSuccess('success', $results);
}
/**
* MySQL 回退搜索任务
*
* @param int $userid 用户ID
* @param string $key 搜索关键词
* @param int $take 获取数量
* @return array
*/
private function searchTaskByMysql(int $userid, string $key, int $take): array
{
$tasks = ProjectTask::with(['taskUser', 'taskTag'])
->whereIn('project_tasks.project_id', function ($query) use ($userid) {
$query->select('project_id')
->from('project_users')
->where('userid', $userid);
})
->whereNull('project_tasks.archived_at')
->whereNull('project_tasks.deleted_at')
->searchByKeyword($key)
->orderByDesc('project_tasks.id')
->take($take)
->get();
return $tasks->map(function ($task) {
$array = $task->toArray();
$array['relevance'] = 0;
$array['desc_preview'] = null;
$array['content_preview'] = null;
return $array;
})->toArray();
}
/**
* @api {get} api/search/file 搜索文件
*
* @apiDescription 需要token身份优先使用 Manticore Search未安装则使用 MySQL 搜索
* @apiVersion 1.0.0
* @apiGroup search
* @apiName file
*
* @apiParam {String} key 搜索关键词
* @apiParam {String} [search_type] 搜索类型text/vector/hybrid默认hybrid仅 Manticore 有效)
* @apiParam {Number} [take] 获取数量默认20最大50
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function file()
{
$user = User::auth();
$key = trim(Request::input('key'));
$searchType = Request::input('search_type', 'hybrid');
$take = Base::getPaginate(50, 20, 'take');
if (empty($key)) {
return Base::retSuccess('success', []);
}
// 优先使用 Manticore 搜索
if (Apps::isInstalled('search')) {
$results = ManticoreFile::search($user->userid, $key, $searchType, 0, $take);
// 补充文件完整信息
$fileIds = array_column($results, 'file_id');
if (!empty($fileIds)) {
$files = File::whereIn('id', $fileIds)
->get()
->keyBy('id');
$formattedResults = [];
foreach ($results as $item) {
$fileData = $files->get($item['file_id']);
if ($fileData) {
$formattedResults[] = array_merge($fileData->toArray(), [
'relevance' => $item['relevance'] ?? 0,
'content_preview' => $item['content_preview'] ?? null,
]);
}
}
return Base::retSuccess('success', $formattedResults);
}
return Base::retSuccess('success', []);
} else {
// MySQL 回退搜索
$results = $this->searchFileByMysql($user->userid, $key, $take);
return Base::retSuccess('success', $results);
}
}
/**
* MySQL 回退搜索文件
*
* @param int $userid 用户ID
* @param string $key 搜索关键词
* @param int $take 获取数量
* @return array
*/
private function searchFileByMysql(int $userid, string $key, int $take): array
{
$results = [];
// 搜索用户自己的文件
$ownFiles = File::where('userid', $userid)
->searchByKeyword($key)
->take($take)
->get();
foreach ($ownFiles as $file) {
$results[] = array_merge($file->toArray(), [
'relevance' => 0,
'content_preview' => null,
]);
}
// 搜索共享给用户的文件
$remaining = $take - count($results);
if ($remaining > 0) {
$sharedFiles = File::sharedToUser($userid)
->searchByKeyword($key)
->take($remaining)
->get();
foreach ($sharedFiles as $file) {
$temp = $file->toArray();
if ($file->pshare === $file->id) {
$temp['pid'] = 0;
}
$temp['relevance'] = 0;
$temp['content_preview'] = null;
$results[] = $temp;
}
}
return $results;
}
/**
* @api {get} api/search/message 搜索消息
*
* @apiDescription 需要token身份优先使用 Manticore Search未安装则使用 MySQL 搜索
* @apiVersion 1.0.0
* @apiGroup search
* @apiName message
*
* @apiParam {String} key 搜索关键词
* @apiParam {String} [search_type] 搜索类型text/vector/hybrid默认hybrid仅 Manticore 有效)
* @apiParam {Number} [take] 获取数量默认20最大50
* @apiParam {String} [mode] 返回模式message/position/dialog默认message
* - message: 返回消息详细信息
* - position: 只返回消息ID
* - dialog: 返回对话级数据
* @apiParam {Number} [dialog_id] 对话ID筛选指定对话内的消息
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function message()
{
$user = User::auth();
$key = trim(Request::input('key'));
$searchType = Request::input('search_type', 'hybrid');
$take = Base::getPaginate(50, 20, 'take');
$mode = Request::input('mode', 'message');
$dialogId = intval(Request::input('dialog_id', 0));
// 验证 mode 参数
if (!in_array($mode, ['message', 'position', 'dialog'])) {
$mode = 'message';
}
if (empty($key)) {
return Base::retSuccess('success', []);
}
// 如果指定了 dialog_id需要验证用户有权限访问该对话
if ($dialogId > 0) {
WebSocketDialog::checkDialog($dialogId);
}
// 优先使用 Manticore 搜索
if (Apps::isInstalled('search')) {
$results = ManticoreMsg::search($user->userid, $key, $searchType, 0, $take, $dialogId);
} else {
// MySQL 回退搜索
$results = $this->searchMessageByMysql($user->userid, $key, $take, $dialogId);
}
// 根据 mode 返回不同格式的数据
return $this->formatMessageResults($results, $mode, $user->userid);
}
/**
* MySQL 回退搜索消息
*
* @param int $userid 用户ID
* @param string $key 搜索关键词
* @param int $take 获取数量
* @param int $dialogId 对话ID0表示不限制
* @return array
*/
private function searchMessageByMysql(int $userid, string $key, int $take, int $dialogId = 0): array
{
$builder = WebSocketDialogMsg::select([
'id as msg_id',
'dialog_id',
'userid',
'type',
'msg',
'created_at',
])
->accessibleByUser($userid)
->where('bot', 0)
->searchByKeyword($key);
if ($dialogId > 0) {
$builder->where('dialog_id', $dialogId);
}
$items = $builder->orderByDesc('id')
->limit($take)
->get();
return $items->map(function ($item) {
return [
'msg_id' => $item->msg_id,
'dialog_id' => $item->dialog_id,
'userid' => $item->userid,
'type' => $item->type,
'msg' => $item->msg,
'created_at' => $item->created_at,
'relevance' => 0,
'content_preview' => null,
];
})->toArray();
}
/**
* 格式化消息搜索结果
*
* @param array $results 搜索结果
* @param string $mode 返回模式
* @param int $userid 用户ID
* @return \Illuminate\Http\JsonResponse
*/
private function formatMessageResults(array $results, string $mode, int $userid)
{
switch ($mode) {
case 'position':
// 只返回消息ID
$data = array_column($results, 'msg_id');
return Base::retSuccess('success', compact('data'));
case 'dialog':
// 返回对话级数据
$list = [];
$seenDialogs = [];
foreach ($results as $item) {
$dialogIdFromResult = $item['dialog_id'];
// 每个对话只返回一次
if (isset($seenDialogs[$dialogIdFromResult])) {
continue;
}
$seenDialogs[$dialogIdFromResult] = true;
if ($dialog = WebSocketDialog::find($dialogIdFromResult)) {
$dialogData = array_merge($dialog->toArray(), [
'search_msg_id' => $item['msg_id'],
]);
$list[] = WebSocketDialog::synthesizeData($dialogData, $userid);
}
}
return Base::retSuccess('success', ['data' => $list]);
case 'message':
default:
// 返回消息详细信息(默认行为)
$msgIds = array_column($results, 'msg_id');
if (!empty($msgIds)) {
$msgs = WebSocketDialogMsg::whereIn('id', $msgIds)
->with(['user' => function ($query) {
$query->select(User::$basicField);
}])
->get()
->keyBy('id');
// 创建结果映射以保持原始顺序和额外字段
$resultsMap = [];
foreach ($results as $item) {
$resultsMap[$item['msg_id']] = $item;
}
$formattedResults = [];
foreach ($msgIds as $msgId) {
$msgData = $msgs->get($msgId);
$originalItem = $resultsMap[$msgId] ?? [];
if ($msgData) {
$formattedResults[] = [
'id' => $msgData->id,
'msg_id' => $msgData->id,
'dialog_id' => $msgData->dialog_id,
'userid' => $msgData->userid,
'type' => $msgData->type,
'msg' => $msgData->msg,
'created_at' => $msgData->created_at,
'user' => $msgData->user,
'relevance' => $originalItem['relevance'] ?? 0,
'content_preview' => $originalItem['content_preview'] ?? null,
];
}
}
return Base::retSuccess('success', $formattedResults);
}
return Base::retSuccess('success', []);
}
}
/**
* 批量获取用户标签映射
*
* @param array $userids 用户ID数组
* @return array 用户ID => 标签名称数组的映射
*/
private function getUserTagsMap(array $userids): array
{
if (empty($userids)) {
return [];
}
// 获取所有用户的标签(带认可数)
$tags = UserTag::whereIn('user_id', $userids)
->withCount('recognitions')
->get();
// 按用户分组,每个用户取 Top 10 标签
$result = [];
foreach ($userids as $userid) {
$result[$userid] = [];
}
$userTags = $tags->groupBy('user_id');
foreach ($userTags as $userid => $tagCollection) {
$result[$userid] = $tagCollection
->sortByDesc('recognitions_count')
->take(10)
->pluck('name')
->values()
->toArray();
}
return $result;
}
}

View File

@@ -35,7 +35,7 @@ class SystemController extends AbstractController
{
/**
* @api {get} api/system/setting 01. 获取设置、保存设置
* @api {get} api/system/setting 获取设置、保存设置
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -44,7 +44,7 @@ class SystemController extends AbstractController
* @apiParam {String} type
* - get: 获取(默认)
* - all: 获取所有(需要管理员权限)
* - save: 保存设置(参数:['reg', 'reg_identity', 'reg_invite', 'temp_account_alias', 'login_code', 'password_policy', 'project_invite', 'chat_information', 'anon_message', 'voice2text', 'translation', 'convert_video', 'compress_video', 'e2e_message', 'auto_archived', 'archived_day', 'task_visible', 'task_default_time', 'all_group_mute', 'all_group_autoin', 'user_private_chat_mute', 'user_group_chat_mute', 'system_alias', 'system_welcome', 'image_compress', 'image_quality', 'image_save_local']
* - save: 保存设置(参数:['reg', 'reg_identity', 'reg_invite', 'temp_account_alias', 'login_code', 'password_policy', 'project_invite', 'chat_information', 'anon_message', 'convert_video', 'compress_video', 'e2e_message', 'auto_archived', 'archived_day', 'task_visible', 'task_default_time', 'task_user_limit', 'all_group_mute', 'all_group_autoin', 'user_private_chat_mute', 'user_group_chat_mute', 'system_alias', 'system_welcome', 'image_compress', 'image_quality', 'image_save_local']
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
@@ -71,8 +71,6 @@ class SystemController extends AbstractController
'project_invite',
'chat_information',
'anon_message',
'voice2text',
'translation',
'convert_video',
'compress_video',
'e2e_message',
@@ -82,6 +80,7 @@ class SystemController extends AbstractController
'archived_day',
'task_visible',
'task_default_time',
'task_user_limit',
'all_group_mute',
'all_group_autoin',
'user_private_chat_mute',
@@ -106,12 +105,6 @@ class SystemController extends AbstractController
return Base::retError('自动归档时间不可大于100天');
}
}
if ($all['voice2text'] == 'open' && !Setting::AIOpen()) {
return Base::retError('开启语音转文字功能需要先设置 AI 助理。');
}
if ($all['translation'] == 'open' && !Setting::AIOpen()) {
return Base::retError('开启翻译功能需要先设置 AI 助理。');
}
if ($all['system_alias'] == env('APP_NAME')) {
$all['system_alias'] = '';
}
@@ -138,8 +131,6 @@ class SystemController extends AbstractController
$setting['project_invite'] = $setting['project_invite'] ?: 'open';
$setting['chat_information'] = $setting['chat_information'] ?: 'optional';
$setting['anon_message'] = $setting['anon_message'] ?: 'open';
$setting['voice2text'] = $setting['voice2text'] ?: 'close';
$setting['translation'] = $setting['translation'] ?: 'close';
$setting['convert_video'] = $setting['convert_video'] ?: 'close';
$setting['compress_video'] = $setting['compress_video'] ?: 'close';
$setting['e2e_message'] = $setting['e2e_message'] ?: 'close';
@@ -158,11 +149,11 @@ class SystemController extends AbstractController
$setting['server_timezone'] = config('app.timezone');
$setting['server_version'] = Base::getVersion();
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/setting/email 02. 获取邮箱设置、保存邮箱设置(限管理员)
* @api {get} api/system/setting/email 获取邮箱设置、保存邮箱设置(限管理员)
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -228,11 +219,11 @@ class SystemController extends AbstractController
$setting = array_intersect_key($setting, array_flip(['reg_verify']));
}
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/setting/meeting 03. 获取会议设置、保存会议设置(限管理员)
* @api {get} api/system/setting/meeting 获取会议设置、保存会议设置(限管理员)
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -282,53 +273,21 @@ class SystemController extends AbstractController
$setting['api_secret'] = substr($setting['api_secret'], 0, 4) . str_repeat('*', strlen($setting['api_secret']) - 8) . substr($setting['api_secret'], -4);
}
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/setting/ai 04. AI助手设置限管理员
* AI助手设置限管理员
*
* @apiVersion 1.0.0
* @apiGroup system
* @apiName setting__ai
*
* @apiParam {String} type
* - get: 获取(默认)
* - save: 保存设置(参数:['ai_provider', 'ai_api_key', 'ai_api_url', 'ai_proxy']
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @deprecated 已废弃方法,仅保留路由占位,后续版本中移除
*/
public function setting__ai()
{
User::auth('admin');
//
$type = trim(Request::input('type'));
if ($type == 'save') {
if (env("SYSTEM_SETTING") == 'disabled') {
return Base::retError('当前环境禁止修改');
}
$all = Base::newTrim(Request::input());
foreach ($all as $key => $value) {
if (!in_array($key, [
'ai_provider',
'ai_api_key',
'ai_api_url',
'ai_proxy',
])) {
unset($all[$key]);
}
}
$setting = Base::setting('aiSetting', Base::newTrim($all));
} else {
$setting = Base::setting('aiSetting');
}
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
Base::checkClientVersion('1.4.35');
}
/**
* @api {get} api/system/setting/aibot 05. 获取会议设置、保存AI机器人设置限管理员
* @api {get} api/system/setting/aibot 获取AI设置、保存AI机器人设置限管理员
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -382,70 +341,31 @@ class SystemController extends AbstractController
}
}
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/setting/aibot_models 06. 获取AI模型
* 获取AI模型
*
* @apiDescription 获取所有AI机器人模型设置
* @apiVersion 1.0.0
* @apiGroup system
* @apiName aibot_models
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @deprecated 已废弃方法,仅保留路由占位,后续版本中移除
*/
public function setting__aibot_models()
{
$setting = Base::setting('aibotSetting');
$setting = array_filter($setting, function($value, $key) {
return str_ends_with($key, '_models') || str_ends_with($key, '_model');
}, ARRAY_FILTER_USE_BOTH);
return Base::retSuccess('success', $setting ?: json_decode('{}'));
Base::checkClientVersion('1.4.35');
}
/**
* @api {get} api/system/setting/aibot_defmodels 07. 获取AI默认模型
* 获取AI默认模型
*
* @apiDescription 获取AI机器人默认模型
* @apiVersion 1.0.0
* @apiGroup system
* @apiName setting__aibot_defmodels
*
* @apiParam {String} type AI类型
* @apiParam {String} [base_url] 基础URL仅 type=ollama 时有效)
* @apiParam {String} [key] Key仅 type=ollama 时有效)
* @apiParam {String} [agency] 使用代理(仅 type=ollama 时有效)
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @deprecated 已废弃方法,仅保留路由占位,后续版本中移除
*/
public function setting__aibot_defmodels()
{
$type = trim(Request::input('type'));
if ($type == 'ollama') {
$baseUrl = trim(Request::input('base_url'));
$key = trim(Request::input('key'));
$agency = trim(Request::input('agency'));
if (empty($baseUrl)) {
return Base::retError('请先填写 Base URL');
}
return AI::ollamaModels($baseUrl, $key, $agency);
}
$models = Setting::AIBotDefaultModels($type);
if (empty($models)) {
return Base::retError('未找到默认模型');
}
return Base::retSuccess('success', [
'models' => $models
]);
Base::checkClientVersion('1.4.35');
}
/**
* @api {get} api/system/setting/checkin 08. 获取签到设置、保存签到设置(限管理员)
* @api {get} api/system/setting/checkin 获取签到设置、保存签到设置(限管理员)
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -536,6 +456,24 @@ class SystemController extends AbstractController
if ($all['modes']) {
$all['modes'] = array_intersect($all['modes'], ['auto', 'manual', 'locat', 'face']);
}
// 验证提前和延后时间是否重叠(跨天打卡支持)
if ($all['open'] === 'open') {
$times = is_array($all['time']) ? $all['time'] : Base::json2array($all['time']);
if (count($times) >= 2) {
$startMinutes = intval(substr($times[0], 0, 2)) * 60 + intval(substr($times[0], 3, 2));
$endMinutes = intval(substr($times[1], 0, 2)) * 60 + intval(substr($times[1], 3, 2));
$shiftDuration = $endMinutes - $startMinutes;
if ($shiftDuration <= 0) {
$shiftDuration += 24 * 60; // 处理跨天班次
}
$advance = intval($all['advance']) ?: 120;
$delay = intval($all['delay']) ?: 120;
$maxAllowed = 24 * 60 - $shiftDuration;
if ($advance + $delay >= $maxAllowed) {
return Base::retError('提前和延后时间设置存在重叠,最大提前+延后时间不能超过 ' . ($maxAllowed - 1) . ' 分钟');
}
}
}
$setting = Base::setting('checkinSetting', Base::newTrim($all));
} else {
$setting = Base::setting('checkinSetting');
@@ -572,11 +510,11 @@ class SystemController extends AbstractController
$setting['cmd'] = base64_encode($setting['cmd']);
}
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/setting/apppush 09. 获取APP推送设置、保存APP推送设置限管理员
* @api {get} api/system/setting/apppush 获取APP推送设置、保存APP推送设置限管理员
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -617,11 +555,11 @@ class SystemController extends AbstractController
//
$setting['push'] = $setting['push'] ?: 'close';
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/setting/thirdaccess 10. 第三方帐号(限管理员)
* @api {get} api/system/setting/thirdaccess 第三方帐号(限管理员)
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -687,11 +625,11 @@ class SystemController extends AbstractController
$setting['ldap_port'] = intval($setting['ldap_port']) ?: 389;
$setting['ldap_sync_local'] = $setting['ldap_sync_local'] ?: 'close';
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/setting/file 11. 文件设置(限管理员)
* @api {get} api/system/setting/file 文件设置(限管理员)
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -727,11 +665,11 @@ class SystemController extends AbstractController
$setting = Base::setting('fileSetting');
}
//
return Base::retSuccess('success', $setting ?: json_decode('{}'));
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting ?: json_decode('{}'));
}
/**
* @api {get} api/system/demo 12. 获取演示帐号
* @api {get} api/system/demo 获取演示帐号
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -755,7 +693,7 @@ class SystemController extends AbstractController
}
/**
* @api {post} api/system/priority 13. 任务优先级
* @api {post} api/system/priority 任务优先级
*
* @apiDescription 获取任务优先级、保存任务优先级
* @apiVersion 1.0.0
@@ -777,34 +715,64 @@ class SystemController extends AbstractController
if ($type == 'save') {
User::auth('admin');
$list = Request::input('list');
$array = [];
if (empty($list) || !is_array($list)) {
return Base::retError('参数错误');
}
foreach ($list AS $item) {
if (empty($item['name']) || empty($item['color']) || empty($item['priority'])) {
continue;
}
$array[] = [
'name' => $item['name'],
'color' => $item['color'],
'days' => intval($item['days']),
'priority' => intval($item['priority']),
];
}
$array = Setting::normalizeTaskPriorityList($list);
if (empty($array)) {
return Base::retError('参数为空');
}
$setting = Base::setting('priority', $array);
} else {
$setting = Base::setting('priority');
$setting = Setting::normalizeTaskPriorityList(Base::setting('priority'));
}
//
return Base::retSuccess('success', $setting);
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting);
}
/**
* @api {post} api/system/column/template 14. 创建项目模板
* @api {post} api/system/microapp_menu 自定义应用菜单
*
* @apiDescription 获取或保存自定义微应用菜单,仅管理员可配置
* @apiVersion 1.0.0
* @apiGroup system
* @apiName microapp_menu
*
* @apiParam {String} type
* - get: 获取(默认)
* - save: 保存(限管理员)
* @apiParam {Array} list 菜单列表,格式:[{id,name,version,menu_items}]
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function microapp_menu()
{
$type = trim(Request::input('type'));
$user = User::auth();
if ($type == 'save') {
User::auth('admin');
$list = Request::input('list');
if (empty($list) || !is_array($list)) {
$list = [];
}
$apps = Setting::normalizeCustomMicroApps($list);
$setting = Base::setting('microapp_menu', $apps);
$setting = Setting::formatCustomMicroAppsForResponse($setting);
} else {
$setting = Base::setting('microapp_menu');
if (!is_array($setting)) {
$setting = [];
}
$setting = Setting::filterCustomMicroAppsForUser($setting, $user);
$setting = Setting::formatCustomMicroAppsForResponse($setting);
}
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting);
}
/**
* @api {post} api/system/column/template 创建项目模板
*
* @apiDescription 获取创建项目模板、保存创建项目模板
* @apiVersion 1.0.0
@@ -847,11 +815,11 @@ class SystemController extends AbstractController
$setting = Base::setting('columnTemplate');
}
//
return Base::retSuccess('success', $setting);
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $setting);
}
/**
* @api {post} api/system/license 15. License
* @api {post} api/system/license License
*
* @apiDescription 获取License信息、保存License限管理员
* @apiVersion 1.0.0
@@ -917,11 +885,11 @@ class SystemController extends AbstractController
];
}
//
return Base::retSuccess('success', $data);
return Base::retSuccess($type == 'save' ? '保存成功' : 'success', $data ?: json_decode('{}'));
}
/**
* @api {get} api/system/get/info 16. 获取终端详细信息
* @api {get} api/system/get/info 获取终端详细信息
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -948,7 +916,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/get/ip 17. 获取IP地址
* @api {get} api/system/get/ip 获取IP地址
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -963,7 +931,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/get/cnip 18. 是否中国IP地址
* @api {get} api/system/get/cnip 是否中国IP地址
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -980,7 +948,7 @@ class SystemController extends AbstractController
}
/**
* @api {post} api/system/imgupload 19. 上传图片
* @api {post} api/system/imgupload 上传图片
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1046,7 +1014,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/get/imgview 20. 浏览图片空间
* @api {get} api/system/get/imgview 浏览图片空间
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1143,7 +1111,7 @@ class SystemController extends AbstractController
}
/**
* @api {post} api/system/fileupload 21. 上传文件
* @api {post} api/system/fileupload 上传文件
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1187,7 +1155,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/get/updatelog 22. 获取更新日志
* @api {get} api/system/get/updatelog 获取更新日志
*
* @apiDescription 获取更新日志
* @apiVersion 1.0.0
@@ -1230,7 +1198,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/email/check 23. 邮件发送测试(限管理员)
* @api {get} api/system/email/check 邮件发送测试(限管理员)
*
* @apiDescription 测试配置邮箱是否能发送邮件
* @apiVersion 1.0.0
@@ -1276,7 +1244,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/checkin/export 24. 导出签到数据(限管理员)
* @api {get} api/system/checkin/export 导出签到数据(限管理员)
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -1321,6 +1289,8 @@ class SystemController extends AbstractController
//
$secondStart = strtotime("2000-01-01 {$time[0]}") - strtotime("2000-01-01 00:00:00");
$secondEnd = strtotime("2000-01-01 {$time[1]}") - strtotime("2000-01-01 00:00:00");
// 获取延后时间配置(用于跨天打卡导出)
$delaySeconds = (intval($setting['delay']) ?: 120) * 60;
//
$botUser = User::botGetOrCreate('system-msg');
if (empty($botUser)) {
@@ -1329,7 +1299,7 @@ class SystemController extends AbstractController
$dialog = WebSocketDialog::checkUserDialog($botUser, $user->userid);
//
$doo = Doo::load();
go(function () use ($doo, $secondStart, $secondEnd, $time, $userid, $date, $user, $botUser, $dialog) {
go(function () use ($doo, $secondStart, $secondEnd, $time, $userid, $date, $user, $botUser, $dialog, $delaySeconds) {
Coroutine::sleep(1);
//
$headings = [];
@@ -1366,9 +1336,10 @@ class SystemController extends AbstractController
$index++;
$sameDate = date("Y-m-d", $startT);
$sameTimes = $recordTimes[$sameDate] ?? [];
$sameCollect = UserCheckinRecord::atCollect($sameDate, $sameTimes);
$sameCollect = UserCheckinRecord::atCollect($sameDate, $sameTimes, $time[0]);
$firstBetween = [Carbon::createFromTimestamp($startT), Carbon::createFromTimestamp($startT + $secondEnd - 1)];
$lastBetween = [Carbon::createFromTimestamp($startT + $secondStart + 1), Carbon::createFromTimestamp($startT + 86400)];
// 扩展下班打卡范围以支持跨天打卡
$lastBetween = [Carbon::createFromTimestamp($startT + $secondStart + 1), Carbon::createFromTimestamp($startT + 86400 + $delaySeconds)];
$firstRecord = $sameCollect?->whereBetween("datetime", $firstBetween)->first();
$lastRecord = $sameCollect?->whereBetween("datetime", $lastBetween)->last();
$firstTimestamp = $firstRecord['timestamp'] ?: 0;
@@ -1498,7 +1469,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/checkin/down 25. 下载导出的签到数据
* @api {get} api/system/checkin/down 下载导出的签到数据
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -1519,7 +1490,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/version 26. 获取版本号
* @api {get} api/system/version 获取版本号
*
* @apiVersion 1.0.0
* @apiGroup system
@@ -1565,7 +1536,7 @@ class SystemController extends AbstractController
}
/**
* @api {get} api/system/prefetch 27. 预加载的资源
* @api {get} api/system/prefetch 预加载的资源
*
* @apiVersion 1.0.0
* @apiGroup system

View File

@@ -34,6 +34,9 @@ use App\Models\WebSocketDialogUser;
use App\Models\UserTaskBrowse;
use App\Models\UserFavorite;
use App\Models\UserRecentItem;
use App\Models\UserTag;
use App\Models\UserTagRecognition;
use App\Models\UserAppSort;
use Illuminate\Support\Facades\DB;
use App\Models\UserEmailVerification;
use App\Module\AgoraIO\AgoraTokenGenerator;
@@ -47,7 +50,7 @@ use Swoole\Coroutine;
class UsersController extends AbstractController
{
/**
* @api {get} api/users/login 01. 登录、注册
* @api {get} api/users/login 登录、注册
*
* @apiVersion 1.0.0
* @apiGroup users
@@ -171,7 +174,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/login/qrcode 02. 二维码登录
* @api {get} api/users/login/qrcode 二维码登录
*
* @apiDescription 通过二维码code登录 (或:是否登录成功)
* @apiVersion 1.0.0
@@ -222,7 +225,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/login/needcode 03. 是否需要验证码
* @api {get} api/users/login/needcode 是否需要验证码
*
* @apiDescription 用于判断是否需要登录验证码
* @apiVersion 1.0.0
@@ -241,7 +244,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/login/codeimg 04. 验证码图片
* @api {get} api/users/login/codeimg 验证码图片
*
* @apiDescription 用于判断是否需要登录验证码
* @apiVersion 1.0.0
@@ -256,7 +259,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/login/codejson 05. 验证码json
* @api {get} api/users/login/codejson 验证码json
*
* @apiDescription 用于判断是否需要登录验证码
* @apiVersion 1.0.0
@@ -274,7 +277,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/logout 06. 退出登录
* @api {get} api/users/logout 退出登录
*
* @apiVersion 1.0.0
* @apiGroup users
@@ -290,7 +293,38 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/reg/needinvite 07. 是否需要邀请码
* @api {get} api/users/token/expire 查询 token 过期时间
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName token__expire
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @apiSuccess {String|null} data.expired_at token 过期时间null 表示永久有效)
* @apiSuccess {Number|null} data.remaining_seconds 距离过期剩余秒数(负值表示已过期)
* @apiSuccess {Boolean} data.expired token 是否已过期
* @apiSuccess {String} data.server_time 当前服务器时间
*/
public function token__expire()
{
User::auth();
$expiredAt = Doo::userExpiredAt();
$expired = Doo::userExpired();
$expiredAtCarbon = $expiredAt ? Carbon::parse($expiredAt) : null;
$data = [
'expired_at' => $expiredAtCarbon?->toDateTimeString(),
'remaining_seconds' => $expiredAtCarbon ? Carbon::now()->diffInSeconds($expiredAtCarbon, false) : null,
'expired' => $expired,
'server_time' => Carbon::now()->toDateTimeString(),
];
return Base::retSuccess('success', $data);
}
/**
* @api {get} api/users/reg/needinvite 是否需要邀请码
*
* @apiDescription 用于判断注册是否需要邀请码
* @apiVersion 1.0.0
@@ -309,7 +343,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/info 08. 获取我的信息
* @api {get} api/users/info 获取我的信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -359,7 +393,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/info/departments 09. 获取我的部门列表
* @api {get} api/users/info/departments 获取我的部门列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -404,7 +438,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/editdata 10. 修改自己的资料
* @api {get} api/users/editdata 修改自己的资料
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -415,6 +449,9 @@ class UsersController extends AbstractController
* @apiParam {String} [tel] 电话
* @apiParam {String} [nickname] 昵称
* @apiParam {String} [profession] 职位/职称
* @apiParam {String} [birthday] 生日格式YYYY-MM-DD
* @apiParam {String} [address] 地址
* @apiParam {String} [introduction] 个人简介
* @apiParam {String} [lang] 语言比如zh/en
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
@@ -478,6 +515,40 @@ class UsersController extends AbstractController
$upLdap['employeeType'] = $profession;
}
}
// 生日
if (Arr::exists($data, 'birthday')) {
$birthday = trim((string) Request::input('birthday'));
if ($birthday === '') {
$user->birthday = null;
} else {
try {
if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $birthday)) {
$birthdayDate = Carbon::createFromFormat('Y-m-d', $birthday);
} else {
$birthdayDate = Carbon::parse($birthday);
}
} catch (\Exception $e) {
return Base::retError('生日格式错误');
}
$user->birthday = $birthdayDate->format('Y-m-d');
}
}
// 地址
if (Arr::exists($data, 'address')) {
$address = trim((string) Request::input('address'));
if (mb_strlen($address) > 100) {
return Base::retError('地址最多只能设置100个字');
}
$user->address = $address ?: null;
}
// 个人简介
if (Arr::exists($data, 'introduction')) {
$introduction = trim((string) Request::input('introduction'));
if (mb_strlen($introduction) > 500) {
return Base::retError('个人简介最多只能设置500个字');
}
$user->introduction = $introduction ?: null;
}
// 语言
if (Arr::exists($data, 'lang')) {
$lang = trim(Request::input('lang'));
@@ -496,7 +567,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/editpass 11. 修改自己的密码
* @api {get} api/users/editpass 修改自己的密码
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -537,7 +608,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/search 12. 搜索会员列表
* @api {get} api/users/search 搜索会员列表
*
* @apiDescription 搜索会员列表
* @apiVersion 1.0.0
@@ -591,7 +662,12 @@ class UsersController extends AbstractController
if (str_contains($keys['key'], "@")) {
$builder->where("email", "like", "%{$keys['key']}%");
} elseif (Base::isNumber($keys['key'])) {
$builder->where("userid", intval($keys['key']));
$builder->where(function ($query) use ($keys) {
$query->where("userid", intval($keys['key']))
->orWhere("nickname", "like", "%{$keys['key']}%")
->orWhere("pinyin", "like", "%{$keys['key']}%")
->orWhere("profession", "like", "%{$keys['key']}%");
});
} else {
$builder->where(function($query) use ($keys) {
$query->where("nickname", "like", "%{$keys['key']}%")
@@ -689,7 +765,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/search/ai 13. 获取AI机器人
* @api {get} api/users/search/ai 获取AI机器人
*
* @apiDescription 搜索会员列表
* @apiVersion 1.0.0
@@ -720,7 +796,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/basic 14. 获取指定会员基础信息
* @api {get} api/users/basic 获取指定会员基础信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -755,7 +831,6 @@ class UsersController extends AbstractController
$basic = UserDelete::userid2basic($id);
}
if ($basic) {
//
$retArray[] = $basic;
}
}
@@ -763,7 +838,70 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/lists 15. 会员列表(限管理员)
* @api {get} api/users/extra 获取会员扩展信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName extra
*
* @apiParam {Number} [userid] 会员ID不传默认为当前用户
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function extra()
{
$user = User::auth();
//
$userid = intval(Request::input('userid'));
if ($userid <= 0) {
$userid = $user->userid;
}
if ($userid <= 0) {
return Base::retError('会员不存在');
}
$user = User::query()
->select(['userid', 'birthday', 'address', 'introduction'])
->whereUserid($userid)
->first();
$birthday = null;
$address = null;
$introduction = null;
if ($user) {
$birthday = $user->birthday;
$address = $user->address;
$introduction = $user->introduction;
} else {
$deleted = UserDelete::whereUserid($userid)->first();
if (empty($deleted) || empty($deleted->cache)) {
return Base::retError('会员不存在');
}
$birthday = $deleted->cache['birthday'] ?? null;
$address = $deleted->cache['address'] ?? null;
$introduction = $deleted->cache['introduction'] ?? null;
}
$tagMeta = UserTag::listWithMeta($userid, $user);
$data = [
'userid' => $userid,
'birthday' => $birthday,
'address' => $address,
'introduction' => $introduction,
'personal_tags' => $tagMeta['top'],
'personal_tags_total' => $tagMeta['total'],
];
return Base::retSuccess('success', $data);
}
/**
* @api {get} api/users/lists 会员列表(限管理员)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -912,7 +1050,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/operation 16. 操作会员(限管理员)
* @api {get} api/users/operation 操作会员(限管理员)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1175,7 +1313,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/email/verification 17. 邮箱验证
* @api {get} api/users/email/verification 邮箱验证
*
* @apiDescription 不需要token身份
* @apiVersion 1.0.0
@@ -1223,7 +1361,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/umeng/alias 18. 设置友盟别名
* @api {get} api/users/umeng/alias 设置友盟别名
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1302,7 +1440,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/meeting/open 19. 【会议】创建会议、加入会议
* @api {get} api/users/meeting/open 【会议】创建会议、加入会议
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1420,7 +1558,229 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/meeting/link 20. 【会议】获取分享链接
* @api {get} api/users/tags/lists 获取个性标签列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName tags__lists
*
* @apiParam {Number} [userid] 会员ID不传默认为当前用户
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
* @apiSuccessExample {json} data:
{
"list": [
{
"id": 1,
"name": "认真负责",
"recognition_total": 3,
"recognized": true,
"can_edit": true,
"can_delete": true
}
],
"top": [ ],
"total": 1
}
*/
public function tags__lists()
{
$viewer = User::auth();
$userid = intval(Request::input('userid')) ?: $viewer->userid;
$target = User::whereUserid($userid)->first();
if (empty($target)) {
return Base::retError('会员不存在');
}
return Base::retSuccess('success', UserTag::listWithMeta($target->userid, $viewer));
}
/**
* @api {post} api/users/tags/add 新增个性标签
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName tags__add
*
* @apiParam {Number} [userid] 会员ID不传默认为当前用户
* @apiParam {String} name 标签名称1-20个字符
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据,同“获取个性标签列表”
*/
public function tags__add()
{
$viewer = User::auth();
$userid = intval(Request::input('userid')) ?: $viewer->userid;
$target = User::whereUserid($userid)->first();
if (empty($target)) {
return Base::retError('会员不存在');
}
$name = trim((string) Request::input('name'));
if ($name === '') {
return Base::retError('请输入个性标签');
}
if (mb_strlen($name) > 20) {
return Base::retError('标签名称最多只能设置20个字');
}
if (UserTag::where('user_id', $userid)->where('name', $name)->exists()) {
return Base::retError('标签已存在');
}
if (UserTag::where('user_id', $userid)->count() >= 100) {
return Base::retError('每位会员最多添加100个标签');
}
$tag = UserTag::create([
'user_id' => $userid,
'name' => $name,
'created_by' => $viewer->userid,
'updated_by' => $viewer->userid,
]);
$tag->save();
return Base::retSuccess('添加成功', UserTag::listWithMeta($userid, $viewer));
}
/**
* @api {post} api/users/tags/update 修改个性标签
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName tags__update
*
* @apiParam {Number} tag_id 标签ID
* @apiParam {String} name 标签名称1-20个字符
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据,同“获取个性标签列表”
*/
public function tags__update()
{
$viewer = User::auth();
$tagId = intval(Request::input('tag_id'));
$name = trim((string) Request::input('name'));
if ($tagId <= 0) {
return Base::retError('参数错误');
}
if ($name === '') {
return Base::retError('请输入个性标签');
}
if (mb_strlen($name) > 20) {
return Base::retError('标签名称最多只能设置20个字');
}
$tag = UserTag::find($tagId);
if (empty($tag)) {
return Base::retError('标签不存在');
}
if (!$tag->canManage($viewer)) {
return Base::retError('无权操作该标签');
}
if ($name !== $tag->name && UserTag::where('user_id', $tag->user_id)->where('name', $name)->where('id', '!=', $tag->id)->exists()) {
return Base::retError('标签已存在');
}
if ($name !== $tag->name) {
$tag->updateInstance([
'name' => $name,
'updated_by' => $viewer->userid,
]);
} else {
$tag->updateInstance([
'updated_by' => $viewer->userid,
]);
}
$tag->save();
return Base::retSuccess('保存成功', UserTag::listWithMeta($tag->user_id, $viewer));
}
/**
* @api {post} api/users/tags/delete 删除个性标签
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName tags__delete
*
* @apiParam {Number} tag_id 标签ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据,同“获取个性标签列表”
*/
public function tags__delete()
{
$viewer = User::auth();
$tagId = intval(Request::input('tag_id'));
if ($tagId <= 0) {
return Base::retError('参数错误');
}
$tag = UserTag::find($tagId);
if (empty($tag)) {
return Base::retError('标签不存在');
}
if (!$tag->canManage($viewer)) {
return Base::retError('无权操作该标签');
}
$userId = $tag->user_id;
$tag->delete();
return Base::retSuccess('删除成功', UserTag::listWithMeta($userId, $viewer));
}
/**
* @api {post} api/users/tags/recognize 认可个性标签
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName tags__recognize
*
* @apiParam {Number} tag_id 标签ID
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据,同“获取个性标签列表”
*/
public function tags__recognize()
{
$viewer = User::auth();
$tagId = intval(Request::input('tag_id'));
if ($tagId <= 0) {
return Base::retError('参数错误');
}
$tag = UserTag::find($tagId);
if (empty($tag)) {
return Base::retError('标签不存在');
}
$recognition = UserTagRecognition::where('tag_id', $tagId)
->where('user_id', $viewer->userid)
->first();
if ($recognition) {
$recognition->delete();
$message = '已取消认可';
} else {
UserTagRecognition::create([
'tag_id' => $tagId,
'user_id' => $viewer->userid,
]);
$message = '认可成功';
}
return Base::retSuccess($message, UserTag::listWithMeta($tag->user_id, $viewer));
}
/**
* @api {get} api/users/meeting/link 【会议】获取分享链接
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1449,7 +1809,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/meeting/tourist 21. 【会议】游客信息
* @api {get} api/users/meeting/tourist 【会议】游客信息
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1472,7 +1832,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/meeting/invitation 22. 【会议】发送邀请
* @api {get} api/users/meeting/invitation 【会议】发送邀请
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1519,7 +1879,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/email/send 23. 发送邮箱验证码
* @api {get} api/users/email/send 发送邮箱验证码
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1559,7 +1919,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/email/edit 24. 修改邮箱
* @api {get} api/users/email/edit 修改邮箱
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1604,7 +1964,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/delete/account 25. 删除帐号
* @api {get} api/users/delete/account 删除帐号
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1666,7 +2026,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/department/list 26. 部门列表(限管理员)
* @api {get} api/users/department/list 部门列表(限管理员)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1685,7 +2045,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/department/add 27. 新建、修改部门(限管理员)
* @api {get} api/users/department/add 新建、修改部门(限管理员)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1771,7 +2131,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/department/del 28. 删除部门(限管理员)
* @api {get} api/users/department/del 删除部门(限管理员)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1804,7 +2164,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/department/sync 29. 同步部门成员(限管理员)
* @api {get} api/users/department/sync 同步部门成员(限管理员)
*
* @apiDescription 需要token身份将子部门成员同步到当前部门
* @apiVersion 1.0.0
@@ -1912,7 +2272,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/checkin/get 30. 获取签到设置
* @api {get} api/users/checkin/get 获取签到设置
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -1939,7 +2299,7 @@ class UsersController extends AbstractController
}
/**
* @api {post} api/users/checkin/save 31. 保存签到设置
* @api {post} api/users/checkin/save 保存签到设置
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2014,7 +2374,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/checkin/list 32. 获取签到数据
* @api {get} api/users/checkin/list 获取签到数据
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2061,7 +2421,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/socket/status 33. 获取socket状态
* @api {get} api/users/socket/status 获取socket状态
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2084,7 +2444,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/key/client 34. 客户端KEY
* @api {get} api/users/key/client 客户端KEY
*
* @apiDescription 获取客户端KEY用于加密数据发送给服务端
* @apiVersion 1.0.0
@@ -2126,7 +2486,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/bot/list 35. 机器人列表
* @api {get} api/users/bot/list 机器人列表
*
* @apiDescription 需要token身份获取我的机器人列表
* @apiVersion 1.0.0
@@ -2150,7 +2510,8 @@ class UsersController extends AbstractController
'users.nickname',
'users.userimg',
'user_bots.clear_day',
'user_bots.webhook_url'
'user_bots.webhook_url',
'user_bots.webhook_events'
])
->orderByDesc('id')
->get()
@@ -2160,6 +2521,7 @@ class UsersController extends AbstractController
$bot['name'] = $bot['nickname'];
$bot['avatar'] = $bot['userimg'];
$bot['system_name'] = UserBot::systemBotName($bot['name']);
$bot['webhook_events'] = UserBot::normalizeWebhookEvents($bot['webhook_events'] ?? null, empty($bot['webhook_events']));
unset($bot['userid'], $bot['nickname'], $bot['userimg']);
}
@@ -2170,7 +2532,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/bot/info 36. 机器人信息
* @api {get} api/users/bot/info 机器人信息
*
* @apiDescription 需要token身份获取我的机器人信息
* @apiVersion 1.0.0
@@ -2211,17 +2573,19 @@ class UsersController extends AbstractController
'avatar' => $botUser->userimg,
'clear_day' => 0,
'webhook_url' => '',
'webhook_events' => [UserBot::WEBHOOK_EVENT_MESSAGE],
'system_name' => UserBot::systemBotName($botUser->email),
];
if ($userBot) {
$data['clear_day'] = $userBot->clear_day;
$data['webhook_url'] = $userBot->webhook_url;
$data['webhook_events'] = $userBot->webhook_events;
}
return Base::retSuccess('success', $data);
}
/**
* @api {post} api/users/bot/edit 37. 添加、编辑机器人
* @api {post} api/users/bot/edit 添加、编辑机器人
*
* @apiDescription 需要token身份编辑 我的机器人 或 管理员修改系统机器人 信息
* @apiVersion 1.0.0
@@ -2296,6 +2660,9 @@ class UsersController extends AbstractController
if (Arr::exists($data, 'webhook_url')) {
$upBot['webhook_url'] = trim($data['webhook_url']);
}
if (Arr::exists($data, 'webhook_events')) {
$upBot['webhook_events'] = UserBot::normalizeWebhookEvents($data['webhook_events'], false);
}
//
if ($upUser) {
$botUser->updateInstance($upUser);
@@ -2312,17 +2679,19 @@ class UsersController extends AbstractController
'avatar' => $botUser->userimg,
'clear_day' => 0,
'webhook_url' => '',
'webhook_events' => [UserBot::WEBHOOK_EVENT_MESSAGE],
'system_name' => UserBot::systemBotName($botUser->email),
];
if ($userBot) {
$data['clear_day'] = $userBot->clear_day;
$data['webhook_url'] = $userBot->webhook_url;
$data['webhook_events'] = $userBot->webhook_events;
}
return Base::retSuccess($botId ? '修改成功' : '添加成功', $data);
}
/**
* @api {get} api/users/bot/delete 38. 删除机器人
* @api {get} api/users/bot/delete 删除机器人
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2372,7 +2741,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/share/list 39. 获取分享列表
* @api {get} api/users/share/list 获取分享列表
*
* @apiVersion 1.0.0
* @apiGroup users
@@ -2460,7 +2829,11 @@ class UsersController extends AbstractController
$dialogIds[] = $dialog['id'];
}
if ($key && count($dialogList) < $dialogTake) {
$dialogUsers = User::searchUser($key, $dialogTake - count($dialogList));
$dialogUsers = User::select(User::$basicField)
->searchByKeyword($key)
->orderBy('userid')
->take($dialogTake - count($dialogList))
->get();
foreach ($dialogUsers as $item) {
$dialog = WebSocketDialog::getUserDialog($user->userid, $item->userid, now()->addDay());
if ($dialog && !in_array($dialog->id, $dialogIds)) {
@@ -2491,7 +2864,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/annual/report 40. 年度报告
* @api {get} api/users/annual/report 年度报告
*
* @apiVersion 1.0.0
* @apiGroup users
@@ -2660,7 +3033,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/device/list 41. 获取设备列表
* @api {get} api/users/device/list 获取设备列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2683,7 +3056,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/device/logout 42. 登出设备(删除设备)
* @api {get} api/users/device/logout 登出设备(删除设备)
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2714,7 +3087,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/device/edit 43. 编辑设备
* @api {get} api/users/device/edit 编辑设备
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2753,7 +3126,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/task/browse 44. 获取任务浏览历史
* @api {get} api/users/task/browse 获取任务浏览历史
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2803,7 +3176,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/task/browse_save 45. 记录任务浏览历史
* @api {get} api/users/task/browse_save 记录任务浏览历史
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2833,7 +3206,7 @@ class UsersController extends AbstractController
}
/**
* @api {post} api/users/task/browse_clean 46. 清理任务浏览历史
* @api {post} api/users/task/browse_clean 清理任务浏览历史
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -2858,7 +3231,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/recent/browse 47. 获取最近访问记录
* @api {get} api/users/recent/browse 获取最近访问记录
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3061,7 +3434,7 @@ class UsersController extends AbstractController
}
/**
* @api {post} api/users/recent/delete 48. 删除最近访问记录
* @api {post} api/users/recent/delete 删除最近访问记录
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3094,7 +3467,52 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/favorites 49. 获取用户收藏列表
* @api {get} api/users/appsort 获取个人应用排序
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName appsort
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function appsort()
{
$user = User::auth();
$sorts = UserAppSort::getSorts($user->userid);
return Base::retSuccess('success', [
'sorts' => $sorts,
]);
}
/**
* @api {post} api/users/appsort/save 保存个人应用排序
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
* @apiGroup users
* @apiName appsort__save
*
* @apiParam {Object} sorts 排序配置,示例:{"base":["micro:calendar"],"admin":["system:ldap"]}
*
* @apiSuccess {Number} ret 返回状态码1正确、0错误
* @apiSuccess {String} msg 返回信息(错误描述)
* @apiSuccess {Object} data 返回数据
*/
public function appsort__save()
{
$user = User::auth();
$sorts = UserAppSort::normalizeSorts(Request::input('sorts'));
$record = UserAppSort::saveSorts($user->userid, $sorts);
return Base::retSuccess('保存成功', [
'sorts' => $record->sorts ?? $sorts,
]);
}
/**
* @api {get} api/users/favorites 获取用户收藏列表
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3129,7 +3547,7 @@ class UsersController extends AbstractController
}
/**
* @api {post} api/users/favorite/toggle 50. 切换收藏状态
* @api {post} api/users/favorite/toggle 切换收藏状态
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3195,7 +3613,7 @@ class UsersController extends AbstractController
}
/**
* @api {post} api/users/favorite/remark 51. 修改收藏备注
* @api {post} api/users/favorite/remark 修改收藏备注
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3247,7 +3665,7 @@ class UsersController extends AbstractController
}
/**
* @api {post} api/users/favorites/clean 52. 清理用户收藏
* @api {post} api/users/favorites/clean 清理用户收藏
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3281,7 +3699,7 @@ class UsersController extends AbstractController
}
/**
* @api {get} api/users/favorite/check 53. 检查收藏状态
* @api {get} api/users/favorite/check 检查收藏状态
*
* @apiDescription 需要token身份
* @apiVersion 1.0.0
@@ -3316,4 +3734,5 @@ class UsersController extends AbstractController
//
return Base::retSuccess('success', ['favorited' => $isFavorited]);
}
}

View File

@@ -0,0 +1,378 @@
# apiDoc 参数标签说明(完整速查)
apiDoc 使用内联注释为 RESTful API 自动生成文档。
以下为所有官方支持的参数与其说明。
---
## @api
**定义 API 方法的基本信息**
```js
@api {method} path title
```
- **method**:请求方法,如 `GET``POST``PUT``DELETE`
- **path**:请求路径,例如 `/user/:id`
- **title**:简短标题(显示在文档中)
📘 示例:
```js
@api {get} /user/:id Get user info
```
---
## @apiBody
**定义请求体参数**
```js
@apiBody [{type}] [field=defaultValue] [description]
```
- `{type}` 参数类型(如 String, Number, Object, String[]
- `[field]` 可选字段(方括号表示可选)
- `=defaultValue` 默认值
- `description` 参数说明
📘 示例:
```js
@apiBody {String} lastname Mandatory Lastname.
@apiBody {Object} [address] Optional address object.
@apiBody {String} [address[city]] Optional city.
```
---
## @apiDefine
**定义可复用的文档块**
```js
@apiDefine name [title] [description]
```
- `name`:唯一标识
- `title`:简短标题
- `description`:多行描述
📘 示例:
```js
@apiDefine MyError
@apiError UserNotFound The <code>id</code> of the User was not found.
```
---
## @apiDeprecated
**标记接口为弃用状态**
```js
@apiDeprecated [text]
```
- `text`:提示文本,可带链接到新方法
📘 示例:
```js
@apiDeprecated use now (#User:GetDetails)
```
---
## @apiDescription
**描述接口详细说明**
```js
@apiDescription text
```
📘 示例:
```js
@apiDescription This is the Description.
It is multiline capable.
```
---
## @apiError
**定义错误返回参数**
```js
@apiError [(group)] [{type}] field [description]
```
📘 示例:
```js
@apiError UserNotFound The id of the User was not found.
```
---
## @apiErrorExample
**定义错误返回示例**
```js
@apiErrorExample [{type}] [title]
example
```
📘 示例:
```js
@apiErrorExample {json} Error-Response:
HTTP/1.1 404 Not Found
{ "error": "UserNotFound" }
```
---
## @apiExample
**定义接口使用示例**
```js
@apiExample [{type}] title
example
```
📘 示例:
```js
@apiExample {curl} Example usage:
curl -i http://localhost/user/4711
```
---
## @apiGroup
**定义所属分组**
```js
@apiGroup name
```
📘 示例:
```js
@apiGroup User
```
---
## @apiHeader
**定义请求头参数**
```js
@apiHeader [(group)] [{type}] [field=defaultValue] [description]
```
📘 示例:
```js
@apiHeader {String} access-key Users unique access-key.
```
---
## @apiHeaderExample
**定义请求头示例**
```js
@apiHeaderExample [{type}] [title]
example
```
📘 示例:
```js
@apiHeaderExample {json} Header-Example:
{
"Accept-Encoding": "gzip, deflate"
}
```
---
## @apiIgnore
**忽略当前文档块**
```js
@apiIgnore [hint]
```
📘 示例:
```js
@apiIgnore Not finished method
```
---
## @apiName
**定义接口唯一名称**
```js
@apiName name
```
📘 示例:
```js
@apiName GetUser
```
---
## @apiParam
**定义请求参数**
```js
@apiParam [(group)] [{type}] [field=defaultValue] [description]
```
📘 示例:
```js
@apiParam {Number} id Users unique ID.
@apiParam {String} [firstname] Optional firstname.
@apiParam {String} country="DE" Mandatory with default.
```
---
## @apiParamExample
**定义参数请求示例**
```js
@apiParamExample [{type}] [title]
example
```
📘 示例:
```js
@apiParamExample {json} Request-Example:
{ "id": 4711 }
```
---
## @apiPermission
**定义权限要求**
```js
@apiPermission name
```
📘 示例:
```js
@apiPermission admin
```
---
## @apiPrivate
**标记接口为私有(可过滤)**
```js
@apiPrivate
```
---
## @apiQuery
**定义查询参数(?query**
```js
@apiQuery [{type}] [field=defaultValue] [description]
```
📘 示例:
```js
@apiQuery {Number} id Users unique ID.
@apiQuery {String} [sort="asc"] Sort order.
```
---
## @apiSampleRequest
**定义接口测试请求 URL**
```js
@apiSampleRequest url
```
📘 示例:
```js
@apiSampleRequest http://test.github.com/some_path/
```
---
## @apiSuccess
**定义成功返回参数**
```js
@apiSuccess [(group)] [{type}] field [description]
```
📘 示例:
```js
@apiSuccess {String} firstname Firstname of the User.
@apiSuccess {String} lastname Lastname of the User.
```
---
## @apiSuccessExample
**定义成功返回示例**
```js
@apiSuccessExample [{type}] [title]
example
```
📘 示例:
```js
@apiSuccessExample {json} Success-Response:
HTTP/1.1 200 OK
{ "firstname": "John", "lastname": "Doe" }
```
---
## @apiUse
**引用定义块(@apiDefine**
```js
@apiUse name
```
📘 示例:
```js
@apiDefine MySuccess
@apiSuccess {String} firstname User firstname.
@apiUse MySuccess
```
---
## @apiVersion
**定义接口版本**
```js
@apiVersion version
```
📘 示例:
```js
@apiVersion 1.6.2
```
---
# 附录:常用标签速查表
| 标签 | 作用 | 示例 |
|------|------|------|
| `@api` | 定义接口 | `@api {get} /user/:id` |
| `@apiName` | 唯一名称 | `@apiName GetUser` |
| `@apiGroup` | 所属分组 | `@apiGroup User` |
| `@apiParam` | 请求参数 | `@apiParam {Number} id Users unique ID.` |
| `@apiBody` | 请求体参数 | `@apiBody {String} name Username.` |
| `@apiQuery` | 查询参数 | `@apiQuery {String} keyword Search term.` |
| `@apiHeader` | Header 参数 | `@apiHeader {String} token Auth token.` |
| `@apiSuccess` | 成功返回字段 | `@apiSuccess {String} name Username.` |
| `@apiError` | 错误返回字段 | `@apiError NotFound User not found.` |
| `@apiVersion` | 版本号 | `@apiVersion 1.0.0` |

View File

@@ -1,89 +1,137 @@
<?php
/**
* 给apidoc项目增加顺序编号
* 给apidoc项目增加顺序编号 / 支持恢复
*/
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$path = dirname(__FILE__). '/';
$lists = scandir($path);
//
foreach ($lists AS $item) {
$fillPath = $path . $item;
if (str_ends_with($fillPath, 'Controller.php')) {
$content = file_get_contents($fillPath);
preg_match_all("/\* @api \{(.+?)\} (.*?)\n/i", $content, $matchs);
$i = 1;
foreach ($matchs[2] AS $key=>$text) {
if (in_array(strtolower($matchs[1][$key]), array('get', 'post'))) {
$expl = explode(" ", __sRemove($text));
$end = $expl[1];
if ($expl[2]) {
$end = '';
foreach ($expl AS $k=>$v) { if ($k >= 2) { $end.= " ".$v; } }
}
$newtext = "* @api {".$matchs[1][$key]."} ".$expl[0]." ".__zeroFill($i, 2).". ".trim($end);
$content = str_replace("* @api {".$matchs[1][$key]."} ".$text, $newtext, $content);
$i++;
//
echo $newtext;
echo "\r\n";
}
}
if ($i > 1) {
file_put_contents($fillPath, $content);
}
}
}
echo "Success \n";
const NUMBER_WIDTH = 2;
/** ************************************************************** */
/** ************************************************************** */
/** ************************************************************** */
$isRestore = isset($argv[1]) && strtolower($argv[1]) === 'restore';
/**
* 替换所有空格
* @param $str
* @return mixed
*/
function __sRemove($str) {
$str = str_replace(" ", " ", $str);
if (__strExists($str, " ")) {
return __sRemove($str);
}
return $str;
$basePath = dirname(__FILE__) . '/';
$controllerFiles = glob($basePath . '*Controller.php');
if (!$controllerFiles) {
echo "No Controller.php files found\n";
exit(0);
}
foreach ($controllerFiles as $filePath) {
$original = file_get_contents($filePath);
[$updated, $linesChanged] = processFile($original, $isRestore);
if (count($linesChanged) === 0) {
continue;
}
file_put_contents($filePath, $updated);
foreach ($linesChanged as $line) {
echo $line . "\n";
}
}
echo $isRestore ? "Restore Success \n" : "Success \n";
/**
* 是否包含字符
* @param $string
* @param $find
* @return bool
* 处理单个文件内容
*
* @param string $content
* @param bool $restore
* @return array{string, array<int, string>}
*/
function __strExists($string, $find)
function processFile(string $content, bool $restore): array
{
return str_contains($string, $find);
$lineChanges = [];
$counter = 1;
$pattern = '/\* @api \{([^\}]+)\}\s+([^\s]+)([^\r\n]*)(\r?\n)/';
$updated = preg_replace_callback(
$pattern,
function (array $matches) use ($restore, &$counter, &$lineChanges) {
$method = trim($matches[1]);
if (!in_array(strtolower($method), ['get', 'post'], true)) {
return $matches[0];
}
$endpoint = trim($matches[2]);
$suffix = normalizeDescription(stripExistingNumbering($matches[3]));
if (!$restore) {
$numberedSuffix = formatNumber($counter) . '.';
if ($suffix !== '') {
$numberedSuffix .= ' ' . $suffix;
}
$counter++;
} else {
$numberedSuffix = $suffix;
}
$newLine = renderAnnotation($method, $endpoint, $numberedSuffix);
if ($newLine !== rtrim($matches[0], "\r\n")) {
$lineChanges[] = $newLine;
}
return $newLine . $matches[4];
},
$content
);
if ($updated === null) {
return [$content, []];
}
return [$updated, $lineChanges];
}
/**
* @param string $str 补零
* @param int $length
* @param int $after
* @return bool|string
* 生成格式化后的注释行
*/
function __zeroFill($str, $length = 0, $after = 1) {
if (strlen($str) >= $length) {
return $str;
function renderAnnotation(string $method, string $endpoint, string $suffix = ''): string
{
$line = "* @api {" . $method . "} " . $endpoint;
if ($suffix !== '') {
if ($suffix[0] !== ' ') {
$line .= ' ';
}
$line .= $suffix;
}
$_str = '';
for ($i = 0; $i < $length; $i++) {
$_str .= '0';
}
if ($after) {
$_ret = substr($_str . $str, $length * -1);
} else {
$_ret = substr($str . $_str, 0, $length);
}
return $_ret;
return $line;
}
/**
* 移除已有编号部分
*/
function stripExistingNumbering(string $text): string
{
$trimmed = ltrim($text);
$pattern = '/^\d+\.\s*/';
return preg_replace($pattern, '', $trimmed) ?? $trimmed;
}
/**
* 压缩多余空格
*/
function normalizeDescription(string $text): string
{
$text = trim($text);
if ($text === '') {
return '';
}
return preg_replace('/\s+/', ' ', $text) ?? $text;
}
/**
* 生成固定宽度的数字
*/
function formatNumber(int $number): string
{
return str_pad((string) $number, NUMBER_WIDTH, '0', STR_PAD_LEFT);
}

View File

@@ -21,7 +21,7 @@ use App\Tasks\AutoArchivedTask;
use App\Tasks\DeleteBotMsgTask;
use App\Tasks\CheckinRemindTask;
use App\Tasks\CloseMeetingRoomTask;
use App\Tasks\ZincSearchSyncTask;
use App\Tasks\ManticoreSyncTask;
use App\Tasks\UnclaimedTaskRemindTask;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Laravolt\Avatar\Avatar;
@@ -258,6 +258,7 @@ class IndexController extends InvokeController
Task::deliver(new DeleteTmpTask('file'));
Task::deliver(new DeleteTmpTask('tmp_file', 24));
Task::deliver(new DeleteTmpTask('user_device', 24));
Task::deliver(new DeleteTmpTask('umeng_log', 24 * 3));
// 删除机器人消息
Task::deliver(new DeleteBotMsgTask());
// 周期任务
@@ -270,8 +271,8 @@ class IndexController extends InvokeController
Task::deliver(new UnclaimedTaskRemindTask());
// 关闭会议室
Task::deliver(new CloseMeetingRoomTask());
// ZincSearch 同步
Task::deliver(new ZincSearchSyncTask());
// Manticore Search 同步
Task::deliver(new ManticoreSyncTask());
return "success";
}

View File

@@ -4,8 +4,10 @@ namespace App\Http\Middleware;
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
use App\Module\Base;
use App\Module\Doo;
use App\Services\RequestContext;
use Cache;
use Closure;
class WebApi
@@ -29,6 +31,12 @@ class WebApi
// 加载Doo类
Doo::load();
// 记录 PC 端活跃时间
$userid = Doo::userId();
if ($userid > 0 && Base::isPc()) {
Cache::put("user_pc_active:{$userid}", time(), 60);
}
// 解密请求内容
$encrypt = Doo::pgpParseStr($request->header('encrypt'));
if ($request->isMethod('post')) {

View File

@@ -6,6 +6,8 @@ use Request;
use App\Module\Apps;
use App\Module\Base;
use App\Tasks\PushTask;
use App\Tasks\ManticoreSyncTask;
use App\Observers\AbstractObserver;
use App\Exceptions\ApiException;
use Illuminate\Support\Facades\DB;
use Hhxsv5\LaravelS\Swoole\Task\Task;
@@ -40,6 +42,8 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Eloquent\Builder|File query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|File searchByKeyword(string $keyword)
* @method static \Illuminate\Database\Eloquent\Builder|File sharedToUser(int $userid)
* @method static \Illuminate\Database\Eloquent\Builder|File whereCid($value)
* @method static \Illuminate\Database\Eloquent\Builder|File whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|File whereCreatedId($value)
@@ -128,6 +132,45 @@ class File extends AbstractModel
*/
const zipMaxSize = 1024 * 1024 * 1024; // 1G
/**
* 按关键词搜索文件Scope
* 支持文件ID纯数字、文件名
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $keyword 搜索关键词
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSearchByKeyword($query, string $keyword)
{
if (is_numeric($keyword)) {
return $query->where(function ($q) use ($keyword) {
$q->where("id", intval($keyword))
->orWhere("name", "like", "%{$keyword}%");
});
}
return $query->where("name", "like", "%{$keyword}%");
}
/**
* 筛选用户可访问的共享文件Scope
* 不包括用户自己的文件,仅返回他人共享给该用户的文件
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $userid 用户ID
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSharedToUser($query, int $userid)
{
return $query->whereIn('pshare', function ($subQuery) use ($userid) {
$subQuery->select('files.id')
->from('files')
->join('file_users', 'files.id', '=', 'file_users.file_id')
->where('files.userid', '!=', $userid)
->where(function ($q) use ($userid) {
$q->whereIn('file_users.userid', [0, $userid]);
});
});
}
/**
* 获取文件列表
@@ -584,6 +627,26 @@ class File extends AbstractModel
return true;
}
/**
* 批量更新子文件的 userid 并同步到 Manticore
* @param int $userid 新的 userid
* @return int 更新的文件数量
*/
public function updateChildFilesUserid(int $userid): int
{
self::where('pids', 'like', "%,{$this->id},%")->update(['userid' => $userid]);
// 批量 update 绕过 Observer手动触发 Manticore 同步
$childFileIds = self::where('pids', 'like', "%,{$this->id},%")
->where('type', '!=', 'folder')
->pluck('id')
->toArray();
foreach ($childFileIds as $childFileId) {
AbstractObserver::taskDeliver(new ManticoreSyncTask('file_sync', ['id' => $childFileId]));
}
return count($childFileIds);
}
/**
* 获取文件分享链接
* @param $userid
@@ -710,7 +773,7 @@ class File extends AbstractModel
/**
* code获取文件ID、名称
* @param $code
* @return File
* @return File|null
*/
public static function code2IdName($code) {
$arr = explode(",", base64_decode($code));

View File

@@ -152,6 +152,23 @@ class FileContent extends AbstractModel
return Base::retSuccess('success', [ 'content' => $content ]);
}
/**
* 获取文件访问URL
* @param int $fileId 文件ID
* @return string|null 返回完整的文件URL如果文件无内容则返回null
*/
public static function getFileUrl($fileId)
{
$content = self::whereFid($fileId)->orderByDesc('id')->first();
if ($content) {
$contentData = Base::json2array($content->content ?: []);
if (!empty($contentData['url'])) {
return Base::fillUrl($contentData['url']);
}
}
return null;
}
/**
* 获取文件内容
* @param $id

View File

@@ -45,7 +45,7 @@ class FileUser extends AbstractModel
} else {
FileLink::whereFileId($file_id)->delete();
}
FileUser::whereFileId($file_id)->delete();
FileUser::whereFileId($file_id)->remove();
});
}
/**
@@ -58,7 +58,7 @@ class FileUser extends AbstractModel
{
return AbstractModel::transaction(function() use ($userid, $file_id) {
FileLink::whereFileId($file_id)->whereUserid($userid)->delete();
return self::whereFileId($file_id)->whereUserid($userid)->delete();
return self::whereFileId($file_id)->whereUserid($userid)->remove();
});
}
}

View File

@@ -48,6 +48,7 @@ use Request;
* @method static \Illuminate\Database\Eloquent\Builder|Project query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|Project searchByKeyword(string $keyword)
* @method static \Illuminate\Database\Eloquent\Builder|Project whereArchiveDays($value)
* @method static \Illuminate\Database\Eloquent\Builder|Project whereArchiveMethod($value)
* @method static \Illuminate\Database\Eloquent\Builder|Project whereArchivedAt($value)
@@ -164,6 +165,18 @@ class Project extends AbstractModel
return $query;
}
/**
* 按关键词搜索项目Scope
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $keyword 搜索关键词
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSearchByKeyword($query, string $keyword)
{
return $query->where("projects.name", "like", "%{$keyword}%");
}
/**
* 获取任务统计数据
* @param $userid

View File

@@ -74,6 +74,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask searchByKeyword(string $keyword)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereArchivedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereArchivedFollow($value)
* @method static \Illuminate\Database\Eloquent\Builder|ProjectTask whereArchivedUserid($value)
@@ -156,7 +157,7 @@ class ProjectTask extends AbstractModel
return;
}
if (!isset($this->appendattrs['sub_num'])) {
$builder = self::whereParentId($this->id)->whereNull('archived_at');
$builder = self::whereParentId($this->id);
$this->appendattrs['sub_num'] = $builder->count();
$this->appendattrs['sub_complete'] = $builder->whereNotNull('complete_at')->count();
//
@@ -353,6 +354,32 @@ class ProjectTask extends AbstractModel
return $query;
}
/**
* 按关键词搜索任务Scope
* 支持任务ID纯数字、任务名称、描述
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $keyword 搜索关键词
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSearchByKeyword($query, string $keyword)
{
if (is_numeric($keyword)) {
// 纯数字匹配任务ID 或 名称/描述
return $query->where(function ($q) use ($keyword) {
$q->where("project_tasks.id", intval($keyword))
->orWhere("project_tasks.name", "like", "%{$keyword}%")
->orWhere("project_tasks.desc", "like", "%{$keyword}%");
});
}
// 普通文本:搜索名称/描述
return $query->where(function ($q) use ($keyword) {
$q->where("project_tasks.name", "like", "%{$keyword}%")
->orWhere("project_tasks.desc", "like", "%{$keyword}%");
});
}
/**
* 生成描述
* @param $content
@@ -396,6 +423,7 @@ class ProjectTask extends AbstractModel
$userid = User::userid();
$visibility = $data['visibility_appoint'] ?? $data['visibility'];
$visibility_userids = $data['visibility_appointor'] ?: [];
$taskUserLimit = intval(Base::settingFind('system', 'task_user_limit'));
//
if (ProjectTask::whereProjectId($project_id)
->whereNull('project_tasks.complete_at')
@@ -417,6 +445,22 @@ class ProjectTask extends AbstractModel
}
//
$retPre = $parent_id ? '子任务' : '任务';
// 优先级:主任务在缺省时按系统默认补齐,并尽量补全 name/color
if ($parent_id == 0) {
$priorityList = Setting::normalizeTaskPriorityList(Base::setting('priority'));
if ($p_level > 0) {
$matched = reset(array_filter($priorityList, fn($item) => intval($item['priority']) === $p_level)) ?: null;
} else {
$matched = Setting::getDefaultTaskPriorityItem($priorityList);
}
if ($matched) {
$p_level = $p_level > 0 ? $p_level : intval($matched['priority']);
$p_name = $p_name ?: $matched['name'];
$p_color = $p_color ?: $matched['color'];
}
}
$task = self::createInstance([
'parent_id' => $parent_id,
'project_id' => $project_id,
@@ -455,8 +499,8 @@ class ProjectTask extends AbstractModel
if (ProjectTask::authData($uid)
->whereNull('project_tasks.complete_at')
->whereNull('project_tasks.archived_at')
->count() > 500) {
throw new ApiException(User::userid2nickname($uid) . '负责或参与的未完成任务最多不能超过500个');
->count() > $taskUserLimit) {
throw new ApiException(User::userid2nickname($uid) . '负责或参与的未完成任务最多不能超过' . $taskUserLimit . '个');
}
$tmpArray[] = $uid;
}
@@ -757,7 +801,7 @@ class ProjectTask extends AbstractModel
$this->visibility = $data["visibility"];
ProjectTask::whereParentId($data['task_id'])->change(['visibility' => $data["visibility"]]);
}
ProjectTaskVisibilityUser::whereTaskId($data['task_id'])->delete();
ProjectTaskVisibilityUser::whereTaskId($data['task_id'])->remove();
if (Arr::exists($data, 'visibility_appointor')) {
foreach ($data['visibility_appointor'] as $uid) {
if ($uid) {
@@ -1185,6 +1229,126 @@ class ProjectTask extends AbstractModel
});
}
/**
* 获取项目的工作流状态项start 和 end
* @param int $projectId 项目ID
* @return array ['start' => ProjectFlowItem|null, 'end' => ProjectFlowItem|null]
*/
public static function getProjectFlowItems(int $projectId): array
{
$startFlowItem = null;
$endFlowItem = null;
$projectFlow = ProjectFlow::whereProjectId($projectId)->orderByDesc('id')->first();
if ($projectFlow) {
$flowItems = ProjectFlowItem::whereFlowId($projectFlow->id)->orderBy('sort')->get();
foreach ($flowItems as $item) {
if ($item->status == 'start' && !$startFlowItem) {
$startFlowItem = $item;
}
if ($item->status == 'end' && !$endFlowItem) {
$endFlowItem = $item;
}
}
}
return ['start' => $startFlowItem, 'end' => $endFlowItem];
}
/**
* 生成工作流状态名称
* @param ProjectFlowItem|null $flowItem
* @return string
*/
public static function formatFlowItemName(?ProjectFlowItem $flowItem): string
{
return $flowItem ? ($flowItem->status . '|' . $flowItem->name . '|' . $flowItem->color) : '';
}
/**
* 复制子任务到新的父任务
* @param ProjectTask $newParentTask 新的父任务
* @param array $options 选项
* - reset_complete: 是否重置完成状态并映射到 start 工作流(默认 true
* - sync_time: 是否同步时间到父任务的时间(默认 false
* - update_project: 是否更新项目相关字段project_id、column_id默认 false
* @return array 新创建的子任务数组
*/
public function copySubTasks(ProjectTask $newParentTask, array $options = []): array
{
$resetComplete = $options['reset_complete'] ?? true;
$syncTime = $options['sync_time'] ?? false;
$updateProject = $options['update_project'] ?? false;
$newSubTasks = [];
$subTasks = self::whereParentId($this->id)->get();
if ($subTasks->isEmpty()) {
return $newSubTasks;
}
// 获取 start 工作流状态
$flowItems = $resetComplete ? self::getProjectFlowItems($newParentTask->project_id) : ['start' => null];
$startFlowItem = $flowItems['start'];
foreach ($subTasks as $subTask) {
$newSubTask = $subTask->copyTask();
$newSubTask->parent_id = $newParentTask->id;
// 同步时间
if ($syncTime) {
$newSubTask->start_at = $newParentTask->start_at;
$newSubTask->end_at = $newParentTask->end_at;
}
// 更新项目相关字段
if ($updateProject) {
$newSubTask->project_id = $newParentTask->project_id;
$newSubTask->column_id = $newParentTask->column_id;
}
// 重置完成状态
if ($resetComplete) {
$newSubTask->complete_at = null;
$newSubTask->flow_item_id = $startFlowItem?->id ?? 0;
$newSubTask->flow_item_name = self::formatFlowItemName($startFlowItem);
}
$newSubTask->save();
$newSubTasks[] = $newSubTask;
}
return $newSubTasks;
}
/**
* 移动子任务到新项目/列
* @param int $projectId 目标项目ID
* @param int $columnId 目标列ID
*/
public function moveSubTasks(int $projectId, int $columnId): void
{
$subTasks = self::whereParentId($this->id)->get();
if ($subTasks->isEmpty()) {
return;
}
$flowItems = self::getProjectFlowItems($projectId);
$startFlowItem = $flowItems['start'];
$endFlowItem = $flowItems['end'];
foreach ($subTasks as $subTask) {
$subTask->project_id = $projectId;
$subTask->column_id = $columnId;
// 根据完成状态映射工作流
if ($subTask->complete_at) {
$subTask->flow_item_id = $endFlowItem?->id ?? 0;
$subTask->flow_item_name = self::formatFlowItemName($endFlowItem);
} else {
$subTask->flow_item_id = $startFlowItem?->id ?? 0;
$subTask->flow_item_name = self::formatFlowItemName($startFlowItem);
}
$subTask->save();
}
}
/**
* 同步项目成员至聊天室
*/
@@ -1917,11 +2081,8 @@ class ProjectTask extends AbstractModel
$taskUser->save();
}
}
// 子任务
ProjectTask::whereParentId($this->id)->change([
'project_id' => $projectId,
'column_id' => $columnId,
]);
// 子任务 - 根据完成状态映射工作流
$this->moveSubTasks($projectId, $columnId);
//
if ($flowItemId) {
// 更新任务流程
@@ -1948,66 +2109,6 @@ class ProjectTask extends AbstractModel
return true;
}
/**
* 生成AI上下文
* @return array
*/
public function AIContext()
{
$contexts = [];
if ($this->archived_at) {
$contexts[] = "任务状态:已归档";
$contexts[] = "归档时间:" . $this->archived_at;
} elseif ($this->complete_at) {
$contexts[] = "任务状态:已完成";
$contexts[] = "完成时间:" . $this->complete_at;
} elseif ($this->end_at && Carbon::parse($this->end_at)->lt(Carbon::now())) {
$contexts[] = "任务状态:已过期";
$contexts[] = "任务截止时间:" . $this->end_at;
} else {
$contexts[] = "任务状态:进行中";
if ($this->start_at) {
$contexts[] = "任务开始时间:" . $this->start_at;
}
if ($this->end_at) {
$contexts[] = "任务截止时间:" . $this->end_at;
}
}
$contexts[] = "当前系统时间:" . Carbon::now()->toDateTimeString();
if ($this->content) {
$taskDesc = $this->content?->getContentInfo();
if ($taskDesc) {
$descContent = Base::cutStr(Base::html2markdown($taskDesc['content'], ['strip_tags' => true]), 2000);
$contexts[] = <<<EOF
任务描述:
```md
{$descContent}
```
EOF;
}
}
$subTask = ProjectTask::select(['id', 'name', 'complete_at', 'end_at'])->whereParentId($this->id)->get();
if ($subTask->isNotEmpty()) {
$subTaskContent = $subTask->map(function($item) {
if ($item->complete_at) {
$status = " (已完成)";
} elseif ($item->end_at && Carbon::parse($item->end_at)->lt(Carbon::now())) {
$status = " (已过期)";
} else {
$status = " (进行中)";
}
return " - {$item->name} {$status}";
})->join("\n");
if ($subTaskContent) {
$contexts[] = <<<EOF
子任务列表:
{$subTaskContent}
EOF;
}
}
return $contexts;
}
/**
* 获取任务
* @param $task_id
@@ -2069,4 +2170,64 @@ class ProjectTask extends AbstractModel
//
return $task;
}
/**
* 构建指定周期内的未完成任务查询(用于周报/日报等)
* @param int $userid
* @param Carbon $start_time
* @param Carbon $end_time
* @param bool $includeUpdatedForNoPlan 无计划时间任务是否按周期内更新时间一并纳入
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function buildUnfinishedTaskQuery(int $userid, Carbon $start_time, Carbon $end_time, bool $includeUpdatedForNoPlan = true)
{
return self::query()
->join("projects", "projects.id", "=", "project_tasks.project_id")
->whereNull("projects.archived_at")
->whereNull("project_tasks.complete_at")
->whereHas("taskUser", function ($query) use ($userid) {
$query->where("userid", $userid);
})
->where(function ($query) use ($start_time, $end_time, $includeUpdatedForNoPlan) {
// 1) 有计划时间:计划时间与给定周期 [start_time, end_time] 有交集
$query->where(function ($q1) use ($start_time, $end_time) {
$q1->whereNotNull('project_tasks.start_at')
->whereNotNull('project_tasks.end_at')
->where(function ($q2) use ($start_time, $end_time) {
$q2->whereBetween('project_tasks.start_at', [$start_time->toDateTimeString(), $end_time->toDateTimeString()])
->orWhereBetween('project_tasks.end_at', [$start_time->toDateTimeString(), $end_time->toDateTimeString()])
->orWhere(function ($q3) use ($start_time, $end_time) {
$q3->where('project_tasks.start_at', '<=', $start_time->toDateTimeString())
->where('project_tasks.end_at', '>=', $end_time->toDateTimeString());
});
});
});
// 2) 无计划时间
$query->orWhere(function ($q1) use ($start_time, $end_time, $includeUpdatedForNoPlan) {
$q1->whereNull('project_tasks.start_at')
->whereNull('project_tasks.end_at')
->where(function ($q2) use ($start_time, $end_time, $includeUpdatedForNoPlan) {
$q2->whereBetween('project_tasks.created_at', [$start_time->toDateTimeString(), $end_time->toDateTimeString()]);
if ($includeUpdatedForNoPlan) {
$q2->orWhereBetween('project_tasks.updated_at', [$start_time->toDateTimeString(), $end_time->toDateTimeString()]);
}
});
});
})
->select("project_tasks.*")
->orderByDesc("project_tasks.id");
}
/**
* 判断工作流名称是否为取消态(多语言)
* @param string|null $flowItemName
* @return bool
*/
public static function isCanceledFlowName(?string $flowItemName): bool
{
if (empty($flowItemName)) {
return false;
}
return preg_match('/已取消|Cancelled|취소됨|キャンセル済み|Abgebrochen|Annulé|Dibatalkan|Отменено/', $flowItemName) === 1;
}
}

View File

@@ -11,6 +11,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use JetBrains\PhpStorm\Pure;
/**
@@ -26,6 +27,9 @@ use JetBrains\PhpStorm\Pure;
* @property string $sign 汇报唯一标识
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ReportReceive> $Receives
* @property-read int|null $receives_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\ReportAnalysis> $aiAnalyses
* @property-read int|null $ai_analyses_count
* @property-read \App\Models\ReportAnalysis|null $aiAnalysis
* @property-read mixed $receives
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $receivesUser
* @property-read int|null $receives_user_count
@@ -55,6 +59,15 @@ class Report extends AbstractModel
const WEEKLY = "weekly";
const DAILY = "daily";
public const LIST_FIELDS = [
'id',
'title',
'type',
'userid',
'sign',
'created_at',
'updated_at',
];
protected $fillable = [
"title",
@@ -78,6 +91,16 @@ class Report extends AbstractModel
->withPivot("receive_at", "read");
}
public function aiAnalyses(): HasMany
{
return $this->hasMany(ReportAnalysis::class, 'rid');
}
public function aiAnalysis(): HasOne
{
return $this->hasOne(ReportAnalysis::class, 'rid');
}
public function sendUser()
{
return $this->hasOne(User::class, "userid", "userid");

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* App\Models\ReportAnalysis
*
* @property int $id
* @property int $rid 报告ID
* @property int $userid 生成分析的会员ID
* @property string $model 使用的模型名称
* @property string $analysis_text AI 分析的原始文本Markdown
* @property array|null $meta 额外的上下文信息
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\Report|null $report
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereAnalysisText($value)
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereMeta($value)
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereModel($value)
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereRid($value)
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|ReportAnalysis whereUserid($value)
* @mixin \Eloquent
*/
class ReportAnalysis extends AbstractModel
{
protected $table = 'report_ai_analyses';
protected $fillable = [
'rid',
'userid',
'model',
'analysis_text',
'meta',
];
protected $casts = [
'meta' => 'array',
];
public function report(): BelongsTo
{
return $this->belongsTo(Report::class, 'rid');
}
}

View File

@@ -6,6 +6,7 @@ use App\Exceptions\ApiException;
use App\Module\Base;
use App\Module\Doo;
use App\Module\Timer;
use App\Module\AI;
use Carbon\Carbon;
/**
@@ -54,6 +55,7 @@ class Setting extends AbstractModel
$value['image_compress'] = $value['image_compress'] ?: 'open';
$value['image_quality'] = min(100, max(0, intval($value['image_quality']) ?: 90));
$value['image_save_local'] = $value['image_save_local'] ?: 'open';
$value['task_user_limit'] = min(2000, max(1, intval($value['task_user_limit']) ?: 500));
if (!is_array($value['task_default_time']) || count($value['task_default_time']) != 2 || !Timer::isTime($value['task_default_time'][0]) || !Timer::isTime($value['task_default_time'][1])) {
$value['task_default_time'] = ['09:00', '18:00'];
}
@@ -65,17 +67,9 @@ class Setting extends AbstractModel
$value['permission_pack_userids'] = is_array($value['permission_pack_userids']) ? $value['permission_pack_userids'] : [];
break;
// AI 助手设置
case 'aiSetting':
$value['ai_provider'] = $value['ai_provider'] ?: 'openai';
$value['ai_api_key'] = $value['ai_api_key'] ?: '';
$value['ai_api_url'] = $value['ai_api_url'] ?: '';
$value['ai_proxy'] = $value['ai_proxy'] ?: '';
break;
// AI 机器人设置
case 'aibotSetting':
if ($value['claude_token'] && empty($value['claude_key'])) {
if (!empty($value['claude_token']) && empty($value['claude_key'])) {
$value['claude_key'] = $value['claude_token'];
}
$array = [];
@@ -84,17 +78,14 @@ class Setting extends AbstractModel
foreach ($aiList as $aiName) {
foreach ($fieldList as $fieldName) {
$key = $aiName . '_' . $fieldName;
$content = $value[$key] ? trim($value[$key]) : '';
$content = !empty($value[$key]) ? trim($value[$key]) : '';
switch ($fieldName) {
case 'models':
if ($content) {
$content = explode("\n", $content);
$content = array_filter($content);
}
if (empty($content)) {
$content = self::AIBotDefaultModels($aiName);
}
$content = implode("\n", $content);
$content = is_array($content) ? implode("\n", $content) : '';
break;
case 'model':
$models = Setting::AIBotModels2Array($array[$key . 's'], true);
@@ -116,100 +107,100 @@ class Setting extends AbstractModel
}
/**
* 是否开启 AI 助理
* 规范任务优先级设置(确保字段完整且仅有一个默认项)
* @param mixed $list
* @return array<int, array{name:string,color:string,days:int,priority:int,is_default:int}>
*/
public static function normalizeTaskPriorityList($list)
{
if (!is_array($list)) {
return [];
}
$normalized = [];
$defaultIndex = null;
foreach ($list as $item) {
if (!is_array($item)) {
continue;
}
$name = trim((string)($item['name'] ?? ''));
$color = trim((string)($item['color'] ?? ''));
$priority = intval($item['priority'] ?? 0);
if ($name === '' || $color === '' || $priority <= 0) {
continue;
}
$days = intval($item['days'] ?? 0);
$isDefault = !empty($item['is_default']) || !empty($item['default']);
if ($defaultIndex === null && $isDefault) {
$defaultIndex = count($normalized);
}
$normalized[] = [
'name' => $name,
'color' => $color,
'days' => $days,
'priority' => $priority,
'is_default' => $isDefault ? 1 : 0,
];
}
if (!empty($normalized)) {
$defaultIndex = $defaultIndex ?? 0;
foreach ($normalized as $i => $row) {
$normalized[$i]['is_default'] = $i === $defaultIndex ? 1 : 0;
}
}
return array_values($normalized);
}
/**
* 获取默认任务优先级(来自 settings.priority
* @param array|null $list
* @return array|null
*/
public static function getDefaultTaskPriorityItem($list = null)
{
$list = $list ?? Base::setting('priority');
$list = self::normalizeTaskPriorityList($list);
if (empty($list)) {
return null;
}
foreach ($list as $item) {
if (!empty($item['is_default'])) {
return $item;
}
}
return $list[0];
}
/**
* 是否开启 AI 助手
* @return bool
*/
public static function AIOpen()
{
return !!Base::settingFind('aiSetting', 'ai_api_key');
$setting = Base::setting('aibotSetting');
if (!is_array($setting) || empty($setting)) {
return false;
}
foreach (AI::TEXT_MODEL_PRIORITY as $vendor) {
if (self::isAIBotVendorEnabled($setting, $vendor)) {
return true;
}
}
return false;
}
/**
* AI 机器人默认模型
* @param string $ai
* @return array
* 判断 AI 机器人厂商是否启用
* @param array $setting
* @param string $vendor
* @return bool
*/
public static function AIBotDefaultModels($ai = 'openai')
protected static function isAIBotVendorEnabled(array $setting, string $vendor): bool
{
return match ($ai) {
'openai' => [
'gpt-4.1 | GPT-4.1',
'gpt-4o | GPT-4o',
'gpt-4 | GPT-4',
'gpt-4o-mini | GPT-4o Mini',
'gpt-4-turbo | GPT-4 Turbo',
'o3 (thinking) | GPT-o3',
'o1 | GPT-o1',
'o4-mini | GPT-o4 Mini',
'o3-mini | GPT-o3 Mini',
'o1-mini | GPT-o1 Mini',
'gpt-3.5-turbo | GPT-3.5 Turbo',
'gpt-3.5-turbo-16k | GPT-3.5 Turbo 16K',
'gpt-3.5-turbo-0125 | GPT-3.5 Turbo 0125',
'gpt-3.5-turbo-1106 | GPT-3.5 Turbo 1106'
],
'claude' => [
'claude-opus-4-0 (thinking) | Claude Opus 4',
'claude-sonnet-4-0 (thinking) | Claude Sonnet 4',
'claude-3-7-sonnet-latest (thinking) | Claude Sonnet 3.7',
'claude-3-5-sonnet-latest | Claude Sonnet 3.5',
'claude-3-5-haiku-latest | Claude Haiku 3.5',
'claude-3-opus-latest | Claude Opus 3'
],
'deepseek' => [
'deepseek-chat | DeepSeek V3',
'deepseek-reasoner | DeepSeek R1'
],
'gemini' => [
'gemini-2.5-pro-preview-05-06 (thinking) | Gemini 2.5 Pro Preview',
'gemini-2.0-flash | Gemini 2.0 Flash',
'gemini-2.0-flash-lite | Gemini 2.0 Flash-Lite',
'gemini-1.5-flash | Gemini 1.5 Flash',
'gemini-1.5-flash-8b | Gemini 1.5 Flash 8B',
'gemini-1.5-pro | Gemini 1.5 Pro',
'gemini-1.0-pro | Gemini 1.0 Pro'
],
'grok' => [
'grok-3-latest | Grok 3',
'grok-3-fast-latest | Grok 3 Fast',
'grok-3-mini-latest | Grok 3 Mini',
'grok-3-mini-fast-latest | Grok 3 Mini Fast',
'grok-2-vision-latest | Grok 2 Vision',
'grok-2-latest | Grok 2',
],
'zhipu' => [
'glm-4 | GLM-4',
'glm-4-plus | GLM-4 Plus',
'glm-4-air | GLM-4 Air',
'glm-4-airx | GLM-4 AirX',
'glm-4-long | GLM-4 Long',
'glm-4-flash | GLM-4 Flash',
'glm-4v | GLM-4V',
'glm-4v-plus | GLM-4V Plus',
'glm-3-turbo | GLM-3 Turbo'
],
'qianwen' => [
'qwen-max | QWEN Max',
'qwen-max-latest | QWEN Max Latest',
'qwen-turbo | QWEN Turbo',
'qwen-turbo-latest | QWEN Turbo Latest',
'qwen-plus | QWEN Plus',
'qwen-plus-latest | QWEN Plus Latest',
'qwen-long | QWEN Long'
],
'wenxin' => [
'ernie-4.0-8k | Ernie 4.0 8K',
'ernie-4.0-8k-latest | Ernie 4.0 8K Latest',
'ernie-4.0-turbo-128k | Ernie 4.0 Turbo 128K',
'ernie-4.0-turbo-8k | Ernie 4.0 Turbo 8K',
'ernie-3.5-128k | Ernie 3.5 128K',
'ernie-3.5-8k | Ernie 3.5 8K',
'ernie-speed-128k | Ernie Speed 128K',
'ernie-speed-8k | Ernie Speed 8K',
'ernie-lite-8k | Ernie Lite 8K',
'ernie-tiny-8k | Ernie Tiny 8K'
],
default => [],
$key = trim((string)($setting[$vendor . '_key'] ?? ''));
return match ($vendor) {
'ollama' => $key !== '' || !empty($setting['ollama_base_url']),
'wenxin' => $key !== '' && !empty($setting['wenxin_secret']),
default => $key !== '',
};
}
@@ -238,6 +229,213 @@ class Setting extends AbstractModel
return $array;
}
/**
* 规范自定义微应用配置
* @param array $list
* @return array
*/
public static function normalizeCustomMicroApps($list)
{
if (!is_array($list)) {
return [];
}
$apps = [];
foreach ($list as $item) {
$app = self::normalizeCustomMicroAppItem($item);
if ($app) {
$apps[] = $app;
}
}
return $apps;
}
/**
* 根据用户身份过滤可见的自定义微应用
* @param array $apps
* @param \App\Models\User|null $user
* @return array
*/
public static function filterCustomMicroAppsForUser(array $apps, $user)
{
if (empty($apps)) {
return [];
}
$isAdmin = $user ? $user->isAdmin() : false;
$userId = $user ? intval($user->userid) : 0;
$filtered = [];
foreach ($apps as $app) {
$visible = self::normalizeCustomMicroVisible($app['visible_to'] ?? ['admin']);
if (!self::isCustomMicroVisibleTo($visible, $isAdmin, $userId)) {
continue;
}
if (empty($app['menu_items']) || !is_array($app['menu_items'])) {
continue;
}
$menus = array_values(array_filter($app['menu_items'], function ($menu) use ($isAdmin, $userId) {
if (!isset($menu['visible_to'])) {
return true;
}
$visible = self::normalizeCustomMicroVisible($menu['visible_to']);
return self::isCustomMicroVisibleTo($visible, $isAdmin, $userId);
}));
if (empty($menus)) {
continue;
}
$app['menu_items'] = $menus;
$filtered[] = $app;
}
return $filtered;
}
/**
* 将存储结构转换成 appstore 接口同款格式
* @param array $apps
* @return array
*/
public static function formatCustomMicroAppsForResponse(array $apps)
{
return array_values(array_map(function ($app) {
unset($app['visible_to']);
if (!empty($app['menu_items']) && is_array($app['menu_items'])) {
$app['menu_items'] = array_values(array_map(function ($menu) {
$menu['keep_alive'] = isset($menu['keep_alive']) ? (bool)$menu['keep_alive'] : true;
$menu['disable_scope_css'] = (bool)($menu['disable_scope_css'] ?? false);
$menu['auto_dark_theme'] = isset($menu['auto_dark_theme']) ? (bool)$menu['auto_dark_theme'] : true;
$menu['transparent'] = (bool)($menu['transparent'] ?? false);
if (isset($menu['visible_to'])) {
unset($menu['visible_to']);
}
return $menu;
}, $app['menu_items']));
}
return $app;
}, $apps));
}
/**
* 规范自定义微应用
* @param array $item
* @return array|null
*/
protected static function normalizeCustomMicroAppItem($item)
{
if (!is_array($item)) {
return null;
}
$id = trim($item['id'] ?? '');
if ($id === '') {
return null;
}
$name = Base::newTrim($item['name'] ?? '');
$version = Base::newTrim($item['version'] ?? '') ?: 'custom';
$menuItems = [];
if (isset($item['menu_items']) && is_array($item['menu_items'])) {
$menuItems = $item['menu_items'];
} elseif (isset($item['menu']) && is_array($item['menu'])) {
$menuItems = [$item['menu']];
}
if (empty($menuItems)) {
return null;
}
$normalizedMenus = [];
foreach ($menuItems as $menu) {
$formattedMenu = self::normalizeCustomMicroMenuItem($menu, $name ?: $id);
if ($formattedMenu) {
$normalizedMenus[] = $formattedMenu;
}
}
if (empty($normalizedMenus)) {
return null;
}
return Base::newTrim([
'id' => $id,
'name' => $name,
'version' => $version,
'menu_items' => $normalizedMenus,
'visible_to' => self::normalizeCustomMicroVisible($item['visible_to'] ?? 'admin'),
]);
}
/**
* 规范自定义微应用菜单项
* @param array $menu
* @param string $fallbackLabel
* @return array|null
*/
protected static function normalizeCustomMicroMenuItem($menu, $fallbackLabel = '')
{
if (!is_array($menu)) {
return null;
}
$url = trim($menu['url'] ?? '');
if ($url === '') {
return null;
}
$location = trim($menu['location'] ?? 'application');
$label = trim($menu['label'] ?? $fallbackLabel);
$type = strtolower(trim($menu['type'] ?? 'iframe'));
$payload = [
'location' => $location,
'label' => $label,
'icon' => Base::newTrim($menu['icon'] ?? ''),
'url' => $url,
'type' => $type,
'keep_alive' => isset($menu['keep_alive']) ? (bool)$menu['keep_alive'] : true,
'disable_scope_css' => (bool)($menu['disable_scope_css'] ?? false),
'auto_dark_theme' => isset($menu['auto_dark_theme']) ? (bool)$menu['auto_dark_theme'] : true,
'transparent' => (bool)($menu['transparent'] ?? false),
];
if (!empty($menu['background'])) {
$payload['background'] = Base::newTrim($menu['background']);
}
if (!empty($menu['capsule']) && is_array($menu['capsule'])) {
$payload['capsule'] = Base::newTrim($menu['capsule']);
}
return $payload;
}
/**
* 规范自定义微应用可见范围
* @param mixed $value
* @return array
*/
protected static function normalizeCustomMicroVisible($value)
{
if (is_array($value)) {
$list = array_filter(array_map('trim', $value));
} else {
$list = array_filter(array_map('trim', explode(',', (string)$value)));
}
if (empty($list)) {
return ['admin'];
}
if (in_array('all', $list)) {
return ['all'];
}
return array_values($list);
}
/**
* 判断自定义微应用是否可见
* @param array $visible
* @param bool $isAdmin
* @param int $userId
* @return bool
*/
protected static function isCustomMicroVisibleTo(array $visible, bool $isAdmin, int $userId)
{
if (in_array('all', $visible)) {
return true;
}
if ($isAdmin && in_array('admin', $visible)) {
return true;
}
if ($userId > 0 && in_array((string)$userId, $visible, true)) {
return true;
}
return false;
}
/**
* 验证邮箱地址(过滤忽略地址)
* @param $array

View File

@@ -70,6 +70,9 @@ class UmengAlias extends AbstractModel
return;
}
$instance = null;
$responsePayload = null;
try {
switch ($first['platform']) {
case 'ios':
@@ -81,8 +84,11 @@ class UmengAlias extends AbstractModel
default:
return;
}
$instance->send($first['data']);
$responsePayload = $instance->send($first['data']);
} catch (\Exception $e) {
$responsePayload = [
'error' => $e->getMessage(),
];
$first['retry'] = intval($first['retry'] ?? 0) + 1;
if ($first['retry'] > 3) {
info("[PushMsg] fail: " . $e->getMessage());
@@ -91,6 +97,12 @@ class UmengAlias extends AbstractModel
self::$waitSend[] = $first;
}
} finally {
if ($instance !== null) {
UmengLog::create([
'request' => Base::array2json($first['data']),
'response' => Base::array2json($responsePayload),
]);
}
self::sendTask();
}
}
@@ -153,7 +165,7 @@ class UmengAlias extends AbstractModel
$description = $array['description'] ?: 'no description'; // 描述
$extra = is_array($array['extra']) ? $array['extra'] : []; // 额外参数
$seconds = intval($array['seconds']) ?: 86400; // 有效时间(单位:秒)
$badge = intval($array['badge']) ?: 0; // 角标数iOS
$badge = intval($array['badge']) ?: 0; // 角标数
//
switch ($platform) {
case 'ios':
@@ -203,6 +215,7 @@ class UmengAlias extends AbstractModel
'title' => $title,
'after_open' => 'go_app',
'play_sound' => true,
'set_badge' => min(99, $badge),
],
], $extra),
'type' => 'customizedcast',
@@ -215,12 +228,17 @@ class UmengAlias extends AbstractModel
],
'category' => 1,
'channel_properties' => [
'main_activity' => 'com.dootask.task.WelcomeActivity',
'oppo_channel_id' => 'dootask',
'vivo_category' => 'IM',
'huawei_channel_importance' => 'NORMAL',
'huawei_channel_category' => 'IM',
'channel_fcm' => 0,
],
'local_properties' => [
'importance' => 'IMPORTANCE_DEFAULT',
'category' => 'CATEGORY_MESSAGE',
]
]
]);
break;

32
app/Models/UmengLog.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App\Models;
/**
* App\Models\UmengLog
*
* @property int $id
* @property string|null $request 请求参数
* @property string|null $response 推送返回
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog whereRequest($value)
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog whereResponse($value)
* @method static \Illuminate\Database\Eloquent\Builder|UmengLog whereUpdatedAt($value)
* @mixin \Eloquent
*/
class UmengLog extends AbstractModel
{
protected $guarded = [];
}

View File

@@ -5,8 +5,11 @@ namespace App\Models;
use App\Exceptions\ApiException;
use App\Module\Base;
use App\Module\Doo;
use App\Module\Apps;
use App\Module\Table\OnlineData;
use App\Observers\AbstractObserver;
use App\Services\RequestContext;
use App\Tasks\ManticoreSyncTask;
use Cache;
use Carbon\Carbon;
@@ -22,6 +25,9 @@ use Carbon\Carbon;
* @property string|null $tel 联系电话
* @property string $nickname 昵称
* @property string|null $profession 职位/职称
* @property string|null $birthday 生日
* @property string|null $address 地址
* @property string|null $introduction 个人简介
* @property string $userimg 头像
* @property string|null $encrypt
* @property string|null $password 登录密码
@@ -49,7 +55,10 @@ use Carbon\Carbon;
* @method static \Illuminate\Database\Eloquent\Builder|User query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|User searchByKeyword(string $keyword)
* @method static \Illuminate\Database\Eloquent\Builder|User whereAddress($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereAz($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereBirthday($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereBot($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereChangepass($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereCreatedAt($value)
@@ -60,6 +69,7 @@ use Carbon\Carbon;
* @method static \Illuminate\Database\Eloquent\Builder|User whereEmailVerity($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereEncrypt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereIdentity($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereIntroduction($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereLang($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereLastAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereLastIp($value)
@@ -310,7 +320,7 @@ class User extends AbstractModel
*/
public function deleteUser($reason)
{
return AbstractModel::transaction(function () use ($reason) {
$ret = AbstractModel::transaction(function () use ($reason) {
// 删除原因
$userDelete = UserDelete::createInstance([
'operator' => User::userid(),
@@ -331,6 +341,7 @@ class User extends AbstractModel
//
return $this->delete();
});
return $ret;
}
/**
@@ -404,7 +415,14 @@ class User extends AbstractModel
$dialog?->joinGroup($user->userid, 0);
}
}
return $user->find($user->userid);
$createdUser = $user->find($user->userid);
if (!$createdUser->bot) {
// Manticore 索引同步
AbstractObserver::taskDeliver(new ManticoreSyncTask('user_sync', $createdUser->toArray()));
// 触发 user_onboard hook
Apps::dispatchUserHook($createdUser, 'user_onboard', 'onboard');
}
return $createdUser;
}
/**
@@ -764,24 +782,35 @@ class User extends AbstractModel
}
/**
* 搜索用户
* @param $key
* @param $take
* @return User[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Query\Builder[]|\Illuminate\Support\Collection
* 按关键词搜索用户Scope
* 支持:邮箱(含@、用户ID纯数字、昵称/拼音/职业
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $keyword 搜索关键词
* @return \Illuminate\Database\Eloquent\Builder
*/
public static function searchUser($key, $take = 20)
public function scopeSearchByKeyword($query, string $keyword)
{
return User::select(User::$basicField)
->where(function ($query) use ($key) {
if (str_contains($key, "@")) {
$query->where("email", "like", "%{$key}%");
} else {
$query->where("nickname", "like", "%{$key}%")
->orWhere("pinyin", "like", "%{$key}%")
->orWhere("profession", "like", "%{$key}%");
}
})->orderBy('userid')
->take($take)
->get();
if (str_contains($keyword, "@")) {
// 包含 @ 按邮箱搜索
return $query->where("email", "like", "%{$keyword}%");
}
if (is_numeric($keyword)) {
// 纯数字匹配用户ID 或 昵称/拼音/职业
return $query->where(function ($q) use ($keyword) {
$q->where("userid", intval($keyword))
->orWhere("nickname", "like", "%{$keyword}%")
->orWhere("pinyin", "like", "%{$keyword}%")
->orWhere("profession", "like", "%{$keyword}%");
});
}
// 普通文本:搜索昵称/拼音/职业
return $query->where(function ($q) use ($keyword) {
$q->where("nickname", "like", "%{$keyword}%")
->orWhere("pinyin", "like", "%{$keyword}%")
->orWhere("profession", "like", "%{$keyword}%");
});
}
}

102
app/Models/UserAppSort.php Normal file
View File

@@ -0,0 +1,102 @@
<?php
namespace App\Models;
/**
* App\Models\UserAppSort
*
* @property int $id
* @property int $userid 用户ID
* @property array|null $sorts 排序配置
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort whereSorts($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserAppSort whereUserid($value)
* @mixin \Eloquent
*/
class UserAppSort extends AbstractModel
{
protected $fillable = [
'userid',
'sorts',
];
protected $casts = [
'sorts' => 'array',
];
/**
* 获取用户排序配置
* @param int $userid
* @return array
*/
public static function getSorts(int $userid): array
{
$record = static::whereUserid($userid)->first();
if (!$record) {
return self::normalizeSorts([]);
}
return self::normalizeSorts($record->sorts);
}
/**
* 保存排序配置
* @param int $userid
* @param array $sorts
* @return static
*/
public static function saveSorts(int $userid, array $sorts): self
{
return static::updateOrCreate(
['userid' => $userid],
['sorts' => self::normalizeSorts($sorts)]
);
}
/**
* 规范化排序数据
* @param mixed $sorts
* @return array
*/
public static function normalizeSorts($sorts): array
{
$result = [
'base' => [],
'admin' => [],
];
if (!is_array($sorts)) {
return $result;
}
foreach (['base', 'admin'] as $group) {
$list = $sorts[$group] ?? [];
if (!is_array($list)) {
$list = [];
}
$normalized = [];
foreach ($list as $value) {
if (!is_string($value)) {
continue;
}
$value = trim($value);
if ($value === '') {
continue;
}
$normalized[] = $value;
}
$result[$group] = array_values(array_unique($normalized));
}
return $result;
}
}

View File

@@ -4,11 +4,12 @@ namespace App\Models;
use App\Module\Base;
use App\Module\Doo;
use App\Module\Extranet;
use App\Module\Ihttp;
use App\Module\Timer;
use App\Tasks\JokeSoupTask;
use Cache;
use Carbon\Carbon;
use Throwable;
/**
* App\Models\UserBot
@@ -20,6 +21,7 @@ use Carbon\Carbon;
* @property \Illuminate\Support\Carbon|null $clear_at 下一次清理时间
* @property string|null $webhook_url 消息webhook地址
* @property int|null $webhook_num 消息webhook请求次数
* @property array $webhook_events Webhook事件配置
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
@@ -38,12 +40,93 @@ use Carbon\Carbon;
* @method static \Illuminate\Database\Eloquent\Builder|UserBot whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserBot whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserBot whereUserid($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserBot whereWebhookEvents($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserBot whereWebhookNum($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserBot whereWebhookUrl($value)
* @mixin \Eloquent
*/
class UserBot extends AbstractModel
{
public const WEBHOOK_EVENT_MESSAGE = 'message';
public const WEBHOOK_EVENT_DIALOG_OPEN = 'dialog_open';
public const WEBHOOK_EVENT_MEMBER_JOIN = 'member_join';
public const WEBHOOK_EVENT_MEMBER_LEAVE = 'member_leave';
protected $casts = [
'webhook_events' => 'array',
];
/**
* 获取 webhook 事件配置
*
* @param mixed $value
* @return array
*/
public function getWebhookEventsAttribute(mixed $value): array
{
if ($value === null || $value === '') {
return self::normalizeWebhookEvents(null, true);
}
return self::normalizeWebhookEvents($value, false);
}
/**
* 设置 webhook 事件配置
*
* @param mixed $value
* @return void
*/
public function setWebhookEventsAttribute(mixed $value): void
{
$useFallback = $value === null;
$this->attributes['webhook_events'] = Base::array2json(self::normalizeWebhookEvents($value, $useFallback));
}
/**
* 判断是否需要触发指定 webhook 事件
*
* @param string $event
* @return bool
*/
public function shouldDispatchWebhook(string $event): bool
{
if (!$this->webhook_url) {
return false;
}
if (!preg_match('/^https?:\/\//', $this->webhook_url)) {
return false;
}
return in_array($event, $this->webhook_events ?? [], true);
}
/**
* 发送 webhook
*
* @param string $event
* @param array $data
* @param int $timeout
* @return array|null
*/
public function dispatchWebhook(string $event, array $data, int $timeout = 30): ?array
{
if (!$this->shouldDispatchWebhook($event)) {
return null;
}
try {
$data['event'] = $event;
$result = Ihttp::ihttp_post($this->webhook_url, $data, $timeout);
$this->increment('webhook_num');
return $result;
} catch (Throwable $th) {
info(Base::array2json([
'webhook_url' => $this->webhook_url,
'data' => $data,
'error' => $th->getMessage(),
]));
return null;
}
}
/**
* 判断是否系统机器人
@@ -270,16 +353,47 @@ class UserBot extends AbstractModel
$advance = (intval($setting['advance']) ?: 120) * 60;
$delay = (intval($setting['delay']) ?: 120) * 60;
//
$currentTime = Timer::time();
$nowDate = date("Y-m-d");
$nowTime = date("H:i:s");
$yesterdayDate = date("Y-m-d", strtotime("-1 day"));
//
// 今天的签到窗口
$timeStart = strtotime("{$nowDate} {$times[0]}");
$timeEnd = strtotime("{$nowDate} {$times[1]}");
$timeAdvance = max($timeStart - $advance, strtotime($nowDate));
$timeDelay = min($timeEnd + $delay, strtotime("{$nowDate} 23:59:59"));
// 移除 23:59:59 限制,允许跨天
$todayTimeDelay = $timeEnd + $delay;
//
// 昨天的延后窗口(用于判断凌晨打卡归属)
$yesterdayTimeEnd = strtotime("{$yesterdayDate} {$times[1]}");
$yesterdayTimeDelay = $yesterdayTimeEnd + $delay;
//
// 判断签到归属哪天
$targetDate = null;
$checkType = null; // 'up' 或 'down'
//
// 情况1在今天的有效窗口内
if ($currentTime >= $timeAdvance && $currentTime <= $todayTimeDelay) {
$targetDate = $nowDate;
if ($currentTime < $timeEnd) {
$checkType = 'up';
} else {
$checkType = 'down';
}
}
// 情况2凌晨时段检查是否在昨天的延后窗口内
elseif ($currentTime < $timeAdvance && $currentTime <= $yesterdayTimeDelay) {
$targetDate = $yesterdayDate;
$checkType = 'down';
}
//
// 构建错误消息
$errorTime = false;
if (Timer::time() < $timeAdvance || $timeDelay < Timer::time()) {
$errorTime = "不在有效时间内,有效时间为:" . date("H:i", $timeAdvance) . "-" . date("H:i", $timeDelay);
if (!$targetDate) {
$displayDelay = date("H:i", $todayTimeDelay % 86400);
$nextDay = ($todayTimeDelay > strtotime("{$nowDate} 23:59:59")) ? "(+1)" : "";
$errorTime = "不在有效时间内,有效时间为:" . date("H:i", $timeAdvance) . "-{$displayDelay}{$nextDay}";
}
//
$macs = explode(",", $mac);
@@ -293,7 +407,7 @@ class UserBot extends AbstractModel
$array[] = [
'userid' => $UserCheckinMac->userid,
'mac' => $UserCheckinMac->mac,
'date' => $nowDate,
'date' => $targetDate ?: $nowDate,
];
$checkins[] = [
'userid' => $UserCheckinMac->userid,
@@ -314,7 +428,7 @@ class UserBot extends AbstractModel
$array[] = [
'userid' => $UserInfo->userid,
'mac' => '00:00:00:00:00:00',
'date' => $nowDate,
'date' => $targetDate ?: $nowDate,
];
$checkins[] = [
'userid' => $UserInfo->userid,
@@ -349,7 +463,8 @@ class UserBot extends AbstractModel
}
return null;
};
$sendMsg = function($type, $checkin) use ($errorTime, $alreadyTip, $getJokeSoup, $botUser, $nowDate) {
$sendMsg = function($type, $checkin) use ($errorTime, $alreadyTip, $getJokeSoup, $botUser, $targetDate, $nowDate) {
$displayDate = $targetDate ?: $nowDate;
$dialog = WebSocketDialog::checkUserDialog($botUser, $checkin['userid']);
if (!$dialog) {
return;
@@ -366,12 +481,13 @@ class UserBot extends AbstractModel
}
return;
}
// 判断已打卡
$cacheKey = "Checkin::sendMsg-{$nowDate}-{$type}:" . $checkin['userid'];
// 判断已打卡(使用目标日期作为缓存键)
$cacheKey = "Checkin::sendMsg-{$displayDate}-{$type}:" . $checkin['userid'];
$typeContent = $type == "up" ? "上班" : "下班";
if (Cache::get($cacheKey) === "yes") {
if ($alreadyTip) {
$text = "今日已{$typeContent}打卡,无需重复打卡。";
$dateHint = ($displayDate != $nowDate) ? "({$displayDate}) " : "今日";
$text = "{$dateHint}{$typeContent}打卡,无需重复打卡。";
$text .= $checkin['remark'] ? " ({$checkin['remark']})": "";
WebSocketDialogMsg::sendMsg(null, $dialog->id, 'template', [
'type' => 'content',
@@ -385,7 +501,8 @@ class UserBot extends AbstractModel
$hi = date("H:i");
$remark = $checkin['remark'] ? " ({$checkin['remark']})": "";
$subcontent = $getJokeSoup($type, $checkin['userid']);
$title = "{$typeContent}打卡成功,打卡时间: {$hi}{$remark}";
$dateInfo = ($displayDate != $nowDate) ? " ({$displayDate})" : "";
$title = "{$typeContent}打卡成功,打卡时间: {$hi}{$remark}{$dateInfo}";
WebSocketDialogMsg::sendMsg(null, $dialog->id, 'template', [
'type' => 'content',
'title' => $title,
@@ -400,14 +517,13 @@ class UserBot extends AbstractModel
],
], $botUser->userid, false, false, $type != "up");
};
if ($timeAdvance <= Timer::time() && Timer::time() < $timeEnd) {
// 上班打卡通知(从最早打卡时间 到 下班打卡时间)
// 根据打卡类型发送通知
if ($checkType === 'up') {
foreach ($checkins as $checkin) {
$sendMsg('up', $checkin);
}
}
if ($timeEnd <= Timer::time() && Timer::time() <= $timeDelay) {
// 下班打卡通知(下班打卡时间 到 最晚打卡时间)
if ($checkType === 'down') {
foreach ($checkins as $checkin) {
$sendMsg('down', $checkin);
}
@@ -479,4 +595,42 @@ class UserBot extends AbstractModel
}
return Base::retSuccess("创建成功。", $data);
}
/**
* 获取可选的 webhook 事件
*
* @return string[]
*/
public static function webhookEventOptions(): array
{
return [
self::WEBHOOK_EVENT_MESSAGE,
self::WEBHOOK_EVENT_DIALOG_OPEN,
self::WEBHOOK_EVENT_MEMBER_JOIN,
self::WEBHOOK_EVENT_MEMBER_LEAVE,
];
}
/**
* 标准化 webhook 事件配置
*
* @param mixed $events
* @param bool $useFallback
* @return array
*/
public static function normalizeWebhookEvents(mixed $events, bool $useFallback = true): array
{
if (is_string($events)) {
$events = Base::json2array($events);
}
if ($events === null) {
$events = [];
}
if (!is_array($events)) {
$events = [$events];
}
$events = array_filter(array_map('strval', $events));
$events = array_values(array_intersect($events, self::webhookEventOptions()));
return $events ?: ($useFallback ? [self::WEBHOOK_EVENT_MESSAGE] : []);
}
}

View File

@@ -88,16 +88,32 @@ class UserCheckinRecord extends AbstractModel
/**
* 时间收集
* @param string $data
* @param array $times
* @param string $data 日期
* @param array $times 签到时间数组
* @param string|null $shiftStart 班次开始时间(如 "09:00"),用于判断跨天
* @return \Illuminate\Support\Collection
*/
public static function atCollect($data, $times)
public static function atCollect($data, $times, $shiftStart = null)
{
$sameTimes = array_map(function($time) use ($data) {
$shiftStartMinutes = null;
if ($shiftStart) {
$parts = explode(':', $shiftStart);
$shiftStartMinutes = intval($parts[0]) * 60 + intval($parts[1]);
}
$sameTimes = array_map(function($time) use ($data, $shiftStartMinutes) {
$parts = explode(':', $time);
$timeMinutes = intval($parts[0]) * 60 + intval($parts[1]);
// 如果签到时间早于班次开始时间,视为跨天打卡(属于次日凌晨)
$targetDate = $data;
if ($shiftStartMinutes !== null && $timeMinutes < $shiftStartMinutes) {
$targetDate = date("Y-m-d", strtotime($data . " +1 day"));
}
return [
"datetime" => "{$data} {$time}",
"timestamp" => strtotime("{$data} {$time}")
"datetime" => "{$targetDate} {$time}",
"timestamp" => strtotime("{$targetDate} {$time}")
];
}, $times);
return collect($sameTimes);

115
app/Models/UserTag.php Normal file
View File

@@ -0,0 +1,115 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
/**
* App\Models\UserTag
*
* @property int $id
* @property int $user_id 被标签用户ID
* @property string $name 标签名称
* @property int $created_by 创建人
* @property int|null $updated_by 最后更新人
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\User $creator
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\UserTagRecognition> $recognitions
* @property-read int|null $recognitions_count
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
* @method static \Illuminate\Database\Eloquent\Builder|UserTag newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserTag newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserTag query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|UserTag whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTag whereCreatedBy($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTag whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTag whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTag whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTag whereUpdatedBy($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTag whereUserId($value)
* @mixin \Eloquent
*/
class UserTag extends AbstractModel
{
protected $table = 'user_tags';
protected $fillable = [
'user_id',
'name',
'created_by',
'updated_by',
];
public function creator(): BelongsTo
{
return $this->belongsTo(User::class, 'created_by', 'userid')
->select(['userid', 'nickname']);
}
public function recognitions(): HasMany
{
return $this->hasMany(UserTagRecognition::class, 'tag_id');
}
public function canManage(User $viewer): bool
{
return $viewer->isAdmin()
|| $viewer->userid === $this->user_id
|| $viewer->userid === $this->created_by;
}
public static function listWithMeta(int $targetUserId, ?User $viewer): array
{
$query = static::query()
->where('user_id', $targetUserId)
->with(['creator'])
->withCount(['recognitions as recognition_total'])
->orderByDesc('recognition_total')
->orderBy('id');
$tags = $query->get();
$viewerId = $viewer?->userid ?? 0;
$viewerIsAdmin = $viewer?->isAdmin() ?? false;
$viewerIsOwner = $viewerId > 0 && $viewerId === $targetUserId;
$recognizedIds = [];
if ($viewerId > 0 && $tags->isNotEmpty()) {
$recognizedIds = UserTagRecognition::query()
->where('user_id', $viewerId)
->whereIn('tag_id', $tags->pluck('id'))
->pluck('tag_id')
->all();
}
$recognizedLookup = array_flip($recognizedIds);
$list = $tags->map(function (self $tag) use ($viewerId, $viewerIsAdmin, $viewerIsOwner, $recognizedLookup) {
$canManage = $viewerIsAdmin || $viewerIsOwner || $viewerId === $tag->created_by;
return [
'id' => $tag->id,
'user_id' => $tag->user_id,
'name' => $tag->name,
'created_by' => $tag->created_by,
'created_by_name' => $tag->creator?->nickname ?: '',
'recognition_total' => (int) $tag->recognition_total,
'recognized' => isset($recognizedLookup[$tag->id]),
'can_edit' => $canManage,
'can_delete' => $canManage,
];
})->values()->toArray();
return [
'list' => $list,
'top' => array_slice($list, 0, 10),
'total' => count($list),
];
}
}

View File

@@ -0,0 +1,52 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* App\Models\UserTagRecognition
*
* @property int $id
* @property int $tag_id 标签ID
* @property int $user_id 认可人ID
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \App\Models\UserTag $tag
* @property-read \App\Models\User $user
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel getKeyValue()
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition whereTagId($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|UserTagRecognition whereUserId($value)
* @mixin \Eloquent
*/
class UserTagRecognition extends AbstractModel
{
protected $table = 'user_tag_recognitions';
protected $fillable = [
'tag_id',
'user_id',
];
public function tag(): BelongsTo
{
return $this->belongsTo(UserTag::class, 'tag_id');
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id', 'userid')
->select(['userid', 'nickname']);
}
}

View File

@@ -10,6 +10,7 @@ namespace App\Models;
* @property string $key
* @property string|null $fd
* @property string|null $path
* @property string|null $platform 平台类型android, ios, win, mac, web
* @property int|null $userid
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
@@ -27,6 +28,7 @@ namespace App\Models;
* @method static \Illuminate\Database\Eloquent\Builder|WebSocket whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocket whereKey($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocket wherePath($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocket wherePlatform($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocket whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocket whereUserid($value)
* @mixin \Eloquent

View File

@@ -530,6 +530,7 @@ class WebSocketDialog extends AbstractModel
}
}
//
$item->operator_id = User::userid();
$item->delete();
//
if ($pushMsg) {
@@ -551,6 +552,42 @@ class WebSocketDialog extends AbstractModel
$this->pushMsg("groupUpdate", $data);
}
/**
* 推送成员事件到机器人 webhook
* @param string $event
* @param int $memberId
* @param int $operatorId
* @return void
*/
public function dispatchMemberWebhook(string $event, int $memberId, int $operatorId): void
{
$botIds = $this->dialogUser()->where('bot', 1)->pluck('userid')->toArray();
if (empty($botIds)) {
return;
}
$userBots = UserBot::whereIn('bot_id', $botIds)->get();
if ($userBots->isEmpty()) {
return;
}
$member = User::find($memberId, ['userid', 'nickname', 'email', 'bot'])?->toArray();
$operator = $operatorId === $memberId ? $member : User::find($operatorId, ['userid', 'nickname', 'email', 'bot'])?->toArray();
$payload = [
'dialog_id' => $this->id,
'dialog_type' => $this->type,
'group_type' => $this->group_type,
'dialog_name' => $this->getGroupName(),
'member' => $member,
'operator' => $operator,
];
foreach ($userBots as $userBot) {
$userBot->dispatchWebhook($event, $payload, 10);
}
}
/**
* 删除会话
* @return bool

View File

@@ -44,6 +44,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @property-read int|mixed $percentage
* @property-read \App\Models\User|null $user
* @property-read \App\Models\WebSocketDialog|null $webSocketDialog
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg accessibleByUser(int $userid)
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelAppend()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel cancelHidden()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel change($array)
@@ -54,6 +55,7 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg query()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel remove()
* @method static \Illuminate\Database\Eloquent\Builder|AbstractModel saveOrIgnore()
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg searchByKeyword(string $keyword)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereBot($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|WebSocketDialogMsg whereDeletedAt($value)
@@ -111,6 +113,36 @@ class WebSocketDialogMsg extends AbstractModel
return $this->hasOne(User::class, 'userid', 'userid');
}
/**
* 按关键词搜索消息Scope
* 搜索 key 字段(消息的可搜索内容)
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $keyword 搜索关键词
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeSearchByKeyword($query, string $keyword)
{
return $query->where('key', 'like', "%{$keyword}%");
}
/**
* 筛选用户可访问的对话消息Scope
* 通过 web_socket_dialog_users 表验证用户对对话的访问权限
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $userid 用户ID
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeAccessibleByUser($query, int $userid)
{
return $query->whereIn('dialog_id', function ($subQuery) use ($userid) {
$subQuery->select('dialog_id')
->from('web_socket_dialog_users')
->where('userid', $userid);
});
}
/**
* 阅读占比
* @return int|mixed
@@ -683,6 +715,7 @@ class WebSocketDialogMsg extends AbstractModel
$text = $msgData['text'] ?? '';
if (!$text) return '';
if ($msgData['type'] === 'md') {
$text = preg_replace('/<\/?tool-use[^>]*>/', '', $text);
$text = preg_replace("/:::\s*reasoning[\s\S]*?:::/", "", $text);
if (preg_match('/:::\s*reasoning\s+/', $text)) {
return Doo::translate('思考中...');
@@ -695,7 +728,6 @@ class WebSocketDialogMsg extends AbstractModel
$text = $title;
} else {
$text = Base::markdown2html($text);
$text = self::previewConvertTaskList($text);
}
}
$text = preg_replace("/<img\s+class=\"emoticon\"[^>]*?alt=\"(\S+)\"[^>]*?>/", "[$1]", $text);
@@ -711,36 +743,6 @@ class WebSocketDialogMsg extends AbstractModel
return $text;
}
/**
* 转换任务列表
* @param $text
* @return array|string|string[]|null
*/
private static function previewConvertTaskList($text) {
$pattern = '/:::\s*(create-task-list|create-subtask-list)(.*?):::/s';
$replacement = function($matches) {
$content = $matches[2];
$lines = explode("\n", trim($content));
$result = [];
$currentTitle = '';
foreach ($lines as $line) {
$line = trim($line);
if (empty($line)) continue;
if (preg_match('/^title:\s*(.+)$/', $line, $titleMatch)) {
$currentTitle = $titleMatch[1];
$result[] = $currentTitle;
} elseif (preg_match('/^desc:\s*(.+)$/', $line, $descMatch)) {
if (!empty($currentTitle)) {
$result[] = $descMatch[1];
}
}
}
return implode("\n", $result);
};
return preg_replace_callback($pattern, $replacement, $text);
}
/**
* 预览文件消息
* @param $msg
@@ -865,8 +867,7 @@ class WebSocketDialogMsg extends AbstractModel
switch ($this->type) {
case "file":
// 提取文件消息
$msgData = Base::json2array($this->getRawOriginal('msg'));
$result = $this->convertMentionFormat("path", $msgData['path'], $msgData['name'], $reserves);
$result = " 文件:{$this->msg['name']}(大小:{$this->msg['size']}BURL{$this->msg['path']} ";
break;
case "text":
@@ -889,29 +890,31 @@ class WebSocketDialogMsg extends AbstractModel
// 提及任务、文件、报告
$result = preg_replace_callback_array([
// 用户
"/<span class=\"mention user\" data-id=\"(\d+)\">(.*?)<\/span>/" => function () {
"/<span class=\"mention user\" data-id=\"(\d+)\">(.*?)<\/span>/" => function ($match) {
return "";
},
// 任务
"/<span class=\"mention task\" data-id=\"(\d+)\">#?(.*?)<\/span>/" => function ($match) use (&$reserves) {
return $this->convertMentionFormat("task", $match[1], $match[2], $reserves);
"/<span class=\"mention task\" data-id=\"(\d+)\">#?(.*?)<\/span>/" => function ($match) {
return " 任务:{$match[2]} (任务ID{$match[1]}) ";
},
// 文件
"/<a class=\"mention file\" href=\"([^\"']+?)\"[^>]*?>~?(.*?)<\/a>/" => function ($match) use (&$reserves) {
$idOrCode = "";
if (preg_match("/single\/file\/(.*?)$/", $match[1], $subMatch)) {
return $this->convertMentionFormat("file", $subMatch[1], $match[2], $reserves);
$idOrCode = " (" . (Base::isNumber($subMatch[1]) ? "文件ID{$subMatch[1]}" : "文件分享码:{$subMatch[1]}") . ")";
}
return "";
return " 文件:{$match[2]}{$idOrCode} ";
},
// 报告
"/<a class=\"mention report\" href=\"([^\"']+?)\"[^>]*?>%?(.*?)<\/a>/" => function ($match) use (&$reserves) {
$idOrCode = "";
if (preg_match("/single\/report\/detail\/(.*?)$/", $match[1], $subMatch)) {
return $this->convertMentionFormat("report", $subMatch[1], $match[2], $reserves);
$idOrCode = " (" . (Base::isNumber($subMatch[1]) ? "报告ID{$subMatch[1]}" : "报告分享码:{$subMatch[1]}") . ")";
}
return "";
return " 工作汇报:{$match[2]}{$idOrCode} ";
},
], $result);
@@ -926,35 +929,15 @@ class WebSocketDialogMsg extends AbstractModel
return '';
}
// 处理 reserves
foreach ($reserves as $rand => $mention) {
$result = str_replace($rand, $mention, $result);
}
// 截取最大长度
if ($maxLength > 0 && mb_strlen($result) > $maxLength) {
$result = mb_substr($result, 0, $maxLength);
}
return $result;
}
// 规范以斜杠开头的命令
$result = preg_replace('/^\s*\\//', '/', $result);
/**
* 转换提及消息格式
* 将提及的任务、文件、报告等转换为统一的格式 [type#key#name]
*
* @param string $type 提及类型task、file、report、path
* @param string $key 提及对象的唯一标识
* @param string $name 提及对象的显示名称
* @return string 格式化后的提及字符串
*/
private function convertMentionFormat($type, $key, $name, &$reserves)
{
$key = str_replace(['#', '-->'], '', $key);
$name = str_replace(['#', '-->'], '', $name);
$rand = Base::generatePassword(12);
$reserves[$rand] = "<!--{$type}#{$key}#{$name}-->";
return $rand;
return $result;
}
/**

View File

@@ -121,4 +121,30 @@ class WebSocketDialogMsgRead extends AbstractModel
DB::update($sql, $bindings);
}
}
/**
* 标记指定会话的历史消息为已读
* @param int $dialogId
* @param int $sessionId
* @param int $chunkSize
* @return void
*/
public static function markSessionMessagesAsRead(int $dialogId, int $sessionId, int $chunkSize = 100): void
{
if ($dialogId <= 0 || $sessionId <= 0) {
return;
}
self::whereDialogId($dialogId)
->whereNull('read_at')
->whereIn('msg_id', function ($query) use ($dialogId, $sessionId) {
$query->select('id')
->from((new WebSocketDialogMsg())->getTable())
->where('dialog_id', $dialogId)
->where('session_id', $sessionId);
})
->chunkById($chunkSize, function ($list) {
self::onlyMarkRead($list);
});
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -3,8 +3,12 @@
namespace App\Module;
use App\Exceptions\ApiException;
use App\Models\User;
use App\Models\UserDepartment;
use App\Services\RequestContext;
use Symfony\Component\Yaml\Yaml;
use App\Module\Base;
use App\Module\Ihttp;
class Apps
{
@@ -22,7 +26,7 @@ class Apps
$key = 'app_installed_' . $appId;
if (RequestContext::has($key)) {
return RequestContext::get($key);
return (bool) RequestContext::get($key, false);
}
$configFile = base_path('docker/appstore/config/' . $appId . '/config.yml');
@@ -44,17 +48,84 @@ class Apps
{
if (!self::isInstalled($appId)) {
$name = match ($appId) {
'ai' => 'AI Robot',
'ai' => 'AI Assistant',
'face' => 'Face check-in',
'appstore' => 'AppStore',
'approve' => 'Approval',
'office' => 'OnlyOffice',
'drawio' => 'Drawio',
'minder' => 'Minder',
'search' => 'ZincSearch',
'manticore' => 'Manticore Search',
default => $appId,
};
throw new ApiException("应用「{$name}」未安装", [], 0, false);
}
}
/**
* Dispatch user lifecycle hook to appstore (user_onboard/user_offboard/user_update).
*
* @param User $user 用户对象
* @param string $action Hook 动作: user_onboard, user_offboard, user_update
* @param string $eventType 事件类型: onboard, restore, offboarded, delete, profile_update, admin_update
* @param array $changedFields 变更字段列表(仅 user_update 时有值)
*/
public static function dispatchUserHook(User $user, string $action, string $eventType = '', array $changedFields = []): void
{
$appKey = env('APP_KEY', '');
if (empty($appKey)) {
info('[appstore_hook] APP_KEY is empty, skip dispatchUserHook');
return;
}
// 获取用户部门信息
$departments = [];
if (!empty($user->department)) {
$deptIds = is_array($user->department)
? $user->department
: array_filter(explode(',', $user->department));
if (!empty($deptIds)) {
$deptList = UserDepartment::whereIn('id', $deptIds)->get(['id', 'name']);
foreach ($deptList as $dept) {
$departments[] = [
'id' => (string) $dept->id,
'name' => (string) $dept->name,
];
}
}
}
$url = sprintf('http://appstore/api/v1/internal/hooks/%s', $action);
$payload = [
'user' => [
'id' => (string) $user->userid,
'email' => (string) $user->email,
'name' => (string) $user->nickname,
'role' => $user->isAdmin() ? 'admin' : 'normal',
'tel' => (string) ($user->tel ?? ''),
'profession' => (string) ($user->profession ?? ''),
'birthday' => $user->birthday ? (string) $user->birthday : '',
'address' => (string) ($user->address ?? ''),
'introduction' => (string) ($user->introduction ?? ''),
'departments' => $departments,
],
'event_type' => $eventType,
'changed_fields' => $changedFields,
];
$headers = [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . md5($appKey),
'Version' => Base::getVersion(),
];
$resp = Ihttp::ihttp_request($url, json_encode($payload, JSON_UNESCAPED_UNICODE), $headers, 5);
if (Base::isError($resp)) {
info('[appstore_hook] dispatch fail', [
'url' => $url,
'payload' => $payload,
'error' => $resp,
]);
}
}
}

View File

@@ -1301,7 +1301,7 @@ class Base
/**
* 获取或设置
* @param $setname // 配置名称
* @param bool $array // 保存内容
* @param bool|array $array // 保存内容
* @param bool $isUpdate // 保存内容为更新模式,默认否
* @return array
*/
@@ -1827,6 +1827,19 @@ class Base
return $platform;
}
/**
* 是否是PC端包括 Electron 桌面端和 Web 浏览器)
* @param string|null $platform 平台类型,不传则自动获取
* @return bool
*/
public static function isPc($platform = null)
{
if ($platform === null) {
$platform = self::platform();
}
return in_array($platform, ['win', 'mac', 'web']);
}
/**
* 是否是App移动端
* @return bool

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,575 @@
<?php
namespace App\Module\Manticore;
use App\Models\File;
use App\Models\FileContent;
use App\Models\FileUser;
use App\Module\Apps;
use App\Module\Base;
use App\Module\TextExtractor;
use App\Module\AI;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
/**
* Manticore Search 文件搜索类
*
* 使用方法:
*
* 1. 搜索方法
* - 搜索文件: search($userid, $keyword, $searchType, $from, $size);
*
* 2. 同步方法
* - 单个同步: sync(File $file);
* - 批量同步: batchSync($files);
* - 删除索引: delete($fileId);
*
* 3. 权限更新方法
* - 更新权限: updateAllowedUsers($fileId);
*
* 4. 工具方法
* - 清空索引: clear();
*/
class ManticoreFile
{
/**
* 可搜索的文件类型
*/
public const SEARCHABLE_TYPES = ['document', 'word', 'excel', 'ppt', 'txt', 'md', 'text', 'code'];
/**
* 最大内容长度(字符)- 提取后的文本内容限制
*/
public const MAX_CONTENT_LENGTH = 100000; // 100K 字符
/**
* 不同文件类型的最大大小限制(字节)
*/
public const MAX_FILE_SIZE = [
'office' => 50 * 1024 * 1024, // 50MB - Office 文件图片占空间大但文本少
'text' => 5 * 1024 * 1024, // 5MB - 纯文本文件
'other' => 20 * 1024 * 1024, // 20MB - PDF 等其他文件
];
/**
* Office 文件扩展名
*/
public const OFFICE_EXTENSIONS = [
'doc', 'docx', 'dot', 'dotx', 'odt', 'ott', 'rtf',
'xls', 'xlsx', 'xlsm', 'xlt', 'xltx', 'ods', 'ots', 'csv', 'tsv',
'ppt', 'pptx', 'pps', 'ppsx', 'odp', 'otp'
];
/**
* 纯文本文件扩展名
*/
public const TEXT_EXTENSIONS = [
'txt', 'md', 'text', 'log', 'json', 'xml', 'html', 'htm', 'css', 'js', 'ts',
'php', 'py', 'java', 'c', 'cpp', 'h', 'go', 'rs', 'rb', 'sh', 'bash', 'sql',
'yaml', 'yml', 'ini', 'conf', 'vue', 'jsx', 'tsx'
];
/**
* 搜索文件(支持全文、向量、混合搜索)
*
* @param int $userid 用户ID
* @param string $keyword 搜索关键词
* @param string $searchType 搜索类型: text/vector/hybrid
* @param int $from 起始位置
* @param int $size 返回数量
* @return array 搜索结果
*/
public static function search(int $userid, string $keyword, string $searchType = 'hybrid', int $from = 0, int $size = 20): array
{
if (empty($keyword)) {
return [];
}
if (!Apps::isInstalled("search")) {
// 未安装 Manticore降级到 MySQL LIKE 搜索
return self::searchByMysql($userid, $keyword, $from, $size);
}
try {
switch ($searchType) {
case 'text':
// 纯全文搜索
return self::formatSearchResults(
ManticoreBase::fullTextSearch($keyword, $userid, $size, $from)
);
case 'vector':
// 纯向量搜索(需要先获取 embedding
$embedding = ManticoreBase::getEmbedding($keyword);
if (empty($embedding)) {
// embedding 获取失败,降级到全文搜索
return self::formatSearchResults(
ManticoreBase::fullTextSearch($keyword, $userid, $size, $from)
);
}
return self::formatSearchResults(
ManticoreBase::vectorSearch($embedding, $userid, $size)
);
case 'hybrid':
default:
// 混合搜索
$embedding = ManticoreBase::getEmbedding($keyword);
return self::formatSearchResults(
ManticoreBase::hybridSearch($keyword, $embedding, $userid, $size)
);
}
} catch (\Exception $e) {
Log::error('Manticore search error: ' . $e->getMessage());
return self::searchByMysql($userid, $keyword, $from, $size);
}
}
/**
* 格式化搜索结果
*
* @param array $results Manticore 返回的结果
* @return array 格式化后的结果
*/
private static function formatSearchResults(array $results): array
{
$formatted = [];
foreach ($results as $item) {
$formatted[] = [
'id' => $item['file_id'],
'file_id' => $item['file_id'],
'name' => $item['file_name'],
'type' => $item['file_type'],
'ext' => $item['file_ext'],
'userid' => $item['userid'],
'content_preview' => isset($item['content']) ? mb_substr($item['content'], 0, 500) : null,
'relevance' => $item['relevance'] ?? $item['similarity'] ?? $item['rrf_score'] ?? 0,
];
}
return $formatted;
}
/**
* MySQL 降级搜索(仅搜索文件名)
*
* @param int $userid 用户ID
* @param string $keyword 关键词
* @param int $from 起始位置
* @param int $size 返回数量
* @return array 搜索结果
*/
private static function searchByMysql(int $userid, string $keyword, int $from, int $size): array
{
// 搜索用户自己的文件
$builder = File::where('userid', $userid)
->where('name', 'like', "%{$keyword}%")
->where('type', '!=', 'folder');
$results = $builder->skip($from)->take($size)->get();
return $results->map(function ($file) {
return [
'id' => $file->id,
'file_id' => $file->id,
'name' => $file->name,
'type' => $file->type,
'ext' => $file->ext,
'userid' => $file->userid,
'content_preview' => null,
'relevance' => 0,
];
})->toArray();
}
// ==============================
// 权限计算方法
// ==============================
/**
* 获取文件的 allowed_users 列表
*
* 有权限查看此文件的用户列表:
* - 文件所有者 (userid)
* - 共享用户FileUser 表中的 userid
* - userid=0 表示公开共享
*
* @param File $file 文件模型
* @return array 有权限的用户ID数组
*/
public static function getAllowedUsers(File $file): array
{
$userids = [$file->userid]; // 所有者
// 获取共享用户(包括 userid=0 表示公开)
$shareUsers = FileUser::where('file_id', $file->id)
->pluck('userid')
->toArray();
return array_unique(array_merge($userids, $shareUsers));
}
// ==============================
// 同步方法
// ==============================
/**
* 同步单个文件到 Manticore含 allowed_users
*
* @param File $file 文件模型
* @param bool $withVector 是否同时生成向量(默认 false向量由后台任务生成
* @return bool 是否成功
*/
public static function sync(File $file, bool $withVector = false): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
// 不处理文件夹
if ($file->type === 'folder') {
return true;
}
// 根据文件类型检查大小限制
$maxSize = self::getMaxFileSizeByExt($file->ext);
if ($file->size > $maxSize) {
// 删除可能存在的旧索引(文件更新后可能超限)
self::delete($file->id);
return true;
}
try {
// 提取文件内容
$content = self::extractFileContent($file);
// 限制提取后的内容长度
$content = mb_substr($content, 0, self::MAX_CONTENT_LENGTH);
// 只有明确要求时才生成向量(默认不生成,由后台任务处理)
$embedding = null;
if ($withVector && Apps::isInstalled('ai')) {
// 向量内容包含文件名和文件内容
$vectorContent = self::buildVectorContent($file->name, $content);
if (!empty($vectorContent)) {
$embeddingResult = ManticoreBase::getEmbedding($vectorContent);
if (!empty($embeddingResult)) {
$embedding = '[' . implode(',', $embeddingResult) . ']';
}
}
}
// 获取文件的 allowed_users
$allowedUsers = self::getAllowedUsers($file);
// 写入 Manticore含 allowed_users
$result = ManticoreBase::upsertFileVector([
'file_id' => $file->id,
'userid' => $file->userid,
'pshare' => $file->pshare ?? 0,
'file_name' => $file->name,
'file_type' => $file->type,
'file_ext' => $file->ext,
'content' => $content,
'content_vector' => $embedding,
'allowed_users' => $allowedUsers,
]);
return $result;
} catch (\Exception $e) {
Log::error('Manticore sync error: ' . $e->getMessage(), [
'file_id' => $file->id,
'file_name' => $file->name,
]);
return false;
}
}
/**
* 根据文件扩展名获取最大文件大小限制
*
* @param string|null $ext 文件扩展名
* @return int 最大文件大小(字节)
*/
private static function getMaxFileSizeByExt(?string $ext): int
{
$ext = strtolower($ext ?? '');
if (in_array($ext, self::OFFICE_EXTENSIONS)) {
return self::MAX_FILE_SIZE['office'];
}
if (in_array($ext, self::TEXT_EXTENSIONS)) {
return self::MAX_FILE_SIZE['text'];
}
return self::MAX_FILE_SIZE['other'];
}
/**
* 获取所有文件类型中的最大文件大小限制
*
* @return int 最大文件大小(字节)
*/
public static function getMaxFileSize(): int
{
return max(self::MAX_FILE_SIZE);
}
/**
* 批量同步文件
*
* @param iterable $files 文件列表
* @param bool $withVector 是否同时生成向量
* @return int 成功同步的数量
*/
public static function batchSync(iterable $files, bool $withVector = false): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
$count = 0;
foreach ($files as $file) {
if (self::sync($file, $withVector)) {
$count++;
}
}
return $count;
}
/**
* 删除文件索引
*
* @param int $fileId 文件ID
* @return bool 是否成功
*/
public static function delete(int $fileId): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::deleteFileVector($fileId);
}
/**
* 提取文件内容
*
* @param File $file 文件模型
* @return string 文件内容文本
*/
private static function extractFileContent(File $file): string
{
// 1. 先尝试从 FileContent 的 text 字段获取(已提取的文本内容)
$fileContent = FileContent::where('fid', $file->id)->orderByDesc('id')->first();
if ($fileContent && !empty($fileContent->text)) {
return $fileContent->text;
}
// 2. 尝试从 FileContent 的 content 字段获取
if ($fileContent && !empty($fileContent->content)) {
$contentData = Base::json2array($fileContent->content);
// 2.1 某些文件类型直接存储内容
if (!empty($contentData['content'])) {
return is_string($contentData['content']) ? $contentData['content'] : '';
}
// 2.2 尝试使用 TextExtractor 提取文件内容
$filePath = $contentData['url'] ?? null;
if ($filePath && str_starts_with($filePath, 'uploads/')) {
$fullPath = public_path($filePath);
if (file_exists($fullPath)) {
// 根据文件类型设置不同的大小限制
$ext = strtolower(pathinfo($fullPath, PATHINFO_EXTENSION));
$maxFileSize = self::getMaxFileSizeByExt($ext);
$maxContentSize = self::MAX_CONTENT_LENGTH;
$result = TextExtractor::extractFile(
$fullPath,
(int) ($maxFileSize / 1024), // 转换为 KB
(int) ($maxContentSize / 1024) // 转换为 KB
);
if (Base::isSuccess($result)) {
return $result['data'] ?? '';
}
}
}
}
return '';
}
/**
* 构建用于生成向量的内容
* 包含文件名和文件内容,确保语义搜索能匹配文件名
*
* @param string $fileName 文件名
* @param string $content 文件内容
* @return string 用于生成向量的文本
*/
private static function buildVectorContent(string $fileName, string $content): string
{
$parts = [];
if (!empty($fileName)) {
$parts[] = $fileName;
}
if (!empty($content)) {
$parts[] = $content;
}
return implode(' ', $parts);
}
/**
* 清空所有索引
*
* @return bool 是否成功
*/
public static function clear(): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::clearAllFileVectors();
}
/**
* 获取已索引文件数量
*
* @return int 数量
*/
public static function getIndexedCount(): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
return ManticoreBase::getIndexedFileCount();
}
// ==============================
// 权限更新方法
// ==============================
/**
* 更新文件的 allowed_users 权限列表
* 从 MySQL 获取最新的共享用户并更新到 Manticore
*
* @param int $fileId 文件ID
* @return bool 是否成功
*/
public static function updateAllowedUsers(int $fileId): bool
{
if (!Apps::isInstalled("search") || $fileId <= 0) {
return false;
}
try {
$file = File::find($fileId);
if (!$file) {
return false;
}
$userids = self::getAllowedUsers($file);
return ManticoreBase::updateFileAllowedUsers($fileId, $userids);
} catch (\Exception $e) {
Log::error('Manticore updateAllowedUsers error: ' . $e->getMessage(), ['file_id' => $fileId]);
return false;
}
}
// ==============================
// 批量向量生成方法
// ==============================
/**
* 批量生成文件向量
* 用于后台异步处理,将已索引文件的向量批量生成
*
* @param array $fileIds 文件ID数组
* @param int $batchSize 每批 embedding 数量默认20
* @return int 成功处理的数量
*/
public static function generateVectorsBatch(array $fileIds, int $batchSize = 20): int
{
if (!Apps::isInstalled("search") || !Apps::isInstalled("ai") || empty($fileIds)) {
return 0;
}
try {
// 1. 查询文件信息
$files = File::whereIn('id', $fileIds)
->where('type', '!=', 'folder')
->get();
if ($files->isEmpty()) {
return 0;
}
// 2. 提取每个文件的内容(包含文件名)
$fileContents = [];
foreach ($files as $file) {
// 检查文件大小限制
$maxSize = self::getMaxFileSizeByExt($file->ext);
if ($file->size > $maxSize) {
continue;
}
$content = self::extractFileContent($file);
// 向量内容包含文件名和文件内容
$vectorContent = self::buildVectorContent($file->name, $content);
if (!empty($vectorContent)) {
// 限制内容长度
$vectorContent = mb_substr($vectorContent, 0, self::MAX_CONTENT_LENGTH);
$fileContents[$file->id] = $vectorContent;
}
}
if (empty($fileContents)) {
return 0;
}
// 3. 分批处理
$successCount = 0;
$chunks = array_chunk($fileContents, $batchSize, true);
foreach ($chunks as $chunk) {
$texts = array_values($chunk);
$ids = array_keys($chunk);
// 4. 批量获取 embedding
$result = AI::getBatchEmbeddings($texts);
if (!Base::isSuccess($result) || empty($result['data'])) {
continue;
}
$embeddings = $result['data'];
// 5. 构建批量更新数据
$vectorData = [];
foreach ($ids as $index => $fileId) {
if (!isset($embeddings[$index]) || empty($embeddings[$index])) {
continue;
}
$vectorData[$fileId] = '[' . implode(',', $embeddings[$index]) . ']';
}
// 6. 批量更新向量
if (!empty($vectorData)) {
$batchCount = ManticoreBase::batchUpdateFileVectors($vectorData);
$successCount += $batchCount;
}
}
return $successCount;
} catch (\Exception $e) {
Log::error('ManticoreFile generateVectorsBatch error: ' . $e->getMessage());
return 0;
}
}
}

View File

@@ -0,0 +1,139 @@
<?php
namespace App\Module\Manticore;
use App\Module\Apps;
use Illuminate\Support\Facades\Log;
/**
* Manticore Search 键值存储类
*
* 用于存储同步进度等配置信息
*/
class ManticoreKeyValue
{
/**
* 获取值
*
* @param string $key 键
* @param mixed $default 默认值
* @return mixed 值
*/
public static function get(string $key, $default = null)
{
if (!Apps::isInstalled("search")) {
return $default;
}
$instance = new ManticoreBase();
$result = $instance->queryOne(
"SELECT v FROM key_values WHERE k = ?",
[$key]
);
return $result ? $result['v'] : $default;
}
/**
* 设置值
*
* @param string $key 键
* @param mixed $value 值
* @return bool 是否成功
*/
public static function set(string $key, $value): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
$instance = new ManticoreBase();
// 先删除已存在的记录
$instance->execute("DELETE FROM key_values WHERE k = ?", [$key]);
// 生成唯一 ID基于 key 的 hash
$id = abs(crc32($key));
// 插入新记录
return $instance->execute(
"INSERT INTO key_values (id, k, v) VALUES (?, ?, ?)",
[$id, $key, (string)$value]
);
}
/**
* 删除值
*
* @param string $key 键
* @return bool 是否成功
*/
public static function delete(string $key): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
$instance = new ManticoreBase();
return $instance->execute("DELETE FROM key_values WHERE k = ?", [$key]);
}
/**
* 清空所有键值
*
* @return bool 是否成功
*/
public static function clear(): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
$instance = new ManticoreBase();
return $instance->execute("TRUNCATE TABLE key_values");
}
/**
* 检查键是否存在
*
* @param string $key 键
* @return bool 是否存在
*/
public static function exists(string $key): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
$instance = new ManticoreBase();
$result = $instance->queryOne(
"SELECT id FROM key_values WHERE k = ?",
[$key]
);
return $result !== null;
}
/**
* 获取所有键值对
*
* @return array 键值对数组
*/
public static function all(): array
{
if (!Apps::isInstalled("search")) {
return [];
}
$instance = new ManticoreBase();
$results = $instance->query("SELECT k, v FROM key_values");
$data = [];
foreach ($results as $row) {
$data[$row['k']] = $row['v'];
}
return $data;
}
}

View File

@@ -0,0 +1,561 @@
<?php
namespace App\Module\Manticore;
use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogUser;
use App\Module\Apps;
use App\Module\Base;
use App\Module\AI;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Log;
/**
* Manticore Search 消息搜索类
*
* 使用方法:
*
* 1. 搜索方法
* - 搜索消息: search($userid, $keyword, $searchType, $from, $size);
*
* 2. 同步方法
* - 单个同步: sync(WebSocketDialogMsg $msg);
* - 批量同步: batchSync($msgs);
* - 删除索引: delete($msgId);
*
* 3. 权限更新方法
* - 更新对话权限: updateDialogAllowedUsers($dialogId);
*
* 4. 工具方法
* - 清空索引: clear();
* - 判断是否索引: shouldIndex($msg);
*/
class ManticoreMsg
{
/**
* 可索引的消息类型
*/
public const INDEXABLE_TYPES = ['text', 'file', 'record', 'meeting', 'vote'];
/**
* 最大内容长度(字符)
*/
public const MAX_CONTENT_LENGTH = 50000; // 50K 字符
/**
* 判断消息是否应该被索引
*
* @param WebSocketDialogMsg $msg 消息模型
* @return bool 是否应该索引
*/
public static function shouldIndex(WebSocketDialogMsg $msg): bool
{
// 1. 排除机器人消息
if ($msg->bot === 1) {
return false;
}
// 2. 检查消息类型
if (!in_array($msg->type, self::INDEXABLE_TYPES)) {
return false;
}
// 3. 排除 key 为空的消息
if (empty($msg->key)) {
return false;
}
return true;
}
/**
* 搜索消息(支持全文、向量、混合搜索)
*
* @param int $userid 用户ID
* @param string $keyword 搜索关键词
* @param string $searchType 搜索类型: text/vector/hybrid
* @param int $from 起始位置
* @param int $size 返回数量
* @param int $dialogId 对话ID0表示不限制
* @return array 搜索结果
*/
public static function search(int $userid, string $keyword, string $searchType = 'hybrid', int $from = 0, int $size = 20, int $dialogId = 0): array
{
if (empty($keyword)) {
return [];
}
if (!Apps::isInstalled("search")) {
return [];
}
try {
switch ($searchType) {
case 'text':
// 纯全文搜索
return self::formatSearchResults(
ManticoreBase::msgFullTextSearch($keyword, $userid, $size, $from, $dialogId)
);
case 'vector':
// 纯向量搜索(需要先获取 embedding
$embedding = ManticoreBase::getEmbedding($keyword);
if (empty($embedding)) {
// embedding 获取失败,降级到全文搜索
return self::formatSearchResults(
ManticoreBase::msgFullTextSearch($keyword, $userid, $size, $from, $dialogId)
);
}
return self::formatSearchResults(
ManticoreBase::msgVectorSearch($embedding, $userid, $size, $dialogId)
);
case 'hybrid':
default:
// 混合搜索
$embedding = ManticoreBase::getEmbedding($keyword);
return self::formatSearchResults(
ManticoreBase::msgHybridSearch($keyword, $embedding, $userid, $size, $dialogId)
);
}
} catch (\Exception $e) {
Log::error('Manticore msg search error: ' . $e->getMessage());
return [];
}
}
/**
* 格式化搜索结果
*
* @param array $results Manticore 返回的结果
* @return array 格式化后的结果
*/
private static function formatSearchResults(array $results): array
{
$formatted = [];
foreach ($results as $item) {
$formatted[] = [
'id' => $item['msg_id'],
'msg_id' => $item['msg_id'],
'dialog_id' => $item['dialog_id'],
'userid' => $item['userid'],
'msg_type' => $item['msg_type'],
'content_preview' => isset($item['content']) ? mb_substr($item['content'], 0, 200) : null,
'created_at' => $item['created_at'] ?? null,
'relevance' => $item['relevance'] ?? $item['similarity'] ?? $item['rrf_score'] ?? 0,
];
}
return $formatted;
}
/**
* 按对话搜索消息(用于对话列表搜索)
*
* 返回包含匹配消息的对话列表,每个对话只返回一次
* 当 Manticore 未安装时,回退到 MySQL LIKE 搜索
*
* @param int $userid 用户ID
* @param string $keyword 搜索关键词
* @param int $from 起始位置
* @param int $size 返回数量
* @return array 对话列表
*/
public static function searchDialogs(int $userid, string $keyword, int $from = 0, int $size = 20): array
{
if (empty($keyword)) {
return [];
}
// 未安装 Manticore 时使用 MySQL 回退搜索
if (!Apps::isInstalled("search")) {
return self::searchDialogsByMysql($userid, $keyword, $from, $size);
}
try {
// 使用全文搜索获取更多结果,然后按对话分组
$results = ManticoreBase::msgFullTextSearch($keyword, $userid, 100, 0);
if (empty($results)) {
return [];
}
// 收集所有对话ID
$dialogIds = array_unique(array_column($results, 'dialog_id'));
// 获取用户在这些对话中的信息
$dialogUsers = WebSocketDialogUser::where('userid', $userid)
->whereIn('dialog_id', $dialogIds)
->get()
->keyBy('dialog_id');
// 按对话分组,每个对话只保留最相关的消息
$msgs = [];
$seenDialogs = [];
foreach ($results as $item) {
$dialogId = $item['dialog_id'];
// 每个对话只取第一条(最相关的)
if (isset($seenDialogs[$dialogId])) {
continue;
}
$seenDialogs[$dialogId] = true;
// 获取用户在该对话的信息
$dialogUser = $dialogUsers->get($dialogId);
if (!$dialogUser) {
continue;
}
$msgs[] = [
'id' => $dialogId,
'search_msg_id' => $item['msg_id'],
'user_at' => $dialogUser->updated_at ? Carbon::parse($dialogUser->updated_at)->format('Y-m-d H:i:s') : null,
'mark_unread' => $dialogUser->mark_unread,
'silence' => $dialogUser->silence,
'hide' => $dialogUser->hide,
'color' => $dialogUser->color,
'top_at' => $dialogUser->top_at ? Carbon::parse($dialogUser->top_at)->format('Y-m-d H:i:s') : null,
'last_at' => $dialogUser->last_at ? Carbon::parse($dialogUser->last_at)->format('Y-m-d H:i:s') : null,
];
// 已达到需要的数量
if (count($msgs) >= $from + $size) {
break;
}
}
// 应用分页
return array_slice($msgs, $from, $size);
} catch (\Exception $e) {
Log::error('Manticore searchDialogs error: ' . $e->getMessage());
// 出错时回退到 MySQL 搜索
return self::searchDialogsByMysql($userid, $keyword, $from, $size);
}
}
/**
* MySQL 回退搜索(按对话搜索消息)
*
* 通过联表查询获取用户有权限的对话中匹配的消息
*
* @param int $userid 用户ID
* @param string $keyword 搜索关键词
* @param int $from 起始位置
* @param int $size 返回数量
* @return array 对话列表
*/
private static function searchDialogsByMysql(int $userid, string $keyword, int $from = 0, int $size = 20): array
{
$items = DB::table('web_socket_dialog_users as u')
->select([
'd.*',
'u.top_at',
'u.last_at',
'u.mark_unread',
'u.silence',
'u.hide',
'u.color',
'u.updated_at as user_at',
'm.id as search_msg_id'
])
->join('web_socket_dialogs as d', 'u.dialog_id', '=', 'd.id')
->join('web_socket_dialog_msgs as m', 'm.dialog_id', '=', 'd.id')
->where('u.userid', $userid)
->where('m.bot', 0)
->whereNull('d.deleted_at')
->where('m.key', 'like', "%{$keyword}%")
->orderByDesc('m.id')
->offset($from)
->limit($size)
->get()
->all();
$msgs = [];
foreach ($items as $item) {
$msgs[] = [
'id' => $item->id,
'search_msg_id' => $item->search_msg_id,
'user_at' => Carbon::parse($item->user_at)->format('Y-m-d H:i:s'),
'mark_unread' => $item->mark_unread,
'silence' => $item->silence,
'hide' => $item->hide,
'color' => $item->color,
'top_at' => Carbon::parse($item->top_at)->format('Y-m-d H:i:s'),
'last_at' => Carbon::parse($item->last_at)->format('Y-m-d H:i:s'),
];
}
return $msgs;
}
// ==============================
// 权限计算方法
// ==============================
/**
* 获取消息的 allowed_users 列表
*
* 对话的所有成员都有权限查看该对话的消息
*
* @param WebSocketDialogMsg $msg 消息模型
* @return array 有权限的用户ID数组
*/
public static function getAllowedUsers(WebSocketDialogMsg $msg): array
{
return self::getDialogUserIds($msg->dialog_id);
}
/**
* 获取对话的所有成员ID
*
* @param int $dialogId 对话ID
* @return array 成员用户ID数组
*/
public static function getDialogUserIds(int $dialogId): array
{
if ($dialogId <= 0) {
return [];
}
return WebSocketDialogUser::where('dialog_id', $dialogId)
->pluck('userid')
->toArray();
}
// ==============================
// 同步方法
// ==============================
/**
* 同步单个消息到 Manticore含 allowed_users
*
* @param WebSocketDialogMsg $msg 消息模型
* @param bool $withVector 是否同时生成向量(默认 false向量由后台任务生成
* @return bool 是否成功
*/
public static function sync(WebSocketDialogMsg $msg, bool $withVector = false): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
// 检查是否应该索引
if (!self::shouldIndex($msg)) {
// 不符合索引条件,尝试删除已存在的索引
return ManticoreBase::deleteMsgVector($msg->id);
}
try {
// 提取消息内容(使用 key 字段)
$content = $msg->key ?? '';
// 限制内容长度
$content = mb_substr($content, 0, self::MAX_CONTENT_LENGTH);
// 只有明确要求时才生成向量(默认不生成,由后台任务处理)
$embedding = null;
if ($withVector && !empty($content) && Apps::isInstalled('ai')) {
$embeddingResult = ManticoreBase::getEmbedding($content);
if (!empty($embeddingResult)) {
$embedding = '[' . implode(',', $embeddingResult) . ']';
}
}
// 获取消息的 allowed_users
$allowedUsers = self::getAllowedUsers($msg);
// 写入 Manticore含 allowed_users
$result = ManticoreBase::upsertMsgVector([
'msg_id' => $msg->id,
'dialog_id' => $msg->dialog_id,
'userid' => $msg->userid,
'msg_type' => $msg->type,
'content' => $content,
'content_vector' => $embedding,
'allowed_users' => $allowedUsers,
'created_at' => $msg->created_at ? $msg->created_at->timestamp : time(),
]);
return $result;
} catch (\Exception $e) {
Log::error('Manticore msg sync error: ' . $e->getMessage(), [
'msg_id' => $msg->id,
'dialog_id' => $msg->dialog_id,
]);
return false;
}
}
/**
* 批量同步消息
*
* @param iterable $msgs 消息列表
* @param bool $withVector 是否同时生成向量
* @return int 成功同步的数量
*/
public static function batchSync(iterable $msgs, bool $withVector = false): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
$count = 0;
foreach ($msgs as $msg) {
if (self::sync($msg, $withVector)) {
$count++;
}
}
return $count;
}
/**
* 批量生成向量(供后台任务调用)
*
* @param array $msgIds 消息ID数组
* @param int $batchSize 每批 embedding 数量
* @return int 成功生成向量的数量
*/
public static function generateVectorsBatch(array $msgIds, int $batchSize = 20): int
{
if (!Apps::isInstalled("search") || !Apps::isInstalled('ai') || empty($msgIds)) {
return 0;
}
$count = 0;
// 分批处理
foreach (array_chunk($msgIds, $batchSize) as $batchIds) {
// 获取消息
$msgs = WebSocketDialogMsg::whereIn('id', $batchIds)
->whereIn('type', self::INDEXABLE_TYPES)
->where('bot', '!=', 1)
->whereNotNull('key')
->where('key', '!=', '')
->get()
->keyBy('id');
if ($msgs->isEmpty()) {
continue;
}
// 准备文本
$texts = [];
$idsArray = [];
foreach ($batchIds as $id) {
if (isset($msgs[$id])) {
$content = mb_substr($msgs[$id]->key ?? '', 0, self::MAX_CONTENT_LENGTH);
if (!empty($content)) {
$texts[] = $content;
$idsArray[] = $id;
}
}
}
if (empty($texts)) {
continue;
}
// 批量获取 embeddings
$result = AI::getBatchEmbeddings($texts);
if (Base::isError($result)) {
continue;
}
$embeddings = $result['data'] ?? [];
// 构建批量更新数据 [msg_id => vectorStr]
$vectorData = [];
foreach ($embeddings as $index => $embedding) {
if (empty($embedding) || !is_array($embedding)) {
continue;
}
$msgId = $idsArray[$index] ?? null;
if (!$msgId) {
continue;
}
$vectorData[$msgId] = '[' . implode(',', $embedding) . ']';
}
// 批量更新向量(优化:减少数据库操作次数)
if (!empty($vectorData)) {
$batchCount = ManticoreBase::batchUpdateMsgVectors($vectorData);
$count += $batchCount;
}
}
return $count;
}
/**
* 删除消息索引
*
* @param int $msgId 消息ID
* @return bool 是否成功
*/
public static function delete(int $msgId): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::deleteMsgVector($msgId);
}
/**
* 清空所有索引
*
* @return bool 是否成功
*/
public static function clear(): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::clearAllMsgVectors();
}
/**
* 获取已索引消息数量
*
* @return int 数量
*/
public static function getIndexedCount(): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
return ManticoreBase::getIndexedMsgCount();
}
// ==============================
// 权限更新方法
// ==============================
/**
* 更新对话下所有消息的 allowed_users 权限列表
* 从 MySQL 获取最新的对话成员并更新到 Manticore
*
* @param int $dialogId 对话ID
* @return int 更新的消息数量
*/
public static function updateDialogAllowedUsers(int $dialogId): int
{
if (!Apps::isInstalled("search") || $dialogId <= 0) {
return 0;
}
try {
$userids = self::getDialogUserIds($dialogId);
return ManticoreBase::updateDialogAllowedUsers($dialogId, $userids);
} catch (\Exception $e) {
Log::error('Manticore updateDialogAllowedUsers error: ' . $e->getMessage(), ['dialog_id' => $dialogId]);
return 0;
}
}
}

View File

@@ -0,0 +1,369 @@
<?php
namespace App\Module\Manticore;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Module\Apps;
use App\Module\Base;
use App\Module\AI;
use Illuminate\Support\Facades\Log;
/**
* Manticore Search 项目搜索类
*
* 使用方法:
*
* 1. 搜索方法
* - 搜索项目: search($userid, $keyword, $searchType, $limit);
*
* 2. 同步方法
* - 单个同步: sync(Project $project);
* - 批量同步: batchSync($projects);
* - 删除索引: delete($projectId);
*
* 3. 权限更新方法
* - 更新权限: updateAllowedUsers($projectId);
*
* 4. 工具方法
* - 清空索引: clear();
*/
class ManticoreProject
{
/**
* 搜索项目(支持全文、向量、混合搜索)
*
* @param int $userid 用户ID权限过滤
* @param string $keyword 搜索关键词
* @param string $searchType 搜索类型: text/vector/hybrid
* @param int $limit 返回数量
* @return array 搜索结果
*/
public static function search(int $userid, string $keyword, string $searchType = 'hybrid', int $limit = 20): array
{
if (empty($keyword)) {
return [];
}
if (!Apps::isInstalled("search")) {
return [];
}
try {
switch ($searchType) {
case 'text':
return self::formatSearchResults(
ManticoreBase::projectFullTextSearch($keyword, $userid, $limit, 0)
);
case 'vector':
$embedding = ManticoreBase::getEmbedding($keyword);
if (empty($embedding)) {
return self::formatSearchResults(
ManticoreBase::projectFullTextSearch($keyword, $userid, $limit, 0)
);
}
return self::formatSearchResults(
ManticoreBase::projectVectorSearch($embedding, $userid, $limit)
);
case 'hybrid':
default:
$embedding = ManticoreBase::getEmbedding($keyword);
return self::formatSearchResults(
ManticoreBase::projectHybridSearch($keyword, $embedding, $userid, $limit)
);
}
} catch (\Exception $e) {
Log::error('Manticore project search error: ' . $e->getMessage());
return [];
}
}
/**
* 格式化搜索结果
*
* @param array $results Manticore 返回的结果
* @return array 格式化后的结果
*/
private static function formatSearchResults(array $results): array
{
$formatted = [];
foreach ($results as $item) {
$formatted[] = [
'project_id' => $item['project_id'],
'id' => $item['project_id'],
'userid' => $item['userid'],
'personal' => $item['personal'],
'name' => $item['project_name'],
'desc_preview' => isset($item['project_desc']) ? mb_substr($item['project_desc'], 0, 300) : null,
'relevance' => $item['relevance'] ?? $item['similarity'] ?? $item['rrf_score'] ?? 0,
];
}
return $formatted;
}
// ==============================
// 同步方法
// ==============================
/**
* 获取项目的 allowed_users 列表
*
* @param int $projectId 项目ID
* @return array 有权限的用户ID数组
*/
public static function getAllowedUsers(int $projectId): array
{
return ProjectUser::where('project_id', $projectId)
->pluck('userid')
->toArray();
}
/**
* 同步单个项目到 Manticore含 allowed_users
*
* @param Project $project 项目模型
* @param bool $withVector 是否同时生成向量(默认 false向量由后台任务生成
* @return bool 是否成功
*/
public static function sync(Project $project, bool $withVector = false): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
// 已归档的项目不索引
if ($project->archived_at) {
return self::delete($project->id);
}
try {
// 构建用于搜索的文本内容
$searchableContent = self::buildSearchableContent($project);
// 只有明确要求时才生成向量(默认不生成,由后台任务处理)
$embedding = null;
if ($withVector && !empty($searchableContent) && Apps::isInstalled('ai')) {
$embeddingResult = ManticoreBase::getEmbedding($searchableContent);
if (!empty($embeddingResult)) {
$embedding = '[' . implode(',', $embeddingResult) . ']';
}
}
// 获取项目成员列表(作为 allowed_users
$allowedUsers = self::getAllowedUsers($project->id);
// 写入 Manticore含 allowed_users
$result = ManticoreBase::upsertProjectVector([
'project_id' => $project->id,
'userid' => $project->userid ?? 0,
'personal' => $project->personal ?? 0,
'project_name' => $project->name ?? '',
'project_desc' => $project->desc ?? '',
'content_vector' => $embedding,
'allowed_users' => $allowedUsers,
]);
return $result;
} catch (\Exception $e) {
Log::error('Manticore project sync error: ' . $e->getMessage(), [
'project_id' => $project->id,
'project_name' => $project->name,
]);
return false;
}
}
/**
* 构建可搜索的文本内容
*
* @param Project $project 项目模型
* @return string 可搜索的文本
*/
private static function buildSearchableContent(Project $project): string
{
$parts = [];
if (!empty($project->name)) {
$parts[] = $project->name;
}
if (!empty($project->desc)) {
$parts[] = $project->desc;
}
return implode(' ', $parts);
}
/**
* 批量同步项目
*
* @param iterable $projects 项目列表
* @param bool $withVector 是否同时生成向量
* @return int 成功同步的数量
*/
public static function batchSync(iterable $projects, bool $withVector = false): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
$count = 0;
foreach ($projects as $project) {
if (self::sync($project, $withVector)) {
$count++;
}
}
return $count;
}
/**
* 删除项目索引
*
* @param int $projectId 项目ID
* @return bool 是否成功
*/
public static function delete(int $projectId): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::deleteProjectVector($projectId);
}
/**
* 清空所有索引
*
* @return bool 是否成功
*/
public static function clear(): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::clearAllProjectVectors();
}
/**
* 获取已索引项目数量
*
* @return int 数量
*/
public static function getIndexedCount(): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
return ManticoreBase::getIndexedProjectCount();
}
// ==============================
// 权限更新方法
// ==============================
/**
* 更新项目的 allowed_users 权限列表
* 从 MySQL 获取最新的项目成员并更新到 Manticore
*
* @param int $projectId 项目ID
* @return bool 是否成功
*/
public static function updateAllowedUsers(int $projectId): bool
{
if (!Apps::isInstalled("search") || $projectId <= 0) {
return false;
}
try {
$userids = self::getAllowedUsers($projectId);
return ManticoreBase::updateProjectAllowedUsers($projectId, $userids);
} catch (\Exception $e) {
Log::error('Manticore updateAllowedUsers error: ' . $e->getMessage(), ['project_id' => $projectId]);
return false;
}
}
// ==============================
// 批量向量生成方法
// ==============================
/**
* 批量生成项目向量
* 用于后台异步处理,将已索引项目的向量批量生成
*
* @param array $projectIds 项目ID数组
* @param int $batchSize 每批 embedding 数量默认20
* @return int 成功处理的数量
*/
public static function generateVectorsBatch(array $projectIds, int $batchSize = 20): int
{
if (!Apps::isInstalled("search") || !Apps::isInstalled("ai") || empty($projectIds)) {
return 0;
}
try {
// 1. 查询项目信息
$projects = Project::whereIn('id', $projectIds)
->whereNull('archived_at')
->get();
if ($projects->isEmpty()) {
return 0;
}
// 2. 提取每个项目的内容
$projectContents = [];
foreach ($projects as $project) {
$searchableContent = self::buildSearchableContent($project);
if (!empty($searchableContent)) {
$projectContents[$project->id] = $searchableContent;
}
}
if (empty($projectContents)) {
return 0;
}
// 3. 分批处理
$successCount = 0;
$chunks = array_chunk($projectContents, $batchSize, true);
foreach ($chunks as $chunk) {
$texts = array_values($chunk);
$ids = array_keys($chunk);
// 4. 批量获取 embedding
$result = AI::getBatchEmbeddings($texts);
if (!Base::isSuccess($result) || empty($result['data'])) {
continue;
}
$embeddings = $result['data'];
// 5. 构建批量更新数据
$vectorData = [];
foreach ($ids as $index => $projectId) {
if (!isset($embeddings[$index]) || empty($embeddings[$index])) {
continue;
}
$vectorData[$projectId] = '[' . implode(',', $embeddings[$index]) . ']';
}
// 6. 批量更新向量
if (!empty($vectorData)) {
$batchCount = ManticoreBase::batchUpdateProjectVectors($vectorData);
$successCount += $batchCount;
}
}
return $successCount;
} catch (\Exception $e) {
Log::error('ManticoreProject generateVectorsBatch error: ' . $e->getMessage());
return 0;
}
}
}

View File

@@ -0,0 +1,593 @@
<?php
namespace App\Module\Manticore;
use App\Models\ProjectTask;
use App\Models\ProjectTaskContent;
use App\Models\ProjectTaskUser;
use App\Models\ProjectTaskVisibilityUser;
use App\Models\ProjectUser;
use App\Module\Apps;
use App\Module\Base;
use App\Module\AI;
use Illuminate\Support\Facades\Log;
/**
* Manticore Search 任务搜索类
*
* 权限逻辑说明:
* - visibility = 1: 项目人员可见,通过项目成员计算 allowed_users
* - visibility = 2: 任务人员可见,通过任务成员计算 allowed_users
* - visibility = 3: 指定成员可见,通过任务成员 + 可见性成员计算 allowed_users
* - 子任务继承父任务的 allowed_users
*
* 使用方法:
*
* 1. 搜索方法
* - 搜索任务: search($userid, $keyword, $searchType, $limit);
*
* 2. 同步方法
* - 单个同步: sync(ProjectTask $task);
* - 批量同步: batchSync($tasks);
* - 删除索引: delete($taskId);
*
* 3. 权限更新方法
* - 更新权限: updateAllowedUsers($taskId);
* - 项目成员变更级联更新: cascadeUpdateByProject($projectId);
* - 父任务变更级联到子任务: cascadeToChildren($taskId);
*
* 4. 工具方法
* - 清空索引: clear();
*/
class ManticoreTask
{
/**
* 最大内容长度(字符)
*/
public const MAX_CONTENT_LENGTH = 50000; // 50K 字符
/**
* 搜索任务(支持全文、向量、混合搜索)
*
* @param int $userid 用户ID权限过滤
* @param string $keyword 搜索关键词
* @param string $searchType 搜索类型: text/vector/hybrid
* @param int $limit 返回数量
* @return array 搜索结果
*/
public static function search(int $userid, string $keyword, string $searchType = 'hybrid', int $limit = 20): array
{
if (empty($keyword)) {
return [];
}
if (!Apps::isInstalled("search")) {
return [];
}
try {
switch ($searchType) {
case 'text':
return self::formatSearchResults(
ManticoreBase::taskFullTextSearch($keyword, $userid, $limit, 0)
);
case 'vector':
$embedding = ManticoreBase::getEmbedding($keyword);
if (empty($embedding)) {
return self::formatSearchResults(
ManticoreBase::taskFullTextSearch($keyword, $userid, $limit, 0)
);
}
return self::formatSearchResults(
ManticoreBase::taskVectorSearch($embedding, $userid, $limit)
);
case 'hybrid':
default:
$embedding = ManticoreBase::getEmbedding($keyword);
return self::formatSearchResults(
ManticoreBase::taskHybridSearch($keyword, $embedding, $userid, $limit)
);
}
} catch (\Exception $e) {
Log::error('Manticore task search error: ' . $e->getMessage());
return [];
}
}
/**
* 格式化搜索结果
*
* @param array $results Manticore 返回的结果
* @return array 格式化后的结果
*/
private static function formatSearchResults(array $results): array
{
$formatted = [];
foreach ($results as $item) {
$formatted[] = [
'task_id' => $item['task_id'],
'id' => $item['task_id'],
'project_id' => $item['project_id'],
'userid' => $item['userid'],
'visibility' => $item['visibility'],
'name' => $item['task_name'],
'desc_preview' => isset($item['task_desc']) ? mb_substr($item['task_desc'], 0, 300) : null,
'content_preview' => isset($item['task_content']) ? mb_substr($item['task_content'], 0, 500) : null,
'relevance' => $item['relevance'] ?? $item['similarity'] ?? $item['rrf_score'] ?? 0,
];
}
return $formatted;
}
// ==============================
// 权限计算方法
// ==============================
/**
* 获取任务的 allowed_users 列表
*
* 根据 visibility 计算有权限查看此任务的用户列表:
* - visibility=1: 项目成员
* - visibility=2: 任务成员(负责人/协作人)
* - visibility=3: 任务成员 + 可见性指定成员
* - 子任务: 还需要继承父任务的成员
*
* @param ProjectTask $task 任务模型
* @param int $depth 递归深度(防止无限递归)
* @param array $visited 已访问的任务ID防止循环引用
* @return array 有权限的用户ID数组
*/
public static function getAllowedUsers(ProjectTask $task, int $depth = 0, array $visited = []): array
{
// 防止无限递归深度超过10层或循环引用
if ($depth > 10 || in_array($task->id, $visited)) {
return [];
}
$visited[] = $task->id;
$userids = [];
// 1. 根据 visibility 获取基础成员
if ($task->visibility == 1) {
// visibility=1: 项目成员
$userids = ProjectUser::where('project_id', $task->project_id)
->pluck('userid')
->toArray();
} else {
// visibility=2,3: 任务成员(负责人/协作人)
$userids = ProjectTaskUser::where('task_id', $task->id)
->orWhere('task_pid', $task->id)
->pluck('userid')
->toArray();
// visibility=3: 加上可见性指定成员
if ($task->visibility == 3) {
$visUsers = ProjectTaskVisibilityUser::where('task_id', $task->id)
->pluck('userid')
->toArray();
$userids = array_merge($userids, $visUsers);
}
}
// 2. 如果是子任务,继承父任务成员
if ($task->parent_id > 0) {
$parentTask = ProjectTask::find($task->parent_id);
if ($parentTask) {
$parentUsers = self::getAllowedUsers($parentTask, $depth + 1, $visited);
$userids = array_merge($userids, $parentUsers);
}
}
return array_unique($userids);
}
// ==============================
// 同步方法
// ==============================
/**
* 同步单个任务到 Manticore含 allowed_users
*
* @param ProjectTask $task 任务模型
* @param bool $withVector 是否同时生成向量(默认 false向量由后台任务生成
* @return bool 是否成功
*/
public static function sync(ProjectTask $task, bool $withVector = false): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
// 已归档或已删除的任务不索引
if ($task->archived_at || $task->deleted_at) {
return self::delete($task->id);
}
try {
// 获取任务详细内容
$taskContent = self::getTaskContent($task);
// 构建用于搜索的文本内容
$searchableContent = self::buildSearchableContent($task, $taskContent);
// 只有明确要求时才生成向量(默认不生成,由后台任务处理)
$embedding = null;
if ($withVector && !empty($searchableContent) && Apps::isInstalled('ai')) {
$embeddingResult = ManticoreBase::getEmbedding($searchableContent);
if (!empty($embeddingResult)) {
$embedding = '[' . implode(',', $embeddingResult) . ']';
}
}
// 获取任务的 allowed_users
$allowedUsers = self::getAllowedUsers($task);
// 写入 Manticore含 allowed_users
$result = ManticoreBase::upsertTaskVector([
'task_id' => $task->id,
'project_id' => $task->project_id ?? 0,
'userid' => $task->userid ?? 0,
'visibility' => $task->visibility ?? 1,
'task_name' => $task->name ?? '',
'task_desc' => $task->desc ?? '',
'task_content' => $taskContent,
'content_vector' => $embedding,
'allowed_users' => $allowedUsers,
]);
return $result;
} catch (\Exception $e) {
Log::error('Manticore task sync error: ' . $e->getMessage(), [
'task_id' => $task->id,
'task_name' => $task->name,
]);
return false;
}
}
/**
* 获取任务详细内容
*
* @param ProjectTask $task 任务模型
* @return string 任务内容
*/
private static function getTaskContent(ProjectTask $task): string
{
try {
$content = ProjectTaskContent::where('task_id', $task->id)->first();
if (!$content) {
return '';
}
// 解析内容
$contentData = Base::json2array($content->content);
$text = '';
// 提取文本内容(内容可能是 blocks 格式)
if (is_array($contentData)) {
$text = self::extractTextFromContent($contentData);
} elseif (is_string($contentData)) {
$text = $contentData;
}
// 限制内容长度
return mb_substr($text, 0, self::MAX_CONTENT_LENGTH);
} catch (\Exception $e) {
return '';
}
}
/**
* 从内容数组中提取文本
*
* @param array $contentData 内容数据
* @return string 提取的文本
*/
private static function extractTextFromContent(array $contentData): string
{
$texts = [];
// 处理 blocks 格式
if (isset($contentData['blocks']) && is_array($contentData['blocks'])) {
foreach ($contentData['blocks'] as $block) {
if (isset($block['text'])) {
$texts[] = $block['text'];
}
if (isset($block['data']['text'])) {
$texts[] = $block['data']['text'];
}
}
}
// 处理其他格式
if (isset($contentData['text'])) {
$texts[] = $contentData['text'];
}
return implode(' ', $texts);
}
/**
* 构建可搜索的文本内容
*
* @param ProjectTask $task 任务模型
* @param string $taskContent 任务详细内容
* @return string 可搜索的文本
*/
private static function buildSearchableContent(ProjectTask $task, string $taskContent): string
{
$parts = [];
if (!empty($task->name)) {
$parts[] = $task->name;
}
if (!empty($task->desc)) {
$parts[] = $task->desc;
}
if (!empty($taskContent)) {
$parts[] = $taskContent;
}
return implode(' ', $parts);
}
/**
* 批量同步任务
*
* @param iterable $tasks 任务列表
* @param bool $withVector 是否同时生成向量
* @return int 成功同步的数量
*/
public static function batchSync(iterable $tasks, bool $withVector = false): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
$count = 0;
foreach ($tasks as $task) {
if (self::sync($task, $withVector)) {
$count++;
}
}
return $count;
}
/**
* 删除任务索引
*
* @param int $taskId 任务ID
* @return bool 是否成功
*/
public static function delete(int $taskId): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::deleteTaskVector($taskId);
}
/**
* 清空所有索引
*
* @return bool 是否成功
*/
public static function clear(): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::clearAllTaskVectors();
}
/**
* 获取已索引任务数量
*
* @return int 数量
*/
public static function getIndexedCount(): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
return ManticoreBase::getIndexedTaskCount();
}
// ==============================
// 权限更新方法
// ==============================
/**
* 更新任务的 allowed_users 权限列表
* 重新计算并更新 Manticore 中的权限
*
* @param int $taskId 任务ID
* @return bool 是否成功
*/
public static function updateAllowedUsers(int $taskId): bool
{
if (!Apps::isInstalled("search") || $taskId <= 0) {
return false;
}
try {
$task = ProjectTask::find($taskId);
if (!$task) {
return false;
}
$userids = self::getAllowedUsers($task);
return ManticoreBase::updateTaskAllowedUsers($taskId, $userids);
} catch (\Exception $e) {
Log::error('Manticore updateAllowedUsers error: ' . $e->getMessage(), ['task_id' => $taskId]);
return false;
}
}
/**
* 级联更新项目下所有 visibility=1 任务的 allowed_users
* 当项目成员变更时调用
*
* @param int $projectId 项目ID
* @return int 更新的任务数量
*/
public static function cascadeUpdateByProject(int $projectId): int
{
if (!Apps::isInstalled("search") || $projectId <= 0) {
return 0;
}
try {
// 获取项目成员
$projectUsers = ProjectUser::where('project_id', $projectId)
->pluck('userid')
->toArray();
// 分批更新该项目下所有 visibility=1 的任务
$count = 0;
ProjectTask::where('project_id', $projectId)
->where('visibility', 1)
->whereNull('deleted_at')
->whereNull('archived_at')
->chunk(100, function ($tasks) use ($projectUsers, &$count) {
foreach ($tasks as $task) {
// 对于子任务,需要合并父任务成员
$allowedUsers = $projectUsers;
if ($task->parent_id > 0) {
$parentTask = ProjectTask::find($task->parent_id);
if ($parentTask) {
$parentUsers = self::getAllowedUsers($parentTask);
$allowedUsers = array_unique(array_merge($allowedUsers, $parentUsers));
}
}
ManticoreBase::updateTaskAllowedUsers($task->id, $allowedUsers);
$count++;
}
});
return $count;
} catch (\Exception $e) {
Log::error('Manticore cascadeUpdateByProject error: ' . $e->getMessage(), ['project_id' => $projectId]);
return 0;
}
}
/**
* 级联更新所有子任务的 allowed_users
* 当父任务的成员变更时调用
*
* @param int $taskId 父任务ID
* @return void
*/
public static function cascadeToChildren(int $taskId): void
{
if (!Apps::isInstalled("search") || $taskId <= 0) {
return;
}
try {
ProjectTask::where('parent_id', $taskId)
->whereNull('deleted_at')
->whereNull('archived_at')
->each(function ($child) {
$allowedUsers = self::getAllowedUsers($child);
ManticoreBase::updateTaskAllowedUsers($child->id, $allowedUsers);
// 递归处理子任务的子任务
self::cascadeToChildren($child->id);
});
} catch (\Exception $e) {
Log::error('Manticore cascadeToChildren error: ' . $e->getMessage(), ['task_id' => $taskId]);
}
}
// ==============================
// 批量向量生成方法
// ==============================
/**
* 批量生成任务向量
* 用于后台异步处理,将已索引任务的向量批量生成
*
* @param array $taskIds 任务ID数组
* @param int $batchSize 每批 embedding 数量默认20
* @return int 成功处理的数量
*/
public static function generateVectorsBatch(array $taskIds, int $batchSize = 20): int
{
if (!Apps::isInstalled("search") || !Apps::isInstalled("ai") || empty($taskIds)) {
return 0;
}
try {
// 1. 查询任务信息
$tasks = ProjectTask::whereIn('id', $taskIds)
->whereNull('deleted_at')
->whereNull('archived_at')
->get();
if ($tasks->isEmpty()) {
return 0;
}
// 2. 提取每个任务的内容
$taskContents = [];
foreach ($tasks as $task) {
$taskContent = self::getTaskContent($task);
$searchableContent = self::buildSearchableContent($task, $taskContent);
if (!empty($searchableContent)) {
// 限制内容长度
$searchableContent = mb_substr($searchableContent, 0, self::MAX_CONTENT_LENGTH);
$taskContents[$task->id] = $searchableContent;
}
}
if (empty($taskContents)) {
return 0;
}
// 3. 分批处理
$successCount = 0;
$chunks = array_chunk($taskContents, $batchSize, true);
foreach ($chunks as $chunk) {
$texts = array_values($chunk);
$ids = array_keys($chunk);
// 4. 批量获取 embedding
$result = AI::getBatchEmbeddings($texts);
if (!Base::isSuccess($result) || empty($result['data'])) {
continue;
}
$embeddings = $result['data'];
// 5. 构建批量更新数据
$vectorData = [];
foreach ($ids as $index => $taskId) {
if (!isset($embeddings[$index]) || empty($embeddings[$index])) {
continue;
}
$vectorData[$taskId] = '[' . implode(',', $embeddings[$index]) . ']';
}
// 6. 批量更新向量
if (!empty($vectorData)) {
$batchCount = ManticoreBase::batchUpdateTaskVectors($vectorData);
$successCount += $batchCount;
}
}
return $successCount;
} catch (\Exception $e) {
Log::error('ManticoreTask generateVectorsBatch error: ' . $e->getMessage());
return 0;
}
}
}

View File

@@ -0,0 +1,362 @@
<?php
namespace App\Module\Manticore;
use App\Models\User;
use App\Models\UserTag;
use App\Module\Apps;
use App\Module\Base;
use App\Module\AI;
use Illuminate\Support\Facades\Log;
/**
* Manticore Search 用户搜索类(联系人搜索)
*
* 使用方法:
*
* 1. 搜索方法
* - 搜索用户: search($keyword, $searchType, $limit);
*
* 2. 同步方法
* - 单个同步: sync(User $user);
* - 批量同步: batchSync($users);
* - 删除索引: delete($userid);
*
* 3. 工具方法
* - 清空索引: clear();
*/
class ManticoreUser
{
/**
* 搜索用户(支持全文、向量、混合搜索)
*
* @param string $keyword 搜索关键词
* @param string $searchType 搜索类型: text/vector/hybrid
* @param int $limit 返回数量
* @return array 搜索结果
*/
public static function search(string $keyword, string $searchType = 'hybrid', int $limit = 20): array
{
if (empty($keyword)) {
return [];
}
if (!Apps::isInstalled("search")) {
return [];
}
try {
switch ($searchType) {
case 'text':
return self::formatSearchResults(
ManticoreBase::userFullTextSearch($keyword, $limit, 0)
);
case 'vector':
$embedding = ManticoreBase::getEmbedding($keyword);
if (empty($embedding)) {
return self::formatSearchResults(
ManticoreBase::userFullTextSearch($keyword, $limit, 0)
);
}
return self::formatSearchResults(
ManticoreBase::userVectorSearch($embedding, $limit)
);
case 'hybrid':
default:
$embedding = ManticoreBase::getEmbedding($keyword);
return self::formatSearchResults(
ManticoreBase::userHybridSearch($keyword, $embedding, $limit)
);
}
} catch (\Exception $e) {
Log::error('Manticore user search error: ' . $e->getMessage());
return [];
}
}
/**
* 格式化搜索结果
*
* @param array $results Manticore 返回的结果
* @return array 格式化后的结果
*/
private static function formatSearchResults(array $results): array
{
$formatted = [];
foreach ($results as $item) {
$formatted[] = [
'userid' => $item['userid'],
'nickname' => $item['nickname'],
'email' => $item['email'],
'profession' => $item['profession'],
'tags' => $item['tags'] ?? '',
'introduction_preview' => isset($item['introduction']) ? mb_substr($item['introduction'], 0, 200) : null,
'relevance' => $item['relevance'] ?? $item['similarity'] ?? $item['rrf_score'] ?? 0,
];
}
return $formatted;
}
// ==============================
// 同步方法
// ==============================
/**
* 获取用户的标签按认可数排序最多10个
*
* @param int $userid 用户ID
* @return string 标签名称,空格分隔
*/
public static function getUserTags(int $userid): string
{
$tags = UserTag::where('user_id', $userid)
->withCount('recognitions')
->orderByDesc('recognitions_count')
->limit(10)
->pluck('name')
->toArray();
return implode(' ', $tags);
}
/**
* 同步单个用户到 Manticore
*
* @param User $user 用户模型
* @param bool $withVector 是否同时生成向量(默认 false向量由后台任务生成
* @return bool 是否成功
*/
public static function sync(User $user, bool $withVector = false): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
// 不处理机器人账号
if ($user->bot) {
return true;
}
// 不处理已禁用的账号
if ($user->disable_at) {
return self::delete($user->userid);
}
try {
// 获取用户标签Top 10
$tags = self::getUserTags($user->userid);
// 构建用于搜索的文本内容
$searchableContent = self::buildSearchableContent($user, $tags);
// 只有明确要求时才生成向量(默认不生成,由后台任务处理)
$embedding = null;
if ($withVector && !empty($searchableContent) && Apps::isInstalled('ai')) {
$embeddingResult = ManticoreBase::getEmbedding($searchableContent);
if (!empty($embeddingResult)) {
$embedding = '[' . implode(',', $embeddingResult) . ']';
}
}
// 写入 Manticore
$result = ManticoreBase::upsertUserVector([
'userid' => $user->userid,
'nickname' => $user->nickname ?? '',
'email' => $user->email ?? '',
'profession' => $user->profession ?? '',
'tags' => $tags,
'introduction' => $user->introduction ?? '',
'content_vector' => $embedding,
]);
return $result;
} catch (\Exception $e) {
Log::error('Manticore user sync error: ' . $e->getMessage(), [
'userid' => $user->userid,
'nickname' => $user->nickname,
]);
return false;
}
}
/**
* 构建可搜索的文本内容
*
* @param User $user 用户模型
* @param string $tags 用户标签(空格分隔)
* @return string 可搜索的文本
*/
private static function buildSearchableContent(User $user, string $tags = ''): string
{
$parts = [];
if (!empty($user->nickname)) {
$parts[] = $user->nickname;
}
if (!empty($user->email)) {
$parts[] = $user->email;
}
if (!empty($user->profession)) {
$parts[] = $user->profession;
}
if (!empty($tags)) {
$parts[] = $tags;
}
if (!empty($user->introduction)) {
$parts[] = $user->introduction;
}
return implode(' ', $parts);
}
/**
* 批量同步用户
*
* @param iterable $users 用户列表
* @param bool $withVector 是否同时生成向量
* @return int 成功同步的数量
*/
public static function batchSync(iterable $users, bool $withVector = false): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
$count = 0;
foreach ($users as $user) {
if (self::sync($user, $withVector)) {
$count++;
}
}
return $count;
}
/**
* 删除用户索引
*
* @param int $userid 用户ID
* @return bool 是否成功
*/
public static function delete(int $userid): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::deleteUserVector($userid);
}
/**
* 清空所有索引
*
* @return bool 是否成功
*/
public static function clear(): bool
{
if (!Apps::isInstalled("search")) {
return false;
}
return ManticoreBase::clearAllUserVectors();
}
/**
* 获取已索引用户数量
*
* @return int 数量
*/
public static function getIndexedCount(): int
{
if (!Apps::isInstalled("search")) {
return 0;
}
return ManticoreBase::getIndexedUserCount();
}
// ==============================
// 批量向量生成方法
// ==============================
/**
* 批量生成用户向量
* 用于后台异步处理,将已索引用户的向量批量生成
*
* @param array $userIds 用户ID数组
* @param int $batchSize 每批 embedding 数量默认20
* @return int 成功处理的数量
*/
public static function generateVectorsBatch(array $userIds, int $batchSize = 20): int
{
if (!Apps::isInstalled("search") || !Apps::isInstalled("ai") || empty($userIds)) {
return 0;
}
try {
// 1. 查询用户信息
$users = User::whereIn('userid', $userIds)
->where('bot', 0)
->whereNull('disable_at')
->get();
if ($users->isEmpty()) {
return 0;
}
// 2. 提取每个用户的内容(包含标签)
$userContents = [];
foreach ($users as $user) {
$tags = self::getUserTags($user->userid);
$searchableContent = self::buildSearchableContent($user, $tags);
if (!empty($searchableContent)) {
$userContents[$user->userid] = $searchableContent;
}
}
if (empty($userContents)) {
return 0;
}
// 3. 分批处理
$successCount = 0;
$chunks = array_chunk($userContents, $batchSize, true);
foreach ($chunks as $chunk) {
$texts = array_values($chunk);
$ids = array_keys($chunk);
// 4. 批量获取 embedding
$result = AI::getBatchEmbeddings($texts);
if (!Base::isSuccess($result) || empty($result['data'])) {
continue;
}
$embeddings = $result['data'];
// 5. 构建批量更新数据
$vectorData = [];
foreach ($ids as $index => $userid) {
if (!isset($embeddings[$index]) || empty($embeddings[$index])) {
continue;
}
$vectorData[$userid] = '[' . implode(',', $embeddings[$index]) . ']';
}
// 6. 批量更新向量
if (!empty($vectorData)) {
$batchCount = ManticoreBase::batchUpdateUserVectors($vectorData);
$successCount += $batchCount;
}
}
return $successCount;
} catch (\Exception $e) {
Log::error('ManticoreUser generateVectorsBatch error: ' . $e->getMessage());
return 0;
}
}
}

View File

@@ -0,0 +1,300 @@
<?php
namespace App\Module;
use App\Models\Project;
use App\Models\ProjectTask;
use App\Models\ProjectTaskUser;
use App\Models\ProjectUser;
use App\Models\User;
use App\Models\UserDepartment;
use App\Models\UserTag;
use App\Models\WebSocketDialog;
use Cache;
use Carbon\Carbon;
use DB;
/**
* AI 提示词模块
*
* 提供用户上下文和条件性提示块的构建能力
*/
class PromptPlaceholder
{
/**
* 构建条件性提示块(用户上下文 + 格式指南)
*
* @param int|null $userid
* @param WebSocketDialog|null $dialog
* @return string
*/
public static function buildOptionalPrompts($userid, ?WebSocketDialog $dialog = null): string
{
$blocks = [];
// 用户上下文块
if ($userid && $userid > 0) {
$userContext = self::buildUserContext($userid, $dialog);
if ($userContext) {
$blocks[] = <<<EOF
<optional-user-context>
以下是当前对话用户的背景信息,当需要了解用户身份、工作职责或任务情况时可参考:
{$userContext}
注意:此上下文仅供参考,用于理解用户背景和提供个性化帮助。如果与当前对话无关,请忽略。
</optional-user-context>
EOF;
}
}
// 格式指南块
$blocks[] = <<<'EOF'
<optional-format-guide>
当你的回答中包含 DooTask 系统资源(任务、项目、文件等)时,建议使用以下链接格式使其可点击:
- 任务: [任务名称](dootask://task/{task_id}/{parent_id}),其中 parent_id 为主任务ID主任务时为 0
- 项目: [项目名称](dootask://project/{project_id})
- 文件: [文件名称](dootask://file/{file_id})
- 联系人: [用户名](dootask://contact/{userid})
- 消息: [消息预览](dootask://message/{dialog_id}/{msg_id})
注意:此格式指南不影响正常对话,仅在涉及上述资源时参考。如果与当前对话无关,请忽略。
</optional-format-guide>
EOF;
return implode("\n\n", $blocks);
}
/**
* 构建完整用户上下文
*/
private static function buildUserContext(int $userid, ?WebSocketDialog $dialog = null): string
{
$lines = [];
// 基础信息
$basicInfo = self::getUserBasicInfo($userid);
$nickname = $basicInfo['nickname'] ?? '';
if ($nickname) {
$basicLine = "与您对话的用户:{$nickname}";
if ($basicInfo['profession'] ?? '') {
$basicLine .= "{$basicInfo['profession']}";
}
$lines[] = "{$basicLine}user_id: {$userid}";
}
if ($basicInfo['department'] ?? '') {
$lines[] = "所属部门:{$basicInfo['department']}";
}
if ($basicInfo['introduction'] ?? '') {
$lines[] = "个人简介:{$basicInfo['introduction']}";
}
// 同事印象
$tags = self::getUserTags($userid);
if ($tags) {
$lines[] = "同事印象:{$tags}";
}
// 场景角色
if ($dialog) {
$role = self::getUserRole($userid, $dialog);
if ($role) {
$lines[] = $role;
}
}
// 进行中任务
$inProgressTasks = self::getInProgressTasks($userid);
if ($inProgressTasks) {
$lines[] = "\n进行中的任务:\n{$inProgressTasks}";
}
// 最近完成
$completedTasks = self::getCompletedTasks($userid);
if ($completedTasks) {
$lines[] = "\n最近完成:\n{$completedTasks}";
}
return implode("\n", $lines);
}
/**
* 获取用户基础信息
*/
private static function getUserBasicInfo(int $userid): array
{
$user = User::find($userid);
if (!$user) {
return [];
}
return [
'nickname' => $user->nickname ?: '',
'profession' => $user->profession ?: '',
'introduction' => $user->introduction ? mb_substr($user->introduction, 0, 100) : '',
'department' => $user->getDepartmentName() ?: '',
];
}
/**
* 获取用户标签 Top 5
*/
private static function getUserTags(int $userid): string
{
$tags = UserTag::where('user_id', $userid)
->withCount(['recognitions as recognition_total'])
->orderByDesc('recognition_total')
->orderBy('id')
->take(5)
->pluck('name')
->toArray();
return implode('、', $tags);
}
/**
* 获取用户在场景中的角色
*/
private static function getUserRole(int $userid, WebSocketDialog $dialog): string
{
if ($dialog->type !== 'group') {
return '';
}
switch ($dialog->group_type) {
case 'project':
$project = Project::whereDialogId($dialog->id)->first();
if ($project) {
$projectUser = ProjectUser::whereProjectId($project->id)->whereUserid($userid)->first();
if ($projectUser?->owner) {
return '该用户是此项目的负责人';
}
}
break;
case 'task':
$task = ProjectTask::whereDialogId($dialog->id)->first();
if ($task) {
$taskUser = ProjectTaskUser::whereTaskId($task->id)->whereUserid($userid)->first();
if ($taskUser) {
return $taskUser->owner ? '该用户是此任务的负责人' : '该用户是此任务的协助人';
}
}
break;
case 'department':
$department = UserDepartment::whereDialogId($dialog->id)->first();
if ($department?->owner_userid === $userid) {
return '该用户是此部门的负责人';
}
break;
}
return '';
}
/**
* 获取进行中的任务(缓存 3 分钟)
*
* 排序策略:逾期优先 → 最近活跃优先 → 负责人优先 → 高优先级优先 → 截止时间近优先
*/
private static function getInProgressTasks(int $userid): string
{
$cacheKey = "prompt:tasks:in_progress:{$userid}";
return Cache::remember($cacheKey, 180, function () use ($userid) {
$now = Carbon::now();
$threeDaysAgo = $now->copy()->subDays(3);
// orderByRaw 中的表名需要带前缀
$prefix = DB::getTablePrefix();
$t = $prefix . 'project_tasks';
$du = $prefix . 'web_socket_dialog_users';
$tasks = ProjectTask::query()
->select([
'project_tasks.id',
'project_tasks.name',
'project_tasks.p_name',
'project_tasks.end_at',
'project_task_users.owner'
])
->join('project_task_users', 'project_tasks.id', '=', 'project_task_users.task_id')
->leftJoin('web_socket_dialog_users', function ($join) use ($userid) {
$join->on('project_tasks.dialog_id', '=', 'web_socket_dialog_users.dialog_id')
->where('web_socket_dialog_users.userid', '=', $userid);
})
->where('project_task_users.userid', $userid)
->where('project_tasks.visibility', 1)
->whereNull('project_tasks.complete_at')
->whereNull('project_tasks.archived_at')
->whereNull('project_tasks.deleted_at')
->orderByRaw("CASE WHEN {$t}.end_at IS NOT NULL AND {$t}.end_at < ? THEN 0 ELSE 1 END", [$now])
->orderByRaw("CASE WHEN {$du}.last_at >= ? THEN 0 ELSE 1 END", [$threeDaysAgo])
->orderByDesc('web_socket_dialog_users.last_at')
->orderByDesc('project_task_users.owner')
->orderByDesc('project_tasks.p_level')
->orderByRaw("CASE WHEN {$t}.end_at IS NULL THEN 1 ELSE 0 END")
->orderBy('project_tasks.end_at')
->take(20)
->get();
return self::formatTaskList($tasks, $now);
});
}
/**
* 获取最近完成的任务(缓存 3 分钟)
*/
private static function getCompletedTasks(int $userid): string
{
$cacheKey = "prompt:tasks:completed:{$userid}";
return Cache::remember($cacheKey, 180, function () use ($userid) {
$tasks = ProjectTask::query()
->select([
'project_tasks.id',
'project_tasks.name'
])
->join('project_task_users', 'project_tasks.id', '=', 'project_task_users.task_id')
->where('project_task_users.userid', $userid)
->where('project_tasks.visibility', 1)
->whereNotNull('project_tasks.complete_at')
->where('project_tasks.complete_at', '>=', Carbon::now()->subDays(7))
->whereNull('project_tasks.deleted_at')
->orderByDesc('project_tasks.complete_at')
->take(5)
->get();
if ($tasks->isEmpty()) {
return '';
}
return $tasks->map(fn($task) => "- {$task->name} (task:{$task->id})")->implode("\n");
});
}
/**
* 格式化任务列表
*/
private static function formatTaskList($tasks, Carbon $now): string
{
if ($tasks->isEmpty()) {
return '';
}
return $tasks->map(function ($task) use ($now) {
$line = '- ';
if ($task->p_name) {
$line .= "[{$task->p_name}] ";
}
$line .= "{$task->name} (task_id:{$task->id})";
if ($task->end_at && Carbon::parse($task->end_at)->lt($now)) {
$line .= ' ⚠️逾期';
}
return $line;
})->implode("\n");
}
}

View File

@@ -1,267 +0,0 @@
<?php
namespace App\Module\ZincSearch;
use App\Module\Apps;
use App\Module\Doo;
/**
* ZincSearch 公共类
*/
class ZincSearchBase
{
private mixed $host;
private mixed $port;
private mixed $user;
private mixed $pass;
/**
* 构造函数
*/
public function __construct()
{
$this->host = env('ZINCSEARCH_HOST', 'search');
$this->port = env('ZINCSEARCH_PORT', '4080');
$this->user = env('DB_USERNAME', '');
$this->pass = env('DB_PASSWORD', '');
}
/**
* 通用请求方法
*/
private function request($path, $body = null, $method = 'POST')
{
if (!Apps::isInstalled("search")) {
return [
'success' => false,
'error' => Doo::translate("应用「ZincSearch」未安装")
];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://{$this->host}:{$this->port}{$path}");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $this->user . ':' . $this->pass);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$headers = ['Content-Type: application/json'];
if ($method === 'BULK') {
$headers = ['Content-Type: text/plain'];
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
if ($body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
}
$result = curl_exec($ch);
$error = curl_error($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($error) {
return ['success' => false, 'error' => $error];
}
$data = json_decode($result, true);
return [
'success' => $status >= 200 && $status < 300,
'status' => $status,
'data' => $data
];
}
// ==============================
// 索引管理相关方法
// ==============================
/**
* 创建索引
*/
public static function createIndex($index, $mappings = []): array
{
$body = json_encode([
'name' => $index,
'mappings' => $mappings
]);
return (new self())->request("/api/index", $body);
}
/**
* 获取索引信息
*/
public static function getIndex($index): array
{
return (new self())->request("/api/index/{$index}", null, 'GET');
}
/**
* 判断索引是否存在
*/
public static function indexExists($index): bool
{
$result = self::getIndex($index);
return $result['success'] && isset($result['data']['name']);
}
/**
* 获取所有索引
*/
public static function listIndices(): array
{
return (new self())->request("/api/index", null, 'GET');
}
/**
* 删除索引
*/
public static function deleteIndex($index): array
{
return (new self())->request("/api/index/{$index}", null, 'DELETE');
}
/**
* 删除所有索引
*/
public static function deleteAllIndices(): array
{
$instance = new self();
$result = $instance->request("/api/index", null, 'GET');
if (!$result['success']) {
return $result;
}
$indices = $result['data'] ?? [];
$deleteResults = [];
$success = true;
foreach ($indices as $index) {
$indexName = $index['name'] ?? '';
if (!empty($indexName)) {
$deleteResult = $instance->request("/api/index/{$indexName}", null, 'DELETE');
$deleteResults[$indexName] = $deleteResult;
if (!$deleteResult['success']) {
$success = false;
}
}
}
return [
'success' => $success,
'message' => $success ? '所有索引删除成功' : '部分索引删除失败',
'details' => $deleteResults
];
}
/**
* 分析文本
*/
public static function analyze($analyzer, $text): array
{
$body = json_encode([
'analyzer' => $analyzer,
'text' => $text
]);
return (new self())->request("/api/_analyze", $body);
}
// ==============================
// 文档管理相关方法
// ==============================
/**
* 写入单条文档
*/
public static function addDoc($index, $doc): array
{
$body = json_encode($doc);
return (new self())->request("/api/{$index}/_doc", $body);
}
/**
* 更新文档
*/
public static function updateDoc($index, $id, $doc): array
{
$body = json_encode($doc);
return (new self())->request("/api/{$index}/_update/{$id}", $body);
}
/**
* 删除文档
*/
public static function deleteDoc($index, $id): array
{
return (new self())->request("/api/{$index}/_doc/{$id}", null, 'DELETE');
}
/**
* 批量写入文档
*/
public static function addDocs($index, $docs): array
{
$body = json_encode([
'index' => $index,
'records' => $docs
]);
return (new self())->request("/api/_bulkv2", $body);
}
/**
* 使用原始BULK API批量写入文档
* 请求格式为Elasticsearch兼容格式
*/
public static function bulkDocs($data): array
{
return (new self())->request("/api/_bulk", $data, 'BULK');
}
// ==============================
// 搜索相关方法
// ==============================
/**
* 查询文档
*/
public static function search($index, $query, $from = 0, $size = 10): array
{
$searchParams = [
'search_type' => 'match',
'query' => [
'term' => $query
],
'from' => $from,
'max_results' => $size
];
$body = json_encode($searchParams);
return (new self())->request("/api/{$index}/_search", $body);
}
/**
* 高级查询文档
*/
public static function advancedSearch($index, $searchParams): array
{
$body = json_encode($searchParams);
return (new self())->request("/api/{$index}/_search", $body);
}
/**
* 兼容ES查询文档
*/
public static function elasticSearch($index, $searchParams): array
{
$body = json_encode($searchParams);
return (new self())->request("/es/{$index}/_search", $body);
}
/**
* 多索引查询
*/
public static function multiSearch($queries): array
{
$body = json_encode($queries);
return (new self())->request("/api/_msearch", $body);
}
}

View File

@@ -1,612 +0,0 @@
<?php
namespace App\Module\ZincSearch;
use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogUser;
use App\Module\Apps;
use Carbon\Carbon;
use DB;
use Illuminate\Support\Facades\Log;
/**
* ZincSearch 会话消息类
*
* 使用方法:
*
* 1. 基础方法
* - 清空所有数据: clear();
*
* 2. 搜索方法
* - 关键词搜索: search('用户ID', '关键词');
*
* 3. 基本方法
* - 单个同步: sync(WebSocketDialogMsg $dialogMsg);
* - 批量同步: batchSync(WebSocketDialogMsg[] $dialogMsgs);
* - 用户同步: userSync(WebSocketDialogUser $dialogUser);
* - 删除消息: delete(WebSocketDialogMsg|WebSocketDialogUser|int $data);
*/
class ZincSearchDialogMsg
{
/**
* 索引名称
*/
protected static string $indexNameMsg = 'dialogMsg';
protected static string $indexNameUser = 'dialogUser';
// ==============================
// 基础方法
// ==============================
/**
* 确保索引存在
*/
private static function ensureIndex(): bool
{
if (!ZincSearchBase::indexExists(self::$indexNameMsg)) {
$mappings = [
'properties' => [
// 拓展数据
'dialog_userid' => ['type' => 'keyword', 'index' => true], // 对话ID+用户ID
'to_userid' => ['type' => 'numeric', 'index' => true], // 此消息发给的用户ID
// 消息数据
'id' => ['type' => 'numeric', 'index' => true],
'dialog_id' => ['type' => 'numeric', 'index' => true],
'dialog_type' => ['type' => 'keyword', 'index' => true],
'session_id' => ['type' => 'numeric', 'index' => true],
'userid' => ['type' => 'numeric', 'index' => true],
'type' => ['type' => 'keyword', 'index' => true],
'key' => ['type' => 'text', 'index' => true],
'created_at' => ['type' => 'date', 'index' => true],
'updated_at' => ['type' => 'date', 'index' => true],
]
];
$result = ZincSearchBase::createIndex(self::$indexNameMsg, $mappings);
return $result['success'] ?? false;
}
if (!ZincSearchBase::indexExists(self::$indexNameUser)) {
$mappings = [
'properties' => [
// 拓展数据
'dialog_userid' => ['type' => 'keyword', 'index' => true], // 对话ID+用户ID
// 用户数据
'id' => ['type' => 'numeric', 'index' => true],
'dialog_id' => ['type' => 'numeric', 'index' => true],
'userid' => ['type' => 'numeric', 'index' => true],
'top_at' => ['type' => 'date', 'index' => true],
'last_at' => ['type' => 'date', 'index' => true],
'mark_unread' => ['type' => 'numeric', 'index' => true],
'silence' => ['type' => 'numeric', 'index' => true],
'hide' => ['type' => 'numeric', 'index' => true],
'color' => ['type' => 'keyword', 'index' => true],
'created_at' => ['type' => 'date', 'index' => true],
'updated_at' => ['type' => 'date', 'index' => true],
]
];
$result = ZincSearchBase::createIndex(self::$indexNameUser, $mappings);
return $result['success'] ?? false;
}
return true;
}
/**
* 清空所有键值
*
* @return bool 是否成功
*/
public static function clear(): bool
{
// 检查索引是否存在然后删除
if (ZincSearchBase::indexExists(self::$indexNameMsg)) {
$deleteResult = ZincSearchBase::deleteIndex(self::$indexNameMsg);
if (!($deleteResult['success'] ?? false)) {
return false;
}
}
if (ZincSearchBase::indexExists(self::$indexNameUser)) {
$deleteResult = ZincSearchBase::deleteIndex(self::$indexNameUser);
if (!($deleteResult['success'] ?? false)) {
return false;
}
}
return self::ensureIndex();
}
// ==============================
// 搜索方法
// ==============================
/**
* 根据用户ID和消息关键词搜索会话
*
* @param string $userid 用户ID
* @param string $keyword 消息关键词
* @param int $from 起始位置
* @param int $size 返回结果数量
* @return array
*/
public static function search(string $userid, string $keyword, int $from = 0, int $size = 20): array
{
if (!Apps::isInstalled("search")) {
// 如果搜索功能未安装,使用数据库查询
return self::searchByMysql($userid, $keyword, $from, $size);
}
$searchParams = [
'query' => [
'bool' => [
'must' => [
['term' => ['to_userid' => $userid]],
['match_phrase' => ['key' => $keyword]]
]
]
],
'from' => $from,
'size' => $size,
'sort' => [
['updated_at' => 'desc']
]
];
try {
$result = ZincSearchBase::elasticSearch(self::$indexNameMsg, $searchParams);
$hits = $result['data']['hits']['hits'] ?? [];
// 收集所有的用户信息
$dialogUserids = [];
foreach ($hits as $hit) {
$source = $hit['_source'];
$dialogUserids[] = $source['dialog_userid'];
}
$userInfos = self::searchUser(array_unique($dialogUserids));
// 组合返回结果,将用户信息合并到消息中
$msgs = [];
foreach ($hits as $hit) {
$msgInfo = $hit['_source'];
$userInfo = $userInfos[$msgInfo['dialog_userid']] ?? [];
if ($userInfo) {
$msgs[] = [
'id' => $msgInfo['dialog_id'],
'search_msg_id' => $msgInfo['id'],
'user_at' => Carbon::parse($msgInfo['updated_at'])->format('Y-m-d H:i:s'),
'mark_unread' => $userInfo['mark_unread'],
'silence' => $userInfo['silence'],
'hide' => $userInfo['hide'],
'color' => $userInfo['color'],
'top_at' => Carbon::parse($userInfo['top_at'])->format('Y-m-d H:i:s'),
'last_at' => Carbon::parse($userInfo['last_at'])->format('Y-m-d H:i:s'),
];
}
}
return $msgs;
} catch (\Exception $e) {
Log::error('search: ' . $e->getMessage());
return [];
}
}
/**
* 根据用户ID和消息关键词搜索会话MySQL 版本主要用于未安装ZincSearch的情况
*
* @param string $userid 用户ID
* @param string $keyword 消息关键词
* @param int $from 起始位置
* @param int $size 返回结果数量
* @return array
*/
private static function searchByMysql(string $userid, string $keyword, int $from = 0, int $size = 20): array
{
$items = DB::table('web_socket_dialog_users as u')
->select(['d.*', 'u.top_at', 'u.last_at', 'u.mark_unread', 'u.silence', 'u.hide', 'u.color', 'u.updated_at as user_at', 'm.id as search_msg_id'])
->join('web_socket_dialogs as d', 'u.dialog_id', '=', 'd.id')
->join('web_socket_dialog_msgs as m', 'm.dialog_id', '=', 'd.id')
->where('u.userid', $userid)
->where('m.bot', 0)
->whereNull('d.deleted_at')
->where('m.key', 'like', "%{$keyword}%")
->orderByDesc('m.id')
->offset($from)
->limit($size)
->get()
->all();
$msgs = [];
foreach ($items as $item) {
$msgs[] = [
'id' => $item->id,
'search_msg_id' => $item->search_msg_id,
'user_at' => Carbon::parse($item->user_at)->format('Y-m-d H:i:s'),
'mark_unread' => $item->mark_unread,
'silence' => $item->silence,
'hide' => $item->hide,
'color' => $item->color,
'top_at' => Carbon::parse($item->top_at)->format('Y-m-d H:i:s'),
'last_at' => Carbon::parse($item->last_at)->format('Y-m-d H:i:s'),
];
}
return $msgs;
}
/**
* 根据对话用户ID搜索用户信息
* @param array $dialogUserids
* @return array
*/
private static function searchUser(array $dialogUserids): array
{
if (empty($dialogUserids)) {
return [];
}
$userInfos = [];
// 构建用户查询条件
$userSearchParams = [
'query' => [
'bool' => [
'should' => []
]
],
'size' => count($dialogUserids) // 确保取到所有符合条件的记录
];
// 添加所有 dialog_userid 到查询条件
foreach ($dialogUserids as $dialogUserid) {
$userSearchParams['query']['bool']['should'][] = [
'term' => ['dialog_userid' => $dialogUserid]
];
}
// 查询用户信息
$userResult = ZincSearchBase::elasticSearch(self::$indexNameUser, $userSearchParams);
$userHits = $userResult['data']['hits']['hits'] ?? [];
// 以 dialog_userid 为键保存用户信息
foreach ($userHits as $userHit) {
$userSource = $userHit['_source'];
$userInfos[$userSource['dialog_userid']] = $userSource;
}
return $userInfos;
}
// ==============================
// 生成内容
// ==============================
/**
* 生成 dialog_userid
*
* @param WebSocketDialogUser $dialogUser
* @return string
*/
private static function generateDialogUserid(WebSocketDialogUser $dialogUser): string
{
return "{$dialogUser->dialog_id}_{$dialogUser->userid}";
}
/**
* 生成文档内容
*
* @param WebSocketDialogMsg $dialogMsg
* @param WebSocketDialogUser $dialogUser
* @return array
*/
private static function generateMsgData(WebSocketDialogMsg $dialogMsg, WebSocketDialogUser $dialogUser): array
{
return [
'_id' => self::$indexNameMsg . "_" . $dialogMsg->id . "_" . $dialogUser->userid,
'dialog_userid' => self::generateDialogUserid($dialogUser),
'to_userid' => $dialogUser->userid,
'id' => $dialogMsg->id,
'dialog_id' => $dialogMsg->dialog_id,
'dialog_type' => $dialogMsg->dialog_type,
'session_id' => $dialogMsg->session_id,
'userid' => $dialogMsg->userid,
'type' => $dialogMsg->type,
'key' => $dialogMsg->key,
'created_at' => $dialogMsg->created_at,
'updated_at' => $dialogMsg->updated_at,
];
}
private static function generateUserData(WebSocketDialogUser $dialogUser): array
{
return [
'_id' => self::$indexNameUser . "_" . $dialogUser->id,
'dialog_userid' => self::generateDialogUserid($dialogUser),
'id' => $dialogUser->id,
'dialog_id' => $dialogUser->dialog_id,
'userid' => $dialogUser->userid,
'top_at' => $dialogUser->top_at,
'last_at' => $dialogUser->last_at,
'mark_unread' => $dialogUser->mark_unread,
'silence' => $dialogUser->silence,
'hide' => $dialogUser->hide,
'color' => $dialogUser->color,
'created_at' => $dialogUser->created_at,
'updated_at' => $dialogUser->updated_at,
];
}
// ==============================
// 基本方法
// ==============================
/**
* 同步消息(建议在异步进程中使用)
*
* @param WebSocketDialogMsg $dialogMsg
* @return bool
*/
public static function sync(WebSocketDialogMsg $dialogMsg): bool
{
if (!self::ensureIndex()) {
return false;
}
if ($dialogMsg->bot) {
// 如果是机器人消息,跳过
return true;
}
try {
// 获取此会话的所有用户
$dialogUsers = WebSocketDialogUser::whereDialogId($dialogMsg->dialog_id)->get();
if ($dialogUsers->isEmpty()) {
return true;
}
$msgs = [];
$users = [];
foreach ($dialogUsers as $dialogUser) {
if (empty($dialogMsg->key)) {
// 如果消息没有关键词,跳过
continue;
}
if ($dialogUser->userid == 0) {
// 跳过系统用户
continue;
}
$msgs[] = self::generateMsgData($dialogMsg, $dialogUser);
$users[$dialogUser->id] = self::generateUserData($dialogUser);
}
if ($msgs) {
// 批量写入消息
ZincSearchBase::addDocs(self::$indexNameMsg, $msgs);
}
if ($users) {
// 批量写入用户
ZincSearchBase::addDocs(self::$indexNameUser, array_values($users));
}
return true;
} catch (\Exception $e) {
Log::error('sync: ' . $e->getMessage());
return false;
}
}
/**
* 批量同步消息(建议在异步进程中使用)
*
* @param WebSocketDialogMsg[] $dialogMsgs
* @return int 成功同步的消息数
*/
public static function batchSync($dialogMsgs): int
{
if (!self::ensureIndex()) {
return 0;
}
$count = 0;
try {
$msgs = [];
$users = [];
$userDialogs = [];
// 预处理收集所有涉及的对话ID
$dialogIds = [];
foreach ($dialogMsgs as $dialogMsg) {
$dialogIds[] = $dialogMsg->dialog_id;
}
$dialogIds = array_unique($dialogIds);
// 获取所有相关的用户-对话关系
if (!empty($dialogIds)) {
$dialogUsers = WebSocketDialogUser::whereIn('dialog_id', $dialogIds)->get();
// 按对话ID组织用户
foreach ($dialogUsers as $dialogUser) {
$userDialogs[$dialogUser->dialog_id][] = $dialogUser;
}
}
// 为每条消息准备所有相关用户的文档
foreach ($dialogMsgs as $dialogMsg) {
if (!isset($userDialogs[$dialogMsg->dialog_id])) {
// 如果该会话没有用户,跳过
continue;
}
if ($dialogMsg->bot) {
// 如果是机器人消息,跳过
continue;
}
/** @var WebSocketDialogUser $dialogUser */
foreach ($userDialogs[$dialogMsg->dialog_id] as $dialogUser) {
if (empty($dialogMsg->key)) {
// 如果消息没有关键词,跳过
continue;
}
if ($dialogUser->userid == 0) {
// 跳过系统用户
continue;
}
$msgs[] = self::generateMsgData($dialogMsg, $dialogUser);
$users[$dialogUser->id] = self::generateUserData($dialogUser);
$count++;
}
}
if ($msgs) {
// 批量写入消息
ZincSearchBase::addDocs(self::$indexNameMsg, $msgs);
}
if ($users) {
// 批量写入用户
ZincSearchBase::addDocs(self::$indexNameUser, array_values($users));
}
} catch (\Exception $e) {
Log::error('batchSync: ' . $e->getMessage());
}
return $count;
}
/**
* 同步用户(建议在异步进程中使用)
* @param WebSocketDialogUser $dialogUser
* @return bool
*/
public static function userSync(WebSocketDialogUser $dialogUser): bool
{
if (!self::ensureIndex()) {
return false;
}
$data = self::generateUserData($dialogUser);
// 生成查询用户条件
$searchParams = [
'query' => [
'bool' => [
'must' => [
['term' => ['dialog_userid' => $data['dialog_userid']]]
]
]
],
'size' => 1
];
try {
// 查询用户是否存在
$result = ZincSearchBase::elasticSearch(self::$indexNameUser, $searchParams);
$hits = $result['data']['hits']['hits'] ?? [];
// 同步用户(存在更新、不存在添加)
$result = ZincSearchBase::addDoc(self::$indexNameUser, $data);
if (!isset($result['success'])) {
return false;
}
// 用户不存在,同步消息
if (empty($hits)) {
$lastId = 0; // 上次同步的最后ID
$batchSize = 500; // 每批处理的消息数量
// 分批同步消息
do {
// 获取一批
$dialogMsgs = WebSocketDialogMsg::whereDialogId($dialogUser->dialog_id)
->where('id', '>', $lastId)
->orderBy('id')
->limit($batchSize)
->get();
if ($dialogMsgs->isEmpty()) {
break;
}
// 同步数据
ZincSearchDialogMsg::batchSync($dialogMsgs);
// 更新最后ID
$lastId = $dialogMsgs->last()->id;
} while (count($dialogMsgs) == $batchSize);
}
return true;
} catch (\Exception $e) {
Log::error('userSync: ' . $e->getMessage());
return false;
}
}
/**
* 删除(建议在异步进程中使用)
*
* @param WebSocketDialogMsg|WebSocketDialogUser|int $data
* @return int
*/
public static function delete(mixed $data): int
{
$batchSize = 500; // 每批处理的文档数量
$totalDeleted = 0; // 总共删除的文档数量
$from = 0;
// 根据数据类型生成查询条件
if ($data instanceof WebSocketDialogMsg) {
$query = [
'field' => 'id',
'term' => (string) $data->id
];
} elseif ($data instanceof WebSocketDialogUser) {
$query = [
'field' => 'dialog_userid',
'term' => self::generateDialogUserid($data),
];
} else {
$query = [
'field' => 'id',
'term' => (string) $data
];
}
try {
while (true) {
// 根据消息ID查找相关文档
$result = ZincSearchBase::advancedSearch(self::$indexNameMsg, [
'search_type' => 'term',
'query' => $query,
'from' => $from,
'max_results' => $batchSize
]);
$hits = $result['data']['hits']['hits'] ?? [];
// 如果没有更多文档,退出循环
if (empty($hits)) {
break;
}
// 删除本批次找到的所有文档
foreach ($hits as $hit) {
if (isset($hit['_id'])) {
ZincSearchBase::deleteDoc(self::$indexNameMsg, $hit['_id']);
$totalDeleted++;
}
}
// 如果返回的文档数少于批次大小,说明已经没有更多文档了
if (count($hits) < $batchSize) {
break;
}
// 移动到下一批
$from += $batchSize;
}
} catch (\Exception $e) {
Log::error('delete: ' . $e->getMessage());
}
return $totalDeleted;
}
}

View File

@@ -1,276 +0,0 @@
<?php
namespace App\Module\ZincSearch;
/**
* ZincSearch 键值存储类
*
* 使用方法:
*
* 1. 基础方法
* - 确保索引存在: ensureIndex();
* - 清空所有数据: clear();
*
* 2. 基本操作
* - 设置键值: set('site_name', '我的网站');
* - 设置复杂数据: set('site_config', ['logo' => 'logo.png', 'theme' => 'dark']);
* - 合并现有数据: set('site_config', ['footer' => '版权所有'], true);
* - 获取键值: $siteName = get('site_name');
* - 获取键值带默认值: $theme = get('theme', 'light');
* - 删除键值: delete('temporary_data');
*
* 3. 批量操作
* - 批量设置: batchSet(['user_count' => 100, 'active_users' => 50]);
* - 批量获取: $stats = batchGet(['user_count', 'active_users']);
*/
class ZincSearchKeyValue
{
/**
* 索引名称
*/
protected static string $indexName = 'keyValue';
// ==============================
// 基础方法
// ==============================
/**
* 确保索引存在
*/
public static function ensureIndex(): bool
{
if (!ZincSearchBase::indexExists(self::$indexName)) {
$mappings = [
'properties' => [
'key' => ['type' => 'keyword', 'index' => true],
'value' => ['type' => 'text', 'index' => true],
'created_at' => ['type' => 'date', 'index' => true],
'updated_at' => ['type' => 'date', 'index' => true]
]
];
$result = ZincSearchBase::createIndex(self::$indexName, $mappings);
return $result['success'] ?? false;
}
return true;
}
/**
* 清空所有键值
*
* @return bool 是否成功
*/
public static function clear(): bool
{
// 检查索引是否存在
if (!ZincSearchBase::indexExists(self::$indexName)) {
return true;
}
// 删除再重建索引
$deleteResult = ZincSearchBase::deleteIndex(self::$indexName);
if (!($deleteResult['success'] ?? false)) {
return false;
}
return self::ensureIndex();
}
// ==============================
// 基本操作
// ==============================
/**
* 设置键值
*
* @param string $key 键名
* @param mixed $value 值
* @param bool $merge 是否合并现有数据(如果值是数组)
* @return bool 是否成功
*/
public static function set(string $key, mixed $value, bool $merge = false): bool
{
if (!self::ensureIndex()) {
return false;
}
// 检查键是否已存在
if ($merge && is_array($value)) {
$existingData = self::get($key);
if (is_array($existingData)) {
$value = array_merge($existingData, $value);
}
}
// 检查是否存在相同键的文档 - 使用精确查询而不是普通搜索
$searchParams = [
'search_type' => 'term',
'query' => [
'field' => 'key',
'term' => $key
],
'from' => 0,
'max_results' => 1
];
$result = ZincSearchBase::advancedSearch(self::$indexName, $searchParams);
$docs = $result['data']['hits']['hits'] ?? [];
$now = date('c');
if (!empty($docs)) {
$docId = $docs[0]['_id'] ?? null;
if ($docId) {
// 更新现有文档
$docData = [
'key' => $key,
'value' => $value,
'updated_at' => $now
];
$updateResult = ZincSearchBase::updateDoc(self::$indexName, $docId, $docData);
return $updateResult['success'] ?? false;
}
}
// 创建新文档
$docData = [
'key' => $key,
'value' => $value,
'created_at' => $now,
'updated_at' => $now
];
$addResult = ZincSearchBase::addDoc(self::$indexName, $docData);
return $addResult['success'] ?? false;
}
/**
* 获取键值
*
* @param string $key 键名
* @param mixed $default 默认值
* @return mixed 值或默认值
*/
public static function get(string $key, mixed $default = null): mixed
{
if (!self::ensureIndex() || empty($key)) {
return $default;
}
// 精确匹配键名
$searchParams = [
'search_type' => 'term',
'query' => [
'field' => 'key',
'term' => $key
],
'from' => 0,
'max_results' => 1
];
$result = ZincSearchBase::advancedSearch(self::$indexName, $searchParams);
if (!($result['success'] ?? false)) {
return $default;
}
$hits = $result['data']['hits']['hits'] ?? [];
if (empty($hits)) {
return $default;
}
return $hits[0]['_source']['value'] ?? $default;
}
/**
* 删除键值
*
* @param string $key 键名
* @return bool 是否成功
*/
public static function delete(string $key): bool
{
if (!self::ensureIndex() || empty($key)) {
return false;
}
// 查找文档ID
$searchParams = [
'search_type' => 'term',
'query' => [
'field' => 'key',
'term' => $key
],
'from' => 0,
'max_results' => 1
];
$result = ZincSearchBase::advancedSearch(self::$indexName, $searchParams);
if (!($result['success'] ?? false)) {
return false;
}
$hits = $result['data']['hits']['hits'] ?? [];
if (empty($hits)) {
return true; // 不存在视为删除成功
}
$docId = $hits[0]['_id'] ?? null;
if (empty($docId)) {
return false;
}
$deleteResult = ZincSearchBase::deleteDoc(self::$indexName, $docId);
return $deleteResult['success'] ?? false;
}
// ==============================
// 批量操作
// ==============================
/**
* 批量设置键值对
*
* @param array $keyValues 键值对数组
* @return bool 是否全部成功
*/
public static function batchSet(array $keyValues): bool
{
if (!self::ensureIndex() || empty($keyValues)) {
return false;
}
$docs = [];
$now = date('c');
foreach ($keyValues as $key => $value) {
$docs[] = [
'key' => $key,
'value' => $value,
'created_at' => $now,
'updated_at' => $now
];
}
$result = ZincSearchBase::addDocs(self::$indexName, $docs);
return $result['success'] ?? false;
}
/**
* 批量获取键值
*
* @param array $keys 键名数组
* @return array 键值对数组
*/
public static function batchGet(array $keys): array
{
if (!self::ensureIndex() || empty($keys)) {
return [];
}
$results = [];
// 遍历查询每个键
foreach ($keys as $key) {
$results[$key] = self::get($key);
}
return $results;
}
}

View File

@@ -3,17 +3,46 @@
namespace App\Observers;
use Hhxsv5\LaravelS\Swoole\Task\Task;
use Illuminate\Support\Facades\Cache;
class AbstractObserver
{
/**
* 任务去重窗口时间(秒)
* 同一个 action+id 在此时间内只投递一次
*/
private const DEDUP_WINDOW = 10;
/**
* 投递异步任务(带去重)
*
* @param $task
* @return void
*/
public static function taskDeliver($task)
{
if (app()->bound('swoole')) {
Task::deliver($task);
if (!app()->bound('swoole')) {
return;
}
// 对 ManticoreSyncTask 进行去重
if ($task instanceof \App\Tasks\ManticoreSyncTask) {
$action = $task->getAction();
$dataId = $task->getDataId();
if ($action && $dataId) {
$cacheKey = "manticore_task:{$action}:{$dataId}";
// 如果已有相同任务在等待,跳过本次投递
if (Cache::has($cacheKey)) {
return;
}
// 标记任务已投递
Cache::put($cacheKey, true, self::DEDUP_WINDOW);
}
}
Task::deliver($task);
}
}

View File

@@ -0,0 +1,95 @@
<?php
namespace App\Observers;
use App\Models\File;
use App\Tasks\ManticoreSyncTask;
class FileObserver extends AbstractObserver
{
/**
* Handle the File "created" event.
*
* @param \App\Models\File $file
* @return void
*/
public function created(File $file)
{
// 文件夹不需要同步
if ($file->type === 'folder') {
return;
}
self::taskDeliver(new ManticoreSyncTask('file_sync', $file->toArray()));
}
/**
* Handle the File "updated" event.
*
* @param \App\Models\File $file
* @return void
*/
public function updated(File $file)
{
// 检查共享设置是否变化(影响子文件的 pshare
if ($file->type === 'folder' && $file->isDirty('share')) {
// 共享文件夹的 share 字段变化,需要批量更新子文件的 pshare
// 注意updateShare 方法会批量更新,但不会触发 Observer
$newPshare = $file->share ? $file->id : 0;
$childFileIds = File::where('pids', 'like', "%,{$file->id},%")
->where('type', '!=', 'folder')
->pluck('id')
->toArray();
if (!empty($childFileIds)) {
self::taskDeliver(new ManticoreSyncTask('file_pshare_update', [
'file_ids' => $childFileIds,
'pshare' => $newPshare,
]));
}
return;
}
// 文件夹不需要同步内容
if ($file->type === 'folder') {
return;
}
self::taskDeliver(new ManticoreSyncTask('file_sync', $file->toArray()));
}
/**
* Handle the File "deleted" event.
*
* @param \App\Models\File $file
* @return void
*/
public function deleted(File $file)
{
self::taskDeliver(new ManticoreSyncTask('file_delete', $file->toArray()));
}
/**
* Handle the File "restored" event.
*
* @param \App\Models\File $file
* @return void
*/
public function restored(File $file)
{
// 文件夹不需要同步
if ($file->type === 'folder') {
return;
}
self::taskDeliver(new ManticoreSyncTask('file_sync', $file->toArray()));
}
/**
* Handle the File "force deleted" event.
*
* @param \App\Models\File $file
* @return void
*/
public function forceDeleted(File $file)
{
self::taskDeliver(new ManticoreSyncTask('file_delete', $file->toArray()));
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace App\Observers;
use App\Models\FileUser;
use App\Tasks\ManticoreSyncTask;
/**
* FileUser 观察者
*/
class FileUserObserver extends AbstractObserver
{
/**
* Handle the FileUser "created" event.
*
* @param \App\Models\FileUser $fileUser
* @return void
*/
public function created(FileUser $fileUser)
{
// 更新文件权限
self::taskDeliver(new ManticoreSyncTask('update_file_allowed_users', [
'file_id' => $fileUser->file_id,
]));
}
/**
* Handle the FileUser "updated" event.
*
* @param \App\Models\FileUser $fileUser
* @return void
*/
public function updated(FileUser $fileUser)
{
// 更新文件权限
self::taskDeliver(new ManticoreSyncTask('update_file_allowed_users', [
'file_id' => $fileUser->file_id,
]));
}
/**
* Handle the FileUser "deleted" event.
*
* @param \App\Models\FileUser $fileUser
* @return void
*/
public function deleted(FileUser $fileUser)
{
// 更新文件权限
self::taskDeliver(new ManticoreSyncTask('update_file_allowed_users', [
'file_id' => $fileUser->file_id,
]));
}
}

View File

@@ -5,8 +5,9 @@ namespace App\Observers;
use App\Models\Deleted;
use App\Models\Project;
use App\Models\ProjectUser;
use App\Tasks\ManticoreSyncTask;
class ProjectObserver
class ProjectObserver extends AbstractObserver
{
/**
* Handle the Project "created" event.
@@ -16,7 +17,7 @@ class ProjectObserver
*/
public function created(Project $project)
{
//
self::taskDeliver(new ManticoreSyncTask('project_sync', $project->toArray()));
}
/**
@@ -35,6 +36,24 @@ class ProjectObserver
Deleted::forget('project', $project->id, $userids);
}
}
// 检查是否有搜索相关字段变化
$searchableFields = ['name', 'desc', 'archived_at'];
$isDirty = false;
foreach ($searchableFields as $field) {
if ($project->isDirty($field)) {
$isDirty = true;
break;
}
}
if ($isDirty) {
if ($project->archived_at) {
self::taskDeliver(new ManticoreSyncTask('project_delete', ['project_id' => $project->id]));
} else {
self::taskDeliver(new ManticoreSyncTask('project_sync', $project->toArray()));
}
}
}
/**
@@ -46,6 +65,7 @@ class ProjectObserver
public function deleted(Project $project)
{
Deleted::record('project', $project->id, $this->userids($project));
self::taskDeliver(new ManticoreSyncTask('project_delete', ['project_id' => $project->id]));
}
/**
@@ -57,6 +77,7 @@ class ProjectObserver
public function restored(Project $project)
{
Deleted::forget('project', $project->id, $this->userids($project));
self::taskDeliver(new ManticoreSyncTask('project_sync', $project->toArray()));
}
/**
@@ -67,7 +88,7 @@ class ProjectObserver
*/
public function forceDeleted(Project $project)
{
//
self::taskDeliver(new ManticoreSyncTask('project_delete', ['project_id' => $project->id]));
}
/**

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Observers;
use App\Models\ProjectTask;
use App\Models\ProjectTaskContent;
use App\Tasks\ManticoreSyncTask;
class ProjectTaskContentObserver extends AbstractObserver
{
/**
* Handle the ProjectTaskContent "created" event.
* 任务内容创建时,触发任务索引更新
*
* @param \App\Models\ProjectTaskContent $content
* @return void
*/
public function created(ProjectTaskContent $content)
{
$this->syncTaskToManticore($content->task_id);
}
/**
* Handle the ProjectTaskContent "updated" event.
* 任务内容更新时,触发任务索引更新
*
* @param \App\Models\ProjectTaskContent $content
* @return void
*/
public function updated(ProjectTaskContent $content)
{
// 只有内容变化时才需要更新
if ($content->isDirty('content')) {
$this->syncTaskToManticore($content->task_id);
}
}
/**
* Handle the ProjectTaskContent "deleted" event.
* 任务内容删除时,触发任务索引更新
*
* @param \App\Models\ProjectTaskContent $content
* @return void
*/
public function deleted(ProjectTaskContent $content)
{
$this->syncTaskToManticore($content->task_id);
}
/**
* 触发任务同步到 Manticore
*
* @param int|null $taskId 任务ID
* @return void
*/
private function syncTaskToManticore(?int $taskId)
{
if (!$taskId || $taskId <= 0) {
return;
}
$task = ProjectTask::find($taskId);
if (!$task || $task->archived_at || $task->deleted_at) {
return;
}
self::taskDeliver(new ManticoreSyncTask('task_sync', $task->toArray()));
}
}

View File

@@ -7,8 +7,9 @@ use App\Models\ProjectTask;
use App\Models\ProjectTaskUser;
use App\Models\ProjectTaskVisibilityUser;
use App\Models\ProjectUser;
use App\Tasks\ManticoreSyncTask;
class ProjectTaskObserver
class ProjectTaskObserver extends AbstractObserver
{
/**
* Handle the ProjectTask "created" event.
@@ -18,7 +19,7 @@ class ProjectTaskObserver
*/
public function created(ProjectTask $projectTask)
{
//
self::taskDeliver(new ManticoreSyncTask('task_sync', $projectTask->toArray()));
}
/**
@@ -39,6 +40,28 @@ class ProjectTaskObserver
Deleted::forget('projectTask', $projectTask->id, self::userids($projectTask));
}
}
// 检查是否有搜索相关字段变化或权限相关字段变化
// visibility 变化会影响 allowed_users 来源
// parent_id 变化会影响子任务继承
// project_id 变化会影响 visibility=1 的任务权限
$searchableFields = ['name', 'desc', 'archived_at', 'project_id', 'visibility', 'parent_id'];
$isDirty = false;
foreach ($searchableFields as $field) {
if ($projectTask->isDirty($field)) {
$isDirty = true;
break;
}
}
if ($isDirty) {
if ($projectTask->archived_at) {
self::taskDeliver(new ManticoreSyncTask('task_delete', ['task_id' => $projectTask->id]));
} else {
// 重新同步任务(会重新计算 allowed_users
self::taskDeliver(new ManticoreSyncTask('task_sync', $projectTask->toArray()));
}
}
}
/**
@@ -50,6 +73,7 @@ class ProjectTaskObserver
public function deleted(ProjectTask $projectTask)
{
Deleted::record('projectTask', $projectTask->id, self::userids($projectTask));
self::taskDeliver(new ManticoreSyncTask('task_delete', ['task_id' => $projectTask->id]));
}
/**
@@ -61,6 +85,7 @@ class ProjectTaskObserver
public function restored(ProjectTask $projectTask)
{
Deleted::forget('projectTask', $projectTask->id, self::userids($projectTask));
self::taskDeliver(new ManticoreSyncTask('task_sync', $projectTask->toArray()));
}
/**
@@ -71,7 +96,7 @@ class ProjectTaskObserver
*/
public function forceDeleted(ProjectTask $projectTask)
{
//
self::taskDeliver(new ManticoreSyncTask('task_delete', ['task_id' => $projectTask->id]));
}
/**

View File

@@ -5,8 +5,9 @@ namespace App\Observers;
use App\Models\Deleted;
use App\Models\ProjectTaskUser;
use App\Models\ProjectUser;
use App\Tasks\ManticoreSyncTask;
class ProjectTaskUserObserver
class ProjectTaskUserObserver extends AbstractObserver
{
/**
* Handle the ProjectTaskUser "created" event.
@@ -20,6 +21,17 @@ class ProjectTaskUserObserver
if ($projectTaskUser->task_pid) {
Deleted::forget('projectTask', $projectTaskUser->task_pid, $projectTaskUser->userid);
}
// 更新任务权限
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $projectTaskUser->task_id,
]));
// 如果是子任务,也更新父任务
if ($projectTaskUser->task_pid) {
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $projectTaskUser->task_pid,
]));
}
}
/**
@@ -30,7 +42,18 @@ class ProjectTaskUserObserver
*/
public function updated(ProjectTaskUser $projectTaskUser)
{
//
// userid 变更时需要更新任务权限(移交场景)
if ($projectTaskUser->isDirty('userid')) {
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $projectTaskUser->task_id,
]));
// 如果是子任务,也更新父任务
if ($projectTaskUser->task_pid) {
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $projectTaskUser->task_pid,
]));
}
}
}
/**
@@ -44,6 +67,11 @@ class ProjectTaskUserObserver
if (!ProjectUser::whereProjectId($projectTaskUser->project_id)->whereUserid($projectTaskUser->userid)->exists()) {
Deleted::record('projectTask', $projectTaskUser->task_id, $projectTaskUser->userid);
}
// 更新任务权限
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $projectTaskUser->task_id,
]));
}
/**

View File

@@ -0,0 +1,78 @@
<?php
namespace App\Observers;
use App\Models\ProjectTaskVisibilityUser;
use App\Tasks\ManticoreSyncTask;
/**
* ProjectTaskVisibilityUser 观察者
*
* 用于处理任务 visibility=3指定成员可见时的成员变更同步
*/
class ProjectTaskVisibilityUserObserver extends AbstractObserver
{
/**
* Handle the ProjectTaskVisibilityUser "created" event.
*
* @param \App\Models\ProjectTaskVisibilityUser $visibilityUser
* @return void
*/
public function created(ProjectTaskVisibilityUser $visibilityUser)
{
// 更新任务权限
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $visibilityUser->task_id,
]));
}
/**
* Handle the ProjectTaskVisibilityUser "updated" event.
*
* @param \App\Models\ProjectTaskVisibilityUser $visibilityUser
* @return void
*/
public function updated(ProjectTaskVisibilityUser $visibilityUser)
{
// 更新任务权限
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $visibilityUser->task_id,
]));
}
/**
* Handle the ProjectTaskVisibilityUser "deleted" event.
*
* @param \App\Models\ProjectTaskVisibilityUser $visibilityUser
* @return void
*/
public function deleted(ProjectTaskVisibilityUser $visibilityUser)
{
// 更新任务权限
self::taskDeliver(new ManticoreSyncTask('update_task_allowed_users', [
'task_id' => $visibilityUser->task_id,
]));
}
/**
* Handle the ProjectTaskVisibilityUser "restored" event.
*
* @param \App\Models\ProjectTaskVisibilityUser $visibilityUser
* @return void
*/
public function restored(ProjectTaskVisibilityUser $visibilityUser)
{
//
}
/**
* Handle the ProjectTaskVisibilityUser "force deleted" event.
*
* @param \App\Models\ProjectTaskVisibilityUser $visibilityUser
* @return void
*/
public function forceDeleted(ProjectTaskVisibilityUser $visibilityUser)
{
//
}
}

View File

@@ -4,8 +4,9 @@ namespace App\Observers;
use App\Models\Deleted;
use App\Models\ProjectUser;
use App\Tasks\ManticoreSyncTask;
class ProjectUserObserver
class ProjectUserObserver extends AbstractObserver
{
/**
* Handle the ProjectUser "created" event.
@@ -16,6 +17,15 @@ class ProjectUserObserver
public function created(ProjectUser $projectUser)
{
Deleted::forget('project', $projectUser->project_id, $projectUser->userid);
// 更新项目权限
self::taskDeliver(new ManticoreSyncTask('update_project_allowed_users', [
'project_id' => $projectUser->project_id,
]));
// 异步级联更新该项目下所有 visibility=1 的任务
self::taskDeliver(new ManticoreSyncTask('cascade_project_users', [
'project_id' => $projectUser->project_id,
]));
}
/**
@@ -26,7 +36,15 @@ class ProjectUserObserver
*/
public function updated(ProjectUser $projectUser)
{
//
// userid 变更时需要更新项目权限和级联任务权限(移交场景)
if ($projectUser->isDirty('userid')) {
self::taskDeliver(new ManticoreSyncTask('update_project_allowed_users', [
'project_id' => $projectUser->project_id,
]));
self::taskDeliver(new ManticoreSyncTask('cascade_project_users', [
'project_id' => $projectUser->project_id,
]));
}
}
/**
@@ -38,6 +56,15 @@ class ProjectUserObserver
public function deleted(ProjectUser $projectUser)
{
Deleted::record('project', $projectUser->project_id, $projectUser->userid);
// 更新项目权限
self::taskDeliver(new ManticoreSyncTask('update_project_allowed_users', [
'project_id' => $projectUser->project_id,
]));
// 异步级联更新该项目下所有 visibility=1 的任务
self::taskDeliver(new ManticoreSyncTask('cascade_project_users', [
'project_id' => $projectUser->project_id,
]));
}
/**

View File

@@ -0,0 +1,127 @@
<?php
namespace App\Observers;
use App\Models\User;
use App\Module\Apps;
use App\Tasks\ManticoreSyncTask;
class UserObserver extends AbstractObserver
{
/**
* 搜索相关字段Manticore 同步)
*/
private static array $searchableFields = [
'nickname', 'email', 'profession', 'introduction', 'disable_at'
];
/**
* 需要监控并触发 user_update hook 的字段
*/
private static array $hookMonitoredFields = [
'email', 'tel', 'nickname', 'profession',
'birthday', 'address', 'introduction', 'department'
];
/**
* Handle the User "created" event.
*
* @param \App\Models\User $user
* @return void
*/
public function created(User $user)
{
// 机器人账号不同步
if ($user->bot) {
return;
}
self::taskDeliver(new ManticoreSyncTask('user_sync', $user->toArray()));
}
/**
* Handle the User "updated" event.
*
* @param \App\Models\User $user
* @return void
*/
public function updated(User $user)
{
// 机器人账号不处理
if ($user->bot) {
return;
}
// 检查是否有搜索相关字段变化Manticore 同步)
$isDirty = false;
foreach (self::$searchableFields as $field) {
if ($user->isDirty($field)) {
$isDirty = true;
break;
}
}
if ($isDirty) {
// 如果用户被禁用,删除索引;否则更新索引
if ($user->disable_at) {
self::taskDeliver(new ManticoreSyncTask('user_delete', ['userid' => $user->userid]));
} else {
self::taskDeliver(new ManticoreSyncTask('user_sync', $user->toArray()));
}
}
// 检测 onboard/offboard 场景disable_at 变化)
if ($user->isDirty('disable_at')) {
$originalDisableAt = $user->getOriginal('disable_at');
$currentDisableAt = $user->disable_at;
if ($originalDisableAt && !$currentDisableAt) {
// disable_at 从有值变为 null → 取消离职 (restore)
Apps::dispatchUserHook($user, 'user_onboard', 'restore');
} elseif (!$originalDisableAt && $currentDisableAt) {
// disable_at 从 null 变为有值 → 离职 (offboarded)
Apps::dispatchUserHook($user, 'user_offboard', 'offboarded');
}
return;
}
// 排除仅 identity 变化的场景
if ($user->isDirty('identity')) {
return;
}
// 检测监控字段变更,触发 user_update hook
$changedFields = [];
foreach (self::$hookMonitoredFields as $field) {
if ($user->isDirty($field)) {
$changedFields[] = $field;
}
}
if (!empty($changedFields)) {
// 判断是用户自己修改还是管理员修改
$currentUser = User::authInfo();
$eventType = ($currentUser && $currentUser->userid === $user->userid)
? 'profile_update'
: 'admin_update';
Apps::dispatchUserHook($user, 'user_update', $eventType, $changedFields);
}
}
/**
* Handle the User "deleted" event.
*
* @param \App\Models\User $user
* @return void
*/
public function deleted(User $user)
{
// Manticore 索引删除
self::taskDeliver(new ManticoreSyncTask('user_delete', ['userid' => $user->userid]));
// 触发 user_offboard (delete) hook
if (!$user->bot) {
Apps::dispatchUserHook($user, 'user_offboard', 'delete');
}
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Observers;
use App\Models\User;
use App\Models\UserTag;
use App\Tasks\ManticoreSyncTask;
class UserTagObserver extends AbstractObserver
{
/**
* Handle the UserTag "created" event.
* 标签创建时,触发用户索引更新
*
* @param \App\Models\UserTag $userTag
* @return void
*/
public function created(UserTag $userTag)
{
$this->syncUserToManticore($userTag->user_id);
}
/**
* Handle the UserTag "updated" event.
* 标签更新时,触发用户索引更新
*
* @param \App\Models\UserTag $userTag
* @return void
*/
public function updated(UserTag $userTag)
{
// 只有标签名称变化时才需要更新
if ($userTag->isDirty('name')) {
$this->syncUserToManticore($userTag->user_id);
}
}
/**
* Handle the UserTag "deleted" event.
* 标签删除时,触发用户索引更新
*
* @param \App\Models\UserTag $userTag
* @return void
*/
public function deleted(UserTag $userTag)
{
$this->syncUserToManticore($userTag->user_id);
}
/**
* 触发用户同步到 Manticore
*
* @param int $userid 用户ID
* @return void
*/
private function syncUserToManticore(int $userid)
{
if ($userid <= 0) {
return;
}
$user = User::find($userid);
if (!$user || $user->bot || $user->disable_at) {
return;
}
self::taskDeliver(new ManticoreSyncTask('user_sync', $user->toArray()));
}
}

View File

@@ -0,0 +1,60 @@
<?php
namespace App\Observers;
use App\Models\User;
use App\Models\UserTag;
use App\Models\UserTagRecognition;
use App\Tasks\ManticoreSyncTask;
class UserTagRecognitionObserver extends AbstractObserver
{
/**
* Handle the UserTagRecognition "created" event.
* 认可创建时,标签排序可能变化,触发用户索引更新
*
* @param \App\Models\UserTagRecognition $recognition
* @return void
*/
public function created(UserTagRecognition $recognition)
{
$this->syncUserByTagId($recognition->tag_id);
}
/**
* Handle the UserTagRecognition "deleted" event.
* 认可删除时,标签排序可能变化,触发用户索引更新
*
* @param \App\Models\UserTagRecognition $recognition
* @return void
*/
public function deleted(UserTagRecognition $recognition)
{
$this->syncUserByTagId($recognition->tag_id);
}
/**
* 根据标签ID触发用户同步
*
* @param int $tagId 标签ID
* @return void
*/
private function syncUserByTagId(int $tagId)
{
if ($tagId <= 0) {
return;
}
$tag = UserTag::find($tagId);
if (!$tag) {
return;
}
$user = User::find($tag->user_id);
if (!$user || $user->bot || $user->disable_at) {
return;
}
self::taskDeliver(new ManticoreSyncTask('user_sync', $user->toArray()));
}
}

View File

@@ -3,7 +3,9 @@
namespace App\Observers;
use App\Models\WebSocketDialogMsg;
use App\Tasks\ZincSearchSyncTask;
use App\Module\Apps;
use App\Module\Manticore\ManticoreMsg;
use App\Tasks\ManticoreSyncTask;
class WebSocketDialogMsgObserver extends AbstractObserver
{
@@ -15,7 +17,10 @@ class WebSocketDialogMsgObserver extends AbstractObserver
*/
public function created(WebSocketDialogMsg $webSocketDialogMsg)
{
self::taskDeliver(new ZincSearchSyncTask('sync', $webSocketDialogMsg->toArray()));
// Manticore 同步(仅在安装 Manticore 且符合索引条件时)
if (Apps::isInstalled('search') && ManticoreMsg::shouldIndex($webSocketDialogMsg)) {
self::taskDeliver(new ManticoreSyncTask('msg_sync', ['msg_id' => $webSocketDialogMsg->id]));
}
}
/**
@@ -26,7 +31,10 @@ class WebSocketDialogMsgObserver extends AbstractObserver
*/
public function updated(WebSocketDialogMsg $webSocketDialogMsg)
{
self::taskDeliver(new ZincSearchSyncTask('sync', $webSocketDialogMsg->toArray()));
// Manticore 同步(更新可能使消息符合或不再符合索引条件,由 sync 方法处理)
if (Apps::isInstalled('search')) {
self::taskDeliver(new ManticoreSyncTask('msg_sync', ['msg_id' => $webSocketDialogMsg->id]));
}
}
/**
@@ -37,7 +45,10 @@ class WebSocketDialogMsgObserver extends AbstractObserver
*/
public function deleted(WebSocketDialogMsg $webSocketDialogMsg)
{
self::taskDeliver(new ZincSearchSyncTask('delete', $webSocketDialogMsg->toArray()));
// Manticore 删除
if (Apps::isInstalled('search')) {
self::taskDeliver(new ManticoreSyncTask('msg_delete', ['msg_id' => $webSocketDialogMsg->id]));
}
}
/**
@@ -59,6 +70,9 @@ class WebSocketDialogMsgObserver extends AbstractObserver
*/
public function forceDeleted(WebSocketDialogMsg $webSocketDialogMsg)
{
//
// Manticore 删除
if (Apps::isInstalled('search')) {
self::taskDeliver(new ManticoreSyncTask('msg_delete', ['msg_id' => $webSocketDialogMsg->id]));
}
}
}

View File

@@ -3,8 +3,10 @@
namespace App\Observers;
use App\Models\Deleted;
use App\Models\UserBot;
use App\Models\WebSocketDialogUser;
use App\Tasks\ZincSearchSyncTask;
use App\Module\Apps;
use App\Tasks\ManticoreSyncTask;
use Carbon\Carbon;
class WebSocketDialogUserObserver extends AbstractObserver
@@ -30,7 +32,19 @@ class WebSocketDialogUserObserver extends AbstractObserver
}
}
Deleted::forget('dialog', $webSocketDialogUser->dialog_id, $webSocketDialogUser->userid);
self::taskDeliver(new ZincSearchSyncTask('userSync', $webSocketDialogUser->toArray()));
// Manticore: 更新对话下所有消息的 allowed_users
if (Apps::isInstalled('search')) {
self::taskDeliver(new ManticoreSyncTask('update_dialog_allowed_users', [
'dialog_id' => $webSocketDialogUser->dialog_id
]));
}
//
$dialog = $webSocketDialogUser->webSocketDialog;
if ($dialog) {
$dialog->dispatchMemberWebhook(UserBot::WEBHOOK_EVENT_MEMBER_JOIN, $webSocketDialogUser->userid, intval($webSocketDialogUser->inviter));
}
}
/**
@@ -41,7 +55,7 @@ class WebSocketDialogUserObserver extends AbstractObserver
*/
public function updated(WebSocketDialogUser $webSocketDialogUser)
{
self::taskDeliver(new ZincSearchSyncTask('userSync', $webSocketDialogUser->toArray()));
//
}
/**
@@ -53,7 +67,20 @@ class WebSocketDialogUserObserver extends AbstractObserver
public function deleted(WebSocketDialogUser $webSocketDialogUser)
{
Deleted::record('dialog', $webSocketDialogUser->dialog_id, $webSocketDialogUser->userid);
self::taskDeliver(new ZincSearchSyncTask('deleteUser', $webSocketDialogUser->toArray()));
// Manticore: 更新对话下所有消息的 allowed_users
if (Apps::isInstalled('search')) {
self::taskDeliver(new ManticoreSyncTask('update_dialog_allowed_users', [
'dialog_id' => $webSocketDialogUser->dialog_id
]));
}
//
$dialog = $webSocketDialogUser->webSocketDialog;
if ($dialog) {
$operatorId = $webSocketDialogUser->operator_id ?? 0;
$dialog->dispatchMemberWebhook(UserBot::WEBHOOK_EVENT_MEMBER_LEAVE, $webSocketDialogUser->userid, intval($operatorId));
}
}
/**

View File

@@ -2,17 +2,31 @@
namespace App\Providers;
use App\Models\File;
use App\Models\FileUser;
use App\Models\Project;
use App\Models\ProjectTask;
use App\Models\ProjectTaskContent;
use App\Models\ProjectTaskUser;
use App\Models\ProjectTaskVisibilityUser;
use App\Models\ProjectUser;
use App\Models\User;
use App\Models\UserTag;
use App\Models\UserTagRecognition;
use App\Models\WebSocketDialog;
use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogUser;
use App\Observers\FileObserver;
use App\Observers\FileUserObserver;
use App\Observers\ProjectObserver;
use App\Observers\ProjectTaskContentObserver;
use App\Observers\ProjectTaskObserver;
use App\Observers\ProjectTaskUserObserver;
use App\Observers\ProjectTaskVisibilityUserObserver;
use App\Observers\ProjectUserObserver;
use App\Observers\UserObserver;
use App\Observers\UserTagObserver;
use App\Observers\UserTagRecognitionObserver;
use App\Observers\WebSocketDialogMsgObserver;
use App\Observers\WebSocketDialogObserver;
use App\Observers\WebSocketDialogUserObserver;
@@ -40,10 +54,17 @@ class EventServiceProvider extends ServiceProvider
*/
public function boot()
{
File::observe(FileObserver::class);
FileUser::observe(FileUserObserver::class);
Project::observe(ProjectObserver::class);
ProjectTask::observe(ProjectTaskObserver::class);
ProjectTaskContent::observe(ProjectTaskContentObserver::class);
ProjectTaskUser::observe(ProjectTaskUserObserver::class);
ProjectTaskVisibilityUser::observe(ProjectTaskVisibilityUserObserver::class);
ProjectUser::observe(ProjectUserObserver::class);
User::observe(UserObserver::class);
UserTag::observe(UserTagObserver::class);
UserTagRecognition::observe(UserTagRecognitionObserver::class);
WebSocketDialog::observe(WebSocketDialogObserver::class);
WebSocketDialogMsg::observe(WebSocketDialogMsgObserver::class);
WebSocketDialogUser::observe(WebSocketDialogUserObserver::class);

View File

@@ -64,7 +64,7 @@ class WebSocketService implements WebSocketHandlerInterface
'ud' => $userid,
],
]));
$this->userOn($fd, $userid);
$this->userOn($fd, $userid, $get['platform']);
} else {
// 用户不存在
$server->push($fd, Base::array2json([
@@ -105,6 +105,11 @@ class WebSocketService implements WebSocketHandlerInterface
// 握手信息
case 'handshake':
// 更新 PC 端活跃时间
$row = WebSocket::whereFd($frame->fd)->first();
if ($row && Base::isPc($row->platform)) {
Cache::put("user_pc_active:{$row->userid}", time(), 60);
}
break;
// 访问状态
@@ -166,17 +171,27 @@ class WebSocketService implements WebSocketHandlerInterface
* 用户上线
* @param $fd
* @param $userid
* @param $platform
* @return void
*/
private function userOn($fd, $userid)
private function userOn($fd, $userid, $platform = 'web')
{
// 校验平台类型
if (!in_array($platform, ['android', 'ios', 'win', 'mac', 'web'])) {
$platform = 'web';
}
WebSocket::updateInsert([
'key' => md5($fd . '@' . $userid)
], [
'fd' => $fd,
'userid' => $userid,
'platform' => $platform,
]);
OnlineData::online($userid);
// PC 端上线时更新活跃时间
if (Base::isPc($platform)) {
Cache::put("user_pc_active:{$userid}", time(), 60);
}
}
/**

View File

@@ -17,6 +17,7 @@ use App\Module\Base;
use App\Module\Doo;
use App\Module\Ihttp;
use App\Module\TextExtractor;
use App\Module\PromptPlaceholder;
use Carbon\Carbon;
use Exception;
use DB;
@@ -427,6 +428,7 @@ class BotReceiveMsgTask extends AbstractTask
private function handleWebhookRequest($sendText, $replyText, WebSocketDialogMsg $msg, WebSocketDialog $dialog, User $botUser)
{
$webhookUrl = null;
$userBot = null;
$extras = ['timestamp' => time()];
try {
@@ -445,7 +447,7 @@ class BotReceiveMsgTask extends AbstractTask
}
// 判断AI应用是否安装
if (!Apps::isInstalled('ai')) {
throw new Exception('应用「AI Robot」未安装');
throw new Exception('应用「AI Assistant」未安装');
}
// 整理机器人参数
$setting = Base::setting('aibotSetting');
@@ -507,17 +509,15 @@ class BotReceiveMsgTask extends AbstractTask
if (empty($extras['api_key'])) {
throw new Exception('机器人未启用。');
}
$this->generateSystemPromptForAI($msg->userid, $dialog, $extras);
$this->generateSystemPromptForAI($msg->userid, $dialog, $botUser, $extras);
// 转换提及格式
$sendText = self::convertMentionForAI($sendText);
$replyText = self::convertMentionForAI($replyText);
if ($replyText) {
$sendText = <<<EOF
<quoted_content>
{$replyText}
</quoted_content>
The content within the above quoted_content tags is a citation.
上述 quoted_content 标签中的内容为引用。
{$sendText}
EOF;
@@ -530,15 +530,10 @@ class BotReceiveMsgTask extends AbstractTask
return;
}
$userBot = UserBot::whereBotId($botUser->userid)->first();
if ($userBot) {
$userBot->webhook_num++;
$userBot->save();
$webhookUrl = $userBot->webhook_url;
if (!$userBot || !$userBot->shouldDispatchWebhook(UserBot::WEBHOOK_EVENT_MESSAGE)) {
return;
}
}
if (!preg_match("/^https?:\/\//", $webhookUrl)) {
return;
}
} catch (\Exception $e) {
WebSocketDialogMsg::sendMsg(null, $msg->dialog_id, 'template', [
'type' => 'content',
@@ -546,145 +541,60 @@ class BotReceiveMsgTask extends AbstractTask
], $botUser->userid, false, false, true); // todo 未能在任务end事件来发送任务
return;
}
//
try {
$data = [
'text' => $sendText,
'reply_text' => $replyText,
'token' => User::generateToken($botUser),
'session_id' => $dialog->session_id,
'dialog_id' => $dialog->id,
'dialog_type' => $dialog->type,
'msg_id' => $msg->id,
'msg_uid' => $msg->userid,
'mention' => $this->mention ? 1 : 0,
'bot_uid' => $botUser->userid,
'version' => Base::getVersion(),
'extras' => Base::array2json($extras)
// 基本请求数据
$data = [
'event' => UserBot::WEBHOOK_EVENT_MESSAGE,
'text' => $sendText,
'reply_text' => $replyText,
'token' => User::generateToken($botUser),
'session_id' => $dialog->session_id,
'dialog_id' => $dialog->id,
'dialog_type' => $dialog->type,
'msg_id' => $msg->id,
'msg_uid' => $msg->userid,
'mention' => $this->mention ? 1 : 0,
'bot_uid' => $botUser->userid,
'extras' => Base::array2json($extras),
'version' => Base::getVersion(),
'timestamp' => time(),
];
// 添加用户信息
$userInfo = User::find($msg->userid);
if ($userInfo) {
$data['msg_user'] = [
'userid' => $userInfo->userid,
'email' => $userInfo->email,
'nickname' => $userInfo->nickname,
'profession' => $userInfo->profession,
'lang' => $userInfo->lang,
'token' => User::generateTokenNoDevice($userInfo, now()->addHour()),
];
// 添加用户信息
$userInfo = User::find($msg->userid);
if ($userInfo) {
$data['msg_user'] = [
'userid' => $userInfo->userid,
'email' => $userInfo->email,
'nickname' => $userInfo->nickname,
'profession' => $userInfo->profession,
'lang' => $userInfo->lang,
'token' => User::generateTokenNoDevice($userInfo, now()->addHour()),
];
}
// 请求Webhook
$result = Ihttp::ihttp_post($webhookUrl, $data, 30);
if ($result['data'] && $data = Base::json2array($result['data'])) {
if ($data['code'] != 200 && $data['message']) {
WebSocketDialogMsg::sendMsg(null, $msg->dialog_id, 'text', [
'text' => $result['data']['message']
], $botUser->userid, false, false, true);
}
}
} catch (\Throwable $th) {
info(Base::array2json([
'bot_userid' => $botUser->userid,
'dialog' => $dialog->id,
'msg' => $msg->id,
'webhook_url' => $webhookUrl,
'error' => $th->getMessage(),
]));
}
}
/**
* 为AI机器人转换提及消息格式
* 将提及的任务、文件、报告转换为AI可理解的格式并提取相关内容
*
* @param string $original 原始消息文本
* @return string 转换后的消息文本,包含相关内容的标签
* @throws Exception 当提及的对象不存在或读取失败时抛出异常
*/
public static function convertMentionForAI($original)
{
$array = [];
$original = preg_replace_callback('/<!--(.*?)#(.*?)#(.*?)-->/', function ($match) use (&$array) {
// 初始化 tag 内容
$pathTag = null;
$pathName = null;
$pathContent = null;
// 根据 type 提取 tag 内容
switch ($match[1]) {
// 任务
case 'task':
$taskInfo = ProjectTask::with(['content'])->whereId(intval($match[2]))->first();
if (!$taskInfo) {
throw new Exception("任务不存在或已被删除");
}
$pathTag = "task_content";
$pathName = addslashes($taskInfo->name) . " (ID:{$taskInfo->id})";
$pathContent = implode("\n", $taskInfo->AIContext());
break;
// 文件
case 'file':
$fileInfo = FileContent::idOrCodeToContent($match[2]);
if (!$fileInfo || !isset($fileInfo->content['url'])) {
throw new Exception("文件不存在或已被删除");
}
$urlPath = public_path($fileInfo->content['url']);
if (!file_exists($urlPath)) {
throw new Exception("文件不存在或已被删除");
}
$fileResult = TextExtractor::extractFile($urlPath);
if (Base::isError($fileResult)) {
throw new Exception("文件读取失败:" . $fileResult['msg']);
}
$pathTag = "file_content";
$pathName = addslashes($match[3]) . " (ID:{$fileInfo->id})";
$pathContent = $fileResult['data'];
break;
// 文件路径
case 'path':
$urlPath = public_path($match[2]);
if (!file_exists($urlPath)) {
throw new Exception("文件不存在或已被删除");
}
$fileResult = TextExtractor::extractFile($urlPath);
if (Base::isError($fileResult)) {
throw new Exception("文件读取失败:" . $fileResult['msg']);
}
$pathTag = "file_content";
$pathName = addslashes($match[3]);
$pathContent = $fileResult['data'];
break;
// 报告
case 'report':
$reportInfo = Report::idOrCodeToContent($match[2]);
if (!$reportInfo) {
throw new Exception("报告不存在或已被删除");
}
$pathTag = "report_content";
$pathName = addslashes($match[3]) . " (ID:{$reportInfo->id})";
$pathContent = Base::html2markdown($reportInfo->content);
break;
}
// 如果提取到 tag 内容,则添加到 contents 数组中
if ($pathTag) {
$array[] = "<{$pathTag} path=\"{$pathName}\">\n{$pathContent}\n</{$pathTag}>";
return "`{$pathName}` (see below for {$pathTag} tag)";
}
return "";
}, $original);
// 添加 tag 内容
if ($array) {
$original .= "\n\n" . implode("\n\n", $array);
}
return $original;
$result = null;
if ($userBot) {
$result = $userBot->dispatchWebhook(UserBot::WEBHOOK_EVENT_MESSAGE, $data);
} else {
try {
$result = Ihttp::ihttp_post($webhookUrl, $data, 30);
} catch (\Throwable $th) {
info(Base::array2json([
'webhook_url' => $webhookUrl,
'data' => $data,
'error' => $th->getMessage(),
]));
}
}
if ($result && isset($result['data'])) {
$responseData = Base::json2array($result['data']);
if (($responseData['code'] ?? 0) === 200 && !empty($responseData['message'])) {
WebSocketDialogMsg::sendMsg(null, $msg->dialog_id, 'text', [
'text' => $responseData['message']
], $botUser->userid, false, false, true);
}
}
}
/**
@@ -693,158 +603,83 @@ class BotReceiveMsgTask extends AbstractTask
*
* @param int|null $userid 用户ID
* @param WebSocketDialog $dialog 对话对象
* @param User $botUser 机器人用户对象
* @param array $extras 额外参数数组通过引用传递以修改system_message
* @return void
*/
private function generateSystemPromptForAI($userid, WebSocketDialog $dialog, array &$extras)
private function generateSystemPromptForAI($userid, WebSocketDialog $dialog, User $botUser, array &$extras)
{
// 构建结构化的系统提示词
$sections = [];
// 基础角色设定(如果有)
if (!empty($extras['system_message'])) {
$sections[] = <<<EOF
<role_setting>
{$extras['system_message']}
</role_setting>
EOF;
}
// 上下文信息(项目、任务、部门等)+ 操作指令
switch ($dialog->type) {
// 用户对话
case "user":
$aiPrompt = WebSocketDialogConfig::where([
'dialog_id' => $dialog->id,
'userid' => $userid,
'type' => 'ai_prompt',
])->value('value');
if ($aiPrompt) {
return $aiPrompt;
}
break;
// 群组对话
case "group":
switch ($dialog->group_type) {
// 用户群
case 'user':
break;
// 项目群
case 'project':
$projectInfo = Project::whereDialogId($dialog->id)->first();
if ($projectInfo) {
$projectDesc = $projectInfo->desc ?: "-";
$projectStatus = $projectInfo->archived_at ? '已归档' : '正在进行中';
$sections[] = <<<EOF
<context_info>
当前我在项目【{$projectInfo->name}】中
项目描述:{$projectDesc}
项目状态:{$projectStatus}
</context_info>
EOF;
$sections[] = <<<EOF
<instructions>
如果你判断我想要或需要添加任务,请按照以下格式回复:
::: create-task-list
title: 任务标题1
desc: 任务描述1
title: 任务标题2
desc: 任务描述2
:::
</instructions>
EOF;
}
break;
// 任务群
case 'task':
$taskInfo = ProjectTask::with(['content'])->whereDialogId($dialog->id)->first();
if ($taskInfo) {
$taskContext = implode("\n", $taskInfo->AIContext());
$sections[] = <<<EOF
<context_info>
当前我在任务【{$taskInfo->name}】中
当前时间:{$taskInfo->updated_at}
任务ID{$taskInfo->id}
{$taskContext}
</context_info>
EOF;
$sections[] = <<<EOF
<instructions>
如果你判断我想要或需要添加子任务,请按照以下格式回复:
::: create-subtask-list
title: 子任务标题1
title: 子任务标题2
:::
</instructions>
EOF;
}
break;
// 部门群
case 'department':
$userDepartment = UserDepartment::whereDialogId($dialog->id)->first();
if ($userDepartment) {
$sections[] = <<<EOF
<context_info>
当前我在【{$userDepartment->name}】的部门群聊中
</context_info>
EOF;
}
break;
// 全体成员群
case 'all':
$sections[] = <<<EOF
<context_info>
当前我在【全体成员】的群聊中
</context_info>
EOF;
break;
}
// 聊天历史
if ($dialog->type === 'group') {
$chatHistory = $this->getRecentChatHistory($dialog, 15);
if ($chatHistory) {
$sections[] = <<<EOF
<chat_history>
{$chatHistory}
</chat_history>
EOF;
}
}
break;
}
// 更新系统提示词
if (!empty($sections)) {
$extras['system_message'] = implode("\n\n", $sections);
// 用户自定义提示词(私聊场景优先使用)
$customPrompt = null;
if ($dialog->type === 'user') {
$customPrompt = WebSocketDialogConfig::where([
'dialog_id' => $dialog->id,
'userid' => $userid,
'type' => 'ai_prompt',
])->value('value');
}
// 添加标签说明
$tagDescs = [
'role_setting' => '你的基础角色和行为定义',
'instructions' => '特定功能的操作指令',
'context_info' => '当前环境和状态信息',
'chat_history' => '最近的对话历史记录',
$prompt = [];
// 1. 基础角色(自定义提示词优先)
if ($customPrompt) {
$prompt[] = $customPrompt;
} elseif (!empty($extras['system_message'])) {
$prompt[] = $extras['system_message'];
}
// 2. 上下文信息
$currentTime = Carbon::now()->toDateTimeString();
$contextLines = [
"您是:{$botUser->nickname}ID: {$botUser->userid}",
"当前对话ID(dialog_id){$dialog->id}",
"当前系统时间(now){$currentTime}",
];
$useTags = [];
foreach ($tagDescs as $tag => $desc) {
if (str_contains($extras['system_message'], '<' . $tag . '>')) {
$useTags[] = '- <' . $tag . '>: ' . $desc;
if ($dialog->type === 'group') {
switch ($dialog->group_type) {
case 'project':
$projectInfo = Project::whereDialogId($dialog->id)->first();
if ($projectInfo) {
$contextLines[] = "场景:项目群聊「{$projectInfo->name}project_id: {$projectInfo->id}";
}
break;
case 'task':
$taskInfo = ProjectTask::with(['content'])->whereDialogId($dialog->id)->first();
if ($taskInfo) {
$contextLines[] = "场景:任务群聊「{$taskInfo->name}task_id: {$taskInfo->id}";
}
break;
case 'department':
$userDepartment = UserDepartment::whereDialogId($dialog->id)->first();
if ($userDepartment) {
$contextLines[] = "场景:部门群聊「{$userDepartment->name}";
}
break;
case 'all':
$contextLines[] = "场景:全体成员群聊";
break;
}
// 3. 聊天历史(仅群聊)
$chatHistory = $this->getRecentChatHistory($dialog, 15);
if ($chatHistory) {
$prompt[] = implode("\n", $contextLines);
$prompt[] = "最近的对话记录:\n{$chatHistory}";
} else {
$prompt[] = implode("\n", $contextLines);
}
} else {
$prompt[] = implode("\n", $contextLines);
}
if (!empty($useTags)) {
$extras['system_message'] = "以下信息按标签组织:\n" . implode("\n", $useTags) . "\n\n" . $extras['system_message'];
}
// 4. 条件性提示块(用户上下文 + 格式指南)
$prompt[] = PromptPlaceholder::buildOptionalPrompts($userid, $dialog);
$extras['system_message'] = implode("\n----\n", array_filter($prompt));
}
/**
@@ -881,14 +716,14 @@ class BotReceiveMsgTask extends AbstractTask
// 使用XML标签格式确保AI能清晰识别边界
// 对用户名进行HTML转义防止特殊字符破坏格式
$safeUserName = htmlspecialchars($userName, ENT_QUOTES, 'UTF-8');
return "<message user=\"{$safeUserName}\">\n{$content}\n</message>";
return "<message userid=\"{$message->userid}\" nickname=\"{$safeUserName}\">\n{$content}\n</message>";
})
->reverse() // 反转集合,让时间顺序正确(最早的在前)
->filter() // 过滤掉空内容的消息
->values() // 重新索引数组
->toArray();
return empty($chatMessages) ? null : implode("\n\n", $chatMessages);
return empty($chatMessages) ? null : implode("\n", $chatMessages);
}
/**

View File

@@ -6,6 +6,7 @@ use App\Models\File;
use App\Models\TaskWorker;
use App\Models\Tmp;
use App\Models\UserDevice;
use App\Models\UmengLog;
use App\Models\WebSocketTmpMsg;
use App\Module\Base;
use Carbon\Carbon;
@@ -103,6 +104,17 @@ class DeleteTmpTask extends AbstractTask
}
});
break;
case 'umeng_log':
UmengLog::where('created_at', '<', Carbon::now()->subHours($this->hours))
->orderBy('id')
->chunk(500, function ($logs) {
/** @var UmengLog $log */
foreach ($logs as $log) {
$log->delete();
}
});
break;
}
}

View File

@@ -65,16 +65,10 @@ class LoopTask extends AbstractTask
$task->end_at = $task->start_at->clone()->addSeconds($diffSecond);
}
// 处理子任务
$subTasks = ProjectTask::whereParentId($item->id)->get();
if (!$subTasks->isEmpty()) {
foreach ($subTasks as $subTask) {
$newSubTask = $subTask->copyTask();
$newSubTask->parent_id = $task->id;
$newSubTask->start_at = $task->start_at;
$newSubTask->end_at = $task->end_at;
$newSubTask->save();
}
}
$item->copySubTasks($task, [
'reset_complete' => true,
'sync_time' => true,
]);
//
$task->refreshLoop(true);
$task->addLog("创建任务来自周期任务ID{$item->id}", [], $task->userid);

View File

@@ -0,0 +1,280 @@
<?php
namespace App\Tasks;
use App\Models\File;
use App\Models\User;
use App\Models\Project;
use App\Models\ProjectTask;
use App\Models\WebSocketDialogMsg;
use App\Module\Apps;
use App\Module\Manticore\ManticoreBase;
use App\Module\Manticore\ManticoreFile;
use App\Module\Manticore\ManticoreUser;
use App\Module\Manticore\ManticoreProject;
use App\Module\Manticore\ManticoreTask;
use App\Module\Manticore\ManticoreMsg;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
/**
* 通用 Manticore Search 同步任务MVA 权限方案)
*
* 支持文件、用户、项目、任务的同步操作
* 使用 MVA (Multi-Value Attribute) 内联权限过滤
*/
class ManticoreSyncTask extends AbstractTask
{
private $action;
private $data;
public function __construct($action = null, $data = null)
{
parent::__construct(...func_get_args());
$this->action = $action;
$this->data = $data;
}
/**
* 获取任务动作类型(用于去重)
*
* @return string|null
*/
public function getAction(): ?string
{
return $this->action;
}
/**
* 获取数据ID用于去重
*
* @return int|null
*/
public function getDataId(): ?int
{
if (!is_array($this->data)) {
return null;
}
// 根据不同的 action 类型提取对应的 ID
return $this->data['id']
?? $this->data['userid']
?? $this->data['file_id']
?? $this->data['project_id']
?? $this->data['task_id']
?? $this->data['msg_id']
?? $this->data['dialog_id']
?? null;
}
public function start()
{
if (!Apps::isInstalled("search")) {
return;
}
switch ($this->action) {
// ==============================
// 文件同步动作
// ==============================
case 'file_sync':
$file = File::find($this->data['id'] ?? 0);
if ($file) {
ManticoreFile::sync($file);
}
break;
case 'file_delete':
$fileId = $this->data['id'] ?? 0;
if ($fileId > 0) {
ManticoreFile::delete($fileId);
}
break;
case 'file_pshare_update':
$fileIds = $this->data['file_ids'] ?? [];
$pshare = $this->data['pshare'] ?? 0;
if (!empty($fileIds)) {
ManticoreBase::batchUpdatePshare($fileIds, $pshare);
}
break;
case 'update_file_allowed_users':
// 更新文件权限
$fileId = $this->data['file_id'] ?? 0;
if ($fileId > 0) {
ManticoreFile::updateAllowedUsers($fileId);
}
break;
// ==============================
// 用户同步动作
// ==============================
case 'user_sync':
$user = User::find($this->data['userid'] ?? 0);
if ($user) {
ManticoreUser::sync($user);
}
break;
case 'user_delete':
$userid = $this->data['userid'] ?? 0;
if ($userid > 0) {
ManticoreUser::delete($userid);
}
break;
// ==============================
// 项目同步动作
// ==============================
case 'project_sync':
$project = Project::find($this->data['id'] ?? 0);
if ($project) {
ManticoreProject::sync($project);
}
break;
case 'project_delete':
$projectId = $this->data['project_id'] ?? 0;
if ($projectId > 0) {
ManticoreProject::delete($projectId);
}
break;
case 'update_project_allowed_users':
// 更新项目权限
$projectId = $this->data['project_id'] ?? 0;
if ($projectId > 0) {
ManticoreProject::updateAllowedUsers($projectId);
}
break;
case 'cascade_project_users':
// 项目成员变更时,级联更新该项目下所有 visibility=1 的任务
// 异步执行,避免阻塞
$projectId = $this->data['project_id'] ?? 0;
if ($projectId > 0) {
ManticoreTask::cascadeUpdateByProject($projectId);
}
break;
// ==============================
// 任务同步动作
// ==============================
case 'task_sync':
$task = ProjectTask::find($this->data['id'] ?? 0);
if ($task) {
ManticoreTask::sync($task);
}
break;
case 'task_delete':
$taskId = $this->data['task_id'] ?? 0;
if ($taskId > 0) {
ManticoreTask::delete($taskId);
}
break;
case 'update_task_allowed_users':
// 更新任务权限
$taskId = $this->data['task_id'] ?? 0;
if ($taskId > 0) {
ManticoreTask::updateAllowedUsers($taskId);
// 级联更新子任务
ManticoreTask::cascadeToChildren($taskId);
}
break;
// ==============================
// 消息同步动作
// ==============================
case 'msg_sync':
$msg = WebSocketDialogMsg::find($this->data['msg_id'] ?? 0);
if ($msg) {
ManticoreMsg::sync($msg);
}
break;
case 'msg_delete':
$msgId = $this->data['msg_id'] ?? 0;
if ($msgId > 0) {
ManticoreMsg::delete($msgId);
}
break;
case 'update_dialog_allowed_users':
// 更新对话消息权限
$dialogId = $this->data['dialog_id'] ?? 0;
if ($dialogId > 0) {
ManticoreMsg::updateDialogAllowedUsers($dialogId);
}
break;
default:
// 增量更新
$this->incrementalUpdate();
break;
}
}
/**
* 增量更新(定时执行 - 兜底机制)
*
* 命令本身会持续处理直到完成,定时器只是确保命令在运行
* 如果命令正在运行(有锁),则跳过本次触发
*
* @return void
*/
private function incrementalUpdate()
{
// 兜底触发:每 2 分钟检查一次,如果命令没在运行则启动
$time = intval(Cache::get("ManticoreSyncTask:CheckTime"));
if (time() - $time < 2 * 60) {
return;
}
Cache::put("ManticoreSyncTask:CheckTime", time(), Carbon::now()->addMinutes(5));
// 执行增量全文索引同步
$this->runIncrementalSync();
// 执行向量生成
$this->runVectorGeneration();
}
/**
* 执行增量全文索引同步(兜底触发)
*
* 命令内部有锁机制,如果已在运行会自动跳过
* 命令会持续处理直到无新数据,然后自动退出
*/
private function runIncrementalSync(): void
{
// 启动各类型的增量同步命令
@shell_exec("php /var/www/artisan manticore:sync-files --i 2>&1 &");
@shell_exec("php /var/www/artisan manticore:sync-users --i 2>&1 &");
@shell_exec("php /var/www/artisan manticore:sync-projects --i 2>&1 &");
@shell_exec("php /var/www/artisan manticore:sync-tasks --i 2>&1 &");
@shell_exec("php /var/www/artisan manticore:sync-msgs --i 2>&1 &");
}
/**
* 执行向量生成(兜底触发)
*
* 命令内部有锁机制,如果已在运行会自动跳过
* 命令会持续处理直到无待处理数据,然后自动退出
*/
private function runVectorGeneration(): void
{
if (!Apps::isInstalled("ai")) {
return;
}
// 启动向量生成命令
@shell_exec("php /var/www/artisan manticore:generate-vectors --type=all --batch=50 2>&1 &");
}
public function end()
{
}
}

View File

@@ -2,7 +2,10 @@
namespace App\Tasks;
use App\Models\UmengAlias;
use App\Models\WebSocketDialogMsgRead;
use App\Module\Base;
use Cache;
use Hhxsv5\LaravelS\Swoole\Task\Task;
/**
* 推送友盟消息
@@ -11,6 +14,7 @@ class PushUmengMsg extends AbstractTask
{
protected $userid = 0;
protected $array = [];
protected $endPush = []; // 需要在 end() 方法中处理的延迟推送列表
/**
* @param array|int $userid
@@ -32,11 +36,68 @@ class PushUmengMsg extends AbstractTask
if ($setting['push'] !== 'open') {
return;
}
UmengAlias::pushMsgToUserid($this->userid, $this->array);
// 消息ID
$msgId = isset($this->array['id']) ? intval($this->array['id']) : 0;
// 处理用户列表
$userids = is_array($this->userid) ? $this->userid : [$this->userid];
$directPushUsers = []; // 直接推送的用户
$delayedPushUsers = []; // 需要延迟推送的用户
foreach ($userids as $uid) {
if ($this->getDelay() > 0) {
// 已经延迟过,检查消息是否已读
if ($msgId > 0) {
$isRead = WebSocketDialogMsgRead::whereMsgId($msgId)
->whereUserid($uid)
->whereNotNull('read_at')
->exists();
if ($isRead) {
// 已读,跳过推送
continue;
}
}
// 未读或无法判断,执行推送
$directPushUsers[] = $uid;
} else {
// 首次推送,检查 PC 端是否活跃
$lastActive = Cache::get("user_pc_active:{$uid}");
$isPcActive = $lastActive && (time() - $lastActive) < 60;
if ($isPcActive) {
// PC 端活跃,需要延迟推送
$delayedPushUsers[] = $uid;
} else {
// PC 端不活跃,直接推送
$directPushUsers[] = $uid;
}
}
}
// 直接推送
if ($directPushUsers) {
UmengAlias::pushMsgToUserid($directPushUsers, $this->array);
}
// 创建延迟推送任务
if ($delayedPushUsers) {
$this->endPush[] = [
'userid' => $delayedPushUsers,
'array' => $this->array,
];
}
}
public function end()
{
if (empty($this->endPush)) {
return;
}
foreach ($this->endPush as $item) {
$task = new PushUmengMsg($item['userid'], $item['array']);
$task->delay(10);
Task::deliver($task);
}
}
}

View File

@@ -140,7 +140,7 @@ class WebSocketDialogMsgTask extends AbstractTask
];
// 机器人收到消处理
$botUser = User::whereUserid($userid)->whereBot(1)->first();
if ($botUser) {
if ($botUser) { // 避免机器人处理自己发送的消息
$this->endArray[] = new BotReceiveMsgTask($botUser->userid, $msg->id, $mentions, $this->client);
}
}
@@ -211,6 +211,10 @@ class WebSocketDialogMsgTask extends AbstractTask
'description' => "MID:{$msg->id}",
'seconds' => 3600,
'badge' => 1,
'extra' => [
'dialog_id' => $msg->dialog_id,
'msg_id' => $msg->id,
]
];
$this->endArray[] = new PushUmengMsg($uids->toArray(), $umengMsg);
}

View File

@@ -1,88 +0,0 @@
<?php
namespace App\Tasks;
use App\Models\WebSocketDialogMsg;
use App\Models\WebSocketDialogUser;
use App\Module\Apps;
use App\Module\ZincSearch\ZincSearchDialogMsg;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
/**
* 同步聊天数据到ZincSearch
*/
class ZincSearchSyncTask extends AbstractTask
{
private $action;
private $data;
public function __construct($action = null, $data = null)
{
parent::__construct(...func_get_args());
$this->action = $action;
$this->data = $data;
}
public function start()
{
if (!Apps::isInstalled("search")) {
// 如果没有安装搜索模块,则不执行
return;
}
switch ($this->action) {
case 'sync':
// 同步消息数据
ZincSearchDialogMsg::sync(WebSocketDialogMsg::fillInstance($this->data));
break;
case 'delete':
// 删除消息数据
ZincSearchDialogMsg::delete(WebSocketDialogMsg::fillInstance($this->data));
break;
case 'userSync':
// 同步用户数据
ZincSearchDialogMsg::userSync(WebSocketDialogUser::fillInstance($this->data));
break;
case 'deleteUser':
// 删除用户数据
ZincSearchDialogMsg::delete(WebSocketDialogUser::fillInstance($this->data));
break;
default:
// 增量更新
$this->incrementalUpdate();
break;
}
}
/**
* 增量更新
* @return void
*/
private function incrementalUpdate()
{
// 120分钟执行一次
$time = intval(Cache::get("ZincSearchSyncTask:Time"));
if (time() - $time < 120 * 60) {
return;
}
// 执行开始120分钟后缓存标记失效
Cache::put("ZincSearchSyncTask:Time", time(), Carbon::now()->addMinutes(120));
// 开始执行同步
@shell_exec("php /var/www/artisan zinc:sync-user-msg --i");
// 执行完成5分钟后缓存标记失效5分钟任务可重复执行
Cache::put("ZincSearchSyncTask:Time", time(), Carbon::now()->addMinutes(5));
}
public function end()
{
}
}

397
bin/version.js vendored

File diff suppressed because one or more lines are too long

43
cmd
View File

@@ -19,6 +19,12 @@ WORK_DIR="$(pwd)"
INPUT_ARGS=$@
COMPOSE="docker-compose"
# TTY 参数检测
TTY_FLAG=""
if [ -t 0 ] && [ -t 1 ]; then
TTY_FLAG="-it"
fi
# 缓存执行
if [ -z "$CACHED_EXECUTION" ] && [ "$1" == "update" ]; then
if ! cat "$0" > ._cmd 2>/dev/null; then
@@ -88,7 +94,7 @@ rand_string() {
if [[ `uname` == 'Linux' ]]; then
echo "$(date +%s%N | md5sum | cut -c 1-${lan})"
else
echo "$(docker run -it --rm nginx:alpine sh -c "date +%s%N | md5sum | cut -c 1-${lan}")"
echo "$(docker run $TTY_FLAG --rm nginx:alpine sh -c "date +%s%N | md5sum | cut -c 1-${lan}")"
fi
}
@@ -241,7 +247,7 @@ container_exec() {
error "没有找到 ${container} 容器!"
exit 1
fi
docker exec -it "$name" /bin/sh -c "$cmd"
docker exec $TTY_FLAG "$name" /bin/sh -c "$cmd"
}
# 备份数据库、还原数据库
@@ -298,8 +304,22 @@ mysql_snapshot() {
remove_by_network() {
local app_id=$(env_get APP_ID)
local network_name="dootask-networks-${app_id}"
for container_id in $(docker ps -q --filter network="$network_name"); do
docker rm -f "$container_id" 1>/dev/null
# 批量删除所有状态的容器(包括已停止的)
local container_ids=$(docker ps -aq --filter network="$network_name")
if [ -n "$container_ids" ]; then
echo "$container_ids" | xargs -r docker rm -f 1>/dev/null
fi
# 等待网络完全清空最多等待10秒
local retry=0
while [ $retry -lt 10 ]; do
local count=$(docker network inspect "$network_name" --format '{{len .Containers}}' 2>/dev/null | tr -d '[:space:]')
if [ -z "$count" ] || [ "$count" = "0" ]; then
break
fi
sleep 1
((retry++))
done
}
@@ -341,11 +361,11 @@ https_auto() {
if [[ "$restart_nginx" == "y" ]]; then
$COMPOSE up -d
fi
docker run -it --rm -v $(pwd):/work nginx:alpine sh /work/bin/https install
docker run $TTY_FLAG --rm -v $(pwd):/work nginx:alpine sh /work/bin/https install
if [[ 0 -eq $? ]]; then
container_exec nginx "nginx -s reload"
fi
new_job="* 6 * * * docker run -it --rm -v $(pwd):/work nginx:alpine sh /work/bin/https renew"
new_job="* 6 * * * docker run --rm -v $(pwd):/work nginx:alpine sh /work/bin/https renew"
current_crontab=$(crontab -l 2>/dev/null)
if ! echo "$current_crontab" | grep -v "https renew"; then
echo "任务已存在,无需添加。"
@@ -376,7 +396,7 @@ env_set() {
if [[ `uname` == 'Linux' ]]; then
sed -i "/^${key}=/c\\${key}=${val}" ${WORK_DIR}/.env
else
docker run -it --rm -v ${WORK_DIR}:/www nginx:alpine sh -c "sed -i "/^${key}=/c\\${key}=${val}" /www/.env"
docker run $TTY_FLAG --rm -v ${WORK_DIR}:/www nginx:alpine sh -c "sed -i "/^${key}=/c\\${key}=${val}" /www/.env"
fi
if [ $? -ne 0 ]; then
error "设置env参数失败"
@@ -402,7 +422,7 @@ env_init() {
if [ -z "$(env_get UPDATE_TIME)" ]; then
env_set DB_HOST "mariadb"
env_set REDIS_HOST "redis"
docker run -it --rm -v ${WORK_DIR}:/www nginx:alpine sh -c "sed -i 's|/etc/nginx/conf.d/site/|/var/www/docker/nginx/site/|g' /www/docker/nginx/site/*.conf &> /dev/null"
docker run $TTY_FLAG --rm -v ${WORK_DIR}:/www nginx:alpine sh -c "sed -i 's|/etc/nginx/conf.d/site/|/var/www/docker/nginx/site/|g' /www/docker/nginx/site/*.conf &> /dev/null"
fi
}
@@ -791,7 +811,7 @@ case "$1" in
elif [[ "$cli" == "dev" ]]; then
por="-p 8880:8880"
fi
docker run -it --rm -v ${WORK_DIR}/resources/mobile:/work -w /work ${por} kuaifan/eeui-cli:0.0.1 eeui ${cli}
docker run $TTY_FLAG --rm -v ${WORK_DIR}/resources/mobile:/work -w /work ${por} kuaifan/eeui-cli:0.0.1 eeui ${cli}
;;
"npm")
shift 1
@@ -799,12 +819,13 @@ case "$1" in
pushd electron || exit
npm "$@"
popd || exit
docker run --rm -it -v ${WORK_DIR}/resources/mobile:/work -w /work --entrypoint=/bin/bash node:16 -c "npm $@"
docker run $TTY_FLAG --rm -v ${WORK_DIR}/resources/mobile:/work -w /work --entrypoint=/bin/bash node:16 -c "npm $@"
;;
"doc")
shift 1
container_exec php "php app/Http/Controllers/Api/apidoc.php"
docker run -it --rm -v ${WORK_DIR}:/home/node/apidoc kuaifan/apidoc -i app/Http/Controllers/Api -o public/docs
docker run $TTY_FLAG --rm -v ${WORK_DIR}:/home/node/apidoc kuaifan/apidoc -i app/Http/Controllers/Api -o public/docs
container_exec php "php app/Http/Controllers/Api/apidoc.php restore"
;;
"debug")
shift 1

View File

@@ -0,0 +1,43 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateReportAiAnalysesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasTable('report_ai_analyses')) {
return;
}
Schema::create('report_ai_analyses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('rid')->comment('报告ID');
$table->unsignedBigInteger('userid')->comment('生成分析的会员ID');
$table->string('model')->default('')->comment('使用的模型名称');
$table->longText('analysis_text')->comment('AI 分析的原始文本Markdown');
$table->json('meta')->nullable()->comment('额外的上下文信息');
$table->timestamps();
$table->unique(['rid', 'userid'], 'uk_report_ai_analysis_rid_userid');
$table->index(['userid', 'updated_at'], 'idx_report_ai_analysis_user_updated');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('report_ai_analyses');
}
}

View File

@@ -1,35 +0,0 @@
<?php
@error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
use App\Module\Base;
use Illuminate\Database\Migrations\Migration;
class CreateAiSettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$setting = Base::setting('aibotSetting');
Base::setting('aiSetting', [
'ai_provider' => 'openai',
'ai_api_key' => $setting['openai_key'],
'ai_api_url' => $setting['openai_base_url'],
'ai_proxy' => $setting['openai_agency'],
]);
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// This migration does not need to be reversible
}
}

View File

@@ -0,0 +1,39 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('user_bots', function (Blueprint $table) {
if (!Schema::hasColumn('user_bots', 'webhook_events')) {
$table->text('webhook_events')->nullable()->after('webhook_num')->comment('Webhook事件配置');
}
});
DB::table('user_bots')
->where(function ($query) {
$query->whereNull('webhook_events')->orWhere('webhook_events', '');
})
->update(['webhook_events' => json_encode(['message'])]);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('user_bots', function (Blueprint $table) {
if (Schema::hasColumn('user_bots', 'webhook_events')) {
$table->dropColumn('webhook_events');
}
});
}
};

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->date('birthday')->nullable()->after('profession');
$table->string('address', 255)->nullable()->after('birthday');
$table->text('introduction')->nullable()->after('address');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['birthday', 'address', 'introduction']);
});
}
};

View File

@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserTagsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id')->index()->comment('被标签用户ID');
$table->string('name', 50)->comment('标签名称');
$table->unsignedBigInteger('created_by')->index()->comment('创建人');
$table->unsignedBigInteger('updated_by')->nullable()->comment('最后更新人');
$table->timestamps();
$table->unique(['user_id', 'name'], 'user_tags_unique_name');
$table->foreign('user_id')->references('userid')->on('users')->onDelete('cascade');
$table->foreign('created_by')->references('userid')->on('users')->onDelete('cascade');
$table->foreign('updated_by')->references('userid')->on('users')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_tags');
}
}

View File

@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserTagRecognitionsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_tag_recognitions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('tag_id')->index()->comment('标签ID');
$table->unsignedBigInteger('user_id')->index()->comment('认可人ID');
$table->timestamps();
$table->unique(['tag_id', 'user_id'], 'user_tag_recognitions_unique');
$table->foreign('tag_id')->references('id')->on('user_tags')->onDelete('cascade');
$table->foreign('user_id')->references('userid')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_tag_recognitions');
}
}

View File

@@ -0,0 +1,23 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::create('umeng_logs', function (Blueprint $table) {
$table->id();
$table->text('request')->nullable()->comment('请求参数');
$table->text('response')->nullable()->comment('推送返回');
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('umeng_logs');
}
};

View File

@@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUserAppSortsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
if (Schema::hasTable('user_app_sorts')) {
return;
}
Schema::create('user_app_sorts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('userid')->unique()->comment('用户ID');
$table->json('sorts')->nullable()->comment('排序配置');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_app_sorts');
}
}

View File

@@ -0,0 +1,33 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddWebSocketsPlatform extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('web_sockets', function (Blueprint $table) {
$table->string('platform', 20)->nullable()->default('')->after('path')->comment('平台类型android, ios, win, mac, web');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('web_sockets', function (Blueprint $table) {
$table->dropColumn('platform');
});
}
}

View File

@@ -0,0 +1,79 @@
<?php
use App\Module\Base;
use App\Models\Setting;
use Illuminate\Database\Migrations\Migration;
class UpdateSettingMicroappMenuType extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$row = Setting::whereName('microapp_menu')->first();
if (!$row) {
return;
}
$data = Base::string2array($row->setting);
if (empty($data) || !is_array($data)) {
return;
}
$changed = false;
foreach ($data as $appIndex => $app) {
if (!is_array($app)) {
continue;
}
$menuItems = [];
if (isset($app['menu_items']) && is_array($app['menu_items'])) {
$menuItems = $app['menu_items'];
} elseif (isset($app['menu']) && is_array($app['menu'])) {
$menuItems = [$app['menu']];
}
if (empty($menuItems)) {
continue;
}
$newMenuItems = [];
foreach ($menuItems as $menu) {
if (!is_array($menu)) {
$newMenuItems[] = $menu;
continue;
}
if (!isset($menu['type']) && isset($menu['url_type'])) {
$menu['type'] = $menu['url_type'];
unset($menu['url_type']);
$changed = true;
} elseif (isset($menu['url_type'])) {
unset($menu['url_type']);
$changed = true;
}
$newMenuItems[] = $menu;
}
if (isset($app['menu_items']) && is_array($app['menu_items'])) {
$data[$appIndex]['menu_items'] = $newMenuItems;
} elseif (isset($app['menu']) && is_array($app['menu'])) {
$data[$appIndex]['menu'] = $newMenuItems[0] ?? $app['menu'];
}
}
if ($changed) {
$row->updateInstance(['setting' => $data]);
$row->save();
}
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// No-op: do not revert settings payload.
}
}

View File

@@ -0,0 +1,61 @@
<?php
use App\Module\Base;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
class ReverseDoneUseridsInTodoDoneMsgs extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$this->reverseDoneUserids('2025-12-19 00:00:00');
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
$this->reverseDoneUserids('2025-12-19 00:00:00');
}
private function reverseDoneUserids(string $after)
{
DB::table('web_socket_dialog_msgs')
->select(['id', 'msg'])
->where('type', 'todo')
->where('created_at', '>', $after)
->orderBy('id')
->chunkById(200, function ($rows) {
foreach ($rows as $row) {
$msg = Base::json2array($row->msg);
if (empty($msg) || !is_array($msg)) {
continue;
}
if (($msg['action'] ?? '') !== 'done') {
continue;
}
$data = $msg['data'] ?? null;
if (!is_array($data)) {
continue;
}
$doneUserids = $data['done_userids'] ?? null;
if (!is_array($doneUserids) || count($doneUserids) < 2) {
continue;
}
$data['done_userids'] = array_reverse($doneUserids);
$msg['data'] = $data;
DB::table('web_socket_dialog_msgs')
->where('id', $row->id)
->update(['msg' => Base::array2json($msg)]);
}
});
}
}

View File

@@ -0,0 +1,40 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddIndexesToWebSocketDialogMsgs extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('web_socket_dialog_msgs', function (Blueprint $table) {
$table->index(['dialog_id', 'deleted_at', 'id']);
$table->index(['reply_id', 'deleted_at']);
});
Schema::table('web_socket_dialog_msg_reads', function (Blueprint $table) {
$table->index(['userid', 'silence', 'read_at'], 'idx_ws_msg_reads_userid_silence_read_at');
$table->index(['dialog_id', 'userid', 'mention', 'read_at'], 'idx_ws_msg_reads_dialog_user_mention_read_at');
});
Schema::table('user_checkin_records', function (Blueprint $table) {
$table->index(['userid', 'mac', 'date'], 'idx_user_checkin_records_userid_mac_date');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// No-op: do not drop indexes automatically.
}
}

View File

@@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('project_users', function (Blueprint $table) {
$table->index(['userid', 'project_id']);
});
Schema::table('project_tasks', function (Blueprint $table) {
$table->index(['project_id', 'archived_at', 'deleted_at', 'id']);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
// No-op: do not drop indexes automatically.
}
};

View File

@@ -37,7 +37,7 @@ class SettingsTableSeeder extends Seeder
array (
'name' => 'priority',
'desc' => '',
'setting' => '[{"name":"\\u91cd\\u8981\\u4e14\\u7d27\\u6025","color":"#ED4014","days":1,"priority":1},{"name":"\\u91cd\\u8981\\u4e0d\\u7d27\\u6025","color":"#F16B62","days":3,"priority":2},{"name":"\\u7d27\\u6025\\u4e0d\\u91cd\\u8981","color":"#19C919","days":5,"priority":3},{"name":"\\u4e0d\\u91cd\\u8981\\u4e0d\\u7d27\\u6025","color":"#2D8CF0","days":0,"priority":4}]',
'setting' => '[{"name":"\\u91cd\\u8981\\u4e14\\u7d27\\u6025","color":"#ED4014","days":1,"priority":1,"is_default":1},{"name":"\\u91cd\\u8981\\u4e0d\\u7d27\\u6025","color":"#F16B62","days":3,"priority":2,"is_default":0},{"name":"\\u7d27\\u6025\\u4e0d\\u91cd\\u8981","color":"#19C919","days":5,"priority":3,"is_default":0},{"name":"\\u4e0d\\u91cd\\u8981\\u4e0d\\u7d27\\u6025","color":"#2D8CF0","days":0,"priority":4,"is_default":0}]',
'created_at' => seeders_at('2021-07-01 08:04:30'),
'updated_at' => seeders_at('2021-07-01 09:20:26'),
),

View File

@@ -96,10 +96,10 @@ services:
appstore:
container_name: "dootask-appstore-${APP_ID}"
privileged: true
image: "dootask/appstore:0.3.0"
image: "dootask/appstore:0.3.9"
volumes:
- shared_data:/usr/share/dootask
- /var/run/docker.sock:/var/run/docker.sock
- ${HOST_DOCKER_SOCK:-/var/run/docker.sock}:/var/run/docker.sock
- ./:/var/www
environment:
HOST_PWD: "${PWD}"

Some files were not shown because too many files have changed in this diff Show More