UploadFile.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\store\model;
  3. use think\Request;
  4. use app\common\model\UploadFile as UploadFileModel;
  5. use app\store\model\Setting as SettingModel;
  6. use app\common\library\storage\Driver as StorageDriver;
  7. /**
  8. * 文件库模型
  9. * Class UploadFile
  10. * @package app\store\model
  11. */
  12. class UploadFile extends UploadFileModel
  13. {
  14. /**
  15. * 获取列表记录
  16. * @param int $groupId 分组id
  17. * @param string $fileType 文件类型
  18. * @param bool|int $isRecycle
  19. * @return \think\Paginator
  20. * @throws \think\exception\DbException
  21. */
  22. public function getList($groupId = -1, $fileType = '', $isRecycle = -1)
  23. {
  24. // 文件分组
  25. $groupId != -1 && $this->where('group_id', '=', (int)$groupId);
  26. // 文件类型
  27. !empty($fileType) && $this->where('file_type', '=', trim($fileType));
  28. // 是否在回收站
  29. $isRecycle > -1 && $this->where('is_recycle', '=', (int)$isRecycle);
  30. // 查询列表数据
  31. return $this->with(['upload_group'])
  32. ->where(['is_user' => 0, 'is_delete' => 0])
  33. ->order(['file_id' => 'desc'])
  34. ->paginate(32, false, [
  35. 'query' => Request::instance()->request()
  36. ]);
  37. }
  38. /**
  39. * 移入|移出回收站
  40. * @param bool $isRecycle
  41. * @return false|int
  42. */
  43. public function setRecycle($isRecycle = true)
  44. {
  45. return $this->save(['is_recycle' => (int)$isRecycle]);
  46. }
  47. /**
  48. * 删除文件
  49. * @return false|int
  50. * @throws \think\Exception
  51. */
  52. public function setDelete()
  53. {
  54. // 存储配置信息
  55. $config = SettingModel::getItem('storage');
  56. // 实例化存储驱动
  57. $StorageDriver = new StorageDriver($config, $this['storage']);
  58. // 删除文件
  59. if (!$StorageDriver->delete($this['file_name'])) {
  60. $this->error = '文件删除失败:' . $StorageDriver->getError();
  61. return false;
  62. }
  63. return $this->save(['is_delete' => 1]);
  64. }
  65. /**
  66. * 批量软删除
  67. * @param $fileIds
  68. * @return $this
  69. */
  70. public function softDelete($fileIds)
  71. {
  72. return $this->where('file_id', 'in', $fileIds)->update(['is_recycle' => 1]);
  73. }
  74. /**
  75. * 批量移动文件分组
  76. * @param $group_id
  77. * @param $fileIds
  78. * @return $this
  79. */
  80. public function moveGroup($group_id, $fileIds)
  81. {
  82. return $this->where('file_id', 'in', $fileIds)->update(compact('group_id'));
  83. }
  84. }