UploadFile.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\common\model\file;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 文件库模型
  6. */
  7. class UploadFile extends BaseModel
  8. {
  9. protected $pk = 'file_id';
  10. protected $name = 'upload_file';
  11. protected $updateTime = false;
  12. protected $deleteTime = false;
  13. protected $append = ['file_path'];
  14. /**
  15. * 关联文件库分组表
  16. */
  17. public function uploadGroup()
  18. {
  19. return $this->belongsTo('UploadGroup', 'group_id');
  20. }
  21. /**
  22. * 获取图片完整路径
  23. * @param $value
  24. * @param $data
  25. * @return string
  26. */
  27. public function getFilePathAttr($value, $data)
  28. {
  29. if ($data['storage'] === 'local') {
  30. return self::$base_url . 'uploads/' . $data['save_name'];
  31. }
  32. return $data['file_url'] . '/' . $data['file_name'];
  33. }
  34. /**
  35. * 文件详情
  36. */
  37. public static function detail($file_id)
  38. {
  39. return (new static())->find($file_id);
  40. }
  41. /**
  42. * 根据文件名查询文件id
  43. */
  44. public static function getFildIdByName($fileName)
  45. {
  46. return (new static)->where(['file_name' => $fileName])->value('file_id');
  47. }
  48. /**
  49. * 查询文件id
  50. */
  51. public static function getFileName($fileId)
  52. {
  53. return (new static)->where(['file_id' => $fileId])->value('file_name');
  54. }
  55. /**
  56. * 添加新记录
  57. */
  58. public function add($data)
  59. {
  60. return $this->save($data);
  61. }
  62. /**
  63. * 获取列表记录
  64. */
  65. public function getList($groupId = 0, $fileType = '', $pageSize = 30, $isRecycle = -1, $shop_supplier_id = 0)
  66. {
  67. $model = $this;
  68. // 文件分组
  69. if ($groupId != 0) {
  70. $model = $model->where('group_id', '=', (int)$groupId);
  71. }
  72. // 文件类型
  73. !empty($fileType) && $model = $model->where('file_type', '=', trim($fileType));
  74. // 是否在回收站
  75. $isRecycle > -1 && $model = $model->where('is_recycle', '=', (int)$isRecycle);
  76. // 查询列表数据
  77. return $model->with(['upload_group'])
  78. ->where(['is_user' => 0, 'is_delete' => 0])
  79. ->where('shop_supplier_id', '=', $shop_supplier_id)
  80. ->order(['file_id' => 'desc'])
  81. ->paginate($pageSize)
  82. ->each(function($item, $k){
  83. $item['file_path'] = preg_replace("/^(http|https):\/\//", '//', $item['file_path']);
  84. });
  85. }
  86. }