SystemAttachment.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\v1\system\attachment;
  12. use app\controller\admin\AuthController;
  13. use app\services\other\CacheServices;
  14. use app\services\system\attachment\SystemAttachmentServices;
  15. use app\services\system\admin\SystemAdminServices;
  16. use think\facade\App;
  17. /**
  18. * 图片管理类
  19. * Class SystemAttachment
  20. * @package app\controller\admin\v1\file
  21. */
  22. class SystemAttachment extends AuthController
  23. {
  24. protected $service;
  25. public function __construct(App $app, SystemAttachmentServices $service)
  26. {
  27. parent::__construct($app);
  28. $this->service = $service;
  29. }
  30. /**
  31. * 显示列表
  32. * @return mixed
  33. */
  34. public function index()
  35. {
  36. $where = $this->request->getMore([
  37. ['name', ''],
  38. ['pid', 0],
  39. ['file_type', 1]
  40. ]);
  41. $where['type'] = 1;
  42. return $this->success($this->service->getImageList($where));
  43. }
  44. /**
  45. * 删除指定资源
  46. *
  47. * @param string $ids
  48. * @return \think\Response
  49. */
  50. public function delete()
  51. {
  52. [$ids] = $this->request->postMore([
  53. ['ids', '']
  54. ], true);
  55. $this->service->del($ids);
  56. return $this->success('删除成功');
  57. }
  58. /**图片上传
  59. * @param $upload_type
  60. * @param $type
  61. * @return mixed
  62. */
  63. public function upload($upload_type = 0, $type = 0)
  64. {
  65. [$pid, $file] = $this->request->postMore([
  66. ['pid', 0],
  67. ['file', 'file'],
  68. ], true);
  69. $res = $this->service->upload((int)$pid, $file, (int)$upload_type, (int)$type);
  70. return $this->success('上传成功', ['src' => $res]);
  71. }
  72. /**
  73. * 移动图片
  74. * @return mixed
  75. */
  76. public function moveImageCate()
  77. {
  78. $data = $this->request->postMore([
  79. ['pid', 0],
  80. ['images', '']
  81. ]);
  82. $this->service->move($data);
  83. return $this->success('移动成功');
  84. }
  85. /**
  86. * 修改文件名
  87. * @param $id
  88. * @return mixed
  89. */
  90. public function update($id)
  91. {
  92. $realName = $this->request->post('real_name', '');
  93. if (!$realName) {
  94. return $this->fail('文件名称不能为空');
  95. }
  96. $this->service->update($id, ['real_name' => $realName]);
  97. return $this->success('修改成功');
  98. }
  99. /**
  100. * 获取上传类型
  101. * @return mixed
  102. */
  103. public function uploadType()
  104. {
  105. $data['upload_type'] = (string)sys_config('upload_type', 1);
  106. return app('json')->success($data);
  107. }
  108. /**
  109. * 视频分片上传
  110. * @return mixed
  111. */
  112. public function videoUpload()
  113. {
  114. $data = $this->request->postMore([
  115. ['chunkNumber', 0],//第几分片
  116. ['currentChunkSize', 0],//分片大小
  117. ['chunkSize', 0],//总大小
  118. ['totalChunks', 0],//分片总数
  119. ['file', 'file'],//文件
  120. ['md5', ''],//MD5
  121. ['filename', ''],//文件名称
  122. ['pid', 0],//分类ID
  123. ]);
  124. $fileHandle = $this->request->file($data['file']);
  125. if (!$fileHandle) return $this->fail('上传信息为空');
  126. $res = $this->service->videoUpload($data, $fileHandle);
  127. return app('json')->success($res);
  128. }
  129. /**
  130. * 保存云端视频记录
  131. * @return mixed
  132. */
  133. public function saveVideoAttachment()
  134. {
  135. $data = $this->request->postMore([
  136. ['path', ''],//视频地址
  137. ['cover_image', ''],//封面地址
  138. ['pid', 0],//分类ID
  139. ['upload_type', 1],//上传类型
  140. ]);
  141. $res = $this->service->saveOssVideoAttachment($data, 1, 0, (int)$data['upload_type']);
  142. return app('json')->success($res);
  143. }
  144. /**获取扫码上传页面链接以及参数
  145. * @return \think\Response
  146. */
  147. public function scanUploadQrcode(CacheServices $services)
  148. {
  149. [$pid] = $this->request->getMore([
  150. ['pid', 0]
  151. ], true);
  152. $uploadToken = md5(time());
  153. $upload_file_size_max = config('upload.filesize');//文件上传大小kb
  154. $services->setDbCache('scan_upload', $uploadToken, 68400);
  155. $url = sys_config('site_url') . '/app/upload?pid=' . $pid . '&cache=scan_upload&type=1&relation_id=0&upload_file_size_max=' . $upload_file_size_max . '&token=' . $uploadToken;
  156. return app('json')->success(['url' => $url]);
  157. }
  158. /**删除二维码
  159. * @return \think\Response
  160. */
  161. public function removeUploadQrcode(CacheServices $services)
  162. {
  163. $services->delectDbCache('scan_upload');
  164. return app('json')->success();
  165. }
  166. /**获取扫码上传的图片数据
  167. * @param $scan_token
  168. * @return \think\Response
  169. */
  170. public function scanUploadImage($scan_token)
  171. {
  172. return app('json')->success($this->service->scanUploadImage($scan_token));
  173. }
  174. /**网络图片上传
  175. * @return \think\Response
  176. */
  177. public function onlineUpload()
  178. {
  179. $data = $this->request->postMore([
  180. ['pid', 0],
  181. ['images', []]
  182. ]);
  183. $this->service->onlineUpload($data);
  184. return app('json')->success('上传完成');
  185. }
  186. /**获取上传信息
  187. * @return \think\Response
  188. * @throws \think\db\exception\DataNotFoundException
  189. * @throws \think\db\exception\DbException
  190. * @throws \think\db\exception\ModelNotFoundException
  191. */
  192. public function getAdminsData()
  193. {
  194. /** @var SystemAdminServices $adminServices */
  195. $adminServices = app()->make(SystemAdminServices::class);
  196. $data['is_way'] = $adminServices->value(['id' => $this->adminId], 'is_way');
  197. $data['upload_file_size_max'] = config('upload.filesize');//文件上传大小kb
  198. return app('json')->success($data);
  199. }
  200. /**保存上传信息
  201. * @param int $is_way
  202. * @return \think\Response
  203. */
  204. public function setAdminsData(int $is_way = 0)
  205. {
  206. /** @var SystemAdminServices $adminServices */
  207. $adminServices = app()->make(SystemAdminServices::class);
  208. if (!in_array($is_way, [0, 1, 2])) return app('json')->fail('参数有误!');
  209. $res = $adminServices->update($this->adminId, ['is_way' => $is_way]);
  210. if ($res) return app('json')->success('ok');
  211. else return app('json')->fail('保存失败!');
  212. }
  213. }