|
|
@@ -0,0 +1,127 @@
|
|
|
+# Transfer订单管理后台页面枚举显示错误修复
|
|
|
+
|
|
|
+## 任务概述
|
|
|
+修复Transfer模块后台订单管理页面(/admin/transfer/orders)的枚举显示错误,解决"array_key_exists(): Argument #1 ($key) must be a valid array offset type"错误。
|
|
|
+
|
|
|
+## 完成时间
|
|
|
+2025-06-18 23:06
|
|
|
+
|
|
|
+## 问题描述
|
|
|
+访问后台Transfer订单管理页面时出现TypeError错误:
|
|
|
+```
|
|
|
+TypeError: "array_key_exists(): Argument #1 ($key) must be a valid array offset type"
|
|
|
+```
|
|
|
+
|
|
|
+## 问题分析
|
|
|
+1. **根本原因**: TransferOrder模型使用了枚举类型转换(enum casting)
|
|
|
+ - `type` 字段转换为 `TransferType` 枚举
|
|
|
+ - `status` 字段转换为 `TransferStatus` 枚举
|
|
|
+
|
|
|
+2. **错误触发**: Dcat Admin的`using()`方法期望接收原始数据库值(整数),但实际接收到的是枚举对象
|
|
|
+
|
|
|
+3. **具体位置**: `TransferOrderHelper::grid()`方法中的type和status列配置
|
|
|
+
|
|
|
+## 解决方案
|
|
|
+将`using()`和`label()`方法组合改为`display()`方法,手动处理枚举对象:
|
|
|
+
|
|
|
+### 修改前代码
|
|
|
+```php
|
|
|
+$grid->column('type', '类型')->using([
|
|
|
+ 1 => '转入',
|
|
|
+ 2 => '转出',
|
|
|
+])->label([
|
|
|
+ 1 => 'success',
|
|
|
+ 2 => 'warning',
|
|
|
+]);
|
|
|
+
|
|
|
+$grid->column('status', '状态')->using([
|
|
|
+ 1 => '已创建',
|
|
|
+ 20 => '处理中',
|
|
|
+ 30 => '已回调',
|
|
|
+ 100 => '已完成',
|
|
|
+ -1 => '失败',
|
|
|
+])->label([
|
|
|
+ 1 => 'info',
|
|
|
+ 20 => 'warning',
|
|
|
+ 30 => 'primary',
|
|
|
+ 100 => 'success',
|
|
|
+ -1 => 'danger',
|
|
|
+]);
|
|
|
+```
|
|
|
+
|
|
|
+### 修改后代码
|
|
|
+```php
|
|
|
+$grid->column('type', '类型')->display(function ($value) {
|
|
|
+ $typeMap = [
|
|
|
+ 1 => '转入',
|
|
|
+ 2 => '转出',
|
|
|
+ ];
|
|
|
+ $labelMap = [
|
|
|
+ 1 => 'success',
|
|
|
+ 2 => 'warning',
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 处理枚举对象
|
|
|
+ $typeValue = is_object($value) ? $value->value : $value;
|
|
|
+ $text = $typeMap[$typeValue] ?? '未知';
|
|
|
+ $label = $labelMap[$typeValue] ?? 'default';
|
|
|
+
|
|
|
+ return "<span class=\"label label-{$label}\">{$text}</span>";
|
|
|
+});
|
|
|
+
|
|
|
+$grid->column('status', '状态')->display(function ($value) {
|
|
|
+ $statusMap = [
|
|
|
+ 1 => '已创建',
|
|
|
+ 20 => '处理中',
|
|
|
+ 30 => '已回调',
|
|
|
+ 100 => '已完成',
|
|
|
+ -1 => '失败',
|
|
|
+ ];
|
|
|
+ $labelMap = [
|
|
|
+ 1 => 'info',
|
|
|
+ 20 => 'warning',
|
|
|
+ 30 => 'primary',
|
|
|
+ 100 => 'success',
|
|
|
+ -1 => 'danger',
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 处理枚举对象
|
|
|
+ $statusValue = is_object($value) ? $value->value : $value;
|
|
|
+ $text = $statusMap[$statusValue] ?? '未知';
|
|
|
+ $label = $labelMap[$statusValue] ?? 'default';
|
|
|
+
|
|
|
+ return "<span class=\"label label-{$label}\">{$text}</span>";
|
|
|
+});
|
|
|
+```
|
|
|
+
|
|
|
+## 技术要点
|
|
|
+1. **枚举兼容性**: 通过`is_object($value) ? $value->value : $value`判断处理枚举对象和原始值
|
|
|
+2. **HTML标签生成**: 手动生成带样式的span标签,保持原有的视觉效果
|
|
|
+3. **容错处理**: 使用`??`操作符提供默认值,避免未知状态导致错误
|
|
|
+
|
|
|
+## 验证结果
|
|
|
+✅ 页面正常加载,无错误信息
|
|
|
+✅ 类型列正确显示"转入"/"转出"标签
|
|
|
+✅ 状态列正确显示各种状态标签
|
|
|
+✅ 颜色标签正常显示
|
|
|
+✅ 其他功能(筛选、排序、操作按钮)正常工作
|
|
|
+
|
|
|
+## 影响范围
|
|
|
+- **修改文件**: `app/Module/Transfer/AdminControllers/Helper/TransferOrderHelper.php`
|
|
|
+- **影响功能**: Transfer订单管理后台列表页面
|
|
|
+- **向后兼容**: 完全兼容,不影响其他功能
|
|
|
+
|
|
|
+## 经验总结
|
|
|
+1. **枚举使用注意**: 在Dcat Admin中使用枚举类型时,需要注意Grid列的显示方法选择
|
|
|
+2. **错误排查**: 类似的array_key_exists错误通常与数据类型不匹配有关
|
|
|
+3. **最佳实践**: 对于复杂的数据转换,使用`display()`方法比`using()`方法更灵活可控
|
|
|
+
|
|
|
+## 提交信息
|
|
|
+```
|
|
|
+修复Transfer订单管理后台页面枚举显示错误
|
|
|
+
|
|
|
+- 修复TransferOrderHelper中type和status字段的显示问题
|
|
|
+- 将using()方法改为display()方法,正确处理枚举对象
|
|
|
+- 确保类型和状态字段能正确显示中文标签和颜色
|
|
|
+- 解决array_key_exists错误,页面现在正常加载
|
|
|
+```
|