UploadFile.php 2.6 KB

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