Attachment.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. protected $model = null;
  13. public function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->model = model('Attachment');
  17. }
  18. /**
  19. * 查看
  20. */
  21. public function index()
  22. {
  23. //设置过滤方法
  24. $this->request->filter(['strip_tags']);
  25. if ($this->request->isAjax())
  26. {
  27. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  28. $total = $this->model
  29. ->where($where)
  30. ->order($sort, $order)
  31. ->count();
  32. $list = $this->model
  33. ->where($where)
  34. ->order($sort, $order)
  35. ->limit($offset, $limit)
  36. ->select();
  37. $cdnurl = preg_replace("/\/(\w+)\.php$/i", '', $this->request->root());
  38. foreach ($list as $k => &$v)
  39. {
  40. $v['fullurl'] = ($v['storage'] == 'local' ? $cdnurl : $this->view->config['upload']['cdnurl']) . $v['url'];
  41. }
  42. unset($v);
  43. $result = array("total" => $total, "rows" => $list);
  44. return json($result);
  45. }
  46. return $this->view->fetch();
  47. }
  48. /**
  49. * 选择附件
  50. */
  51. public function select()
  52. {
  53. if ($this->request->isAjax())
  54. {
  55. return $this->index();
  56. }
  57. return $this->view->fetch();
  58. }
  59. /**
  60. * 添加
  61. */
  62. public function add()
  63. {
  64. if ($this->request->isAjax())
  65. {
  66. $this->error();
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 删除附件
  72. * @param array $ids
  73. */
  74. public function del($ids = "")
  75. {
  76. if ($ids)
  77. {
  78. \think\Hook::add('upload_delete', function($params) {
  79. $attachmentFile = ROOT_PATH . '/public' . $params['url'];
  80. if (is_file($attachmentFile))
  81. {
  82. @unlink($attachmentFile);
  83. }
  84. });
  85. $attachmentlist = $this->model->where('id', 'in', $ids)->select();
  86. foreach ($attachmentlist as $attachment)
  87. {
  88. \think\Hook::listen("upload_delete", $attachment);
  89. $attachment->delete();
  90. }
  91. $this->success();
  92. }
  93. $this->error(__('Parameter %s can not be empty', 'ids'));
  94. }
  95. }