PublicController.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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;
  12. use app\Request;
  13. use app\services\system\attachment\SystemAttachmentServices;
  14. use app\services\system\SystemRouteServices;
  15. use crmeb\services\CacheService;
  16. use think\Response;
  17. class PublicController
  18. {
  19. /**
  20. * 下载文件
  21. * @param string $key
  22. * @return Response|\think\response\File
  23. */
  24. public function download(Request $request, string $key = '')
  25. {
  26. if ($key == '') {
  27. $key = $request->getMore([
  28. ['key', ''],
  29. ], true);
  30. }
  31. if (!$key) {
  32. return Response::create()->code(500);
  33. }
  34. $fileName = CacheService::get($key);
  35. if (is_array($fileName) && isset($fileName['path']) && isset($fileName['fileName']) && $fileName['path'] && $fileName['fileName'] && file_exists($fileName['path'])) {
  36. CacheService::delete($key);
  37. return download($fileName['path'], $fileName['fileName']);
  38. }
  39. return Response::create()->code(500);
  40. }
  41. /**
  42. * 获取workerman请求域名
  43. * @return mixed
  44. */
  45. public function getWorkerManUrl()
  46. {
  47. return app('json')->success(getWorkerManUrl());
  48. }
  49. /**
  50. * 扫码上传
  51. * @param Request $request
  52. * @param int $upload_type
  53. * @param int $type
  54. * @return Response
  55. * @author 吴汐
  56. * @email 442384644@qq.com
  57. * @date 2023/06/13
  58. */
  59. public function scanUpload(Request $request, $upload_type = 0, $type = 0)
  60. {
  61. [$file, $uploadToken, $pid] = $request->postMore([
  62. ['file', 'file'],
  63. ['uploadToken', ''],
  64. ['pid', 0]
  65. ], true);
  66. $service = app()->make(SystemAttachmentServices::class);
  67. if (CacheService::get('scan_upload') != $uploadToken) {
  68. return app('json')->fail(410086);
  69. }
  70. $service->upload((int)$pid, $file, $upload_type, $type, '', $uploadToken);
  71. return app('json')->success(100032);
  72. }
  73. public function import(Request $request)
  74. {
  75. $filePath = $request->param('file_path', '');
  76. if (empty($filePath)) {
  77. return app('json')->fail(12894);
  78. }
  79. app()->make(SystemRouteServices::class)->import($filePath);
  80. return app('json')->success(100010);
  81. }
  82. }