UploadFile.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2025 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model;
  13. use app\store\model\Setting as SettingModel;
  14. use app\common\model\UploadFile as UploadFileModel;
  15. use app\common\library\storage\Driver as StorageDriver;
  16. use app\common\enum\Setting as SettingEnum;
  17. /**
  18. * 文件库模型
  19. * Class UploadFile
  20. * @package app\store\model
  21. */
  22. class UploadFile extends UploadFileModel
  23. {
  24. /**
  25. * 获取列表记录
  26. * @param array $param
  27. * @return \think\Paginator
  28. * @throws \think\db\exception\DbException
  29. */
  30. public function getList(array $param = []): \think\Paginator
  31. {
  32. // 商品列表获取条件
  33. $params = $this->setQueryDefaultValue($param, [
  34. 'fileType' => -1, // 文件类型(-1全部 10图片 20附件 30视频)
  35. 'groupId' => -1, // 分组ID(-1全部 0未分组)
  36. 'fileName' => '', // 文件名称
  37. 'storage' => '', // 存储方式(StorageEnum)
  38. 'channel' => -1, // 上传来源(-1全部 10商户后台 20用户端)
  39. 'isRecycle' => false // 是否在回收站
  40. ]);
  41. // 查询对象
  42. $query = $this->getNewQuery();
  43. // 文件分组
  44. $params['groupId'] > -1 && $query->where('group_id', '=', (int)$params['groupId']);
  45. // 文件类型
  46. $params['fileType'] > -1 && $query->where('file_type', '=', (int)$params['fileType']);
  47. // 存储方式
  48. !empty($params['storage']) && $query->where('storage', '=', $params['storage']);
  49. // 上传来源
  50. $params['channel'] > -1 && $query->where('channel', '=', (int)$params['channel']);
  51. // 文件名称
  52. !empty($params['fileName']) && $query->where('file_name', 'like', "%{$params['fileName']}%");
  53. // 是否在回收站
  54. $query->where('is_recycle', '=', (int)$params['isRecycle']);
  55. // 查询列表数据
  56. return $query->where('is_delete', '=', 0)
  57. ->order(['file_id' => 'desc'])
  58. ->paginate(15);
  59. }
  60. /**
  61. * 移入|移出回收站
  62. * @param bool $isRecycle
  63. * @return bool|false
  64. */
  65. public function setRecycle(bool $isRecycle = true): bool
  66. {
  67. return $this->save(['is_recycle' => (int)$isRecycle]);
  68. }
  69. /**
  70. * 删除文件(批量)
  71. * @param array $fileIds 文件ID集
  72. * @return bool
  73. * @throws \think\Exception
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public function setDelete(array $fileIds): bool
  79. {
  80. // 验证文件数量
  81. if (\count($fileIds) > 15) {
  82. $this->error = '一次性最多删除15个文件';
  83. return false;
  84. }
  85. // 存储配置信息
  86. $config = SettingModel::getItem(SettingEnum::STORAGE);
  87. foreach ($fileIds as $fileId) {
  88. // 获取文件详情
  89. $fileInfo = static::detail((int)$fileId);
  90. if ($fileInfo['storage'] != 'external') {
  91. // 实例化存储驱动
  92. $storage = new StorageDriver($config, $fileInfo['storage']);
  93. // 删除文件
  94. if (!$storage->delete($fileInfo['file_path'])) {
  95. $this->error = '文件删除失败:' . $storage->getError();
  96. return false;
  97. }
  98. }
  99. // 标记为已删除
  100. $fileInfo->save(['is_delete' => 1]);
  101. }
  102. return true;
  103. }
  104. /**
  105. * 批量移动文件分组
  106. * @param int $groupId
  107. * @param array $fileIds
  108. * @return bool
  109. */
  110. public function moveGroup(int $groupId, array $fileIds): bool
  111. {
  112. return static::updateBase(['group_id' => $groupId], [['file_id', 'in', $fileIds]]);
  113. }
  114. /**
  115. * 添加文件库记录
  116. * @param array $data
  117. * @param int $fileType
  118. * @param int $groupId
  119. * @param int|null $storeId
  120. * @return bool|false
  121. */
  122. public function add(array $data, int $fileType, int $groupId = 0, int $storeId = null): bool
  123. {
  124. return $this->save([
  125. 'group_id' => max($groupId, 0),
  126. 'channel' => 10,
  127. 'storage' => $data['storage'],
  128. 'domain' => $data['domain'],
  129. 'file_name' => $data['file_name'],
  130. 'file_path' => $data['file_path'],
  131. 'file_size' => $data['file_size'],
  132. 'file_ext' => $data['file_ext'],
  133. 'file_type' => $fileType,
  134. 'store_id' => $storeId ?: self::$storeId
  135. ]);
  136. }
  137. /**
  138. * 编辑记录
  139. * @param array $data
  140. * @return bool
  141. */
  142. public function edit(array $data): bool
  143. {
  144. return $this->allowField(['file_name', 'group_id'])->save($data) !== false;
  145. }
  146. }