Attachment.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\admin\controller\routine;
  3. use Throwable;
  4. use ba\Filesystem;
  5. use think\facade\Event;
  6. use app\common\controller\Backend;
  7. use app\common\model\Attachment as AttachmentModel;
  8. class Attachment extends Backend
  9. {
  10. /**
  11. * @var object
  12. * @phpstan-var AttachmentModel
  13. */
  14. protected object $model;
  15. protected string|array $quickSearchField = 'name';
  16. protected array $withJoinTable = ['admin', 'user'];
  17. protected string|array $defaultSortField = 'last_upload_time,desc';
  18. public function initialize(): void
  19. {
  20. parent::initialize();
  21. $this->model = new AttachmentModel();
  22. }
  23. /**
  24. * 删除
  25. * @param array $ids
  26. * @throws Throwable
  27. */
  28. public function del(array $ids = []): void
  29. {
  30. if (!$this->request->isDelete() || !$ids) {
  31. $this->error(__('Parameter error'));
  32. }
  33. $dataLimitAdminIds = $this->getDataLimitAdminIds();
  34. if ($dataLimitAdminIds) {
  35. $this->model->where($this->dataLimitField, 'in', $dataLimitAdminIds);
  36. }
  37. $pk = $this->model->getPk();
  38. $data = $this->model->where($pk, 'in', $ids)->select();
  39. $count = 0;
  40. try {
  41. foreach ($data as $v) {
  42. Event::trigger('AttachmentDel', $v);
  43. $filePath = Filesystem::fsFit(public_path() . ltrim($v->url, '/'));
  44. if (file_exists($filePath)) {
  45. unlink($filePath);
  46. Filesystem::delEmptyDir(dirname($filePath));
  47. }
  48. $count += $v->delete();
  49. }
  50. } catch (Throwable $e) {
  51. $this->error(__('%d records and files have been deleted', [$count]) . $e->getMessage());
  52. }
  53. if ($count) {
  54. $this->success(__('%d records and files have been deleted', [$count]));
  55. } else {
  56. $this->error(__('No rows were deleted'));
  57. }
  58. }
  59. }