Attachment.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. return $this->view->fetch();
  77. }
  78. /**
  79. * 添加
  80. */
  81. public function add()
  82. {
  83. if ($this->request->isAjax()) {
  84. $this->error();
  85. }
  86. return $this->view->fetch();
  87. }
  88. /**
  89. * 删除附件
  90. * @param array $ids
  91. */
  92. public function del($ids = "")
  93. {
  94. if (!$this->request->isPost()) {
  95. $this->error(__("Invalid parameters"));
  96. }
  97. $ids = $ids ? $ids : $this->request->post("ids");
  98. if ($ids) {
  99. \think\Hook::add('upload_delete', function ($params) {
  100. if ($params['storage'] == 'local') {
  101. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  102. if (is_file($attachmentFile)) {
  103. @unlink($attachmentFile);
  104. }
  105. }
  106. });
  107. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  108. foreach ($attachmentlist as $attachment) {
  109. \think\Hook::listen("upload_delete", $attachment);
  110. $attachment->delete();
  111. }
  112. $this->success();
  113. }
  114. $this->error(__('Parameter %s can not be empty', 'ids'));
  115. }
  116. /**
  117. * 归类
  118. */
  119. public function classify()
  120. {
  121. if (!$this->auth->check('general/attachment/edit')) {
  122. \think\Hook::listen('admin_nopermission', $this);
  123. $this->error(__('You have no permission'), '');
  124. }
  125. if (!$this->request->isPost()) {
  126. $this->error(__("Invalid parameters"));
  127. }
  128. $category = $this->request->post('category', '');
  129. $ids = $this->request->post('ids');
  130. if (!$ids) {
  131. $this->error(__('Parameter %s can not be empty', 'ids'));
  132. }
  133. $categoryList = \app\common\model\Attachment::getCategoryList();
  134. if ($category && !isset($categoryList[$category])) {
  135. $this->error(__('Category not found'));
  136. }
  137. $category = $category == 'unclassed' ? '' : $category;
  138. \app\common\model\Attachment::where('id', 'in', $ids)->update(['category' => $category]);
  139. $this->success();
  140. }
  141. }