Attachment.php 1.8 KB

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