Attachment.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 = explode(',', $mimetype);
  46. foreach ($mimetypeArr as $index => $item) {
  47. if (stripos($item, "/*") !== false) {
  48. $query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
  49. } else {
  50. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  51. }
  52. }
  53. };
  54. }
  55. $this->request->get(['filter' => json_encode($filterArr)]);
  56. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  57. $where1 = is_sys_admin();
  58. $list = $this->model
  59. ->where($mimetypeQuery)->where($where1)
  60. ->where($where)
  61. ->order($sort, $order)
  62. ->paginate($limit);
  63. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  64. foreach ($list as $k => &$v) {
  65. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  66. }
  67. unset($v);
  68. $result = array("total" => $list->total(), "rows" => $list->items());
  69. return json($result);
  70. }
  71. return $this->view->fetch();
  72. }
  73. /**
  74. * 选择附件
  75. */
  76. public function select()
  77. {
  78. if ($this->request->isAjax()) {
  79. return $this->index();
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 添加
  85. */
  86. public function add()
  87. {
  88. if ($this->request->isAjax()) {
  89. $this->error();
  90. }
  91. return $this->view->fetch();
  92. }
  93. /**
  94. * 删除附件
  95. * @param array $ids
  96. */
  97. public function del($ids = "")
  98. {
  99. if (!$this->request->isPost()) {
  100. $this->error(__("Invalid parameters"));
  101. }
  102. $ids = $ids ? $ids : $this->request->post("ids");
  103. if ($ids) {
  104. \think\Hook::add('upload_delete', function ($params) {
  105. if ($params['storage'] == 'local') {
  106. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  107. if (is_file($attachmentFile)) {
  108. @unlink($attachmentFile);
  109. }
  110. }
  111. });
  112. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  113. foreach ($attachmentlist as $attachment) {
  114. \think\Hook::listen("upload_delete", $attachment);
  115. $attachment->delete();
  116. }
  117. $this->success();
  118. }
  119. $this->error(__('Parameter %s can not be empty', 'ids'));
  120. }
  121. /**
  122. * 归类
  123. */
  124. public function classify()
  125. {
  126. if (!$this->auth->check('general/attachment/edit')) {
  127. \think\Hook::listen('admin_nopermission', $this);
  128. $this->error(__('You have no permission'), '');
  129. }
  130. if (!$this->request->isPost()) {
  131. $this->error(__("Invalid parameters"));
  132. }
  133. $category = $this->request->post('category', '');
  134. $ids = $this->request->post('ids');
  135. if (!$ids) {
  136. $this->error(__('Parameter %s can not be empty', 'ids'));
  137. }
  138. $categoryList = \app\common\model\Attachment::getCategoryList();
  139. if ($category && !isset($categoryList[$category])) {
  140. $this->error(__('Category not found'));
  141. }
  142. $category = $category == 'unclassed' ? '' : $category;
  143. \app\common\model\Attachment::where('id', 'in', $ids)->update(['category' => $category]);
  144. $this->success();
  145. }
  146. }