SystemAttachmentServices.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 crmeb\exceptions\AdminException;
  16. use crmeb\exceptions\ApiException;
  17. use crmeb\exceptions\UploadException;
  18. use app\services\other\UploadService;
  19. /**
  20. *
  21. * Class SystemAttachmentServices
  22. * @package app\services\attachment
  23. * @method getYesterday() 获取昨日生成数据
  24. * @method delYesterday() 删除昨日生成数据
  25. */
  26. class SystemAttachmentServices extends BaseServices
  27. {
  28. /**
  29. * SystemAttachmentServices constructor.
  30. * @param SystemAttachmentDao $dao
  31. */
  32. public function __construct(SystemAttachmentDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取单个资源
  38. * @param array $where
  39. * @param string $field
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. */
  45. public function getInfo(array $where, string $field = '*')
  46. {
  47. return $this->dao->getOne($where, $field);
  48. }
  49. /**
  50. * 获取图片列表
  51. * @param array $where
  52. * @return array
  53. */
  54. public function getImageList(array $where)
  55. {
  56. [$page, $limit] = $this->getPageValue();
  57. $list = $this->dao->getList($where, $page, $limit);
  58. $site_url = sys_config('site_url');
  59. foreach ($list as &$item) {
  60. if ($site_url) {
  61. $item['satt_dir'] = (strpos($item['satt_dir'], $site_url) !== false || strstr($item['satt_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['satt_dir'];
  62. $item['att_dir'] = (strpos($item['att_dir'], $site_url) !== false || strstr($item['att_dir'], 'http') !== false) ? $item['satt_dir'] : $site_url . $item['att_dir'];
  63. }
  64. }
  65. $where['module_type'] = 1;
  66. $count = $this->dao->count($where);
  67. return compact('list', 'count');
  68. }
  69. /**
  70. * 删除图片
  71. * @param string $ids
  72. */
  73. public function del(string $ids)
  74. {
  75. $ids = explode(',', $ids);
  76. if (empty($ids)) throw new AdminException(400599);
  77. foreach ($ids as $v) {
  78. $attinfo = $this->dao->get((int)$v);
  79. if ($attinfo) {
  80. try {
  81. $upload = UploadService::init($attinfo['image_type']);
  82. if ($attinfo['image_type'] == 1) {
  83. if (strpos($attinfo['att_dir'], '/') == 0) {
  84. $attinfo['att_dir'] = substr($attinfo['att_dir'], 1);
  85. }
  86. if ($attinfo['att_dir']) $upload->delete($attinfo['att_dir']);
  87. } else {
  88. if ($attinfo['name']) $upload->delete($attinfo['name']);
  89. }
  90. } catch (\Throwable $e) {
  91. }
  92. $this->dao->delete((int)$v);
  93. }
  94. }
  95. }
  96. /**
  97. * 图片上传
  98. * @param int $pid
  99. * @param string $file
  100. * @param int $upload_type
  101. * @param int $type
  102. * @return mixed
  103. */
  104. public function upload(int $pid, string $file, int $upload_type, int $type, $menuName)
  105. {
  106. $realName = false;
  107. if ($upload_type == 0) {
  108. $upload_type = sys_config('upload_type', 1);
  109. }
  110. if ($menuName == 'weixin_ckeck_file' || $menuName == 'ico_path') {
  111. $upload_type = 1;
  112. $realName = true;
  113. }
  114. try {
  115. $path = make_path('attach', 2, true);
  116. $upload = UploadService::init($upload_type);
  117. $res = $upload->to($path)->validate()->move($file, $realName);
  118. if ($res === false) {
  119. throw new UploadException($upload->getError());
  120. } else {
  121. $fileInfo = $upload->getUploadInfo();
  122. $fileType = pathinfo($fileInfo['name'], PATHINFO_EXTENSION);
  123. if ($fileInfo && $type == 0 && !in_array($fileType, ['xlsx', 'xls', 'mp4'])) {
  124. $data['name'] = $fileInfo['name'];
  125. $data['real_name'] = $fileInfo['real_name'];
  126. $data['att_dir'] = $fileInfo['dir'];
  127. $data['satt_dir'] = $fileInfo['thumb_path'];
  128. $data['att_size'] = $fileInfo['size'];
  129. $data['att_type'] = $fileInfo['type'];
  130. $data['image_type'] = $upload_type;
  131. $data['module_type'] = 1;
  132. $data['time'] = $fileInfo['time'] ?? time();
  133. $data['pid'] = $pid;
  134. $this->dao->save($data);
  135. }
  136. return $res->filePath;
  137. }
  138. } catch (\Exception $e) {
  139. throw new UploadException($e->getMessage());
  140. }
  141. }
  142. /**
  143. * @param array $data
  144. * @return \crmeb\basic\BaseModel
  145. */
  146. public function move(array $data)
  147. {
  148. $res = $this->dao->move($data);
  149. if (!$res) throw new AdminException(400600);
  150. }
  151. /**
  152. * 添加信息
  153. * @param array $data
  154. */
  155. public function save(array $data)
  156. {
  157. $this->dao->save($data);
  158. }
  159. /**
  160. * TODO 添加附件记录
  161. * @param $name
  162. * @param $att_size
  163. * @param $att_type
  164. * @param $att_dir
  165. * @param string $satt_dir
  166. * @param int $pid
  167. * @param int $imageType
  168. * @param int $time
  169. * @return SystemAttachment
  170. */
  171. public function attachmentAdd($name, $att_size, $att_type, $att_dir, $satt_dir = '', $pid = 0, $imageType = 1, $time = 0, $module_type = 1)
  172. {
  173. $data['name'] = $name;
  174. $data['att_dir'] = $att_dir;
  175. $data['satt_dir'] = $satt_dir;
  176. $data['att_size'] = $att_size;
  177. $data['att_type'] = $att_type;
  178. $data['image_type'] = $imageType;
  179. $data['module_type'] = $module_type;
  180. $data['time'] = $time ?: time();
  181. $data['pid'] = $pid;
  182. if (!$this->dao->save($data)) {
  183. throw new ApiException(100022);
  184. }
  185. return true;
  186. }
  187. /**
  188. * 推广名片生成
  189. * @param $name
  190. */
  191. public function getLikeNameList($name)
  192. {
  193. return $this->dao->getLikeNameList(['like_name' => $name], 0, 0);
  194. }
  195. /**
  196. * 清除昨日海报
  197. * @return bool
  198. * @throws \Exception
  199. */
  200. public function emptyYesterdayAttachment(): bool
  201. {
  202. try {
  203. $list = $this->dao->getYesterday();
  204. foreach ($list as $key => $item) {
  205. $upload = UploadService::init((int)$item['image_type']);
  206. if ($item['image_type'] == 1) {
  207. $att_dir = $item['att_dir'];
  208. if ($att_dir && strstr($att_dir, 'uploads') !== false) {
  209. if (strstr($att_dir, 'http') === false)
  210. $upload->delete($att_dir);
  211. else {
  212. $filedir = substr($att_dir, strpos($att_dir, 'uploads'));
  213. if ($filedir) $upload->delete($filedir);
  214. }
  215. }
  216. } else {
  217. if ($item['name']) $upload->delete($item['name']);
  218. }
  219. }
  220. $this->dao->delYesterday();
  221. return true;
  222. } catch (\Exception $e) {
  223. $this->dao->delYesterday();
  224. return true;
  225. }
  226. }
  227. /**
  228. * 视频分片上传
  229. * @param $data
  230. * @param $file
  231. * @return mixed
  232. */
  233. public function videoUpload($data, $file)
  234. {
  235. $pathinfo = pathinfo($data['filename']);
  236. if (isset($pathinfo['extension']) && !in_array($pathinfo['extension'], ['avi', 'mp4', 'wmv', 'rm', 'mpg', 'mpeg', 'mov', 'flv', 'swf'])) {
  237. throw new AdminException(400558);
  238. }
  239. $public_dir = app()->getRootPath() . 'public';
  240. $dir = '/uploads/attach/' . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  241. $all_dir = $public_dir . $dir;
  242. if (!is_dir($all_dir)) mkdir($all_dir, 0777, true);
  243. $filename = $all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'];
  244. move_uploaded_file($file['tmp_name'], $filename);
  245. $res['code'] = 0;
  246. $res['msg'] = 'error';
  247. $res['file_path'] = '';
  248. if ($data['chunkNumber'] == $data['totalChunks']) {
  249. $blob = '';
  250. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  251. $blob .= file_get_contents($all_dir . '/' . $data['filename'] . '__' . $i);
  252. }
  253. file_put_contents($all_dir . '/' . $data['filename'], $blob);
  254. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  255. @unlink($all_dir . '/' . $data['filename'] . '__' . $i);
  256. }
  257. if (file_exists($all_dir . '/' . $data['filename'])) {
  258. $res['code'] = 2;
  259. $res['msg'] = 'success';
  260. $res['file_path'] = sys_config('site_url') . $dir . '/' . $data['filename'];
  261. }
  262. } else {
  263. if (file_exists($all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'])) {
  264. $res['code'] = 1;
  265. $res['msg'] = 'waiting';
  266. $res['file_path'] = '';
  267. }
  268. }
  269. return $res;
  270. }
  271. }