Attachment.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = model('Attachment');
  21. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  22. $this->view->assign("categoryList", \app\common\model\Attachment::getCategoryList());
  23. $this->assignconfig("categoryList", \app\common\model\Attachment::getCategoryList());
  24. }
  25. /**
  26. * 查看
  27. */
  28. public function index()
  29. {
  30. //设置过滤方法
  31. $this->request->filter(['strip_tags', 'trim']);
  32. if ($this->request->isAjax()) {
  33. $mimetypeQuery = [];
  34. $filter = $this->request->request('filter');
  35. $filterArr = (array)json_decode($filter, true);
  36. if (isset($filterArr['category']) && $filterArr['category'] == 'unclassed') {
  37. $filterArr['category'] = '';
  38. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['category' => '']))]);
  39. }
  40. if (isset($filterArr['mimetype']) && preg_match("/[]\,|\*]/", $filterArr['mimetype'])) {
  41. $mimetype = $filterArr['mimetype'];
  42. $filterArr = array_diff_key($filterArr, ['mimetype' => '']);
  43. $mimetypeQuery = function ($query) use ($mimetype) {
  44. $mimetypeArr = explode(',', $mimetype);
  45. foreach ($mimetypeArr as $index => $item) {
  46. if (stripos($item, "/*") !== false) {
  47. $query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
  48. } else {
  49. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  50. }
  51. }
  52. };
  53. }
  54. $this->request->get(['filter' => json_encode($filterArr)]);
  55. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  56. $list = $this->model
  57. ->where($mimetypeQuery)
  58. ->where($where)
  59. ->order($sort, $order)
  60. ->paginate($limit);
  61. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  62. foreach ($list as $k => &$v) {
  63. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  64. }
  65. unset($v);
  66. $result = array("total" => $list->total(), "rows" => $list->items());
  67. return json($result);
  68. }
  69. return $this->view->fetch();
  70. }
  71. /**
  72. * 选择附件
  73. */
  74. public function select()
  75. {
  76. if ($this->request->isAjax()) {
  77. return $this->index();
  78. }
  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 move($ids = "")
  123. {
  124. if (!$this->request->isPost()) {
  125. $this->error(__("Invalid parameters"));
  126. }
  127. $category = $this->request->post('category', '');
  128. $ids = $this->request->post('ids');
  129. if (!$ids) {
  130. $this->error(__('Parameter %s can not be empty', 'ids'));
  131. }
  132. $categoryList = \app\common\model\Attachment::getCategoryList();
  133. if ($category && !isset($categoryList[$category])) {
  134. $this->error(__('Category not found'));
  135. }
  136. \app\common\model\Attachment::where('id', 'in', $ids)->update(['category' => $category]);
  137. $this->success();
  138. }
  139. }