UploadFile.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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('channel', '=', 10)
  57. ->where('is_delete', '=', 0)
  58. ->order(['file_id' => 'desc'])
  59. ->paginate(15);
  60. }
  61. /**
  62. * 移入|移出回收站
  63. * @param bool $isRecycle
  64. * @return bool|false
  65. */
  66. public function setRecycle(bool $isRecycle = true): bool
  67. {
  68. return $this->save(['is_recycle' => (int)$isRecycle]);
  69. }
  70. /**
  71. * 删除文件(批量)
  72. * @param array $fileIds 文件ID集
  73. * @return bool
  74. * @throws \think\Exception
  75. * @throws \think\db\exception\DataNotFoundException
  76. * @throws \think\db\exception\DbException
  77. * @throws \think\db\exception\ModelNotFoundException
  78. */
  79. public function setDelete(array $fileIds): bool
  80. {
  81. // 验证文件数量
  82. if (count($fileIds) > 15) {
  83. $this->error = '一次性最多删除15个文件';
  84. return false;
  85. }
  86. // 存储配置信息
  87. $config = SettingModel::getItem(SettingEnum::STORAGE);
  88. foreach ($fileIds as $fileId) {
  89. // 获取文件详情
  90. $fileInfo = static::detail($fileId);
  91. if ($fileInfo['storage'] != 'external') {
  92. // 实例化存储驱动
  93. $storage = new StorageDriver($config, $fileInfo['storage']);
  94. // 删除文件
  95. if (!$storage->delete($fileInfo['file_path'])) {
  96. $this->error = '文件删除失败:' . $storage->getError();
  97. return false;
  98. }
  99. }
  100. // 标记为已删除
  101. $fileInfo->save(['is_delete' => 1]);
  102. }
  103. return true;
  104. }
  105. /**
  106. * 批量移动文件分组
  107. * @param int $groupId
  108. * @param array $fileIds
  109. * @return bool
  110. */
  111. public function moveGroup(int $groupId, array $fileIds): bool
  112. {
  113. return static::updateBase(['group_id' => $groupId], [['file_id', 'in', $fileIds]]);
  114. }
  115. /**
  116. * 添加文件库记录
  117. * @param array $data
  118. * @param int $fileType
  119. * @param int $groupId
  120. * @param int|null $storeId
  121. * @return bool|false
  122. */
  123. public function add(array $data, int $fileType, int $groupId = 0, int $storeId = null): bool
  124. {
  125. return $this->save([
  126. 'group_id' => max($groupId, 0),
  127. 'channel' => 10,
  128. 'storage' => $data['storage'],
  129. 'domain' => $data['domain'],
  130. 'file_name' => $data['file_name'],
  131. 'file_path' => $data['file_path'],
  132. 'file_size' => $data['file_size'],
  133. 'file_ext' => $data['file_ext'],
  134. 'file_type' => $fileType,
  135. 'store_id' => $storeId ?: self::$storeId
  136. ]);
  137. }
  138. /**
  139. * 编辑记录
  140. * @param array $data
  141. * @return bool
  142. */
  143. public function edit(array $data): bool
  144. {
  145. return $this->allowField(['file_name', 'group_id'])->save($data) !== false;
  146. }
  147. }