SystemAttachmentServices.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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\model\system\attachment\SystemAttachment;
  14. use qiniu\basic\BaseServices;
  15. use qiniu\exceptions\AdminException;
  16. use qiniu\exceptions\UploadException;
  17. use qiniu\services\DownloadImageService;
  18. use qiniu\services\UploadService;
  19. use think\Collection;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\exception\ValidateException;
  24. /**
  25. *
  26. * Class SystemAttachmentServices
  27. * @package app\services\attachment
  28. * @mixin SystemAttachment
  29. */
  30. class SystemAttachmentServices extends BaseServices
  31. {
  32. /**
  33. * SystemAttachmentServices constructor.
  34. * @param SystemAttachment $model
  35. */
  36. public function __construct(SystemAttachment $model)
  37. {
  38. $this->model = $model;
  39. }
  40. /**
  41. * 获取单个资源
  42. * @param array $where
  43. * @param string $field
  44. * @return array
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. */
  49. public function getInfo(array $where, string $field = '*')
  50. {
  51. return $this->getOne($where, $field);
  52. }
  53. /**
  54. * 获取图片列表
  55. * @param array $where
  56. * @return array
  57. * @throws DbException
  58. */
  59. public function getImageList(array $where)
  60. {
  61. [$page, $limit] = $this->getPageValue();
  62. $list = $this->getList($where,'*', $page, $limit);
  63. if ($list) {
  64. $site_url = sys_config('site_url');
  65. foreach ($list as &$item) {
  66. if ($item['file_type'] == 1) {
  67. if ($site_url) {
  68. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  69. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  70. }
  71. }
  72. $item['att_size'] = formatFileSize($item['att_size']);
  73. $item['time'] = date('Y-m-d H:i:s', $item['time']);
  74. }
  75. $list = get_thumb_water($list, 'mid', ['satt_dir']);
  76. }
  77. $where['module_type'] = 1;
  78. $count = $this->getCount($where);
  79. return compact('list', 'count');
  80. }
  81. /**
  82. * 删除图片
  83. * @param string $ids
  84. * @throws DataNotFoundException
  85. * @throws DbException
  86. * @throws ModelNotFoundException
  87. */
  88. public function del(string $ids)
  89. {
  90. $ids = explode(',', $ids);
  91. if (empty($ids)) throw new AdminException('请选择要删除的图片');
  92. foreach ($ids as $v) {
  93. $attinfo = $this->get((int)$v);
  94. if ($attinfo) {
  95. try {
  96. $upload = UploadService::init($attinfo['image_type']);
  97. if ($attinfo['image_type'] == 1) {
  98. if (strpos($attinfo['att_dir'], '/') == 0) {
  99. $attinfo['att_dir'] = substr($attinfo['att_dir'], 1);
  100. }
  101. if ($attinfo['att_dir']) $upload->delete(public_path() . $attinfo['att_dir']);
  102. } else {
  103. if ($attinfo['name']) $upload->delete($attinfo['name']);
  104. }
  105. } catch (\Throwable $e) {
  106. }
  107. $this->delete((int)$v);
  108. }
  109. }
  110. }
  111. /**
  112. * 图片上传
  113. * @param int $pid
  114. * @param string $file
  115. * @param int $upload_type
  116. * @param int $type
  117. * @return mixed
  118. */
  119. public function upload(int $pid, string $file, int $upload_type, int $type)
  120. {
  121. if ($upload_type == 0) {
  122. $upload_type = (int)sys_config('upload_type', 1);
  123. }
  124. try {
  125. $path = make_path('attach', 2, true);
  126. $upload = UploadService::init($upload_type);
  127. $res = $upload->to($path)->validate()->setAuthThumb(true)->move($file);
  128. if ($res === false) {
  129. throw new UploadException($upload->getError());
  130. } else {
  131. $fileInfo = $upload->getUploadInfo();
  132. //保存附件记录
  133. if ($type == 0) $this->saveAttachment($fileInfo, $pid, $type, 0, 1, $upload_type);
  134. return $res->filePath;
  135. }
  136. } catch (\Exception $e) {
  137. throw new UploadException($e->getMessage());
  138. }
  139. }
  140. public function move(array $data)
  141. {
  142. $res = $this->search()->whereIn('att_id', $data['images'])->update(['pid' => $data['pid']]);
  143. if (!$res) throw new AdminException('移动失败或不能重复移动到同一分类下');
  144. }
  145. /**添加附件记录
  146. * @param $name
  147. * @param $att_size
  148. * @param $att_type
  149. * @param $att_dir
  150. * @param $satt_dir
  151. * @param $pid
  152. * @param $imageType
  153. * @param $time
  154. * @param $module_type
  155. * @return bool
  156. */
  157. public function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  158. {
  159. $data['name'] = $name;
  160. $data['att_dir'] = $att_dir;
  161. $data['satt_dir'] = $satt_dir;
  162. $data['att_size'] = $att_size;
  163. $data['att_type'] = $att_type;
  164. $data['image_type'] = $imageType;
  165. $data['module_type'] = $module_type;
  166. $data['time'] = $time ? $time : time();
  167. $data['pid'] = $pid;
  168. if (!$this->save($data)) {
  169. throw new ValidateException('添加附件失败');
  170. }
  171. return true;
  172. }
  173. /**
  174. * 清除昨日海报
  175. * @return bool
  176. * @throws \Exception
  177. */
  178. public function emptyYesterdayAttachment()
  179. {
  180. try {
  181. $list = $this->getYesterday();
  182. foreach ($list as $key => $item) {
  183. $upload = UploadService::init((int)$item['image_type']);
  184. if ($item['image_type'] == 1) {
  185. $att_dir = $item['att_dir'];
  186. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  187. if (strstr($att_dir, 'http') === false)
  188. $upload->delete($att_dir);
  189. else {
  190. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  191. if ($filedir) $upload->delete($filedir);
  192. }
  193. }
  194. } else {
  195. if ($item['name']) $upload->delete($item['name']);
  196. }
  197. }
  198. $this->delYesterday();
  199. return true;
  200. } catch (\Exception $e) {
  201. $this->delYesterday();
  202. return true;
  203. }
  204. }
  205. /**
  206. * 获取昨日系统生成
  207. * @param int $type
  208. * @param int $relationId
  209. * @return Collection
  210. * @throws DataNotFoundException
  211. * @throws DbException
  212. * @throws ModelNotFoundException
  213. */
  214. public function getYesterday(int $type = 1, $relationId = 0)
  215. {
  216. return $this->model->where('type', $type)->when($relationId, function ($query) use ($relationId) {
  217. $query->where('relation_id', $relationId);
  218. })->whereTime('time', 'yesterday')->where('module_type', 2)->field(['name', 'att_dir', 'att_id', 'image_type'])->select();
  219. }
  220. /**
  221. * 删除昨日生成海报
  222. * @throws \Exception
  223. */
  224. public function delYesterday()
  225. {
  226. $this->model->whereTime('time', 'yesterday')->where('module_type', 2)->delete();
  227. }
  228. /**
  229. * 门店图片上传
  230. * @param int $pid
  231. * @param string $file
  232. * @param int $relationId
  233. * @param int $type
  234. * @param int $upload_type
  235. * @param string $uploadToken
  236. * @return mixed
  237. */
  238. public function storeUpload(int $pid, string $file, int $relationId, int $type = 2, int $upload_type = 0, string $uploadToken = '')
  239. {
  240. try {
  241. if ($type == 4) {
  242. $upload_type = 0;
  243. } else {
  244. if ($upload_type == 0) {
  245. $upload_type = (int)sys_config('upload_type', 1);
  246. }
  247. }
  248. $path = make_path('attach/' . ($type == 4 ? 'supplier' : ($type == 2 ? 'store' : '')), 2, true);
  249. $upload = UploadService::init($upload_type);
  250. $res = $upload->to($path)->validate()->setAuthThumb(true)->move($file);
  251. if ($res === false) {
  252. throw new UploadException($upload->getError());
  253. } else {
  254. $fileInfo = $upload->getUploadInfo();
  255. //保存附件记录
  256. $this->saveAttachment($fileInfo, $pid, $type, $relationId, 1, $upload_type, $uploadToken);
  257. return $res->filePath;
  258. }
  259. } catch (\Exception $e) {
  260. throw new UploadException($e->getMessage());
  261. }
  262. }
  263. /**
  264. * 视频分片上传
  265. * @param $data
  266. * @param $file
  267. * @param int $type
  268. * @param int $relation_id
  269. * @return mixed
  270. */
  271. public function videoUpload($data, $file, int $type = 1, int $relation_id = 0)
  272. {
  273. $public_dir = app()->getRootPath() . 'public';
  274. $store_dir = '';
  275. switch ($type) {
  276. case 2:
  277. $store_dir = 'store' . DIRECTORY_SEPARATOR;
  278. break;
  279. case 4:
  280. $store_dir = 'supplier' . DIRECTORY_SEPARATOR;
  281. break;
  282. }
  283. $dir = '/uploads/video/' . $store_dir . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  284. $all_dir = $public_dir . $dir;
  285. if (!is_dir($all_dir)) mkdir($all_dir, 0777, true);
  286. $filename = $all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'];
  287. move_uploaded_file($file->getPathName(), $filename);
  288. $res['code'] = 0;
  289. $res['msg'] = 'error';
  290. $res['file_path'] = '';
  291. if ($data['chunkNumber'] == $data['totalChunks']) {
  292. $blob = '';
  293. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  294. $blob .= file_get_contents($all_dir . '/' . $data['filename'] . '__' . $i);
  295. }
  296. file_put_contents($all_dir . '/' . $data['filename'], $blob);
  297. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  298. @unlink($all_dir . '/' . $data['filename'] . '__' . $i);
  299. }
  300. if (file_exists($all_dir . '/' . $data['filename'])) {
  301. $res['code'] = 2;
  302. $res['msg'] = 'success';
  303. $res['file_path'] = $dir . '/' . $data['filename'];
  304. }
  305. } else {
  306. if (file_exists($all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'])) {
  307. $res['code'] = 1;
  308. $res['msg'] = 'waiting';
  309. $res['file_path'] = '';
  310. }
  311. }
  312. $res['name'] = $res['dir'] = $res['file_path'];
  313. if (strpos($res['file_path'], 'http') === false) {
  314. $res['dir'] = $res['file_path'] = sys_config('site_url') . $res['file_path'];
  315. }
  316. if ($res['code'] == 2) {
  317. $res['size'] = getFileHeaders($res['file_path'])['size'] ?? 0;
  318. $this->saveAttachment($res, (int)($data['pid'] ?? 0), $type, $relation_id, 2);
  319. }
  320. return $res;
  321. }
  322. /**
  323. * 云端上传的视频保存记录
  324. * @param array $data
  325. * @param int $type
  326. * @param int $relation_id
  327. * @param int $upload_type
  328. * @return bool
  329. */
  330. public function saveOssVideoAttachment(array $data, int $type = 1, int $relation_id = 0, int $upload_type = 1)
  331. {
  332. $fileInfo = [];
  333. $fileInfo['name'] = $fileInfo['real_name'] = $data['path'];
  334. $fileInfo['cover_image'] = $data['cover_image'];
  335. if (!$fileInfo['cover_image'] && $upload_type != 1) {//云端视频
  336. $res = UploadService::init($upload_type)->videoCoverImage($data['path'], 'mid');
  337. $fileInfo['cover_image'] = $res['mid'] ?? '';
  338. }
  339. $fileInfo['size'] = getFileHeaders($data['path'])['size'] ?? 0;
  340. $this->saveAttachment($fileInfo, (int)$data['pid'], $type, $relation_id, 2, $upload_type);
  341. return true;
  342. }
  343. /**
  344. * 保存附件信息
  345. * @param $fileInfo
  346. * @param int $pid
  347. * @param int $type
  348. * @param int $relation_id
  349. * @param int $file_type
  350. * @param int $upload_type
  351. * @param string $uploadToken
  352. * @return bool
  353. */
  354. public function saveAttachment($fileInfo, int $pid = 0, int $type = 1, int $relation_id = 0, int $file_type = 1, int $upload_type = 1, string $uploadToken = '')
  355. {
  356. $fileType = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
  357. if ($fileInfo && !in_array($fileType, ['xlsx', 'xls'])) {
  358. $data['file_type'] = $file_type;
  359. $data['type'] = $type == 0 ? 1 : $type;
  360. $data['relation_id'] = $relation_id;
  361. $data['name'] = $fileInfo['name'];
  362. $data['real_name'] = $fileInfo['real_name'] ?? $fileInfo['name'] ?? '';
  363. $data['att_dir'] = $fileInfo['dir'] ?? $fileInfo['name'] ?? '';
  364. if ($data['att_dir'] && strpos($data['att_dir'], 'http') === false) {
  365. $data['att_dir'] = sys_config('site_url') . $data['att_dir'];
  366. }
  367. $data['satt_dir'] = $fileInfo['thumb_path'] ?? $fileInfo['cover_image'] ?? '';
  368. if ($file_type == 2) {
  369. if (!$data['satt_dir']) {//视频 默认封面
  370. $data['satt_dir'] = sys_config('site_url') . '/statics/images/video_default_cover.png';
  371. }
  372. if ($data['real_name'] && strpos($data['real_name'], '/') !== false) {
  373. $nameArr = explode('/', $data['real_name']);
  374. $data['real_name'] = end($nameArr) ?? $data['real_name'];
  375. }
  376. }
  377. $data['att_size'] = $fileInfo['size'] ?? '';
  378. $data['att_type'] = $fileInfo['type'] ?? '';
  379. $data['image_type'] = $upload_type;
  380. $data['module_type'] = 1;
  381. $data['time'] = $fileInfo['time'] ?? time();
  382. $data['pid'] = $pid;
  383. $data['scan_token'] = $uploadToken;
  384. $this->save($data);
  385. }
  386. return true;
  387. }
  388. /**
  389. * 网络图片上传
  390. * @param array $data
  391. * @param int $type
  392. * @param int $relation_id
  393. * @return bool
  394. * @throws \Exception
  395. */
  396. public function onlineUpload(array $data, int $type = 1, int $relation_id = 0)
  397. {
  398. //生成附件目录
  399. if (make_path('attach', 3, true) === '') {
  400. throw new AdminException('无法创建文件夹,请检查您的上传目录权限');
  401. }
  402. //上传图片
  403. /** @var SystemAttachmentServices $systemAttachmentService */
  404. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  405. $siteUrl = sys_config('site_url');
  406. foreach ($data['images'] as $image) {
  407. try {
  408. $uploadValue = app()->make(DownloadImageService::class)->thumb(true)->downloadImage($image);
  409. } catch (\Throwable $e) {
  410. throw new AdminException('文件下载失败,请更换链接');
  411. }
  412. if (is_array($uploadValue)) {
  413. //TODO 拼接图片地址
  414. if ($uploadValue['image_type'] == 1) {
  415. $imagePath = $siteUrl . $uploadValue['path'];
  416. } else {
  417. $imagePath = $uploadValue['path'];
  418. }
  419. //写入数据库
  420. if (!$uploadValue['is_exists']) {
  421. $systemAttachmentService->save([
  422. 'type' => $type == 0 ? 1 : $type,
  423. 'relation_id' => $relation_id,
  424. 'name' => $uploadValue['name'],
  425. 'real_name' => $uploadValue['name'],
  426. 'att_dir' => $imagePath,
  427. 'satt_dir' => $imagePath,
  428. 'att_size' => $uploadValue['size'],
  429. 'att_type' => $uploadValue['mime'],
  430. 'image_type' => $uploadValue['image_type'],
  431. 'module_type' => 1,
  432. 'time' => time(),
  433. 'pid' => $data['pid']
  434. ]);
  435. }
  436. }
  437. }
  438. return true;
  439. }
  440. /**
  441. * 删除门店、供应商附件
  442. * @param array $where
  443. * @param int $type
  444. * @param int $relation_id
  445. * @return bool
  446. * @throws DataNotFoundException
  447. * @throws DbException
  448. * @throws ModelNotFoundException
  449. */
  450. public function delAttachment(array $where = [], int $type = 0, int $relation_id = 0)
  451. {
  452. $where['type'] = $type;
  453. $where['relation_id'] = $relation_id;
  454. //删除附件
  455. $attachments = $this->getColumn($where, 'att_id');
  456. if ($attachments) {
  457. $idsArr = array_chunk($attachments, 100);
  458. foreach ($idsArr as $ids) {
  459. $this->del(implode(',', $ids));
  460. }
  461. }
  462. return true;
  463. }
  464. }