FileLogic.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic;
  15. use app\common\logic\BaseLogic;
  16. use app\common\model\file\File;
  17. use app\common\model\file\FileCate;
  18. use app\common\service\ConfigService;
  19. use app\common\service\storage\Driver as StorageDriver;
  20. /**
  21. * 文件逻辑层
  22. * Class FileLogic
  23. * @package app\adminapi\logic
  24. */
  25. class FileLogic extends BaseLogic
  26. {
  27. /**
  28. * @notes 移动文件
  29. * @param $params
  30. * @author 张无忌
  31. * @date 2021/7/28 15:29
  32. */
  33. public static function move($params)
  34. {
  35. (new File())->whereIn('id', $params['ids'])
  36. ->update([
  37. 'cid' => $params['cid'],
  38. 'update_time' => time()
  39. ]);
  40. }
  41. /**
  42. * @notes 重命名文件
  43. * @param $params
  44. * @author 张无忌
  45. * @date 2021/7/29 17:16
  46. */
  47. public static function rename($params)
  48. {
  49. (new File())->where('id', $params['id'])
  50. ->update([
  51. 'name' => $params['name'],
  52. 'update_time' => time()
  53. ]);
  54. }
  55. /**
  56. * @notes 批量删除文件
  57. * @param $params
  58. * @author 张无忌
  59. * @date 2021/7/28 15:41
  60. */
  61. public static function delete($params)
  62. {
  63. $result = File::whereIn('id', $params['ids'])->select();
  64. $StorageDriver = new StorageDriver([
  65. 'default' => ConfigService::get('storage', 'default', 'local'),
  66. 'engine' => ConfigService::get('storage') ?? ['local'=>[]],
  67. ]);
  68. foreach ($result as $item) {
  69. $StorageDriver->delete($item['uri']);
  70. }
  71. File::destroy($params['ids']);
  72. }
  73. /**
  74. * @notes 添加文件分类
  75. * @param $params
  76. * @author 张无忌
  77. * @date 2021/7/28 11:32
  78. */
  79. public static function addCate($params)
  80. {
  81. FileCate::create([
  82. 'type' => $params['type'],
  83. 'pid' => $params['pid'],
  84. 'name' => $params['name']
  85. ]);
  86. }
  87. /**
  88. * @notes 编辑文件分类
  89. * @param $params
  90. * @author 张无忌
  91. * @date 2021/7/28 14:03
  92. */
  93. public static function editCate($params)
  94. {
  95. FileCate::update([
  96. 'name' => $params['name'],
  97. 'update_time' => time()
  98. ], ['id' => $params['id']]);
  99. }
  100. /**
  101. * @notes 删除文件分类
  102. * @param $params
  103. * @author 张无忌
  104. * @date 2021/7/28 14:21
  105. */
  106. public static function delCate($params)
  107. {
  108. $fileModel = new File();
  109. $cateModel = new FileCate();
  110. $cateIds = self::getCateIds($params['id']);
  111. array_push($cateIds, $params['id']);
  112. // 删除分类及子分类
  113. $cateModel->whereIn('id', $cateIds)->update(['delete_time' => time()]);
  114. // 删除文件
  115. $fileIds = $fileModel->whereIn('cid', $cateIds)->column('id');
  116. if (!empty($fileIds)) {
  117. self::delete(['ids' => $fileIds]);
  118. }
  119. }
  120. /**
  121. * @notes 获取所有分类id
  122. * @param $parentId
  123. * @param array $cateArr
  124. * @return array
  125. * @author 段誉
  126. * @date 2024/2/7 15:03
  127. */
  128. public static function getCateIds($parentId, array $cateArr = []): array
  129. {
  130. $childIds = FileCate::where(['pid' => $parentId])->column('id');
  131. if (empty($childIds)) {
  132. return $childIds;
  133. } else {
  134. $allChildIds = $childIds;
  135. foreach ($childIds as $childId) {
  136. $allChildIds = array_merge($allChildIds, static::getCateIds($childId, $cateArr));
  137. }
  138. return $allChildIds;
  139. }
  140. }
  141. }