SystemAttachmentServices.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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 app\services\product\product\CopyTaobaoServices;
  16. use crmeb\exceptions\AdminException;
  17. use crmeb\exceptions\ApiException;
  18. use crmeb\exceptions\UploadException;
  19. use app\services\other\UploadService;
  20. /**
  21. *
  22. * Class SystemAttachmentServices
  23. * @package app\services\attachment
  24. * @method getYesterday() 获取昨日生成数据
  25. * @method delYesterday() 删除昨日生成数据
  26. * @method scanUploadImage($scan_token) 获取扫码上传的图片数据
  27. */
  28. class SystemAttachmentServices extends BaseServices
  29. {
  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. $item['time'] = date('Y-m-d H:i:s', $item['time']);
  66. }
  67. }
  68. $where['module_type'] = 1;
  69. $count = $this->dao->count($where);
  70. return compact('list', 'count');
  71. }
  72. /**
  73. * 删除图片
  74. * @param string $ids
  75. */
  76. public function del(string $ids)
  77. {
  78. $ids = explode(',', $ids);
  79. if (empty($ids)) throw new AdminException(400599);
  80. foreach ($ids as $v) {
  81. $attinfo = $this->dao->get((int)$v);
  82. if ($attinfo) {
  83. try {
  84. $upload = UploadService::init($attinfo['image_type']);
  85. if ($attinfo['image_type'] == 1) {
  86. if (strpos($attinfo['att_dir'], '/') == 0) {
  87. $attinfo['att_dir'] = substr($attinfo['att_dir'], 1);
  88. }
  89. if ($attinfo['att_dir']) $upload->delete($attinfo['att_dir']);
  90. } else {
  91. if ($attinfo['name']) $upload->delete($attinfo['name']);
  92. }
  93. } catch (\Throwable $e) {
  94. }
  95. $this->dao->delete((int)$v);
  96. }
  97. }
  98. }
  99. /**
  100. * 图片上传
  101. * @param int $pid
  102. * @param string $file
  103. * @param int $upload_type
  104. * @param int $type
  105. * @return mixed
  106. */
  107. public function upload(int $pid, string $file, int $upload_type, int $type, $menuName, $uploadToken = '')
  108. {
  109. $realName = false;
  110. if ($upload_type == 0) {
  111. $upload_type = sys_config('upload_type', 1);
  112. }
  113. if ($menuName == 'weixin_ckeck_file' || $menuName == 'ico_path') {
  114. $upload_type = 1;
  115. $realName = true;
  116. }
  117. try {
  118. $path = make_path('attach', 2, true);
  119. if ($path === '') {
  120. throw new AdminException(400555);
  121. }
  122. $upload = UploadService::init($upload_type);
  123. $res = $upload->to($path)->validate()->move($file, $realName);
  124. if ($res === false) {
  125. throw new UploadException($upload->getError());
  126. } else {
  127. $fileInfo = $upload->getUploadInfo();
  128. $fileType = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
  129. if ($fileInfo && $type == 0 && !in_array($fileType, ['xlsx', 'xls', 'mp4'])) {
  130. $data['name'] = $fileInfo['name'];
  131. $data['real_name'] = $fileInfo['real_name'];
  132. $data['att_dir'] = $fileInfo['dir'];
  133. $data['satt_dir'] = $fileInfo['thumb_path'];
  134. $data['att_size'] = $fileInfo['size'];
  135. $data['att_type'] = $fileInfo['type'];
  136. $data['image_type'] = $upload_type;
  137. $data['module_type'] = 1;
  138. $data['time'] = $fileInfo['time'] ?? time();
  139. $data['pid'] = $pid;
  140. $data['scan_token'] = $uploadToken;
  141. $this->dao->save($data);
  142. }
  143. return $res->filePath;
  144. }
  145. } catch (\Exception $e) {
  146. throw new UploadException($e->getMessage());
  147. }
  148. }
  149. /**
  150. * @param array $data
  151. * @return \crmeb\basic\BaseModel
  152. */
  153. public function move(array $data)
  154. {
  155. $this->dao->move($data);
  156. }
  157. /**
  158. * 添加信息
  159. * @param array $data
  160. */
  161. public function save(array $data)
  162. {
  163. $this->dao->save($data);
  164. }
  165. /**
  166. * TODO 添加附件记录
  167. * @param $name
  168. * @param $att_size
  169. * @param $att_type
  170. * @param $att_dir
  171. * @param string $satt_dir
  172. * @param int $pid
  173. * @param int $imageType
  174. * @param int $time
  175. * @return SystemAttachment
  176. */
  177. public function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  178. {
  179. $data['name'] = $name;
  180. $data['att_dir'] = $att_dir;
  181. $data['satt_dir'] = $satt_dir;
  182. $data['att_size'] = $att_size;
  183. $data['att_type'] = $att_type;
  184. $data['image_type'] = $imageType;
  185. $data['module_type'] = $module_type;
  186. $data['time'] = $time ?: time();
  187. $data['pid'] = $pid;
  188. if (!$this->dao->save($data)) {
  189. throw new ApiException(100022);
  190. }
  191. return true;
  192. }
  193. /**
  194. * 推广名片生成
  195. * @param $name
  196. */
  197. public function getLikeNameList($name)
  198. {
  199. return $this->dao->getLikeNameList(['like_name' => $name], 0, 0);
  200. }
  201. /**
  202. * 清除昨日海报
  203. * @return bool
  204. * @throws \Exception
  205. */
  206. public function emptyYesterdayAttachment(): bool
  207. {
  208. try {
  209. $list = $this->dao->getYesterday();
  210. foreach ($list as $key => $item) {
  211. $upload = UploadService::init((int)$item['image_type']);
  212. if ($item['image_type'] == 1) {
  213. $att_dir = $item['att_dir'];
  214. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  215. if (strstr($att_dir, 'http') === false)
  216. $upload->delete($att_dir);
  217. else {
  218. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  219. if ($filedir) $upload->delete($filedir);
  220. }
  221. }
  222. } else {
  223. if ($item['name']) $upload->delete($item['name']);
  224. }
  225. }
  226. $this->dao->delYesterday();
  227. return true;
  228. } catch (\Exception $e) {
  229. $this->dao->delYesterday();
  230. return true;
  231. }
  232. }
  233. /**
  234. * 视频分片上传
  235. * @param $data
  236. * @param $file
  237. * @return mixed
  238. */
  239. public function videoUpload($data, $file)
  240. {
  241. $pathinfo = pathinfo($data['filename']);
  242. if (isset($pathinfo['extension']) && !in_array($pathinfo['extension'], ['avi', 'mp4', 'wmv', 'rm', 'mpg', 'mpeg', 'mov', 'flv', 'swf'])) {
  243. throw new AdminException(400558);
  244. }
  245. $data['chunkNumber'] = (int)$data['chunkNumber'];
  246. $public_dir = app()->getRootPath() . 'public';
  247. $dir = '/uploads/attach/' . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  248. $all_dir = $public_dir . $dir;
  249. if (!is_dir($all_dir)) mkdir($all_dir, 0777, true);
  250. $filename = $all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'];
  251. move_uploaded_file($file['tmp_name'], $filename);
  252. $res['code'] = 0;
  253. $res['msg'] = 'error';
  254. $res['file_path'] = '';
  255. if ($data['chunkNumber'] == $data['totalChunks']) {
  256. $blob = '';
  257. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  258. $blob .= file_get_contents($all_dir . '/' . $data['filename'] . '__' . $i);
  259. }
  260. file_put_contents($all_dir . '/' . $data['filename'], $blob);
  261. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  262. @unlink($all_dir . '/' . $data['filename'] . '__' . $i);
  263. }
  264. if (file_exists($all_dir . '/' . $data['filename'])) {
  265. $res['code'] = 2;
  266. $res['msg'] = 'success';
  267. $res['file_path'] = sys_config('site_url') . $dir . '/' . $data['filename'];
  268. }
  269. } else {
  270. if (file_exists($all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'])) {
  271. $res['code'] = 1;
  272. $res['msg'] = 'waiting';
  273. $res['file_path'] = '';
  274. }
  275. }
  276. return $res;
  277. }
  278. /**
  279. * 网络图片上传
  280. * @param $data
  281. * @return bool
  282. * @throws \Exception
  283. * @author 吴汐
  284. * @email 442384644@qq.com
  285. * @date 2023/06/13
  286. */
  287. public function onlineUpload($data)
  288. {
  289. //生成附件目录
  290. if (make_path('attach', 3, true) === '') {
  291. throw new AdminException(400555);
  292. }
  293. //上传图片
  294. /** @var SystemAttachmentServices $systemAttachmentService */
  295. $systemAttachmentService = app()->make(SystemAttachmentServices::class);
  296. $siteUrl = sys_config('site_url');
  297. foreach ($data['images'] as $image) {
  298. $uploadValue = app()->make(CopyTaobaoServices::class)->downloadImage($image);
  299. if (is_array($uploadValue)) {
  300. //TODO 拼接图片地址
  301. if ($uploadValue['image_type'] == 1) {
  302. $imagePath = $siteUrl . $uploadValue['path'];
  303. } else {
  304. $imagePath = $uploadValue['path'];
  305. }
  306. //写入数据库
  307. if (!$uploadValue['is_exists']) {
  308. $systemAttachmentService->save([
  309. 'name' => $uploadValue['name'],
  310. 'real_name' => $uploadValue['name'],
  311. 'att_dir' => $imagePath,
  312. 'satt_dir' => $imagePath,
  313. 'att_size' => $uploadValue['size'],
  314. 'att_type' => $uploadValue['mime'],
  315. 'image_type' => $uploadValue['image_type'],
  316. 'module_type' => 1,
  317. 'time' => time(),
  318. 'pid' => $data['pid']
  319. ]);
  320. }
  321. }
  322. }
  323. return true;
  324. }
  325. }