Excel.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * @package merchant
  4. *
  5. * @author Qinii
  6. * @day 2020-08-15
  7. *
  8. *
  9. */
  10. namespace app\controller\merchant\store;
  11. use app\common\repositories\store\ExcelRepository;
  12. use crmeb\exceptions\UploadException;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. class Excel extends BaseController
  16. {
  17. protected $repository;
  18. public function __construct(App $app, ExcelRepository $repository)
  19. {
  20. parent::__construct($app);
  21. $this->repository = $repository;
  22. }
  23. /**
  24. * TODO
  25. * @return mixed
  26. * @author Qinii
  27. * @day 2020-08-15
  28. */
  29. public function lst()
  30. {
  31. [$page, $limit] = $this->getPage();
  32. $where['type'] = $this->request->param('type','');
  33. $where['mer_id'] = $this->request->merId();
  34. $where['admin_id'] = $this->request->adminId();
  35. return app('json')->success($this->repository->getList($where,$page,$limit));
  36. }
  37. /**
  38. * TODO 下载文件
  39. * @param $id
  40. * @return \think\response\File
  41. * @author Qinii
  42. * @day 2020-07-30
  43. */
  44. public function download()
  45. {
  46. try{
  47. $id = $this->request->param('id');
  48. $file = $this->repository->getWhere(['excel_id' => $id,'mer_id' => $this->request->merId()]);
  49. $parsedUrl = parse_url($file['path']);
  50. if(isset($parsedUrl['host'])){
  51. //从oss下载
  52. echo "<script>window.location.href = '".$file['path']."';</script>";die;
  53. }
  54. $path = app()->getRootPath().'public'.$file['path'];
  55. if(!$file || !file_exists($path)){
  56. $path = $this->request->param('path','');
  57. if($path){
  58. $path = app()->getRootPath().'public'.$path;
  59. }
  60. if(!$path || !file_exists($path)){
  61. return app('json')->fail('文件不存在');
  62. }
  63. $file['name']= basename($path); // 使用basename函数获取图片的名称
  64. }
  65. return download($path,$file['name']);
  66. }catch (UploadException $e){
  67. return app('json')->fail('下载失败');
  68. }
  69. }
  70. }