HasCustomFields.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace App\Traits;
  3. use App\Model\CustomFieldValue;
  4. use App\Model\SysOssTasks;
  5. use App\Service\FileUploadService;
  6. trait HasCustomFields
  7. {
  8. /**
  9. * 基础关联:关联到新的自定义值表
  10. * 自动通过当前模型的 getTable() 获取 table_name 过滤
  11. */
  12. public function customFields()
  13. {
  14. return $this->hasMany(CustomFieldValue::class, 'model_id')
  15. ->where('table_name', $this->getTable())
  16. ->where('del_time', 0)
  17. ->select('id','field_label','field_value','field_type','model_id', 'field_name');
  18. }
  19. /**
  20. * 获取格式化后的自定义字段列表
  21. * 用于详情页接口返回给前端 custom_fields 数组
  22. */
  23. public function getFormattedCustomFields()
  24. {
  25. return $this->customFields->map(function (&$item) {
  26. $field_value_show = "";
  27. if($item->field_type == SysOssTasks::type_two) $field_value_show = $this->getCustomFileUrl($item->field_value);
  28. $item['field_value_show'] = $field_value_show;
  29. return $item;
  30. })->values()->toArray();
  31. }
  32. /**
  33. * 内部方法:处理附件显示地址
  34. */
  35. protected function getCustomFileUrl($path)
  36. {
  37. if (empty($path)) return "";
  38. $fileUploadService = new FileUploadService();
  39. return $fileUploadService->getFileShow($path);
  40. }
  41. }