UploadFile.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 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. use app\common\enum\file\Storage as StorageEnum;
  18. /**
  19. * 文件库模型
  20. * Class UploadFile
  21. * @package app\store\model
  22. */
  23. class UploadFile extends UploadFileModel
  24. {
  25. /**
  26. * 获取列表记录
  27. * @param array $param
  28. * @return \think\Paginator
  29. * @throws \think\db\exception\DbException
  30. */
  31. public function getList(array $param = []): \think\Paginator
  32. {
  33. // 商品列表获取条件
  34. $params = $this->setQueryDefaultValue($param, [
  35. 'fileType' => -1, // 文件类型(-1全部 10图片 20附件 30视频)
  36. 'groupId' => -1, // 分组ID(-1全部 0未分组)
  37. 'fileName' => '', // 文件名称
  38. 'storage' => '', // 存储方式(StorageEnum)
  39. 'channel' => -1, // 上传来源(-1全部 10商户后台 20用户端)
  40. 'isRecycle' => false // 是否在回收站
  41. ]);
  42. // 查询对象
  43. $query = $this->getNewQuery();
  44. // 文件分组
  45. $params['groupId'] > -1 && $query->where('group_id', '=', (int)$params['groupId']);
  46. // 文件类型
  47. $params['fileType'] > -1 && $query->where('file_type', '=', (int)$params['fileType']);
  48. // 存储方式
  49. !empty($params['storage']) && $query->where('storage', '=', $params['storage']);
  50. // 上传来源
  51. $params['channel'] > -1 && $query->where('channel', '=', (int)$params['channel']);
  52. // 文件名称
  53. !empty($params['fileName']) && $query->where('file_name', 'like', "%{$params['fileName']}%");
  54. // 是否在回收站
  55. $query->where('is_recycle', '=', (int)$params['isRecycle']);
  56. // 查询列表数据
  57. return $query->where('channel', '=', 10)
  58. ->where('is_delete', '=', 0)
  59. ->order(['file_id' => 'desc'])
  60. ->paginate(15);
  61. }
  62. /**
  63. * 移入|移出回收站
  64. * @param bool $isRecycle
  65. * @return bool|false
  66. */
  67. public function setRecycle(bool $isRecycle = true): bool
  68. {
  69. return $this->save(['is_recycle' => (int)$isRecycle]);
  70. }
  71. /**
  72. * 删除文件(批量)
  73. * @param array $fileIds 文件ID集
  74. * @return bool
  75. * @throws \think\Exception
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function setDelete(array $fileIds): bool
  81. {
  82. // 验证文件数量
  83. if (count($fileIds) > 15) {
  84. $this->error = '一次性最多删除15个文件';
  85. return false;
  86. }
  87. // 存储配置信息
  88. $config = SettingModel::getItem(SettingEnum::STORAGE);
  89. foreach ($fileIds as $fileId) {
  90. // 获取文件详情
  91. $fileInfo = static::detail($fileId);
  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. $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. * @return bool|false
  120. */
  121. public function add(array $data, int $fileType, int $groupId = 0): bool
  122. {
  123. return $this->save([
  124. 'group_id' => $groupId > 0 ? (int)$groupId : 0,
  125. 'channel' => 10,
  126. 'storage' => $data['storage'],
  127. 'domain' => $data['domain'],
  128. 'file_name' => $data['file_name'],
  129. 'file_path' => $data['file_path'],
  130. 'file_size' => $data['file_size'],
  131. 'file_ext' => $data['file_ext'],
  132. 'file_type' => $fileType,
  133. 'store_id' => self::$storeId
  134. ]);
  135. }
  136. /**
  137. * 编辑记录
  138. * @param array $data
  139. * @return bool
  140. */
  141. public function edit(array $data): bool
  142. {
  143. return $this->allowField(['file_name', 'group_id'])->save($data) !== false;
  144. }
  145. }