DownloadController.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\controller;
  15. use app\common\cache\ExportCache;
  16. use app\common\service\JsonService;
  17. class DownloadController extends BaseAdminController
  18. {
  19. public array $notNeedLogin = ['export'];
  20. /**
  21. * @notes 导出文件
  22. * @return \think\response\File|\think\response\Json
  23. * @author 段誉
  24. * @date 2022/11/24 16:10
  25. */
  26. public function export()
  27. {
  28. //获取文件缓存的key
  29. $fileKey = request()->get('file');
  30. //通过文件缓存的key获取文件储存的路径
  31. $exportCache = new ExportCache();
  32. $fileInfo = $exportCache->getFile($fileKey);
  33. if (empty($fileInfo)) {
  34. return JsonService::fail('下载文件不存在');
  35. }
  36. //下载前删除缓存
  37. $exportCache->delete($fileKey);
  38. return download($fileInfo['src'] . $fileInfo['name'], $fileInfo['name']);
  39. }
  40. }