SystemAttachment.php 5.3 KB

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