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 635 from issue
id
635
repo_id
21
index
286
poster_id
1
original_author
original_author_id
0
name
🔍 代码审查报告:pay-260616 - 预订开房后退款
content
## 自动代码审查报告 **分支**: pay-260616 **提交**: `6d68c74c8
## 自动代码审查报告 **分支**: pay-260616 **提交**: `6d68c74c8a53ee6664976274f5246cdd087173f6` **提交人**: LITTLEMAIDI (11833999+littlemaidi@user.noreply.gitee.com) **时间**: 2026-06-09 15:18:13 --- ## 1. 审查摘要 - **代码质量评分**:6.5 / 10 分 - **总体评价**:代码完整覆盖了预订下单、支付回调、退款、消息推送等核心电商/预订业务链路,具备一定的幂等性设计与缓存优化意识。但存在**事务与外部网络请求强耦合、SQL 拼接隐患、模型重复加载、魔法数字泛滥**等问题,在资金流转场景下存在较高的数据一致性风险与可维护性瓶颈。 - **风险等级**:🔴 高(涉及支付退款、事务一致性、潜在 SQL 注入) > 📌 **框架说明**:代码语法与架构高度匹配 **CodeIgniter 3** 框架(如 `$this->load->model()`、`$this->db->trans_start()`)。若 `phpci` 为贵司定制框架,请确认其事务与模型加载机制是否与 CI3 一致。以下建议基于 CI3 最佳实践给出。 ## 2. 问题详情 | 严重程度 | 文件/行号 | 问题描述 | 建议修改方案 | 代码示例 (可选) | | :--- | :--- | :--- | :--- | :--- | | 🔴 严重 | `Ahead_book_order_model.php`<br>`check_notify` / `refund_by_notify` | **事务内调用外部支付 API**:微信/银联退款请求在 `trans_start()` 与 `trans_complete()` 之间执行。网络超时或第三方异常会导致数据库长事务锁表,且手动 `trans_rollback()` 与 CI 自动事务管理机制冲突。 | 将第三方 API 调用移出数据库事务。先完成本地数据状态更新与提交,再执行退款请求;若退款失败,通过补偿任务或异步队列处理。 | ```php<br>// 错误做法<br>$this->db->trans_start();<br>$res = WxPayApi::refund($input); // 阻塞事务<br><br>// 正确做法<br>$this->db->trans_start();<br>// 1. 更新本地状态<br>$this->update(...);<br>$this->db->trans_complete();<br>// 2. 事务提交后执行外部请求<br>$res = WxPayApi::refund($input);<br>``` | | 🔴 严重 | `Ahead_book_order_model.php`<br>`refund_by_notify` (约 380 行) | **SQL 注入风险**:`$log_where` 与 `$log_up` 使用字符串拼接直接传入底层查询方法,未使用查询构建器或参数绑定。若 `_id` 来源不可控,将导致注入。 | 严格使用 CI 查询构建器或数组传参,避免原始 SQL 拼接。 | ```php<br>// 错误<br>$log_where = '_relation_id="'.$order_data['_id'].'" ...';<br>$this->ahead_pay_log_model->up($log_up, $log_where);<br><br>// 正确<br$this->db->where('_relation_id', $order_data['_id'])<br> ->where_in('_type', [5, 13])<br> ->where('_status', 1)<br> ->update('ahead_pay_log', [<br> '_status' => 4,<br> '_refund_amount' => $this->db->escape_identifiers('_actual_pay')<br> ]);<br>``` | | 🟠 警告 | `Ahead_book_order_model.php`<br>`refund` (约 560 行) | **退款幂等性逻辑缺陷**:执行 `update` 后通过 `$this->db->affected_rows()` 判断是否成功。若订单已处于退款状态(`_status=4`),`affected_rows()` 为 0,代码会回滚并返回失败,不符合接口幂等性规范。 | 更新前先校验状态,若已退款直接返回成功;或忽略 `affected_rows() === 0` 的情况。 | ```php<br>$current = $this->get_one(['_id' => $order_id], '_status');<br>if ($current['_status'] == 4) {<br> return ['status' => true, 'msg' => '订单已退款'];<br>}<br>$this->update(['_status' => 4], ['_id' => $order_id]);<br>``` | | 🟠 警告 | `Ahead_book_order_model.php`<br>`send_success_msg` / `refund_by_notify` | **频繁重复加载模型/配置**:方法内部多次调用 `$this->load->model()` 与 `$CI->config->load()`,增加文件 I/O 与内存开销。 | 将依赖模型移至构造函数加载,或使用 CI 自动加载配置。配置项建议全局缓存。 | ```php<br>public function __construct() {<br> parent::__construct();<br> $this->load->model(['ahead_merchant_room_type_model', 'ahead_yc_notice_model', 'ahead_user_model']);<br>}<br>``` | | 🟠 警告 | `Ahead_book_order_operation_log_model.php`<br>`add_log_by_renewals_package` | **变量未初始化**:`$source` 仅在 `if/elseif` 分支中赋值,若未匹配任何条件,`$source ?? 0` 虽兼容 PHP7+,但逻辑不严谨且易引发静态分析警告。 | 方法首行显式初始化 `$source = 0;`。 | ```php<br>public function add_log_by_renewals_package($order_data) {<br> $source = 0;<br> if ($order_data['_pay_scene'] == '1') { $source = 2; }<br> // ...<br>}<br>``` | | 🟡 建议 | `Ahead_book_order_model.php`<br>全局 | **魔法数字/字符串泛滥**:大量硬编码状态码、支付类型、模板 ID(如 `1, 3, 5, 8, 9, 14, 22, 56, 58, 99`),严重降低可读性与后期维护成本。 | 提取为类常量或独立配置文件,配合 IDE 提示与类型约束。 | ```php<br>class Ahead_book_order_model extends Simple_model {<br> const STATUS_PENDING = -1;<br> const STATUS_PAID = 1;<br> const PAY_SCENE_WECHAT = '5';<br> // ...<br>}<br>``` | | 🟡 建议 | `Ahead_book_order_model.php`<br>文件头部 | **全局实例获取不规范**:`$CI = &get_instance();` 写在类外部,违反 PSR-12 规范,且可能导致作用域污染或单例状态异常。 | 删除头部代码。CI 模型内部可直接通过 `$this->config`、`$this->load` 访问,无需手动获取 CI 实例。 | ```php<br>// 删除文件顶部的 $CI = &get_instance();<br>// 类内部直接使用 $this->config->item('wx');<br>``` | | 🟡 建议 | `Ahead_book_order_model.php`<br>`create_community_shop_book_order` | **代码片段不完整**:文件末尾 `if ($this->tuangou->verify_token) {` 后缺失逻辑与闭合括号,无法评估完整业务流程与异常处理。 | 补充完整代码或说明截断原因,确保事务闭合与返回值完整。 | *(需补充完整代码)* | ## 3. 总结与行动建议 ### 🔑 优先修复的关键问题 1. **解耦事务与外部请求**:立即将 `refund_by_notify` 与 `check_notify` 中的微信/银联退款、短信发送等网络请求移出 `trans_start()` 范围。资金类操作必须保证“本地状态变更”与“第三方调用”的异步或最终一致性。 2. **修复 SQL 拼接隐患**:全面替换 `$log_where`、`$log_up` 等原始字符串拼接,统一使用 CI 查询构建器(`$this->db->where()` / `update()` 数组传参),杜绝注入风险。 3. **修正退款幂等逻辑**:调整 `refund` 方法的状态更新判断逻辑,确保重复请求或已退款订单能安全返回成功状态,避免业务层误判失败。 ### 🛠 后续重构与优化方向 - **拆分“上帝类” (SRP 原则)**:`Ahead_book_order_model` 承载了订单、支付、退款、消息推送、社区门店、团购核销等过多职责。建议拆分为: - `BookOrderModel`(核心订单 CRUD) - `PaymentService`(支付/退款网关封装) - `NotificationService`(短信/微信模板消息) - `CommunityShopService`(社区门店专属逻辑) - **统一状态与配置管理**:建立全局 `OrderStatus`、`PayPlatform`、`PayScene` 常量类或枚举(PHP 8.1+),替换魔法数字。将 `wx_template` 等配置项移至 `config/` 目录并启用缓存。 - **引入队列机制**:支付成功后的短信/微信推送、流水记录、社区收入同步等**非强一致性操作**,建议接入 Redis/RabbitMQ 队列异步执行,大幅降低主流程响应时间与数据库锁竞争。 - **补充自动化测试**:针对 `check_notify`(支付回调)与 `refund_by_notify` 编写单元测试与集成测试,覆盖正常支付、重复回调、退款失败、库存不足等边界场景,确保资金链路健壮性。 > 💡 **提示**:若 `phpci` 框架对事务或模型加载有特殊封装(如自动连接池、读写分离、自定义 ORM),请对照其官方文档调整上述 CI3 语法建议。代码审查基于当前提供的片段,若存在未暴露的中间件或全局钩子,实际风险可能有所差异。 --- *此 Issue 由代码审查服务自动创建*
...
milestone_id
0
priority
0
is_closed
0
is_pull
0
num_comments
0
ref
deadline_unix
0
created_unix
1780989493
updated_unix
1780989493
closed_unix
0
is_locked
0
content_version
0
time_estimate
0
Delete
Cancel