AttachmentRepository.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\system\attachment;
  12. //附件
  13. use app\common\dao\BaseDao;
  14. use crmeb\exceptions\AdminException;
  15. use app\common\dao\system\attachment\AttachmentDao;
  16. use app\common\repositories\BaseRepository;
  17. use FormBuilder\Factory\Elm;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. use think\facade\Route;
  22. use think\Model;
  23. /**
  24. * 附件
  25. */
  26. class AttachmentRepository extends BaseRepository
  27. {
  28. /**
  29. * @var AttachmentCategoryRepository
  30. */
  31. private $attachmentCategoryRepository;
  32. /**
  33. * AttachmentRepository constructor.
  34. * @param AttachmentDao $dao
  35. * @param AttachmentCategoryRepository $attachmentCategoryRepository
  36. */
  37. public function __construct(AttachmentDao $dao, AttachmentCategoryRepository $attachmentCategoryRepository)
  38. {
  39. /**
  40. * @var AttachmentDao
  41. */
  42. $this->dao = $dao;
  43. $this->attachmentCategoryRepository = $attachmentCategoryRepository;
  44. }
  45. /**
  46. * 根据条件获取列表数据
  47. *
  48. * 本函数用于根据给定的条件查询数据库,并返回分页后的数据列表以及总条数。
  49. * 主要用于数据查询和分页处理,适用于各种数据展示场景。
  50. *
  51. * @param array $where 查询条件,以数组形式传递,键值对表示字段和值。
  52. * @param int $page 当前页码,用于分页查询。
  53. * @param int $limit 每页的数据条数,用于分页查询。
  54. * @return array 返回包含 'count' 和 'list' 两个元素的数组,'count' 表示总条数,'list' 表示分页后的数据列表。
  55. */
  56. public function getList(array $where, int $page, int $limit)
  57. {
  58. // 根据条件进行查询
  59. $query = $this->search($where);
  60. // 计算满足条件的数据总条数
  61. $count = $query->count($this->dao->getPk());
  62. // 进行分页查询,并隐藏某些字段
  63. $list = $query->page($page, $limit)->hidden(['user_type', 'user_id'])
  64. ->select();
  65. $list = getThumbWaterImage($list,['attachment_src'],'mid','','src');
  66. // 返回查询结果的总条数和分页后的数据列表
  67. return compact('count', 'list');
  68. }
  69. /**
  70. * 创建新记录。
  71. *
  72. * 该方法用于根据给定的参数创建一个新的数据记录。它首先将上传类型、用户类型和用户ID添加到数据数组中,
  73. * 然后调用DAO层的create方法来实际执行数据插入操作。
  74. *
  75. * @param int $uploadType 上传类型,表示数据的来源或上传方式。
  76. * @param int $userType 用户类型,用于区分不同类型的用户。
  77. * @param int $userId 用户ID,标识数据的创建者。
  78. * @param array $data 数据数组,包含需要创建的记录的具体数据。
  79. * @return mixed 返回DAO层create方法的执行结果,通常是一个自增的ID或影响行数。
  80. */
  81. public function create(int $uploadType, int $userType, int $userId, array $data)
  82. {
  83. // 将上传相关和用户相关信息加入到数据数组中
  84. $data['upload_type'] = $uploadType;
  85. $data['user_type'] = $userType;
  86. $data['user_id'] = $userId;
  87. // 调用DAO层的create方法,实际执行数据插入操作,并返回执行结果
  88. return $this->dao->create($data);
  89. }
  90. /**
  91. * 批量更改分类
  92. *
  93. * 本函数用于批量将指定附件ID的分类更改为新的分类ID。它可以用于管理附件的分类,无需逐个手动更改。
  94. *
  95. * @param array $ids 附件ID的数组,表示需要更改分类的附件。
  96. * @param int $categoryId 新的分类ID,指定附件将被更改到的分类。
  97. * @param int $merId 商家ID,可选参数,默认为0。用于指定操作的商家,如果系统支持多商家,则此参数可能有用。
  98. * @return mixed 返回执行结果,具体类型取决于DAO层的实现。
  99. */
  100. public function batchChangeCategory(array $ids, int $categoryId, $merId = 0)
  101. {
  102. // 调用DAO层的方法,执行批量更改分类的操作。
  103. return $this->dao->batchChange($ids, ['attachment_category_id' => $categoryId], $merId);
  104. }
  105. /**
  106. * 根据给定的ID和商家ID生成表单
  107. * 该方法用于创建一个编辑配置的表单,表单数据来源于指定ID的数据项。
  108. * 如果商家ID存在,则表单提交的地址是针对商家附件更新的路由;
  109. * 否则,表单提交的地址是针对系统附件更新的路由。
  110. *
  111. * @param int $id 配置项的ID,用于获取当前配置项的数据。
  112. * @param int $merId 商家ID,用于确定表单提交的地址是商家附件更新还是系统附件更新。
  113. * @return mixed 返回生成的表单对象,包含了表单的结构和数据。
  114. */
  115. public function form(int $id, int $merId)
  116. {
  117. // 根据$merId的存在与否确定表单提交的行动动词
  118. if ($merId) {
  119. $action = 'merchantAttachmentUpdate';
  120. } else {
  121. $action = 'systemAttachmentUpdate';
  122. }
  123. // 通过ID获取配置项的数据,并转换为数组格式
  124. $formData = $this->dao->get($id)->toArray();
  125. // 创建表单对象,并设置表单的提交URL
  126. $form = Elm::createForm(Route::buildUrl($action, is_null($id) ? [] : ['id' => $id])->build());
  127. // 设置表单的验证规则
  128. $form->setRule([
  129. Elm::input('attachment_name', '名称:')->placeholder('请输入名称')->required(),
  130. ]);
  131. // 设置表单的标题,并加载获取的配置项数据
  132. return $form->setTitle('编辑配置')->formData($formData);
  133. }
  134. /**
  135. * 视频分片上传
  136. * @param $data
  137. * @param $file
  138. * @return mixed
  139. */
  140. public function videoUpload($data, $file)
  141. {
  142. $pathinfo = pathinfo($data['filename']);
  143. if (isset($pathinfo['extension']) && !in_array($pathinfo['extension'], ['avi', 'mp4', 'wmv', 'rm', 'mpg', 'mpeg', 'mov', 'flv', 'swf'])) {
  144. throw new AdminException(400558);
  145. }
  146. $data['chunkNumber'] = (int)$data['chunkNumber'];
  147. $public_dir = app()->getRootPath() . 'public';
  148. $dir = '/uploads/attach/' . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  149. $all_dir = $public_dir . $dir;
  150. if (!is_dir($all_dir)) mkdir($all_dir, 0777, true);
  151. $filename = $all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'];
  152. move_uploaded_file($file['tmp_name'], $filename);
  153. $res['code'] = 0;
  154. $res['msg'] = 'error';
  155. $res['file_path'] = '';
  156. if ($data['chunkNumber'] == $data['totalChunks']) {
  157. $blob = '';
  158. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  159. $blob .= file_get_contents($all_dir . '/' . $data['filename'] . '__' . $i);
  160. }
  161. file_put_contents($all_dir . '/' . $data['filename'], $blob);
  162. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  163. @unlink($all_dir . '/' . $data['filename'] . '__' . $i);
  164. }
  165. if (file_exists($all_dir . '/' . $data['filename'])) {
  166. $res['code'] = 2;
  167. $res['msg'] = 'success';
  168. $res['file_path'] = systemConfig('site_url') . $dir . '/' . $data['filename'];
  169. }
  170. } else {
  171. if (file_exists($all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'])) {
  172. $res['code'] = 1;
  173. $res['msg'] = 'waiting';
  174. $res['file_path'] = '';
  175. }
  176. }
  177. return $res;
  178. }
  179. }