SystemAttachmentServices.php 10.0 KB

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