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 274 from issue
id
274
repo_id
21
index
59
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pay-260519 - 1
content
## 自动代码审查报告 **分支**: pay-260519 **提交**: `5d31b5604
## 自动代码审查报告 **分支**: pay-260519 **提交**: `5d31b56044a16cb5b46fa6ec83c79701df0309ec` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-05-21 15:47:23 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 分 - **总体评价**:代码整体实现了预订、支付、退款及营收流水的核心业务逻辑,具备一定的事务意识与异常捕获机制。但存在**资金状态与第三方API调用顺序倒置**的严重逻辑缺陷,模型中频繁动态加载组件导致性能损耗,且大量硬编码魔法数字与不规范的日志/中断处理降低了可维护性。部分核心方法过长,违反单一职责原则。 - **风险等级**:🔴 高(涉及资金流转状态不一致、潜在数据脏写、模型层强制中断流程) > 📌 **框架说明**:基于提供的目录结构(`system/`、`application/`)及 `$CI->load->model()` 语法,推测该项目基于 **CodeIgniter 3.x** 架构或深度定制版(`phpci`)。以下审查建议基于 CI3 最佳实践与通用 PHP 规范,若为自研框架请对照官方文档调整。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `Ahead_book_order_change_pay_log_model.php` / `refund()` | **状态提前更新导致资金不一致**:在调用微信/银联退款 API 前,已执行 `$this->update(['_status' => 4], ...)`。若第三方 API 返回失败或网络超时,数据库状态已变更为“退款完成”,但资金未退回,造成严重账务差异。 | 将状态更新移至 API 调用成功之后;或使用数据库事务包裹状态更新,API 失败时回滚。 | `// 1. 调用 API<br>$res = WxPayApi::refund($input);<br>if ($res['result_code'] !== 'SUCCESS') { return ['status'=>false, 'msg'=>...]; }<br>// 2. API 成功后再更新状态<br>$this->update(['_status' => 4], ['_id' => $data['_id']]);` | | 🔴 严重 | `Ahead_book_order_model.php` / `refund_by_notify()` | **退款后置操作缺乏事务保护**:支付网关退款成功后,执行了 `ahead_book_order_refund_model`、`ahead_pay_log_model`、`jh_community_shop_revenues_detail_model` 等多表更新。若中途报错或超时,将产生部分成功、部分失败的脏数据。 | 使用 CI3 事务机制包裹 API 成功后的所有数据库写操作。 | `$this->db->trans_start();<br>// ... 执行所有 update/insert ...<br>$this->db->trans_complete();<br>if ($this->db->trans_status() === FALSE) { $this->db->trans_rollback(); return ['status'=>false, 'msg'=>'数据更新异常']; }` | | 🟠 警告 | 全局多处 | **浮点数直接参与金额计算**:`$data['_actual_pay'] * 100` 在 PHP 中可能因 IEEE 754 精度问题产生 `100.00000000000001` 或 `99.99999999999999`,导致支付/退款金额差 1 分。 | 金额计算统一转为“分”整数处理,或使用 `bcmul()` / `round()` 确保精度。 | `$total_fee = (int)round($data['_actual_pay'] * 100, 0);` | | 🟠 警告 | `Jh_community_shop_revenues_detail_model.php` / `update_room_info()` 等 | **模型层使用 `exit()` 中断流程**:直接调用 `exit()` 会破坏框架生命周期,导致无法捕获异常、无法返回标准 JSON 响应,且极难进行单元测试。 | 改为抛出异常或返回状态数组,交由控制器统一处理错误响应。 | `if (!$this->check_table_exist(false)) { throw new RuntimeException('商家分表不存在'); }` | | 🟠 警告 | 多个 Model 文件 | **方法内频繁动态加载模型**:如 `$this->load->model('ahead_yc_shop_model')` 散落在业务方法中,每次调用都会触发文件包含与实例化,增加 I/O 与内存开销。 | 将依赖模型统一移至 `__construct()` 中加载,或配置 CI 自动加载。 | `public function __construct() { parent::__construct(); $this->load->model(['ahead_yc_shop_model', 'ahead_chinaums_set_model']); }` | | 🟡 建议 | `Ahead_book_order_model.php` / `refund_by_notify()` | **潜在 SQL 注入风险**:`$log_up = '_status=4,_refund_amount=_actual_pay'; $this->ahead_pay_log_model->up($log_up, $log_where);` 若 `up()` 为自定义方法且直接拼接 SQL,未使用 Query Builder 或参数绑定,存在注入隐患。 | 优先使用 CI 的 `$this->db->set()` 与 `$this->db->update()`,或确保自定义 `up()` 内部严格转义。 | `$this->db->set('_status', 4);<br>$this->db->set('_refund_amount', '_actual_pay', FALSE);<br>$this->db->where($log_where);<br>$this->db->update('table_name');` | | 🟡 建议 | 全局 | **魔法数字泛滥**:`1, 3, 4, 8, 14` 等状态码、平台码硬编码在逻辑中,可读性差且后期维护极易出错。 | 提取为类常量或独立配置文件。 | `const PAY_PLATFORM_WX = 1; const PAY_PLATFORM_VIP = 3; const STATUS_REFUNDED = 4;` | | 🟡 建议 | 全局 | **日志函数命名不一致 & 敏感信息泄露**:混用 `doLog` 与 `do_log`;且 `doLog(var_export($res, true), ...)` 可能将包含 `sign`、`access_token` 的完整 API 响应写入日志。 | 统一日志函数名;记录前过滤敏感字段。 | `do_log($this->filter_sensitive_data($res), 'wx_refund');` | | 🟡 建议 | `Ahead_book_order_model.php` (末尾) | **代码片段截断**:`if ($this->tuangou->con` 未闭合,无法评估完整逻辑与潜在缺陷。 | 请补充完整代码以便进行闭环审查。 | N/A | ## 3. 总结与行动建议 ### 🔑 优先修复的关键问题 1. **修复退款状态时序问题**:立即调整 `Ahead_book_order_change_pay_log_model::refund()` 的执行顺序,确保**第三方退款 API 成功返回后再更新本地数据库状态**。这是当前最高优先级的资金安全漏洞。 2. **补全事务边界**:在 `Ahead_book_order_model::refund_by_notify()` 中,将支付网关调用后的所有关联表更新(退款日志、流水记录、营收明细、团购核销撤销等)包裹在 `$this->db->trans_start()` 与 `$this->db->trans_complete()` 中,失败时自动回滚。 3. **移除模型层 `exit()`**:将 `Jh_community_shop_revenues_detail_model` 中的 `exit()` 替换为异常抛出或状态返回,确保 Web 请求能正常返回 HTTP 响应码与业务提示。 ### 🛠 后续重构与优化方向 - **架构解耦与单一职责**:当前 `refund()`、`refund_by_notify()`、`send_success_msg()` 等方法均超过 150 行,嵌套过深。建议按业务域拆分: - 将支付网关调用封装为独立的 `PaymentGatewayService`。 - 将消息推送(微信模板、短信)抽离为 `NotificationService`。 - Model 层仅保留数据读写与基础校验,复杂编排逻辑下沉至 Service 层。 - **金额计算规范化**:全局统一金额存储与计算单位(建议底层统一使用“分”整数,展示层再格式化)。废弃 `float` 直接运算,引入 `bcmath` 扩展或封装 `Money` 值对象。 - **依赖注入与自动加载**:利用 CI3 的 `config/autoload.php` 或模型构造函数预加载高频依赖模型,消除运行时动态加载的性能损耗。 - **常量与配置集中化**:建立 `config/payment.php` 与 `config/order_status.php`,将平台标识、状态码、超时阈值等魔法数字集中管理,提升代码可读性与多环境适配能力。 - **日志安全规范**:制定日志脱敏规范,禁止直接 `var_export` 第三方 API 全量响应。关键资金操作日志需包含 `trace_id`、操作人、前后状态快照,便于审计与对账。 > 💡 **提示**:若 `phpci` 为内部定制框架,请重点核对框架对数据库事务的自动提交机制、自定义 `up()` 方法的底层实现是否支持预处理语句,以及全局异常处理器是否已覆盖模型层抛出的异常。建议补充完整代码后,可进一步进行静态扫描(如 PHPStan / Psalm)与支付沙箱联调验证。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1779349643
updated_unix
1779349643
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel