SystemAttachmentServices.php 12 KB

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