SystemAttachmentServices.php 17 KB

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