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 489 from issue
id
489
repo_id
22
index
62
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:app-260616 - 1
content
## 自动代码审查报告 **分支**: app-260616 **提交**: `d682eb31c
## 自动代码审查报告 **分支**: app-260616 **提交**: `d682eb31c6980fa06ec90a57de0d6c9016cbfe22` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-06-03 10:29:27 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 分 - **总体评价**:该模型实现了社区商家营收明细的日志记录、多维度统计与趋势查询功能,业务逻辑覆盖较全。但存在**全局实例化滥用、静态缓存未隔离、SQL 拼接隐患、大量重复聚合逻辑**等核心问题。代码结构偏向过程式,未充分利用面向对象特性与框架生命周期,可维护性与安全性有较大提升空间。 - **风险等级**:🔴 高(存在数据串扰与潜在 SQL 注入风险) ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | 文件顶部 (1-3行) | `$CI = &get_instance();` 在类外部直接执行。文件被 `include/require` 时即触发,若此时框架未完全初始化会导致致命错误,且破坏单例模式。 | 移除顶部代码。在模型内部通过 `$this->load->model()` 或 `$this->ci` 访问 CI 实例。 | `// 删除顶部两行,改为在方法内按需加载或使用构造函数` | | 🔴 严重 | `get_date_type_info` | `public static $date_type_info = [];` 作为静态缓存未区分 `$shop_id`。同一请求内查询不同门店时,会返回首个门店的缓存数据,导致**严重业务数据错乱**。 | 使用 `$shop_id` 作为缓存键,或改为实例属性。 | `private static $date_cache = [];`<br>`if (!isset(self::$date_cache[$shop_id])) { ... }`<br>`return self::$date_cache[$shop_id];` | | 🔴 严重 | `get_community_revenues_sum_data`<br>`get_community_revenues_detail_data` | 使用字符串拼接构造复杂 `WHERE` 条件:`'(a._pay_platform=' . $pay_platform . ' ...'`。若 `$params` 来源未严格校验,将引发 **SQL 注入**。 | 严禁手动拼接 SQL。使用框架查询构建器的 `group_start()`/`group_end()` 或 `where_in` 参数绑定。 | `$this->db->group_start();`<br>`foreach (...) { $this->db->or_where(...); }`<br>`$this->db->group_end();` | | 🟠 警告 | 多个方法内部 | `$this->load->model()` 在每个业务方法中重复调用。CI 框架中重复加载会增加文件 I/O 与内存开销,且不符合依赖注入规范。 | 统一移至 `__construct()` 中加载,或配置自动加载。 | `public function __construct() { parent::__construct(); $this->load->model(['ahead_yc_merchant_model', 'ahead_yc_order_model', ...]); }` | | 🟠 警告 | `get_community_revenues_data`<br>`get_community_revenues_sum_data` | 营收分类聚合逻辑(`wx_income_amount` 等计算)在两个方法中**完全重复**。后续新增支付类型需修改多处,极易遗漏导致数据不一致。 | 抽取为私有方法 `_aggregate_revenues(array $data): array`,统一处理。 | 见下方重构示例 | | 🟠 警告 | `_add_log` | `isset(self::ORDER_TYPE[$insert['_order_type']])` 中常量键为字符串,若传入整型在 PHP 8+ 严格类型场景下可能匹配失败。 | 统一类型转换或使用 `array_key_exists`。 | `isset(self::ORDER_TYPE[(string)$insert['_order_type']])` | | 🟡 建议 | 类定义/属性 | 类名 `Jh_community_shop_revenues_detail_model` 与方法名不符合 PSR-12 规范(应为 `StudlyCaps`/`camelCase`)。属性声明为 `public` 破坏封装性。 | 重命名类与方法,属性改为 `protected`。 | `class JhCommunityShopRevenuesDetailModel extends Report_model`<br>`protected $main_table_name = ...;` | | 🟡 建议 | 多处 | 魔法数字/字符串硬编码(如 `'1'`, `'8'`, `86400`, `3600`),降低可读性与可配置性。 | 提取为类常量或业务配置项。 | `const PAY_PLATFORM_WECHAT = '1';`<br>`const SECONDS_PER_DAY = 86400;` | | 🟡 建议 | PHPDoc 注释 | 多处 `@return true` 不符合 PHPDoc 标准,IDE 无法正确推断类型。 | 统一改为 `@return bool`。 | `@return bool` | ## 3. 总结与行动建议 ### 🔑 优先修复项(P0/P1) 1. **移除全局 `$CI` 实例化**:立即删除文件顶部的 `$CI = &get_instance();`,避免框架初始化时序冲突。 2. **修复静态缓存串扰**:将 `get_date_type_info` 的静态缓存改为按 `$shop_id` 隔离,否则多门店并发请求必现数据污染。 3. **消除 SQL 拼接风险**:将 `pay_platform_where` 的字符串拼接替换为框架安全的参数绑定或 `or_where` 链式调用。 4. **统一模型加载**:将分散的 `$this->load->model()` 收敛至构造函数,降低运行时开销。 ### 🛠 重构与优化方向 1. **聚合逻辑抽象**: ```php private function _aggregate_revenues(array $data): array { $totals = ['wx' => 0, 'tuangou_pay' => 0, 'tuangou' => 0, 'other' => 0]; foreach ($data as $v) { $key = $v['type'] == 1 ? 'income' : 'refund'; $platform = $v['pay_platform'] == 1 ? 'wx' : ($v['pay_platform'] == 8 ? 'tuangou_pay' : ($v['pay_platform'] == 9 ? 'tuangou' : 'other')); $totals[$platform][$key] += $v['amount']; } // 统一格式化并返回结果数组 return array_map(fn($v) => change_number_format($v['income'] - $v['refund']), $totals); } ``` 2. **数据库连接切换优化**:频繁调用 `enforce_con_db()` 会引发连接池抖动。建议在 `Report_model` 基类中实现**读写分离/分库路由的懒加载机制**,或在事务外层统一控制连接切换。 3. **框架适配说明**:代码中大量使用 `enforce_con_db()`、`set_table_v2()`、`check_table_exist()` 等自定义方法。若 `phpci` 框架未提供官方文档,建议补充内部注释说明其底层实现(如是否基于 CI 的 `DB::instance()` 或自定义分表中间件),以便后续维护者理解生命周期。 4. **异常处理规范化**:将 `throwError('...')` 替换为标准 `throw new \InvalidArgumentException('...')` 或框架内置异常类,便于全局错误拦截与日志追踪。 > 💡 **提示**:若代码片段仅展示模型层,建议同步审查调用该模型的 Controller/Service 层,确保 `$params` 输入经过严格过滤(如 `intval()`、`filter_var()`),从源头阻断越权与注入风险。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1780453767
updated_unix
1780453767
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel