Attachment.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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']);
  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. $total = $this->model
  48. ->where($mimetypeQuery)
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->count();
  52. $list = $this->model
  53. ->where($mimetypeQuery)
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  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" => $total, "rows" => $list);
  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 ($ids) {
  95. \think\Hook::add('upload_delete', function ($params) {
  96. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  97. if (is_file($attachmentFile)) {
  98. @unlink($attachmentFile);
  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. }