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
Delete row 179 from issue
id
179
repo_id
21
index
11
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pay-260519 - 1
content
## 自动代码审查报告 **分支**: pay-260519 **提交**: `e0309e97c
## 自动代码审查报告 **分支**: pay-260519 **提交**: `e0309e97c19e9542fbbf526e45cc4d0aff8b24e8` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-05-19 13:22:54 --- ## 1. 审查摘要 - **代码质量评分**:5.5 / 10 分 - **总体评价**:该 Model 承载了大量门店业务逻辑,功能覆盖较全,但存在明显的架构与编码规范问题。核心隐患集中在 **SQL 注入风险、N+1 查询性能瓶颈、框架生命周期误用** 以及 **变量拼写错误**。代码整体偏向“脚本式”堆砌,缺乏面向对象设计原则(如单一职责、依赖注入)的约束,后期维护与扩展成本较高。 - **风险等级**:🔴 高 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | 全局/第3-4行 | 文件顶部直接调用 `get_instance()` 并加载 `Simple_model`。此代码在文件被 `include/require` 时即执行,而非实例化时,严重违反 CI/PHPCI 框架生命周期,易导致类未定义或单例污染。 | 移除全局调用。模型依赖应通过 CI 自动加载配置或移至 `__construct()` 中。 | `// 删除顶部这两行<br>$CI = &get_instance();<br>$CI->load->model('Simple_model');` | | 🔴 严重 | `get_shop_list_order_by_distance` (~L750) | 原始 SQL 拼接未对 `$shop_name`、`$satisfy_shop_ids`、`$city_id` 进行转义或参数绑定,存在高危 **SQL 注入** 漏洞。 | 使用 CI 查询构建器(Query Builder)或 `$this->db->escape()` / `escape_like_str()` 进行安全过滤。 | `$safe_name = $this->db->escape_like_str($shop_name);<br>$sql .= " AND _name LIKE '%{$safe_name}%'";` | | 🔴 严重 | `get_community_shop_list` (~L630) | 变量名拼写错误:`if (!empty($param['bookFrom']))`,实际入参为 `$params`。将导致 `Undefined variable` 警告,且后续 JOIN 逻辑永远不执行。 | 修正为 `$params['bookFrom']`,并建议开启 `error_reporting(E_ALL)` 在开发环境拦截此类错误。 | `if (!empty($params['bookFrom'])) { ... }` | | 🟠 警告 | `get_community_shop_list` & `get_shop_list_order_by_distance` | **N+1 查询性能瓶颈**:在 `foreach` 循环中逐条调用 `$this->ahead_shop_config_second_model->get_miniprogram_consumption_methods()`。若返回 100 家门店,将产生 100+ 次额外 DB 请求。 | 提取所有 `shop_id` 数组,编写批量查询方法 `get_batch_methods($merchant_id, $shop_ids)`,在循环外一次性获取,再在内存中映射。 | `$ids = array_column($shop_data, 'shop_id');<br>$batch_data = $this->ahead_shop_config_second_model->get_batch_methods($merchant_id, $ids);<br>foreach ($shop_data as &$v) { $config = $batch_data[$v['shop_id']] ?? []; ... }` | | 🟠 警告 | `get_date_shop_no_business_time` (~L840) | 循环步长依赖外部模型属性 `$this->ahead_shop_book_time_info_model->min_minute_unit_time`。若该值为 `0` 或未初始化,将导致 **死循环** 耗尽 CPU/内存。 | 增加步长安全校验,或提取为类常量。 | `$step = max(1, $this->ahead_shop_book_time_info_model->min_minute_unit_time);<br>for ($i = 0; $i < $business_from; $i += $step) { ... }` | | 🟠 警告 | 全局属性 `$shop_no_business_time` | 使用 `public $shop_no_business_time = [];` 累积状态。在并发请求或多次调用时会产生 **数据污染**,且破坏方法纯函数特性。 | 改为方法局部变量返回,或封装为独立 DTO/Value Object。 | `// 移除 public 属性<br>public function get_date_shop_no_business_time(...) {<br> $no_business_times = []; // 局部变量<br> // ... 逻辑<br> return ['result' => $result, 'no_business_times' => $no_business_times];<br>}` | | 🟡 建议 | 全文 | 方法命名风格不统一(如 `getAliAuthInfo` 驼峰 vs `get_list_for_search` 下划线),不符合 PSR-12 规范。 | 统一使用 `camelCase` 命名方法,属性使用 `snake_case`。 | `public function getAliAuthInfo()` → `public function getAliAuthInfo()` (保持) 或 `get_ali_auth_info()` (统一) | | 🟡 建议 | `get_cache_shop_data` | Redis 过期时间使用 `expireAt` 配合 `time()+86400` 非原子操作。若 `set` 成功但 `expireAt` 失败,将产生永久缓存。 | 使用原子命令 `setex` 或 `set($key, $val, ['ex' => 86400])`。 | `$redis->setex($redis_key, 86400, json_encode($shop_data));` | | 🟡 建议 | `get_list_by_id` | `explode(",", $shop_id)` 在传入空字符串时会返回 `['']`,导致后续 `WHERE IN ('')` 语法错误或全表扫描。 | 增加空值过滤与类型转换。 | `$shop_id = array_filter(array_map('intval', explode(',', $shop_id)));<br>if (empty($shop_id)) return [];` | ## 3. 总结与行动建议 ### 🚨 优先修复的关键问题 1. **立即修复 SQL 注入**:`get_shop_list_order_by_distance` 中的原始 SQL 拼接必须替换为 CI Query Builder 或严格转义。这是最高优先级的安全红线。 2. **修正致命拼写错误**:`get_community_shop_list` 中的 `$param` 必须改为 `$params`,否则业务逻辑将静默失效。 3. **清理全局实例化代码**:删除文件顶部的 `$CI = &get_instance();`,将其移至 `__construct()` 或依赖 CI 的 `autoload.php` 配置。 ### 🛠 后续重构与优化方向 1. **性能架构升级**: - **批量查询替代循环查询**:针对 `get_community_shop_list` 等列表接口,将配置、评论、距离计算等依赖改为 `IN` 批量查询或 Redis Pipeline 获取。 - **距离计算下沉**:PHP 循环计算 `get_distance()` 在数据量大时极慢。建议改用 MySQL 空间函数(如 `ST_Distance_Sphere`)或 Redis `GEO` 模块在数据库/缓存层完成排序。 2. **代码规范与可维护性**: - **统一命名与类型声明**:全面采用 PSR-12 规范,为所有方法添加 PHP 7.4+ 类型提示(如 `public function get_info(array $param): array`),提升 IDE 提示与静态分析能力。 - **消除魔法数字**:将 `86400`、`1`、`-1`、`5` 等硬编码提取为类常量(如 `const CACHE_TTL = 86400; const STATUS_ACTIVE = 1;`)。 - **异常处理标准化**:当前混用 `throwError()` 和 `showErrorView()`。建议统一使用 PHP 原生 `throw new \RuntimeException()` 或框架标准异常,便于全局异常拦截与日志记录。 3. **框架适配建议**: - 基于代码结构,该项目高度类似 **CodeIgniter 3**。若确为 `phpci` 定制框架,请确认其是否支持 CI4 的依赖注入容器。若支持,建议逐步将 `$this->load->model()` 替换为构造函数注入,以提升单元测试覆盖率。 - `Simple_model` 作为基类,建议封装统一的 `batchSelect()`、`cache()` 方法,避免各子类重复实现缓存与批量逻辑。 > 💡 **注**:由于未提供 `Simple_model` 基类及全局辅助函数(如 `locationTurnTxMap`, `get_distance`)的实现,部分性能与逻辑评估基于常规实践推断。建议在修复上述高危问题后,结合 Xdebug 或 Blackfire 进行实际压测,验证 N+1 与距离计算优化效果。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1779168174
updated_unix
1779168174
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel