| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
- <?php
- namespace App\Traits;
- use App\Model\CustomFieldValue;
- use App\Model\SysOssTasks;
- use App\Service\FileUploadService;
- trait HasCustomFields
- {
- /**
- * 基础关联:关联到新的自定义值表
- * 自动通过当前模型的 getTable() 获取 table_name 过滤
- */
- public function customFields()
- {
- return $this->hasMany(CustomFieldValue::class, 'model_id')
- ->where('table_name', $this->getTable())
- ->where('del_time', 0)
- ->select('id','field_label','field_value','field_type','model_id', 'field_name', 'crt_id', 'is_delivery');
- }
- /**
- * 获取格式化后的自定义字段列表
- * 用于详情页接口返回给前端 custom_fields 数组
- */
- public function getFormattedCustomFields()
- {
- return $this->customFields->map(function (&$item) {
- $field_value_show = "";
- if($item->field_type == SysOssTasks::type_two) $field_value_show = $this->getCustomFileUrl($item->field_value);
- $item['field_value_show'] = $field_value_show;
- return $item;
- })->values()->toArray();
- }
- /**
- * 内部方法:处理附件显示地址
- */
- protected function getCustomFileUrl($path)
- {
- if (empty($path)) return "";
- $fileUploadService = new FileUploadService();
- return $fileUploadService->getFileShow($path);
- }
- }
|