PublicController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. /**
  18. * 公共控制器
  19. */
  20. class PublicController
  21. {
  22. /**
  23. * 下载文件
  24. * @param string $key
  25. * @return Response|\think\response\File
  26. */
  27. public function download(string $key = '')
  28. {
  29. if (!$key) {
  30. return Response::create()->code(500);//参数错误
  31. }
  32. $fileName = CacheService::get($key);//缓存中的文件路径
  33. //判断文件是否存在
  34. if (is_array($fileName) && isset($fileName['path']) && isset($fileName['fileName']) && $fileName['path'] && $fileName['fileName'] && file_exists($fileName['path'])) {
  35. CacheService::delete($key);//删除缓存
  36. return download($fileName['path'], $fileName['fileName']);//返回文件
  37. }
  38. return Response::create()->code(500);
  39. }
  40. /**
  41. * 获取workerman请求域名
  42. * @return mixed
  43. */
  44. public function getWorkerManUrl()
  45. {
  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. //附件服务
  67. $service = app()->make(SystemAttachmentServices::class);
  68. // 获取缓存中的上传令牌并与当前上传令牌比较
  69. if (CacheService::get('scan_upload') != $uploadToken) {
  70. return app('json')->fail(410086); // 如果不一致则返回错误码410086
  71. }
  72. //上传文件
  73. $service->upload((int)$pid, $file, $upload_type, $type, '', $uploadToken);
  74. return app('json')->success(100032);
  75. }
  76. public function import(Request $request)
  77. {
  78. //获取文件路径
  79. $filePath = $request->param('file_path', '');
  80. if (empty($filePath)) {
  81. return app('json')->fail(12894);
  82. }
  83. //导入数据
  84. app()->make(SystemRouteServices::class)->import($filePath);
  85. return app('json')->success(100010);
  86. }
  87. }