SystemAttachmentService.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\extra\service\system;
  3. use app\extra\basic\Service;
  4. use app\extra\dao\system\SystemAttachmentDao;
  5. use app\extra\tools\UploadExtend;
  6. use think\exception\ValidateException;
  7. class SystemAttachmentService extends Service
  8. {
  9. /**
  10. * 设置模型
  11. * @return string
  12. */
  13. public function __construct()
  14. {
  15. $this->dao = new SystemAttachmentDao();
  16. }
  17. /**
  18. * 列表
  19. * @param array $param
  20. */
  21. public function getList(array $param = [])
  22. {
  23. [$page,$limit] = $this->getPageValue();
  24. $list = $this->dao->getList($this->searchFilter($param),'*',$page,$limit,'id desc');
  25. $count = $this->dao->count($this->searchFilter($param));
  26. return compact('list','count');
  27. }
  28. protected function searchFilter(array $param = []): array
  29. {
  30. $filter = [];
  31. !empty($param['status']) && $filter[] = ["status", '=', $param['status']];
  32. !empty($param['is_del']) && $filter[] = ["is_del", '=', $param['is_del']];
  33. !empty($param['file_type']) && $filter[] = ["file_type", '=', $param['file_type']];
  34. !empty($param['real_name']) && $filter[] = ["real_name", 'like', '%'.$param['real_name'].'%'];
  35. !empty($param['pid']) && $filter[] = ["pid", '=', $param['pid']];
  36. return $filter;
  37. }
  38. /**
  39. * 获取图片列表
  40. * @param array $where
  41. * @return array
  42. */
  43. public function getImageList(array $where)
  44. {
  45. [$page, $limit] = $this->getPageValue();
  46. $list = $this->dao->getList($this->searchFilter($where), '*',$page, $limit);
  47. if ($list) {
  48. $site_url = sConf('service.site_url');
  49. foreach ($list as &$item) {
  50. if ($item['file_type'] == 1) {
  51. if ($site_url) {
  52. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  53. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  54. }
  55. }
  56. $item['att_size'] = formatFileSize($item['att_size']);
  57. $item['time'] = date('Y-m-d H:i:s', $item['time']);
  58. }
  59. // $list = get_thumb_water($list, 'mid', ['satt_dir']);
  60. }
  61. $where['module_type'] = 1;
  62. $count = $this->dao->count($this->searchFilter($where));
  63. return compact('list', 'count');
  64. }
  65. /**
  66. * 删除图片
  67. * @param string $ids
  68. */
  69. public function del(string $ids)
  70. {
  71. $ids = explode(',', $ids);
  72. if (empty($ids)) throw new ValidateException('请选择要删除的图片');
  73. foreach ($ids as $v) {
  74. $attinfo = $this->dao->getOne(['att_id'=>(int)$v]);
  75. if ($attinfo) {
  76. try {
  77. $upload = UploadExtend::disk($attinfo['image_type']);
  78. if ($attinfo['image_type'] == 'local') {
  79. $fileArr = parse_url($attinfo['att_dir']);
  80. $filePath = $fileArr['path'] ?? $attinfo['att_dir'];
  81. if (strpos($filePath, '/') == 0) {
  82. $filePath = substr($filePath, 1);
  83. }
  84. if ($filePath) $upload->delete($filePath);
  85. } else {
  86. if ($attinfo['name']) $upload->delete($attinfo['name']);
  87. }
  88. } catch (\Throwable $e) {
  89. }
  90. $this->dao->delete((int)$v);
  91. }
  92. }
  93. }
  94. /**
  95. * @param array $data
  96. * @return \crmeb\basic\BaseModel
  97. */
  98. public function move(array $data)
  99. {
  100. $res = $this->dao->move($data);
  101. if (!$res) throw new ValidateException('移动失败或不能重复移动到同一分类下');
  102. }
  103. /**
  104. * 保存附件信息
  105. * @param $fileInfo
  106. * @param int $pid
  107. * @param int $type
  108. * @param int $relation_id
  109. * @param int $file_type
  110. * @param int $upload_type
  111. * @return bool
  112. */
  113. public function saveAttachment($fileInfo,int $pid = 0,int $file_type = 1, int $upload_type = 1)
  114. {
  115. $fileType = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
  116. if ($fileInfo && !in_array($fileType, ['xlsx', 'xls'])) {
  117. $data['file_type'] = $file_type;
  118. $data['name'] = $fileInfo['name'];
  119. $data['real_name'] = $fileInfo['real_name'] ?? $fileInfo['name'] ?? '';
  120. $data['att_dir'] = $fileInfo['dir'] ?? $fileInfo['name'] ?? '';
  121. if ($data['att_dir'] && strpos($data['att_dir'], 'http') === false) {
  122. $data['att_dir'] = sConf('service.site_url') . $data['att_dir'];
  123. }
  124. $data['satt_dir'] = $fileInfo['thumb_path'] ?? $fileInfo['cover_image'] ?? '';
  125. if ($file_type == 2) {
  126. // if (!$data['satt_dir']) {//视频 默认封面
  127. // $data['satt_dir'] = sys_config('site_url') . '/statics/images/video_default_cover.png';
  128. // }
  129. if ($data['real_name'] && strpos($data['real_name'], '/') !== false) {
  130. $nameArr = explode('/', $data['real_name']);
  131. $data['real_name'] = end($nameArr) ?? $data['real_name'];
  132. }
  133. }
  134. $data['att_size'] = $fileInfo['size'] ?? '';
  135. $data['att_type'] = $fileInfo['type'] ?? '';
  136. $data['image_type'] = $fileInfo['image_type'];
  137. $data['module_type'] = 1;
  138. $data['time'] = $fileInfo['time'] ?? time();
  139. $data['pid'] = $pid;
  140. $this->dao->save($data);
  141. }
  142. return true;
  143. }
  144. }