SystemAttachmentServices.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\services\system\attachment;
  13. use app\services\BaseServices;
  14. use app\dao\system\attachment\SystemAttachmentDao;
  15. use crmeb\exceptions\AdminException;
  16. use crmeb\exceptions\UploadException;
  17. use crmeb\services\UploadService;
  18. use crmeb\traits\ServicesTrait;
  19. use think\exception\ValidateException;
  20. /**
  21. *
  22. * Class SystemAttachmentServices
  23. * @package app\services\attachment
  24. * @method getYesterday() 获取昨日生成数据
  25. * @method delYesterday() 删除昨日生成数据
  26. */
  27. class SystemAttachmentServices extends BaseServices
  28. {
  29. use ServicesTrait;
  30. /**
  31. * SystemAttachmentServices constructor.
  32. * @param SystemAttachmentDao $dao
  33. */
  34. public function __construct(SystemAttachmentDao $dao)
  35. {
  36. $this->dao = $dao;
  37. }
  38. /**
  39. * 获取单个资源
  40. * @param array $where
  41. * @param string $field
  42. * @return array
  43. * @throws \think\db\exception\DataNotFoundException
  44. * @throws \think\db\exception\DbException
  45. * @throws \think\db\exception\ModelNotFoundException
  46. */
  47. public function getInfo(array $where, string $field = '*')
  48. {
  49. return $this->dao->getOne($where, $field);
  50. }
  51. /**
  52. * 获取图片列表
  53. * @param array $where
  54. * @return array
  55. */
  56. public function getImageList(array $where)
  57. {
  58. [$page, $limit] = $this->getPageValue();
  59. $list = $this->dao->getList($where, $page, $limit);
  60. $site_url = sys_config('site_url');
  61. foreach ($list as &$item) {
  62. if ($site_url) {
  63. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  64. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  65. }
  66. }
  67. $where['module_type'] = 1;
  68. $count = $this->dao->count($where);
  69. return compact('list', 'count');
  70. }
  71. /**
  72. * 删除图片
  73. * @param string $ids
  74. */
  75. public function del(string $ids)
  76. {
  77. $ids = explode(',', $ids);
  78. if (empty($ids)) throw new AdminException('请选择要删除的图片');
  79. foreach ($ids as $v) {
  80. $attinfo = $this->dao->get((int)$v);
  81. if ($attinfo) {
  82. try {
  83. $upload = UploadService::init($attinfo['image_type']);
  84. if ($attinfo['image_type'] == 1) {
  85. if (strpos($attinfo['att_dir'], '/') == 0) {
  86. $attinfo['att_dir'] = substr($attinfo['att_dir'], 1);
  87. }
  88. if ($attinfo['att_dir']) $upload->delete($attinfo['att_dir']);
  89. } else {
  90. if ($attinfo['name']) $upload->delete($attinfo['name']);
  91. }
  92. } catch (\Throwable $e) {
  93. }
  94. $this->dao->delete((int)$v);
  95. }
  96. }
  97. }
  98. /**
  99. * 图片上传
  100. * @param int $pid
  101. * @param string $file
  102. * @param int $upload_type
  103. * @param int $type
  104. * @return mixed
  105. */
  106. public function upload(int $pid, string $file, int $upload_type, int $type)
  107. {
  108. if ($upload_type == 0) {
  109. $upload_type = sys_config('upload_type', 1);
  110. }
  111. try {
  112. $path = make_path('attach', 2, true);
  113. $upload = UploadService::init($upload_type);
  114. $res = $upload->to($path)->validate()->move($file);
  115. if ($res === false) {
  116. throw new UploadException($upload->getError());
  117. } else {
  118. $fileInfo = $upload->getUploadInfo();
  119. if ($fileInfo && $type == 0) {
  120. $data['name'] = $fileInfo['name'];
  121. $data['real_name'] = $fileInfo['real_name'];
  122. $data['att_dir'] = $fileInfo['dir'];
  123. $data['satt_dir'] = $fileInfo['thumb_path'];
  124. $data['att_size'] = $fileInfo['size'];
  125. $data['att_type'] = $fileInfo['type'];
  126. $data['image_type'] = $upload_type;
  127. $data['module_type'] = 1;
  128. $data['time'] = $fileInfo['time'] ?? time();
  129. $data['pid'] = $pid;
  130. $this->dao->save($data);
  131. }
  132. return $res->filePath;
  133. }
  134. } catch (\Exception $e) {
  135. throw new UploadException($e->getMessage());
  136. }
  137. }
  138. /**
  139. * @param array $data
  140. * @return \crmeb\basic\BaseModel
  141. */
  142. public function move(array $data)
  143. {
  144. $res = $this->dao->move($data);
  145. if (!$res) throw new AdminException('移动失败或不能重复移动到同一分类下');
  146. }
  147. /**
  148. * 添加信息
  149. * @param array $data
  150. */
  151. public function save(array $data)
  152. {
  153. $this->dao->save($data);
  154. }
  155. /**
  156. * TODO 添加附件记录
  157. * @param $name
  158. * @param $att_size
  159. * @param $att_type
  160. * @param $att_dir
  161. * @param string $satt_dir
  162. * @param int $pid
  163. * @param int $imageType
  164. * @param int $time
  165. * @return SystemAttachment
  166. */
  167. public function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  168. {
  169. $data['name'] = $name;
  170. $data['att_dir'] = $att_dir;
  171. $data['satt_dir'] = $satt_dir;
  172. $data['att_size'] = $att_size;
  173. $data['att_type'] = $att_type;
  174. $data['image_type'] = $imageType;
  175. $data['module_type'] = $module_type;
  176. $data['time'] = $time ? $time : time();
  177. $data['pid'] = $pid;
  178. if (!$this->dao->save($data)) {
  179. throw new ValidateException('添加附件失败');
  180. }
  181. return true;
  182. }
  183. /**
  184. * 推广名片生成
  185. * @param $name
  186. */
  187. public function getLikeNameList($name)
  188. {
  189. return $this->dao->getLikeNameList(['like_name' => $name], 0, 0);
  190. }
  191. /**
  192. * 清除昨日海报
  193. * @return bool
  194. * @throws \Exception
  195. */
  196. public function emptyYesterdayAttachment()
  197. {
  198. try {
  199. $list = $this->dao->getYesterday();
  200. foreach ($list as $key => $item) {
  201. $upload = UploadService::init((int)$item['image_type']);
  202. if ($item['image_type'] == 1) {
  203. $att_dir = $item['att_dir'];
  204. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  205. if (strstr($att_dir, 'http') === false)
  206. $upload->delete($att_dir);
  207. else {
  208. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  209. if ($filedir) $upload->delete($filedir);
  210. }
  211. }
  212. } else {
  213. if ($item['name']) $upload->delete($item['name']);
  214. }
  215. }
  216. $this->dao->delYesterday();
  217. return true;
  218. } catch (\Exception $e) {
  219. $this->dao->delYesterday();
  220. return true;
  221. }
  222. }
  223. }