Attachment.php 3.7 KB

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