SystemAttachmentServices.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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\UploadService;
  18. use crmeb\traits\ServicesTrait;
  19. use think\exception\ValidateException;
  20. /**
  21. *
  22. * Class SystemAttachmentServices
  23. * @package app\services\attachment
  24. * @method getYesterday() 获取昨日生成数据
  25. * @method delYesterday() 删除昨日生成数据
  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. $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('请选择要删除的图片');
  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)
  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. $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('移动失败或不能重复移动到同一分类下');
  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 ValidateException('添加附件失败');
  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()
  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. $public_dir = app()->getRootPath() . 'public';
  238. $dir = '/uploads/attach/' . date('Y') . DIRECTORY_SEPARATOR . date('m') . DIRECTORY_SEPARATOR . date('d');
  239. $all_dir = $public_dir . $dir;
  240. if (!is_dir($all_dir)) mkdir($all_dir, 0777, true);
  241. $filename = $all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'];
  242. move_uploaded_file($file['tmp_name'], $filename);
  243. $res['code'] = 0;
  244. $res['msg'] = 'error';
  245. $res['file_path'] = '';
  246. if ($data['chunkNumber'] == $data['totalChunks']) {
  247. $blob = '';
  248. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  249. $blob .= file_get_contents($all_dir . '/' . $data['filename'] . '__' . $i);
  250. }
  251. file_put_contents($all_dir . '/' . $data['filename'], $blob);
  252. for ($i = 1; $i <= $data['totalChunks']; $i++) {
  253. @unlink($all_dir . '/' . $data['filename'] . '__' . $i);
  254. }
  255. if (file_exists($all_dir . '/' . $data['filename'])) {
  256. $res['code'] = 2;
  257. $res['msg'] = 'success';
  258. $res['file_path'] = sys_config('site_url') . $dir . '/' . $data['filename'];
  259. }
  260. } else {
  261. if (file_exists($all_dir . '/' . $data['filename'] . '__' . $data['chunkNumber'])) {
  262. $res['code'] = 1;
  263. $res['msg'] = 'waiting';
  264. $res['file_path'] = '';
  265. }
  266. }
  267. return $res;
  268. }
  269. }