酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

162 lines
5.2 KiB

  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. protected $noNeedRight = ['classify'];
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = model('Attachment');
  22. $this->view->assign("mimetypeList", \app\common\model\Attachment::getMimetypeList());
  23. $this->view->assign("categoryList", \app\common\model\Attachment::getCategoryList());
  24. $this->assignconfig("categoryList", \app\common\model\Attachment::getCategoryList());
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags', 'trim']);
  33. if ($this->request->isAjax()) {
  34. $mimetypeQuery = [];
  35. $filter = $this->request->request('filter');
  36. $filterArr = (array)json_decode($filter, true);
  37. if (isset($filterArr['category']) && $filterArr['category'] == 'unclassed') {
  38. $filterArr['category'] = ',unclassed';
  39. $this->request->get(['filter' => json_encode(array_diff_key($filterArr, ['category' => '']))]);
  40. }
  41. if (isset($filterArr['mimetype']) && preg_match("/[]\,|\*]/", $filterArr['mimetype'])) {
  42. $mimetype = $filterArr['mimetype'];
  43. $filterArr = array_diff_key($filterArr, ['mimetype' => '']);
  44. $mimetypeQuery = function ($query) use ($mimetype) {
  45. $mimetypeArr = explode(',', $mimetype);
  46. foreach ($mimetypeArr as $index => $item) {
  47. if (stripos($item, "/*") !== false) {
  48. $query->whereOr('mimetype', 'like', str_replace("/*", "/", $item) . '%');
  49. } else {
  50. $query->whereOr('mimetype', 'like', '%' . $item . '%');
  51. }
  52. }
  53. };
  54. }
  55. $this->request->get(['filter' => json_encode($filterArr)]);
  56. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  57. $list = $this->model
  58. ->where($mimetypeQuery)
  59. ->where($where)
  60. ->order($sort, $order)
  61. ->paginate($limit);
  62. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  63. foreach ($list as $k => &$v) {
  64. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  65. }
  66. unset($v);
  67. $result = array("total" => $list->total(), "rows" => $list->items());
  68. return json($result);
  69. }
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 选择附件
  74. */
  75. public function select()
  76. {
  77. if ($this->request->isAjax()) {
  78. return $this->index();
  79. }
  80. return $this->view->fetch();
  81. }
  82. /**
  83. * 添加
  84. */
  85. public function add()
  86. {
  87. if ($this->request->isAjax()) {
  88. $this->error();
  89. }
  90. return $this->view->fetch();
  91. }
  92. /**
  93. * 删除附件
  94. * @param array $ids
  95. */
  96. public function del($ids = "")
  97. {
  98. if (!$this->request->isPost()) {
  99. $this->error(__("Invalid parameters"));
  100. }
  101. $ids = $ids ? $ids : $this->request->post("ids");
  102. if ($ids) {
  103. \think\Hook::add('upload_delete', function ($params) {
  104. if ($params['storage'] == 'local') {
  105. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  106. if (is_file($attachmentFile)) {
  107. @unlink($attachmentFile);
  108. }
  109. }
  110. });
  111. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  112. foreach ($attachmentlist as $attachment) {
  113. \think\Hook::listen("upload_delete", $attachment);
  114. $attachment->delete();
  115. }
  116. $this->success();
  117. }
  118. $this->error(__('Parameter %s can not be empty', 'ids'));
  119. }
  120. /**
  121. * 归类
  122. */
  123. public function classify()
  124. {
  125. if (!$this->auth->check('general/attachment/edit')) {
  126. \think\Hook::listen('admin_nopermission', $this);
  127. $this->error(__('You have no permission'), '');
  128. }
  129. if (!$this->request->isPost()) {
  130. $this->error(__("Invalid parameters"));
  131. }
  132. $category = $this->request->post('category', '');
  133. $ids = $this->request->post('ids');
  134. if (!$ids) {
  135. $this->error(__('Parameter %s can not be empty', 'ids'));
  136. }
  137. $categoryList = \app\common\model\Attachment::getCategoryList();
  138. if ($category && !isset($categoryList[$category])) {
  139. $this->error(__('Category not found'));
  140. }
  141. $category = $category == 'unclassed' ? '' : $category;
  142. \app\common\model\Attachment::where('id', 'in', $ids)->update(['category' => $category]);
  143. $this->success();
  144. }
  145. }