SystemAttachment.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. namespace app\controller\admin\system\attachment;
  12. use app\common\AdminBaseController;
  13. use app\Request;
  14. use app\services\system\attachment\SystemAttachmentServices;
  15. use app\services\system\admin\SystemAdminServices;
  16. use qiniu\services\CacheService;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. use think\Response;
  21. /**
  22. * 图片管理类
  23. * Class SystemAttachment
  24. * @package app\controller\admin\v1\file
  25. */
  26. class SystemAttachment extends AdminBaseController
  27. {
  28. protected $service;
  29. public function __construct(Request $request, SystemAttachmentServices $service)
  30. {
  31. parent::__construct($request);
  32. $this->service = $service;
  33. $this->searchable = [
  34. ['name', ''],
  35. ['pid', 0],
  36. ['file_type', 1]
  37. ];
  38. }
  39. /**
  40. * 显示列表
  41. * @return mixed
  42. * @throws DbException
  43. */
  44. public function index()
  45. {
  46. $where = $this->request->getMore($this->searchable);
  47. $where['type'] = 1;
  48. return $this->success($this->service->getImageList($where));
  49. }
  50. /**
  51. * 删除指定资源
  52. *
  53. * @return Response
  54. * @throws DataNotFoundException
  55. * @throws DbException
  56. * @throws ModelNotFoundException
  57. */
  58. public function batchDelete()
  59. {
  60. $ids = $this->request->post('ids', '');
  61. $this->service->del($ids);
  62. return $this->success('删除成功');
  63. }
  64. /**图片上传
  65. * @param int $upload_type
  66. * @param int $type
  67. * @return mixed
  68. */
  69. public function upload(int $upload_type = 0, int $type = 0)
  70. {
  71. [$pid, $file] = $this->request->postMore([
  72. ['pid', 0],
  73. ['file', 'file'],
  74. ], true);
  75. $res = $this->service->upload((int)$pid, $file, (int)$upload_type, (int)$type);
  76. return $this->success('上传成功', ['src' => $res]);
  77. }
  78. /**
  79. * 移动图片
  80. * @return mixed
  81. */
  82. public function moveImageCate()
  83. {
  84. $data = $this->request->postMore([
  85. ['pid', 0],
  86. ['images', '']
  87. ]);
  88. $this->service->move($data);
  89. return $this->success('移动成功');
  90. }
  91. /**
  92. * 修改文件名
  93. * @param $id
  94. * @return mixed
  95. */
  96. public function update($id)
  97. {
  98. $realName = $this->request->post('real_name', '');
  99. if (!$realName) {
  100. return $this->error('文件名称不能为空');
  101. }
  102. $this->service->update($id, ['real_name' => $realName]);
  103. return $this->success('修改成功');
  104. }
  105. /**
  106. * 获取上传类型
  107. * @return mixed
  108. */
  109. public function uploadType()
  110. {
  111. $data['upload_type'] = (string)sys_config('upload_type', 1);
  112. return app('json')->success($data);
  113. }
  114. /**
  115. * 视频分片上传
  116. * @return mixed
  117. */
  118. public function videoUpload()
  119. {
  120. $data = $this->request->postMore([
  121. ['chunkNumber', 0],//第几分片
  122. ['currentChunkSize', 0],//分片大小
  123. ['chunkSize', 0],//总大小
  124. ['totalChunks', 0],//分片总数
  125. ['file', 'file'],//文件
  126. ['md5', ''],//MD5
  127. ['filename', ''],//文件名称
  128. ['pid', 0],//分类ID
  129. ]);
  130. $fileHandle = $this->request->file($data['file']);
  131. if (!$fileHandle) return $this->error('上传信息为空');
  132. $res = $this->service->videoUpload($data, $fileHandle);
  133. return app('json')->success($res);
  134. }
  135. /**
  136. * 保存云端视频记录
  137. * @return mixed
  138. */
  139. public function saveVideoAttachment()
  140. {
  141. $data = $this->request->postMore([
  142. ['path', ''],//视频地址
  143. ['cover_image', ''],//封面地址
  144. ['pid', 0],//分类ID
  145. ['upload_type', 1],//上传类型
  146. ]);
  147. $res = $this->service->saveOssVideoAttachment($data, 1, 0, (int)$data['upload_type']);
  148. return app('json')->success($res);
  149. }
  150. /**网络图片上传
  151. * @return Response
  152. * @throws \Exception
  153. */
  154. public function onlineUpload()
  155. {
  156. $data = $this->request->postMore([
  157. ['pid', 0],
  158. ['images', []]
  159. ]);
  160. $this->service->onlineUpload($data);
  161. return app('json')->success('上传完成');
  162. }
  163. }