Attachment.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace app\admin\controller\general;
  3. use app\common\controller\Backend;
  4. /**
  5. * 附件管理
  6. *
  7. * @icon fa fa-circle-o
  8. * @remark 主要用于管理上传到又拍云的数据或上传至本服务的上传数据
  9. */
  10. class Attachment extends Backend
  11. {
  12. /**
  13. * @var \app\common\model\Attachment
  14. */
  15. protected $model = null;
  16. public function _initialize()
  17. {
  18. parent::_initialize();
  19. $this->model = model('Attachment');
  20. }
  21. /**
  22. * 查看
  23. */
  24. public function index()
  25. {
  26. //设置过滤方法
  27. $this->request->filter(['strip_tags']);
  28. if ($this->request->isAjax())
  29. {
  30. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  31. $total = $this->model
  32. ->where($where)
  33. ->order($sort, $order)
  34. ->count();
  35. $list = $this->model
  36. ->where($where)
  37. ->order($sort, $order)
  38. ->limit($offset, $limit)
  39. ->select();
  40. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  41. foreach ($list as $k => &$v)
  42. {
  43. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  44. }
  45. unset($v);
  46. $result = array("total" => $total, "rows" => $list);
  47. return json($result);
  48. }
  49. return $this->view->fetch();
  50. }
  51. /**
  52. * 选择附件
  53. */
  54. public function select()
  55. {
  56. if ($this->request->isAjax())
  57. {
  58. return $this->index();
  59. }
  60. return $this->view->fetch();
  61. }
  62. /**
  63. * 添加
  64. */
  65. public function add()
  66. {
  67. if ($this->request->isAjax())
  68. {
  69. $this->error();
  70. }
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 删除附件
  75. * @param array $ids
  76. */
  77. public function del($ids = "")
  78. {
  79. if ($ids)
  80. {
  81. \think\Hook::add('upload_delete', function($params) {
  82. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  83. if (is_file($attachmentFile))
  84. {
  85. @unlink($attachmentFile);
  86. }
  87. });
  88. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  89. foreach ($attachmentlist as $attachment)
  90. {
  91. \think\Hook::listen("upload_delete", $attachment);
  92. $attachment->delete();
  93. }
  94. $this->success();
  95. }
  96. $this->error(__('Parameter %s can not be empty', 'ids'));
  97. }
  98. }