sqlite-web 0.7.2
gitea.db
issue
Create
Query
access
access_token
action
action_artifact
action_run
action_run_index
action_run_job
action_runner
action_runner_token
action_schedule
action_schedule_spec
action_task
action_task_output
action_task_step
action_tasks_version
action_variable
app_state
attachment
auth_token
badge
branch
collaboration
comment
commit_status
commit_status_index
commit_status_summary
commit_sync_log
commit_sync_status
dbfs_data
dbfs_meta
deploy_key
email_address
email_hash
external_login_user
follow
gpg_key
gpg_key_import
hook_task
issue
issue_assignees
issue_content_history
issue_dependency
issue_index
issue_label
issue_pin
issue_user
issue_watch
label
language_stat
lfs_lock
lfs_meta_object
login_source
milestone
mirror
notice
notification
oauth2_application
oauth2_authorization_code
oauth2_grant
org_user
package
package_blob
package_blob_upload
package_cleanup_rule
package_file
package_property
package_version
project
project_board
project_issue
protected_branch
protected_tag
public_key
pull_auto_merge
pull_request
push_mirror
reaction
release
renamed_branch
repo_archiver
repo_hidden_file
repo_indexer_status
repo_license
repo_redirect
repo_topic
repo_transfer
repo_unit
repository
review
review_state
secret
session
sqlite_sequence
star
stopwatch
system_setting
task
team
team_invite
team_repo
team_unit
team_user
topic
tracked_time
two_factor
upload
user
user_badge
user_blocking
user_open_id
user_redirect
user_setting
version
watch
webauthn_credential
webhook
Toggle helper tables
Structure
Content
Query
Insert
Drop
Import
Export
Update row 243 in issue
id
Primary key.
INTEGER NOT NULL
repo_id
INTEGER
index
INTEGER
poster_id
INTEGER
original_author
TEXT
original_author_id
INTEGER
name
🔍 代码审查报告:app-260616 - 需求 聚旺设备先软关机 16371
TEXT
content
## 自动代码审查报告 **分支**: app-260616 **提交**: `17ad716cdae26f841ea9d2c837550eab31b387c8` **提交人**: chenjunfeng (developer.jeff.c@gmail.com) **时间**: 2026-05-20 16:10:40 --- ## 1. 审查摘要 - **代码质量评分**:5.5 / 10 - **总体评价**:代码实现了复杂的 IoT 设备联动控制逻辑,业务覆盖较全。但存在严重的代码重复、死代码残留、同步阻塞调用及框架误用问题。数据库交互未充分利用批量操作,网络请求缺乏容错机制,整体可维护性与生产稳定性存在较大隐患。 - **风险等级**:🟠 中高(存在逻辑死区、Web 进程阻塞风险、高频单条写入导致的性能瓶颈) > 📌 **框架说明**:根据目录结构(`system/`、`application/`)、`get_instance()`、`$this->load->model()` 等特征,判定该项目实际基于 **CodeIgniter 3 (CI3)** 架构。若确为内部自研 `phpci` 框架,请对照其官方文档调整模型加载与生命周期管理。以下审查基于 CI3/PHP 现代规范进行。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | 文件顶部 (1-3行) | 在模型文件顶层直接调用 `$CI = &get_instance();` 加载模型。CI 核心在模型实例化前可能未完全初始化,易引发 `Call to a member function on null` 致命错误。 | 移除顶部全局代码。依赖 CI 的自动加载机制,或在 `__construct()` 中按需加载。 | `// 删除顶部两行<br>class Ahead_intelligent_control_log_model extends Simple_model { ... }` | | 🔴 严重 | `do_after_sequencer()` | 方法首行直接 `return true;`,导致后续 80+ 行业务逻辑全部成为**死代码**。若为临时调试遗留,将导致回调逻辑永久失效。 | 确认需求后彻底清理死代码,或恢复完整逻辑。建议配合版本控制回滚。 | `public function do_after_sequencer($params) {<br> // 删除 return true;<br> $family_server_id = $params['family_server_id'] ?? ''; ...` | | 🟠 警告 | `action_data()` & `action_data_power_fist()` | 两个方法代码重复率 >90%,仅设备执行顺序不同。严重违反 DRY 原则,后续新增设备类型需修改两处,极易遗漏。 | 提取公共数据获取逻辑,通过传入**执行顺序数组**或**策略回调**控制流程。 | 见下方重构示例 | | 🟠 警告 | `power_operate_new()` | `foreach ($types as $tv)` 循环内每次重置 `$line_status = [2,2,2,2,2,2]`。若多房态同时触发,仅最后一次循环的指令生效,状态覆盖存在逻辑隐患。 | 明确多房态优先级,或合并 `$switch_actions` 后统一计算最终线路状态再下发。 | `$final_status = array_fill(0, 6, 2);<br>foreach ($types as $tv) { ... // 累加状态 }` | | 🟠 警告 | `sequencer_operate()` / `power_operate()` | 循环内调用 `$this->insert($arr)` 执行单条 INSERT;且 `sequencer_operate` 中使用 `usleep(200000)` 阻塞 Web 进程,高并发下易导致 PHP-FPM 超时。 | 收集日志数组后统一使用 `$this->db->insert_batch()`;将 `usleep` 移至消息队列/CLI 异步任务。 | `$logs[] = $arr; // 循环外<br>$this->db->insert_batch($this->table_name, $logs);` | | 🟠 警告 | `sequencer_operate()` | `json_decode($v['_box_notify_config'], 1)` 未校验 JSON 合法性。若数据库存入非法字符串,将返回 `null` 并触发后续数组访问警告。 | 增加安全解码校验,或使用 PHP 8.3+ `json_validate()`。 | `$config = json_decode($v['_box_notify_config'], true);<br>if (json_last_error() !== JSON_ERROR_NONE) { continue; }` | | 🟡 建议 | 全局方法命名 | 命名不规范且含拼写错误:`type_to_type_fun`、`action_data_power_fist`(应为 first)、大量魔法数字(如 `1,2,3,06,EDF2`)。 | 遵循 PSR-12,使用语义化命名;提取协议常量;补充类型声明。 | `const DEVICE_PROTOCOL_HEADER = 'EDF2';<br>public function mapControlType(int $control, int $type): int` | | 🟡 建议 | 全局模型加载 | 频繁在业务方法内 `$this->load->model()`。CI 虽会缓存,但增加解析开销且降低可读性。 | 将高频依赖模型统一移至 `__construct()` 或配置 `autoload.php`。 | `public function __construct() {<br> parent::__construct();<br> $this->load->model(['Ahead_intelligent_power_control_model', 'Ahead_family_servers_model']);<br>}` | ### 💡 核心重复代码重构示例 (`action_data` 与 `action_data_power_fist` 合并) ```php /** * 统一设备控制入口 * @param string $execution_order 执行顺序:'power_first' 或 'sequencer_first' */ public function execute_device_control($merchant_id, $params, $type = 1, string $execution_order = 'sequencer_first') { // 1. 提取公共前置逻辑(获取 room_data, shop_data, control_data 等)... // 2. 分类设备动作 $breaker_actions, $ttlock_actions, $xinfeng_actions, $sequencer_actions, $switch_actions ... // 3. 根据顺序执行 $sequence = $execution_order === 'power_first' ? ['power', 'breaker', 'ttlock', 'xinfeng', 'sequencer'] : ['sequencer', 'power', 'breaker', 'ttlock', 'xinfeng']; foreach ($sequence as $step) { switch ($step) { case 'power': $this->power_operate($merchant_id, $room_data, $shop_data, $data, $types, $type, $switch_actions); break; case 'sequencer': $this->sequencer_operate($sequencer_actions, $merchant_id, $shop_data, $room_data, $data, $type); break; // ... 其他步骤 } } return true; } ``` ## 3. 总结与行动建议 ### 🚀 优先修复的关键问题 1. **清理死代码与错误加载**:立即移除 `do_after_sequencer` 首行的 `return true;` 及文件顶部的 `$CI = &get_instance();`,避免生产环境隐性崩溃。 2. **消除核心重复逻辑**:将 `action_data` 与 `action_data_power_fist` 合并为单一入口,通过参数控制执行顺序,降低后续维护成本。 3. **解除 Web 进程阻塞**:将 `usleep(200000)` 及耗时网络请求(MQTT/HTTP)剥离至异步队列(如 Redis + Supervisor 或 CI CLI 脚本),保障 HTTP 接口响应时间 `< 2s`。 4. **批量写入优化**:将循环内的 `$this->insert()` 替换为 `$this->db->insert_batch()`,单次请求可减少 90% 以上的数据库连接开销。 ### 🛠 后续重构与优化方向 - **架构分层**:当前 Model 承担了过多业务编排、协议组装、外部 API 调用职责。建议引入 `Service` 层处理业务流,Model 仅负责数据存取,符合单一职责原则(SRP)。 - **容错与重试机制**:`Mqttapi`、`curlWebsocketApi`、`TtlockApi` 调用均无异常捕获与重试逻辑。建议封装统一的外部服务客户端,集成指数退避重试与失败降级策略。 - **配置与协议管理**:硬编码的协议头(`EDF2`)、线路状态(`2222222222`)、魔法数字应提取至配置文件或常量类,便于后续协议升级。 - **类型安全**:逐步引入 PHP 7.4+ 类型声明(`declare(strict_types=1);`、参数类型、返回值类型),配合静态分析工具(PHPStan/Psalm)提前拦截类型错误。 > ⚠️ **局限性说明**:本次审查基于提供的单一 Model 文件。若 `Simple_model`、`Mqttapi`、`TtlockApi` 等底层组件存在未暴露的缺陷(如 SQL 注入过滤不严、连接池泄漏),可能影响整体安全性。建议结合全链路压测与日志监控进行验证。 --- *此 Issue 由代码审查服务自动创建*
TEXT
milestone_id
INTEGER
priority
INTEGER
is_closed
INTEGER
is_pull
INTEGER
num_comments
INTEGER
ref
TEXT
deadline_unix
INTEGER
created_unix
INTEGER
updated_unix
INTEGER
closed_unix
INTEGER
is_locked
INTEGER NOT NULL (default 0
content_version
INTEGER NOT NULL (default 0
time_estimate
INTEGER NOT NULL (default 0
Update
Cancel