UploadFile.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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 = [])
  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('is_delete', '=', 0)
  58. ->order(['file_id' => 'desc'])
  59. ->paginate(15);
  60. }
  61. /**
  62. * 移入|移出回收站
  63. * @param bool $isRecycle
  64. * @return false|int
  65. */
  66. public function setRecycle(bool $isRecycle = true)
  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)
  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. // 实例化存储驱动
  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. $fileInfo->save(['is_delete' => 1]);
  100. }
  101. return true;
  102. }
  103. // /**
  104. // * 批量软删除
  105. // * @param $fileIds
  106. // * @return $this
  107. // */
  108. // public function softDelete($fileIds)
  109. // {
  110. // return $this->where('file_id', 'in', $fileIds)->update(['is_recycle' => 1]);
  111. // }
  112. /**
  113. * 批量移动文件分组
  114. * @param int $groupId
  115. * @param array $fileIds
  116. * @return $this
  117. */
  118. public function moveGroup(int $groupId, array $fileIds)
  119. {
  120. return $this->where('file_id', 'in', $fileIds)->update(['group_id' => $groupId]);
  121. }
  122. /**
  123. * 添加文件库记录
  124. * @param array $data
  125. * @param int $fileType
  126. * @param int $groupId
  127. * @return false|int|mixed
  128. */
  129. public function add(array $data, int $fileType, int $groupId = 0)
  130. {
  131. return $this->save([
  132. 'group_id' => $groupId > 0 ? (int)$groupId : 0,
  133. 'channel' => 10,
  134. 'storage' => $data['storage'],
  135. 'domain' => $data['domain'],
  136. 'file_name' => $data['file_name'],
  137. 'file_path' => $data['file_path'],
  138. 'file_size' => $data['file_size'],
  139. 'file_ext' => $data['file_ext'],
  140. 'file_type' => $fileType,
  141. 'store_id' => self::$storeId
  142. ]);
  143. }
  144. /**
  145. * 编辑记录
  146. * @param array $data
  147. * @return bool
  148. */
  149. public function edit(array $data)
  150. {
  151. return $this->allowField(['file_name', 'group_id'])->save($data) !== false;
  152. }
  153. }