Attachment.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. protected $searchFields = 'id,filename,url';
  17. protected $noNeedRight = ['classify'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('Attachment');
  22. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  23. $this->view->assign("categoryList", \app\common\model\Attachment::getCategoryList());
  24. $this->assignconfig("categoryList", \app\common\model\Attachment::getCategoryList());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags', 'trim']);
  33. if ($this->request->isAjax()) {
  34. $mimetypeQuery = [];
  35. $filter = $this->request->request('filter');
  36. $filterArr = (array)json_decode($filter, true);
  37. if (isset($filterArr['category']) && $filterArr['category'] == 'unclassed') {
  38. $filterArr['category'] = ',unclassed';
  39. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['category' => '']))]);
  40. }
  41. if (isset($filterArr['mimetype']) && preg_match("/(\/|\,|\*)/", $filterArr['mimetype'])) {
  42. $mimetype = $filterArr['mimetype'];
  43. $filterArr = array_diff_key($filterArr, ['mimetype' => '']);
  44. $mimetypeQuery = function ($query) use ($mimetype) {
  45. $mimetypeArr = array_filter(explode(',', $mimetype));
  46. foreach ($mimetypeArr as $index => $item) {
  47. $query->whereOr('mimetype', 'like', '%' . str_replace("/*", "/", $item) . '%');
  48. }
  49. };
  50. }
  51. $this->request->get(['filter' => json_encode($filterArr)]);
  52. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53. $list = $this->model
  54. ->where($mimetypeQuery)
  55. ->where($where)
  56. ->order($sort, $order)
  57. ->paginate($limit);
  58. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  59. foreach ($list as $k => &$v) {
  60. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  61. }
  62. unset($v);
  63. $result = array("total" => $list->total(), "rows" => $list->items());
  64. return json($result);
  65. }
  66. return $this->view->fetch();
  67. }
  68. /**
  69. * 选择附件
  70. */
  71. public function select()
  72. {
  73. if ($this->request->isAjax()) {
  74. return $this->index();
  75. }
  76. $mimetype = $this->request->get('mimetype', '');
  77. $mimetype = substr($mimetype, -1) === '/' ? $mimetype . '*' : $mimetype;
  78. $this->view->assign('mimetype', $mimetype);
  79. return $this->view->fetch();
  80. }
  81. /**
  82. * 添加
  83. */
  84. public function add()
  85. {
  86. if ($this->request->isAjax()) {
  87. $this->error();
  88. }
  89. return $this->view->fetch();
  90. }
  91. /**
  92. * 删除附件
  93. * @param array $ids
  94. */
  95. public function del($ids = "")
  96. {
  97. if (!$this->request->isPost()) {
  98. $this->error(__("Invalid parameters"));
  99. }
  100. $ids = $ids ? $ids : $this->request->post("ids");
  101. if ($ids) {
  102. \think\Hook::add('upload_delete', function ($params) {
  103. if ($params['storage'] == 'local') {
  104. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  105. if (is_file($attachmentFile)) {
  106. @unlink($attachmentFile);
  107. }
  108. }
  109. });
  110. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  111. foreach ($attachmentlist as $attachment) {
  112. \think\Hook::listen("upload_delete", $attachment);
  113. $attachment->delete();
  114. }
  115. $this->success();
  116. }
  117. $this->error(__('Parameter %s can not be empty', 'ids'));
  118. }
  119. /**
  120. * 归类
  121. */
  122. public function classify()
  123. {
  124. if (!$this->auth->check('general/attachment/edit')) {
  125. \think\Hook::listen('admin_nopermission', $this);
  126. $this->error(__('You have no permission'), '');
  127. }
  128. if (!$this->request->isPost()) {
  129. $this->error(__("Invalid parameters"));
  130. }
  131. $category = $this->request->post('category', '');
  132. $ids = $this->request->post('ids');
  133. if (!$ids) {
  134. $this->error(__('Parameter %s can not be empty', 'ids'));
  135. }
  136. $categoryList = \app\common\model\Attachment::getCategoryList();
  137. if ($category && !isset($categoryList[$category])) {
  138. $this->error(__('Category not found'));
  139. }
  140. $category = $category == 'unclassed' ? '' : $category;
  141. \app\common\model\Attachment::where('id', 'in', $ids)->update(['category' => $category]);
  142. $this->success();
  143. }
  144. }