Attachment.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\attachment;
  12. use app\common\repositories\system\attachment\AttachmentCategoryRepository;
  13. use app\common\repositories\system\attachment\AttachmentRepository;
  14. use crmeb\basic\BaseController;
  15. use crmeb\services\DownloadImageService;
  16. use crmeb\services\ImageHostService;
  17. use crmeb\services\UploadService;
  18. use Exception;
  19. use think\App;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\facade\Cache;
  24. use think\response\Json;
  25. /**
  26. * 附件
  27. */
  28. class Attachment extends BaseController
  29. {
  30. /**
  31. * @var AttachmentRepository
  32. */
  33. protected $repository;
  34. /**
  35. * @var int
  36. */
  37. protected $merId;
  38. /**
  39. * Attachment constructor.
  40. * @param App $app
  41. * @param AttachmentRepository $repository
  42. */
  43. public function __construct(App $app, AttachmentRepository $repository)
  44. {
  45. parent::__construct($app);
  46. $this->repository = $repository;
  47. $this->merId = $this->request->merId();
  48. }
  49. /**
  50. * 上传图片
  51. * @param int $id
  52. * @param string $field
  53. * @param AttachmentCategoryRepository $repository
  54. * @return mixed
  55. * @throws DataNotFoundException
  56. * @throws DbException
  57. * @throws ModelNotFoundException
  58. * @author xaboy
  59. * @day 2020-04-15
  60. */
  61. public function image($id, $field, AttachmentCategoryRepository $repository)
  62. {
  63. $file = $this->request->file($field);
  64. $ueditor = $this->request->param('ueditor');
  65. if (!$file)
  66. return app('json')->fail('请上传图片');
  67. $file = is_array($file) ? $file[0] : $file;
  68. if ($id) {
  69. if (!$category = $repository->get($id, $this->merId))
  70. return app('json')->fail('目录不存在');
  71. $info = [
  72. 'enname' => $category->attachment_category_enname,
  73. 'id' => $category->attachment_category_id
  74. ];
  75. } else {
  76. $info = [
  77. 'enname' => 'def',
  78. 'id' => 0
  79. ];
  80. }
  81. validate(["$field|图片" => [
  82. 'fileSize' => config('upload.filesize'),
  83. 'fileExt' => config('upload.iamge_fileExt'),
  84. 'fileMime' => config('upload.image_fileMime'),
  85. ]])->check([$field => $file]);
  86. $type = (int)systemConfig('upload_type') ?: 1;
  87. $upload = UploadService::create($type);
  88. $data = $upload->to($info['enname'])->move($field);
  89. if ($data === false) {
  90. return app('json')->fail($upload->getError());
  91. }
  92. $res = $upload->getUploadInfo();
  93. $res['dir'] = tidy_url($res['dir']);
  94. $_name = '.' . $file->getOriginalExtension();
  95. $data = [
  96. 'attachment_category_id' => $info['id'],
  97. 'attachment_name' => str_replace($_name, '', $file->getOriginalName()),
  98. 'attachment_src' => $res['dir']
  99. ];
  100. $image = $this->repository->create($type, $this->merId, $this->request->adminId(), $data);
  101. if ($ueditor)
  102. return response([
  103. 'state' => 'SUCCESS',
  104. 'url' => $data['attachment_src'],
  105. 'title' => $data['attachment_src'],
  106. 'original' => $data['attachment_src'],
  107. ], 200, [], 'json');
  108. return app('json')->success(['src' => $data['attachment_src'], 'attachment_id' => $image->attachment_id]);
  109. }
  110. /**
  111. * 获取列表
  112. * @return Json
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws ModelNotFoundException
  116. * @author 张先生
  117. * @date 2020-03-27
  118. */
  119. public function getList()
  120. {
  121. [$page, $limit] = $this->getPage();
  122. $where = $this->request->params([['attachment_category_id', 0], 'order', 'attachment_name']);
  123. $where['user_type'] = $this->merId;
  124. return app('json')->success($this->repository->getList($where, $page, $limit));
  125. }
  126. /**
  127. * 批量修改分类
  128. * @param AttachmentCategoryRepository $attachmentCategoryRepository
  129. * @return mixed
  130. * @throws DbException
  131. * @author xaboy
  132. * @day 2020-04-16
  133. */
  134. public function batchChangeCategory(AttachmentCategoryRepository $attachmentCategoryRepository)
  135. {
  136. [$ids, $attachment_category_id] = $this->request->params([['ids', []], 'attachment_category_id'], true);
  137. if ($attachment_category_id && !$attachmentCategoryRepository->merExists($this->merId, $attachment_category_id))
  138. return app('json')->fail('分类不存在');
  139. if (!is_array($ids) || !count($ids))
  140. return app('json')->fail('请选择要修改分类的附件');
  141. $this->repository->batchChangeCategory(array_map('intval', $ids), intval($attachment_category_id), $this->merId);
  142. return app('json')->success('图片移动成功');
  143. }
  144. /**
  145. * 批量删除
  146. *
  147. * @return Json
  148. * @throws Exception
  149. * @author 张先生
  150. * @date 2020-03-30
  151. */
  152. public function delete()
  153. {
  154. $ids = (array)$this->request->param('ids', []);
  155. if (!count($ids))
  156. return app('json')->fail('数据不存在');
  157. $this->repository->batchDelete($ids, $this->merId);
  158. return app('json')->success('删除成功');
  159. }
  160. public function updateForm($id)
  161. {
  162. if (!$this->repository->getWhereCount(['attachment_id' => $id, 'user_type' => $this->request->merId()]))
  163. return app('json')->fail('数据不存在');
  164. return app('json')->success(formToData($this->repository->form($id, $this->request->merId())));
  165. }
  166. /**
  167. * 编辑名称
  168. * @param $id
  169. * @return Json
  170. * @author Qinii
  171. */
  172. public function update($id)
  173. {
  174. $data = $this->request->params(['attachment_name']);
  175. if (!$this->repository->getWhereCount(['attachment_id' => $id, 'user_type' => $this->request->merId()]))
  176. return app('json')->fail('数据不存在');
  177. $this->repository->update($id, $data);
  178. return app('json')->success('修改成功');
  179. }
  180. /**
  181. * 扫码上传图片,生成二维码
  182. * @param $pid
  183. * @return Json
  184. * @author Qinii
  185. * @day 2023/7/18
  186. */
  187. public function scanUploadQrcode($pid)
  188. {
  189. $merId = $this->request->merId();
  190. $token = $merId . md5(time());
  191. $url = rtrim(systemConfig('site_url'), '/') . '/pages/admin/scan/index?pid=' . $pid . '&mer_id=' . $merId . '&token=' . $token;
  192. Cache::set('scan_mer_' . $token, $merId);
  193. return app('json')->success(compact('token', 'pid', 'url'));
  194. }
  195. /**
  196. * 获取通过二维码上传的图片
  197. * @param $token
  198. * @return Json
  199. * @author Qinii
  200. * @day 2023/7/18
  201. */
  202. public function scanUploadImage($token)
  203. {
  204. $res = Cache::smembers('scan_' . $token);
  205. $data = [];
  206. if ($res) {
  207. $data = $this->repository->getSearch(['ids' => $res])->field('attachment_id,attachment_src')->select();
  208. }
  209. return app('json')->success($data);
  210. }
  211. /**
  212. * 提交通过扫码上传的图片信息,可修改分类
  213. * @param $token
  214. * @param AttachmentCategoryRepository $attachmentCategoryRepository
  215. * @return Json
  216. * @author Qinii
  217. * @day 2023/7/18
  218. */
  219. public function scanUploadSave($token, AttachmentCategoryRepository $attachmentCategoryRepository)
  220. {
  221. $id = $this->request->param('pid');
  222. $ids = $this->request->param('ids');
  223. if ($id && !$attachmentCategoryRepository->get($id, $this->merId))
  224. return app('json')->fail('目录不存在');
  225. $this->repository->updates($ids, ['attachment_category_id' => $id]);
  226. Cache::del('scan_' . $token);
  227. Cache::del('scan_mer_' . $token);
  228. return app('json')->success('操作成功');
  229. }
  230. /**
  231. * 线上图片上传
  232. * @param AttachmentCategoryRepository $attachmentCategoryRepository
  233. * @return Json
  234. * @author Qinii
  235. * @day 2023/7/19
  236. */
  237. public function onlineUpload(AttachmentCategoryRepository $attachmentCategoryRepository)
  238. {
  239. $id = $this->request->param('id', 0);
  240. $images = $this->request->param('images', []);
  241. if (!$images) return app('json')->fail('请上传图片地址');
  242. if ($id) {
  243. if (!$category = $attachmentCategoryRepository->get($id, $this->merId))
  244. return app('json')->fail('目录不存在');
  245. $info = [
  246. 'enname' => $category->attachment_category_enname,
  247. 'id' => $category->attachment_category_id
  248. ];
  249. } else {
  250. $info = [
  251. 'enname' => 'def',
  252. 'id' => 0
  253. ];
  254. }
  255. $type = (int)systemConfig('upload_type') ?: 1;
  256. $upload = app()->make(DownloadImageService::class)->downloadImages($images, $info['enname']);
  257. $data = [];
  258. foreach ($upload as $item) {
  259. $data[] = [
  260. 'attachment_category_id' => $info['id'],
  261. 'attachment_name' => $item['name'],
  262. 'attachment_src' => $item['path'],
  263. 'upload_type' => $type,
  264. 'user_type' => $this->merId,
  265. 'user_id' => $this->request->adminId()
  266. ];
  267. }
  268. if ($data) $this->repository->insertAll($data);
  269. return app('json')->success('操作成功');
  270. }
  271. /**
  272. * 替换图片域名
  273. * @return Json
  274. * @author Qinii
  275. */
  276. public function replaceHost()
  277. {
  278. $origin = $this->request->param('origin');
  279. $replace = $this->request->param('replace');
  280. $server = ImageHostService::getInstance();
  281. $res = $server->execute($origin,$replace);
  282. return app('json')->success($res ? '操作成功' : '执行过程中存在错误,请在runtime/log中查看具体错误信息');
  283. }
  284. /**
  285. * 视频分片上传
  286. * @return mixed
  287. */
  288. public function videoUpload()
  289. {
  290. $data = $this->request->params([
  291. ['chunkNumber', 0],//第几分片
  292. ['currentChunkSize', 0],//分片大小
  293. ['chunkSize', 0],//总大小
  294. ['totalChunks', 0],//分片总数
  295. ['file', 'file'],//文件
  296. ['md5', ''],//MD5
  297. ['filename', ''],//文件名称
  298. ]);
  299. $res = $this->repository->videoUpload($data, $_FILES['file']);
  300. return app('json')->success($res);
  301. }
  302. }