Attachment.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. $mimetypeQuery = [];
  30. $filter = $this->request->request('filter');
  31. $filterArr = (array)json_decode($filter, TRUE);
  32. if (isset($filterArr['mimetype']) && stripos($filterArr['mimetype'], ',') !== false) {
  33. $this->request->get(['filter' => json_encode(array_merge($filterArr, ['mimetype' => '']))]);
  34. $mimetypeQuery = function ($query) use ($filterArr) {
  35. $mimetypeArr = explode(',', $filterArr['mimetype']);
  36. foreach ($mimetypeArr as $index => $item) {
  37. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  38. }
  39. };
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $total = $this->model
  43. ->where($mimetypeQuery)
  44. ->where($where)
  45. ->order($sort, $order)
  46. ->count();
  47. $list = $this->model
  48. ->where($mimetypeQuery)
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->limit($offset, $limit)
  52. ->select();
  53. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  54. foreach ($list as $k => &$v) {
  55. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  56. }
  57. unset($v);
  58. $result = array("total" => $total, "rows" => $list);
  59. return json($result);
  60. }
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 选择附件
  65. */
  66. public function select()
  67. {
  68. if ($this->request->isAjax()) {
  69. return $this->index();
  70. }
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 添加
  75. */
  76. public function add()
  77. {
  78. if ($this->request->isAjax()) {
  79. $this->error();
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 删除附件
  85. * @param array $ids
  86. */
  87. public function del($ids = "")
  88. {
  89. if ($ids) {
  90. \think\Hook::add('upload_delete', function ($params) {
  91. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  92. if (is_file($attachmentFile)) {
  93. @unlink($attachmentFile);
  94. }
  95. });
  96. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  97. foreach ($attachmentlist as $attachment) {
  98. \think\Hook::listen("upload_delete", $attachment);
  99. $attachment->delete();
  100. }
  101. $this->success();
  102. }
  103. $this->error(__('Parameter %s can not be empty', 'ids'));
  104. }
  105. }