Attachment.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\user\controller\general;
  3. use app\common\controller\Userend;
  4. /**
  5. * 附件管理
  6. *
  7. * @icon fa fa-circle-o
  8. * @remark 主要用于管理上传到又拍云的数据或上传至本服务的上传数据
  9. */
  10. class Attachment extends Userend
  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. }
  21. /**
  22. * 选择附件
  23. */
  24. public function select()
  25. {
  26. if ($this->request->isAjax()) {
  27. return $this->index();
  28. }
  29. return $this->view->fetch();
  30. }
  31. /**
  32. * 查看
  33. */
  34. public function index()
  35. {
  36. //设置过滤方法
  37. $this->request->filter(['strip_tags']);
  38. if ($this->request->isAjax()) {
  39. $mimetypeQuery = [];
  40. $filter = $this->request->request('filter');
  41. $filterArr = (array)json_decode($filter, TRUE);
  42. if (isset($filterArr['mimetype']) && stripos($filterArr['mimetype'], ',') !== false) {
  43. $this->request->get(['filter' => json_encode(array_merge($filterArr, ['mimetype' => '']))]);
  44. $mimetypeQuery = function ($query) use ($filterArr) {
  45. $mimetypeArr = explode(',', $filterArr['mimetype']);
  46. foreach ($mimetypeArr as $index => $item) {
  47. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  48. }
  49. };
  50. }
  51. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  52. $total = $this->model
  53. ->where($mimetypeQuery)
  54. ->where($where)
  55. ->where("user_id", $this->auth->id)
  56. ->order($sort, $order)
  57. ->count();
  58. $list = $this->model
  59. ->where($mimetypeQuery)
  60. ->where($where)
  61. ->where("user_id", $this->auth->id)
  62. ->order($sort, $order)
  63. ->limit($offset, $limit)
  64. ->select();
  65. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  66. foreach ($list as $k => &$v) {
  67. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  68. }
  69. unset($v);
  70. $result = array("total" => $total, "rows" => $list);
  71. return json($result);
  72. }
  73. return $this->view->fetch();
  74. }
  75. /**
  76. * 添加
  77. */
  78. public function add()
  79. {
  80. if ($this->request->isAjax()) {
  81. $this->error();
  82. }
  83. return $this->view->fetch();
  84. }
  85. /**
  86. * 删除附件
  87. * @param array $ids
  88. */
  89. public function del($ids = "")
  90. {
  91. if ($ids) {
  92. \think\Hook::add('upload_delete', function ($params) {
  93. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  94. if (is_file($attachmentFile)) {
  95. @unlink($attachmentFile);
  96. }
  97. });
  98. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  99. foreach ($attachmentlist as $attachment) {
  100. \think\Hook::listen("upload_delete", $attachment);
  101. $attachment->delete();
  102. }
  103. $this->success();
  104. }
  105. $this->error(__('Parameter %s can not be empty', 'ids'));
  106. }
  107. }