SystemAttachment.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\file;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\system\attachment\SystemAttachmentServices;
  14. use crmeb\services\CacheService;
  15. use think\facade\App;
  16. /**
  17. * 附件管理类
  18. * Class SystemAttachment
  19. * @package app\adminapi\controller\v1\file
  20. */
  21. class SystemAttachment extends AuthController
  22. {
  23. /**
  24. * @var SystemAttachmentServices
  25. */
  26. protected $service;
  27. /**
  28. * 每分钟最大上传次数
  29. */
  30. const UPLOAD_PER_MINUTE = 60;
  31. /**
  32. * 每小时最大上传次数
  33. */
  34. const UPLOAD_PER_HOUR = 500;
  35. /**
  36. * @param App $app
  37. * @param SystemAttachmentServices $service
  38. */
  39. public function __construct(App $app, SystemAttachmentServices $service)
  40. {
  41. parent::__construct($app);
  42. $this->service = $service;
  43. }
  44. /**
  45. * 检查上传速率限制
  46. * @return bool
  47. */
  48. protected function checkUploadRateLimit()
  49. {
  50. $adminId = $this->adminId;
  51. $minuteKey = 'admin_upload_minute_' . $adminId;
  52. $hourKey = 'admin_upload_hour_' . $adminId;
  53. if (CacheService::has($minuteKey) && CacheService::get($minuteKey) >= self::UPLOAD_PER_MINUTE) {
  54. return false;
  55. }
  56. if (CacheService::has($hourKey) && CacheService::get($hourKey) >= self::UPLOAD_PER_HOUR) {
  57. return false;
  58. }
  59. return true;
  60. }
  61. /**
  62. * 更新上传速率计数
  63. */
  64. protected function incUploadRateLimit()
  65. {
  66. $adminId = $this->adminId;
  67. $minuteKey = 'admin_upload_minute_' . $adminId;
  68. $hourKey = 'admin_upload_hour_' . $adminId;
  69. $minuteCount = CacheService::has($minuteKey) ? (int)CacheService::get($minuteKey) : 0;
  70. $hourCount = CacheService::has($hourKey) ? (int)CacheService::get($hourKey) : 0;
  71. CacheService::set($minuteKey, $minuteCount + 1, 60);
  72. CacheService::set($hourKey, $hourCount + 1, 3600);
  73. }
  74. /**
  75. * 显示列表
  76. * @return mixed
  77. */
  78. public function index()
  79. {
  80. $where = $this->request->getMore([
  81. ['pid', 0],
  82. ['real_name', ''],
  83. ['type', 0],
  84. ]);
  85. return app('json')->success($this->service->getImageList($where));
  86. }
  87. /**
  88. * 删除指定资源
  89. * @return mixed
  90. */
  91. public function delete()
  92. {
  93. [$ids] = $this->request->postMore([
  94. ['ids', '']
  95. ], true);
  96. $this->service->del($ids);
  97. return app('json')->success('删除成功');
  98. }
  99. /**
  100. * 图片上传
  101. * @param int $upload_type
  102. * @param int $type
  103. * @return mixed
  104. */
  105. public function upload($upload_type = 0, $type = 0)
  106. {
  107. if (!$this->checkUploadRateLimit()) {
  108. return app('json')->fail('上传过于频繁,请稍后再试');
  109. }
  110. [$pid, $file, $menuName] = $this->request->postMore([
  111. ['pid', 0],
  112. ['file', 'file'],
  113. ['menu_name', '']
  114. ], true);
  115. $res = $this->service->upload((int)$pid, $file, $upload_type, $type, $menuName);
  116. $this->incUploadRateLimit();
  117. return app('json')->success('上传成功', ['src' => $res]);
  118. }
  119. /**
  120. * 移动图片
  121. * @return mixed
  122. */
  123. public function moveImageCate()
  124. {
  125. $data = $this->request->postMore([
  126. ['pid', 0],
  127. ['images', '']
  128. ]);
  129. $this->service->move($data);
  130. return app('json')->success('移动成功');
  131. }
  132. /**
  133. * 修改文件名
  134. * @param $id
  135. * @return mixed
  136. */
  137. public function update($id)
  138. {
  139. $realName = $this->request->post('real_name', '');
  140. if (!$realName) {
  141. return app('json')->fail('文件名称不能为空');
  142. }
  143. $this->service->update($id, ['real_name' => $realName]);
  144. return app('json')->success('修改成功');
  145. }
  146. /**
  147. * 获取上传类型
  148. * @return mixed
  149. */
  150. public function uploadType()
  151. {
  152. $data['upload_type'] = (string)sys_config('upload_type', 1);
  153. return app('json')->success($data);
  154. }
  155. /**
  156. * 视频分片上传
  157. * @return mixed
  158. */
  159. public function videoUpload()
  160. {
  161. if (!$this->checkUploadRateLimit()) {
  162. return app('json')->fail('上传过于频繁,请稍后再试');
  163. }
  164. $data = $this->request->postMore([
  165. ['chunkNumber', 0],//第几分片
  166. ['currentChunkSize', 0],//分片大小
  167. ['chunkSize', 0],//总大小
  168. ['totalChunks', 0],//分片总数
  169. ['file', 'file'],//文件
  170. ['md5', ''],//MD5
  171. ['filename', ''],//文件名称
  172. ]);
  173. $res = $this->service->videoUpload($data, $_FILES['file']);
  174. $this->incUploadRateLimit();
  175. return app('json')->success($res);
  176. }
  177. /**
  178. * 获取扫码上传页面链接以及参数
  179. * @return \think\Response
  180. * @author 吴汐
  181. * @email 442384644@qq.com
  182. * @date 2023/06/13
  183. */
  184. public function scanUploadQrcode()
  185. {
  186. [$pid] = $this->request->getMore([
  187. ['pid', 0]
  188. ], true);
  189. $uploadToken = md5(time());
  190. CacheService::set('scan_upload', $uploadToken, 600);
  191. $url = sys_config('site_url') . '/app/upload?pid=' . $pid . '&token=' . $uploadToken;
  192. return app('json')->success(['url' => $url]);
  193. }
  194. /**
  195. * 删除二维码
  196. * @return \think\Response
  197. * @author 等风来
  198. * @email 136327134@qq.com
  199. * @date 2023/6/26
  200. */
  201. public function removeUploadQrcode()
  202. {
  203. CacheService::delete('scan_upload');
  204. return app('json')->success();
  205. }
  206. /**
  207. * 获取扫码上传的图片数据
  208. * @param $scan_token
  209. * @return \think\Response
  210. * @author 吴汐
  211. * @email 442384644@qq.com
  212. * @date 2023/06/13
  213. */
  214. public function scanUploadImage($scan_token)
  215. {
  216. return app('json')->success($this->service->scanUploadImage($scan_token));
  217. }
  218. /**
  219. * 网络图片上传
  220. * @return \think\Response
  221. * @throws \Exception
  222. * @author 吴汐
  223. * @email 442384644@qq.com
  224. * @date 2023/06/13
  225. */
  226. public function onlineUpload()
  227. {
  228. $data = $this->request->postMore([
  229. ['pid', 0],
  230. ['images', []]
  231. ]);
  232. $this->service->onlineUpload($data);
  233. return app('json')->success('上传成功');
  234. }
  235. public function videoDataSave()
  236. {
  237. $data = $this->request->postMore([
  238. ['pid', 0],
  239. ['video_name', ''],
  240. ['video_path', '']
  241. ]);
  242. $this->service->attachmentAdd(
  243. $data['video_name'],
  244. 0,
  245. 'video/mp4',
  246. $data['video_path'],
  247. $data['video_path'],
  248. $data['pid'],
  249. (int)sys_config('upload_type', 1),
  250. time(),
  251. 1,
  252. 1,
  253. $data['video_name']
  254. );;
  255. return app('json')->success('上传成功');
  256. }
  257. }