Attachment.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. $map=[];
  47. if($this->auth->id>1){
  48. $map['admin_id']=$this->auth->id;
  49. }
  50. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  51. $total = $this->model
  52. ->where($mimetypeQuery)
  53. ->where($where)
  54. ->where($map)
  55. ->order($sort, $order)
  56. ->count();
  57. $list = $this->model
  58. ->where($mimetypeQuery)
  59. ->where($where)
  60. ->where($map)
  61. ->order($sort, $order)
  62. ->limit($offset, $limit)
  63. ->select();
  64. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  65. foreach ($list as $k => &$v) {
  66. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  67. }
  68. unset($v);
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. return $this->view->fetch();
  73. }
  74. /**
  75. * 选择附件
  76. */
  77. public function select()
  78. {
  79. if ($this->request->isAjax()) {
  80. return $this->index();
  81. }
  82. return $this->view->fetch();
  83. }
  84. /**
  85. * 添加
  86. */
  87. public function add()
  88. {
  89. if ($this->request->isAjax()) {
  90. $this->error();
  91. }
  92. return $this->view->fetch();
  93. }
  94. /**
  95. * 删除附件
  96. * @param array $ids
  97. */
  98. public function del($ids = "")
  99. {
  100. if ($ids) {
  101. \think\Hook::add('upload_delete', function ($params) {
  102. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  103. if (is_file($attachmentFile)) {
  104. @unlink($attachmentFile);
  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. }