FileLogic.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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\tenantapi\logic;
  15. use app\common\logic\BaseLogic;
  16. use app\common\model\file\TenantFile;
  17. use app\common\model\file\TenantFileCate;
  18. use app\common\service\ConfigService;
  19. use app\common\service\storage\Driver as StorageDriver;
  20. /**
  21. * 文件逻辑层
  22. * Class FileLogic
  23. * @package app\tenantapi\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 TenantFile())->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 TenantFile())->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 = TenantFile::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. TenantFile::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. TenantFileCate::create([
  82. 'tenant_id'=> $params['tenant_id'],
  83. 'type' => $params['type'],
  84. 'pid' => $params['pid'],
  85. 'name' => $params['name']
  86. ]);
  87. }
  88. /**
  89. * @notes 编辑文件分类
  90. * @param $params
  91. * @author 张无忌
  92. * @date 2021/7/28 14:03
  93. */
  94. public static function editCate($params)
  95. {
  96. TenantFileCate::update([
  97. 'name' => $params['name'],
  98. 'update_time' => time()
  99. ], ['id' => $params['id']]);
  100. }
  101. /**
  102. * @notes 删除文件分类
  103. * @param $params
  104. * @author 张无忌
  105. * @date 2021/7/28 14:21
  106. */
  107. public static function delCate($params)
  108. {
  109. $fileModel = new TenantFile();
  110. $cateModel = new TenantFileCate();
  111. $cateIds = self::getCateIds($params['id']);
  112. array_push($cateIds, $params['id']);
  113. // 删除分类及子分类
  114. $cateModel->whereIn('id', $cateIds)->update(['delete_time' => time()]);
  115. // 删除文件
  116. $fileIds = $fileModel->whereIn('cid', $cateIds)->column('id');
  117. if (!empty($fileIds)) {
  118. self::delete(['ids' => $fileIds]);
  119. }
  120. }
  121. /**
  122. * @notes 获取所有分类id
  123. * @param $parentId
  124. * @param array $cateArr
  125. * @return array
  126. * @author 段誉
  127. * @date 2024/2/7 15:03
  128. */
  129. public static function getCateIds($parentId, array $cateArr = []): array
  130. {
  131. $childIds = TenantFileCate::where(['pid' => $parentId])->column('id');
  132. if (empty($childIds)) {
  133. return $childIds;
  134. } else {
  135. $allChildIds = $childIds;
  136. foreach ($childIds as $childId) {
  137. $allChildIds = array_merge($allChildIds, static::getCateIds($childId, $cateArr));
  138. }
  139. return $allChildIds;
  140. }
  141. }
  142. }