Attachment.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * ----------------------------------------------------------------------------
  4. * 行到水穷处,坐看云起时
  5. * 开发软件,找贵阳云起信息科技,官网地址:https://www.56q7.com/
  6. * ----------------------------------------------------------------------------
  7. * Author: 老成
  8. * email:85556713@qq.com
  9. */
  10. declare(strict_types=1);
  11. namespace app\admin\controller\general;
  12. use app\admin\traits\Actions;
  13. use app\common\controller\Backend;
  14. use app\common\model\Attachment as AttaModel;
  15. use think\annotation\route\Group;
  16. use think\annotation\route\Route;
  17. use think\facade\Cache;
  18. use think\facade\Db;
  19. use app\common\model\Config;
  20. /**
  21. * 附件管理
  22. */
  23. #[Group("general/attachment")]
  24. class Attachment extends Backend
  25. {
  26. protected $noNeedRight=['select'];
  27. use Actions{
  28. add as private _add;
  29. del as private _del;
  30. }
  31. public function _initialize()
  32. {
  33. parent::_initialize();
  34. $this->model=new AttaModel();
  35. $this->assign('categoryList',AttaModel::getCategory());
  36. $this->assign('disksList',AttaModel::getDisksType());
  37. }
  38. #[Route("GET","select")]
  39. public function select()
  40. {
  41. if($this->request->isAjax()){
  42. $limit=[
  43. 'page' => $this->request->get('page/d',1),
  44. 'list_rows' => 17
  45. ];
  46. $list = $this->model->where(function($query){
  47. $category=$this->request->param('category');
  48. $keywords=$this->request->param('keywords');
  49. $query->where('is_image',1);
  50. if($category && $category=='unclassed'){
  51. $query->where('category="" or category is null');
  52. }
  53. if($category && $category!='all' && $category!='unclassed'){
  54. $query->where('category',$category);
  55. }
  56. if($keywords){
  57. $query->where('filename','like',"%{$keywords}%");
  58. }
  59. })->order('weigh desc,id desc')->paginate($limit);
  60. $result = ['total' => $list->total(), 'rows' => $list->items()];
  61. $this->success('',$result);
  62. }
  63. $this->assign('limit', $this->request->get('limit',5));
  64. return $this->fetch();
  65. }
  66. #[Route("POST,GET","add")]
  67. public function add()
  68. {
  69. if($this->request->isPost()){
  70. $this->success();
  71. }
  72. return $this->_add();
  73. }
  74. #[Route("POST","setcate")]
  75. public function setcate()
  76. {
  77. $type=$this->request->post('type');
  78. $key=$this->request->post('key');
  79. $value=$this->request->post('value');
  80. $cateconfig=Config::where(['group'=>'dictionary','name'=>'filegroup'])->find();
  81. $arr=$cateconfig->value;
  82. if($type=='add' && isset($arr[$key])){
  83. $this->error('分类已存在');
  84. }
  85. $message='';
  86. if($type=='add') {
  87. $arr[$key] = $value;
  88. $message = '添加成功';
  89. }
  90. if($type=='edit') {
  91. $arr[$key] = $value;
  92. $message = '修改成功';
  93. }
  94. if($type=='del') {
  95. unset($arr[$key]);
  96. AttaModel::where('category',$key)->update(['category'=>'']);
  97. $message = '删除成功';
  98. }
  99. $cateconfig->value=json_encode($arr,JSON_UNESCAPED_UNICODE);
  100. $cateconfig->save();
  101. Cache::delete('site_config_dictionary');
  102. $this->success($message,$arr);
  103. }
  104. #[Route("POST,GET","del")]
  105. public function del()
  106. {
  107. $ids = $this->request->param("ids");
  108. $list = $this->model->where('id', 'in', $ids)->select();
  109. $count = 0;
  110. Db::startTrans();
  111. try {
  112. foreach ($list as $item) {
  113. $classname=config('filesystem.disks')[$item->storage]['class'];
  114. $classname::deleteFile($item);
  115. $count += $item->delete();
  116. }
  117. Db::commit();
  118. } catch (\Exception $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. }
  122. if ($count) {
  123. $this->success();
  124. }
  125. $this->error(__('没有记录被删除'));
  126. }
  127. }