Files.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2021 勾股工作室
  4. * @license https://opensource.org/licenses/GPL-3.0
  5. * @link https://www.gougucms.com
  6. */
  7. declare (strict_types = 1);
  8. namespace app\admin\controller;
  9. use app\admin\BaseController;
  10. use app\admin\model\File;
  11. use think\exception\ValidateException;
  12. use think\facade\Db;
  13. use think\facade\View;
  14. class Files extends BaseController
  15. {
  16. public function index()
  17. {
  18. if (request()->isAjax()) {
  19. $param = get_params();
  20. $where = array();
  21. $fileext = ['','jpg,jpeg,png,gif','mp4','doc,docx,xls,xlsx,ppt,pptx,txt,pdf','zip,rar,7z'];
  22. if (!empty($param['keywords'])) {
  23. $where[] = ['f.name|g.title', 'like', '%' . $param['keywords'] . '%'];
  24. }
  25. if (isset($param['group_id']) && $param['group_id']!='') {
  26. $where[] = ['f.group_id','=',$param['group_id']];
  27. }
  28. if (!empty($param['tab'])) {
  29. $where[] = ['f.fileext','in',$fileext[$param['tab']]];
  30. }
  31. $where[] = ['f.delete_time','=',0];
  32. $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
  33. $list = DB::name('File')
  34. ->field("f.*,a.username,g.title as group_title")
  35. ->alias('f')
  36. ->join('Admin a', 'f.admin_id = a.id','left')
  37. ->join('FileGroup g', 'f.group_id = g.id','left')
  38. ->order('f.create_time desc')
  39. ->where($where)
  40. ->paginate($rows, false, ['query' => $param])
  41. ->each(function($item, $key){
  42. $item['create_time'] = date('Y-m-d H:i:s', $item['create_time']);
  43. return $item;
  44. });
  45. return table_assign(0, '', $list);
  46. } else {
  47. return view();
  48. }
  49. }
  50. //编辑
  51. public function edit()
  52. {
  53. if (request()->isAjax()) {
  54. $param = get_params();
  55. if (Db::name('File')->where('id',$param['id'])->update(['name'=>$param['title']]) !== false) {
  56. add_log('edit', $param['id'], []);
  57. return to_assign(0, "操作成功");
  58. } else {
  59. return to_assign(1, "操作失败");
  60. }
  61. }
  62. }
  63. //移动
  64. public function move()
  65. {
  66. if (request()->isAjax()) {
  67. $group_id = get_params("group_id");
  68. $ids = get_params("ids");
  69. $idArray = explode(',', strval($ids));
  70. $list = [];
  71. foreach ($idArray as $key => $val) {
  72. $list[] = [
  73. 'id' => $val,
  74. 'group_id' => $group_id,
  75. 'update_time' => time()
  76. ];
  77. }
  78. $res = (new File())->saveAll($list);
  79. if ($res!== false) {
  80. return to_assign(0, "操作成功");
  81. } else {
  82. return to_assign(1, "操作失败");
  83. }
  84. }
  85. }
  86. //删除
  87. public function delete()
  88. {
  89. if (request()->isDelete()) {
  90. $ids = get_params("ids");
  91. $idArray = explode(',', strval($ids));
  92. $list = [];
  93. foreach ($idArray as $key => $val) {
  94. $list[] = [
  95. 'id' => $val,
  96. 'delete_time' => time()
  97. ];
  98. }
  99. $res = (new File())->saveAll($list);
  100. if ($res!== false) {
  101. return to_assign(0, "删除成功");
  102. } else {
  103. return to_assign(1, "删除失败");
  104. }
  105. } else {
  106. return to_assign(1, "错误的请求");
  107. }
  108. }
  109. //获取分组
  110. public function get_group()
  111. {
  112. $list = Db::name('FileGroup')->where([['delete_time','=',0]])->select()->toArray();
  113. return to_assign(0, '',$list);
  114. }
  115. //添加&编辑
  116. public function add_group()
  117. {
  118. if (request()->isAjax()) {
  119. $param = get_params();
  120. if($param['title'] == '全部' || $param['title']=='未分组'){
  121. return to_assign(1, '该分组名称已经存在');
  122. }
  123. if (!empty($param['id']) && $param['id'] > 0) {
  124. $count = Db::name('FileGroup')->where([['id','<>',$param['id']],['delete_time','=',0],['title','=',$param['title']]])->count();
  125. if ($count > 0) {
  126. return to_assign(1, '该分组名称已经存在');
  127. }
  128. $res = Db::name('FileGroup')->where(['id' => $param['id']])->strict(false)->field(true)->update($param);
  129. if($res!=false){
  130. add_log('edit', $param['id'], $param);
  131. return to_assign(0,'编辑成功',$param['id']);
  132. }else{
  133. return to_assign(1,'操作失败');
  134. }
  135. } else {
  136. $count = Db::name('FileGroup')->where([['delete_time','=',0],['title','=',$param['title']]])->count();
  137. if ($count > 0) {
  138. return to_assign(1, '该分组名称已经存在');
  139. }
  140. $gid = Db::name('FileGroup')->strict(false)->field(true)->insertGetId($param);
  141. if($gid!=false){
  142. add_log('add', $gid, $param);
  143. return to_assign(0,'添加成功',$gid);
  144. }else{
  145. return to_assign(1,'操作失败');
  146. }
  147. }
  148. }
  149. }
  150. //删除
  151. public function del_group()
  152. {
  153. if (request()->isDelete()) {
  154. $id = get_params("id");
  155. $count = Db::name('File')->where(["group_id" => $id])->count();
  156. if ($count > 0) {
  157. return to_assign(1, "该分组还存在文件,请去除文件或者转移文件后再删除");
  158. }
  159. if (Db::name('FileGroup')->delete($id) !== false) {
  160. add_log('delete', $id, []);
  161. return to_assign(0, "删除成功");
  162. } else {
  163. return to_assign(1, "删除失败");
  164. }
  165. } else {
  166. return to_assign(1, "错误的请求");
  167. }
  168. }
  169. }